-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: isolate fixed prefix at well-founded recursion
closes #1017
- Loading branch information
1 parent
75e771b
commit e61d0be
Showing
12 changed files
with
114 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
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 | ||
|
||
end Stream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
def f (as : Array Nat) (hsz : as.size > 0) (i : Nat) : Nat := | ||
if h : i < as.size then | ||
as.get ⟨i, h⟩ + f as hsz (i + 1) | ||
else | ||
0 | ||
termination_by f a h i => a.size - i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def sum (as : Array Nat) : Nat := | ||
go 0 0 | ||
where | ||
go (i : Nat) (s : Nat) : Nat := | ||
if h : i < as.size then | ||
go (i+1) (s + as.get ⟨i, h⟩) | ||
else | ||
s | ||
termination_by' measure (fun ⟨i, s⟩ => as.size - i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters