Skip to content

Commit 8a4b7ed

Browse files
committed
wip
1 parent b4d63d7 commit 8a4b7ed

File tree

1 file changed

+50
-50
lines changed

1 file changed

+50
-50
lines changed

HumanEvalLean/HumanEval123.lean

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public def WellFounded.partialExtrinsicFix [∀ a, Nonempty (C a)] (R : α →
3939
apply TransGen.single
4040
assumption⟩ ‹_›) ⟨a, Or.inl rfl⟩
4141

42-
public theorem WellFounded.bla {α' : Sort _} [∀ a, Nonempty (C a)] (R : α → α → Prop) (f : α' → α)
42+
public theorem WellFounded.extrinsicFix_invImage {α' : Sort _} [∀ a, Nonempty (C a)] (R : α → α → Prop) (f : α' → α)
4343
(F : ∀ a, (∀ a', R a' a → C a') → C a) (F' : ∀ a, (∀ a', R (f a') (f a) → C (f a')) → C (f a))
4444
(h : ∀ a r, F (f a) r = F' a fun a' hR => r (f a') hR) (a : α') (h : WellFounded R) :
4545
extrinsicFix (C := (C <| f ·)) (InvImage R f) F' a = extrinsicFix (C := C) R F (f a) := by
@@ -73,7 +73,7 @@ public theorem WellFounded.partialExtrinsicFix_eq [∀ a, Nonempty (C a)] (R :
7373
apply Or.inr
7474
refine TransGen.trans h ?_
7575
exact .single hR⟩
76-
have := bla (C := (C ·.val)) (R := (R ·.1 ·.1)) (f := f) (F := fun a r => F a.1 fun a' hR => r ⟨a', Or.inr (by cases a.2; grind [TransGen.single]; exact .trans (.single hR) ‹_›)⟩ hR)
76+
have := extrinsicFix_invImage (C := (C ·.val)) (R := (R ·.1 ·.1)) (f := f) (F := fun a r => F a.1 fun a' hR => r ⟨a', Or.inr (by cases a.2; grind [TransGen.single]; exact .trans (.single hR) ‹_›)⟩ hR)
7777
(F' := fun a r => F a.1 fun a' hR => r ⟨a', by cases a.2; grind [TransGen.single]; exact Or.inr (.trans (.single hR) ‹_›)⟩ hR)
7878
unfold InvImage at this
7979
rw [this]
@@ -115,7 +115,7 @@ public theorem WellFounded.partialExtrinsicFix₂_eq_partialExtrinsicFix [∀ a
115115
simp only [partialExtrinsicFix, partialExtrinsicFix₂, extrinsicFix₂]
116116
let f (x : ((a' : α) ×' { b' // PSigma.mk a' b' = ⟨a, b⟩ ∨ TransGen R ⟨a', b'⟩ ⟨a, b⟩ })) : { a' // a' = ⟨a, b⟩ ∨ TransGen R a' ⟨a, b⟩ } :=
117117
⟨⟨x.1, x.2.1⟩, x.2.2
118-
have := bla (C := fun a => C₂ a.1.1 a.1.2) (f := f) (R := (R ·.1 ·.1))
118+
have := extrinsicFix_invImage (C := fun a => C₂ a.1.1 a.1.2) (f := f) (R := (R ·.1 ·.1))
119119
(F := fun a r => F a.1.1 a.1.2 fun a' b' hR => r ⟨⟨a', b'⟩, ?refine_a⟩ hR)
120120
(F' := fun a r => F a.1 a.2.1 fun a' b' hR => r ⟨a', b', ?refine_b⟩ hR)
121121
(a := ⟨a, b, ?refine_c⟩); rotate_left
@@ -173,53 +173,6 @@ theorem collatzRel_collatzStep {n : Nat} (h : n > 1) :
173173
CollatzRel (collatzStep n) n := by
174174
grind [CollatzRel]
175175

176-
/-!
177-
## Implementation 1: no termination proof required
178-
179-
Until the Collatz conjecture is solved, it is not clear that the function we are going to write
180-
will terminate on all inputs. There are two ways to address this problem.
181-
182-
1. Write a function that is not guaranteed to terminate. It can be verified on inputs for which
183-
the Collatz sequence reaches `1` after finitely many steps.
184-
2. Write a function that requires proof that the Collatz sequence reaches `1` from the given input.
185-
186-
The following solution follows approach 1. After that, we show another solution following
187-
approach 2.
188-
-/
189-
190-
def oddCollatz₁ (n : Nat) : List Nat :=
191-
(collectOddCollatz n ∅).toList
192-
where
193-
-- This function is recursive and, depending on the Collatz conjecture, it may or may not terminate.
194-
-- By relying on the fixpoint combinator `partialExtrinsicFix₂` instead of using the `partial` modifier,
195-
-- we will be able to verify the function whenever the Collatz sequence terminates after
196-
-- finitely many steps. A termination proof is not required for *calling* this function,
197-
-- only for verifying it.
198-
collectOddCollatz : (n : Nat) → (acc : TreeSet Nat compare) → TreeSet Nat compare :=
199-
-- `partialExtrinsicFix₂` is a fixpoint combinator that produces a function that may or may
200-
-- not terminate. It can be verified on inputs on which the fixpoint is well-founded.
201-
-- If we had used the `partial` modifier instead, no verification would be possible at all.
202-
WellFounded.partialExtrinsicFix₂ (CollatzRel ·.1 ·.1) fun n acc recur =>
203-
if h : n > 1 then
204-
recur (collatzStep n) (if n % 2 = 0 then acc else acc.insert n) (by grind [CollatzRel])
205-
else if n = 1 then
206-
acc.insert 1
207-
else
208-
acc
209-
210-
/-!
211-
## Tests for `oddCollatz₁`
212-
-/
213-
214-
example : oddCollatz₁ 14 = [1, 5, 7, 11, 13, 17] := by native_decide
215-
example : oddCollatz₁ 5 = [1, 5] := by native_decide
216-
example : oddCollatz₁ 12 = [1, 3, 5] := by native_decide
217-
example : oddCollatz₁ 1 = [1] := by native_decide
218-
219-
/-!
220-
We'll verify `oddCollatz₁` after having verified `oddCollatz₂`.
221-
-/
222-
223176
/-!
224177
## Preliminaries regarding termination
225178
@@ -380,6 +333,53 @@ theorem mem_oddCollatz₂_iff {m n : Nat} {h : Acc CollatzRel n} :
380333
grind [mod_two_eq_one_of_mem_oddCollatz₂, transGen_collatzRel_of_mem_oddCollatz₂,
381334
mem_self_oddCollatz₂, mem_oddCollatz₂_of_transGen]
382335

336+
/-!
337+
## Implementation 1: no termination proof required
338+
339+
Until the Collatz conjecture is solved, it is not clear that the function we are going to write
340+
will terminate on all inputs. There are two ways to address this problem.
341+
342+
1. Write a function that is not guaranteed to terminate. It can be verified on inputs for which
343+
the Collatz sequence reaches `1` after finitely many steps.
344+
2. Write a function that requires proof that the Collatz sequence reaches `1` from the given input.
345+
346+
The following solution follows approach 1. After that, we show another solution following
347+
approach 2.
348+
-/
349+
350+
def oddCollatz₁ (n : Nat) : List Nat :=
351+
(collectOddCollatz n ∅).toList
352+
where
353+
-- This function is recursive and, depending on the Collatz conjecture, it may or may not terminate.
354+
-- By relying on the fixpoint combinator `partialExtrinsicFix₂` instead of using the `partial` modifier,
355+
-- we will be able to verify the function whenever the Collatz sequence terminates after
356+
-- finitely many steps. A termination proof is not required for *calling* this function,
357+
-- only for verifying it.
358+
collectOddCollatz : (n : Nat) → (acc : TreeSet Nat compare) → TreeSet Nat compare :=
359+
-- `partialExtrinsicFix₂` is a fixpoint combinator that produces a function that may or may
360+
-- not terminate. It can be verified on inputs on which the fixpoint is well-founded.
361+
-- If we had used the `partial` modifier instead, no verification would be possible at all.
362+
WellFounded.partialExtrinsicFix₂ (CollatzRel ·.1 ·.1) fun n acc recur =>
363+
if h : n > 1 then
364+
recur (collatzStep n) (if n % 2 = 0 then acc else acc.insert n) (by grind [CollatzRel])
365+
else if n = 1 then
366+
acc.insert 1
367+
else
368+
acc
369+
370+
/-!
371+
## Tests for `oddCollatz₁`
372+
-/
373+
374+
example : oddCollatz₁ 14 = [1, 5, 7, 11, 13, 17] := by native_decide
375+
example : oddCollatz₁ 5 = [1, 5] := by native_decide
376+
example : oddCollatz₁ 12 = [1, 3, 5] := by native_decide
377+
example : oddCollatz₁ 1 = [1] := by native_decide
378+
379+
/-!
380+
We'll verify `oddCollatz₁` after having verified `oddCollatz₂`.
381+
-/
382+
383383
/-!
384384
## Verification of `oddCollatz₁`
385385
-/

0 commit comments

Comments
 (0)