Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Well-founded recursion issue #1017

Closed
leodemoura opened this issue Feb 14, 2022 · 0 comments
Closed

Well-founded recursion issue #1017

leodemoura opened this issue Feb 14, 2022 · 0 comments

Comments

@leodemoura
Copy link
Member

leodemoura commented Feb 14, 2022

See the issue here https://gist.github.com/JamesGallicchio/9e5d08bce7ce4f9f563d1a9edc765d5b
See discussion here: https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Termination.20via.20well.20founded.20relation.20on.20subtype.3F

namespace Stream

variable [Stream ρ τ] (s : ρ)

def take (s : ρ) : Nat → List τ × ρ
| 0 => ([], s)
| n+1 =>
  match next? s with
  | none => ([], s)
  | some (x,rest) =>
    let (L,rest) := take rest n
    (x::L, rest)

def isEmpty : Bool :=
  Option.isNone (next? s)

def lengthBoundedBy (n : Nat) : Prop :=
  isEmpty (take s n).2

def hasNext : ρ → ρ → Prop
  := λ s1 s2 => ∃ x, next? s1 = some ⟨x,s2⟩
  
def isFinite : Prop :=
  ∃ n, lengthBoundedBy s n

instance hasNextWF : WellFoundedRelation {s : ρ // isFinite s} where
  rel := λ s1 s2 => hasNext s2.val s1.val
  wf := ⟨λ ⟨s,h⟩ => ⟨⟨s,h⟩, by
    simp
    cases h; case intro w h =>
    induction w generalizing s
    case zero =>
      intro ⟨s',h'⟩ h_next
      simp [hasNext] at h_next
      cases h_next; case intro x h_next =>
      simp [lengthBoundedBy, isEmpty, Option.isNone, take, h_next] at h
    case succ n ih =>
      intro ⟨s',h'⟩ h_next
      simp [hasNext] at h_next
      cases h_next; case intro x h_next =>
      simp [lengthBoundedBy, take, h_next] at h
      have := ih s' h
      exact Acc.intro (⟨s',h'⟩ : {s : ρ // isFinite s}) this
  ⟩⟩

def mwe [Stream ρ τ] (acc : α) : {l : ρ // isFinite l} → α
  | ⟨l,h⟩ =>
    match h:next? l with
    | none => acc
    | some (x,xs) =>
      have h_next : hasNext l xs := by exists x; simp [hasNext, h]
      mwe acc ⟨xs, by sorry⟩
  termination_by _ l => l  -- ERROR here
  decreasing_by
    trace_state

end Stream
leodemoura added a commit that referenced this issue Feb 18, 2022
TODO: adapt `elabWFRel`, `mkFix`, and etc.

This is needed for #1017
leodemoura added a commit that referenced this issue Feb 18, 2022
This is needed for #1017

TODO: `addNonRecPreDefs` and equation theorems
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant