Skip to content

Commit 7ffaab8

Browse files
committed
Add push? for pushing an arg and an op
1 parent a2033fa commit 7ffaab8

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

HumanEvalLean/HumanEval160.lean

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ def pushOp? (σ : ParseState) (op : Op) : Option ParseState :=
8181
| .lt, _ | .eq, false => some { σ with hold := op :: top :: hold }
8282
| .gt, _ | .eq, true =>
8383
match σ.output with
84-
| arg₂ :: arg₁ :: out => some { σ with hold := op :: hold, output := .app top arg₁ arg₂ :: out }
84+
| arg₂ :: arg₁ :: out => some { σ with hold := op :: hold, output := app top arg₁ arg₂ :: out }
8585
| _ => none
8686

8787
@[simp]
88-
theorem pushOp?_ops {σ : ParseState} (h : σ.pushOp? op = some σ') : σ'.ops = σ.ops := by
88+
theorem pushOp?_ops : ParseState} (h : σ.pushOp? op = some σ) : σ.ops = σ.ops := by
8989
rw [pushOp?] at h
9090
repeat' split at h
9191
all_goals
@@ -107,6 +107,23 @@ theorem pushOp?_hold_length_le_output_length
107107
(repeat' split at hp) <;> injection hp
108108
all_goals next hp => simp_all +arith [←hp]
109109

110+
def push? (σ : ParseState) (arg : Nat) (op : Op) : Option ParseState :=
111+
σ.pushArg arg |>.pushOp? op
112+
113+
@[simp]
114+
theorem push?_ops {σ₁ : ParseState} (h : σ₁.push? arg op = some σ₂) : σ₁.ops = σ₂.ops := by
115+
rw [push?, pushArg] at h
116+
have := pushOp?_ops h
117+
repeat simp_all only
118+
119+
theorem push?_output_hold_length
120+
{σ₁ : ParseState} (hp : σ₁.push? arg op = some σ₂) (hl : σ₁.hold.length ≤ σ₁.output.length) :
121+
σ₁.output.length - σ₁.hold.length = σ₂.output.length - σ₂.hold.length := by
122+
rw [push?, pushArg] at hp
123+
have hh : σ₁.hold.length < σ₁.output.length + 1 := by omega
124+
simp only [pushOp?_output_hold_length hp hh, List.length_cons]
125+
omega
126+
110127
def finalize (σ : ParseState) : ParseState :=
111128
match _ : σ.hold, σ.output with
112129
| op :: hold, arg₂ :: arg₁ :: out => finalize { σ with hold, output := app op arg₁ arg₂ :: out }
@@ -121,29 +138,28 @@ theorem finalize_output_length {σ : ParseState} (h : σ.hold.length < σ.output
121138
case cons.cons out => cases out <;> simp_all
122139
all_goals simp_all
123140

124-
def run (σ : ParseState) : Option ParseState :=
141+
def run? (σ : ParseState) : Option ParseState :=
125142
match _ : σ.ops, σ.args with
126143
| op :: ops, arg :: args =>
127-
match _ : { σ with ops, args } |>.pushArg arg |>.pushOp? op with
144+
match _ : { σ with ops, args }.push? arg op with
128145
| none => none
129-
| some σ => run σ
146+
| some σ => run? σ
130147
| [], [arg] => { σ with args := [] } |>.pushArg arg |>.finalize
131148
| _, _ => none
132149
termination_by σ.ops
133-
decreasing_by simp_all +arith [pushOp?_ops ‹_›]
150+
decreasing_by have := push?_ops ‹_›; simp_all +arith
134151

135-
theorem run_output_length
136-
{σ₁ : ParseState} (hr : σ₁.run = some σ₂) (hl : σ₁.hold.length ≤ σ₁.output.length) :
152+
theorem run?_output_length
153+
{σ₁ : ParseState} (hr : σ₁.run? = some σ₂) (hl : σ₁.hold.length ≤ σ₁.output.length) :
137154
σ₂.output.length = σ₁.output.length + 1 - σ₁.hold.length := by
138-
rw [run] at hr
155+
rw [run?] at hr
139156
repeat' split at hr
140157
· contradiction
141158
next σ' h =>
142-
have : σ'.ops.length < σ₁.ops.length := by simp [pushOp?_ops h, *]
143-
have hl := pushOp?_hold_length_le_output_length h <| by simp_all +arith
144-
rw [run_output_length hr hl]
145-
have := pushOp?_output_hold_length h <| by simp only [pushArg, List.length_cons]; omega
146-
simp only [pushArg, List.length_cons] at this
159+
have : σ'.ops.length < σ₁.ops.length := by have := push?_ops h; simp_all
160+
have hl' := pushOp?_hold_length_le_output_length h <| by simp_all +arith
161+
have := push?_output_hold_length h hl
162+
simp only [run?_output_length hr hl', pushArg, List.length_cons] at *
147163
omega
148164
· injection hr with hr
149165
rw [←hr, finalize_output_length]
@@ -152,17 +168,17 @@ theorem run_output_length
152168
· contradiction
153169
termination_by σ₁.ops.length
154170

155-
-- This implies that `parse?` either fails, or returns precisely the single output element.
156-
theorem run_output_singleton {ops args} (h : run { ops, args } = some σ) : σ.output.length = 1 :=
157-
run_output_length h .refl
171+
-- This implies that `parse?` either fails, or returns precisely a single output element.
172+
theorem run?_output_singleton {ops args} (h : run? { ops, args } = some σ) : σ.output.length = 1 :=
173+
run?_output_length h .refl
158174

159175
end ParseState
160176

161177
-- Parses an expression based on the "shunting yard algorithm".
162178
def parse? (ops : List Op) (args : List Nat) : Option Expr :=
163-
match h : { ops, args : ParseState }.run with
179+
match h : { ops, args : ParseState }.run? with
164180
| none => none
165-
| some σ => σ.output[0]'(by simp [ParseState.run_output_singleton h])
181+
| some σ => σ.output[0]'(by simp [ParseState.run?_output_singleton h])
166182

167183
theorem parse?_isSome_iff : (parse? ops args).isSome ↔ (args.length = ops.length + 1) where
168184
mp := sorry

0 commit comments

Comments
 (0)