File size: 16,393 Bytes
760f90e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import OAI.Computability.Scheduling.CostBounds

namespace OAI

universe u1 u2

section

namespace ThreeMachine.StackCompiler

def Poly {I : Type u1} (size f : I → ℕ) (d : ℕ) : Prop :=
  Majorized f (fun i => (size i+2)^d)

namespace Poly
variable {I : Type u2} {s f g : I → ℕ} {d e : ℕ}

theorem lift (h : Poly s f d) (hde : d ≤ e) : Poly s f e :=
  h.trans (Majorized.pow_mono (fun _ => by omega) hde)

theorem of_le (h : ∀ i, f i ≤ g i) (hg : Poly s g d) : Poly s f d :=
  (Majorized.of_le h).trans hg

theorem const (s : I → ℕ) (C : ℕ) : Poly s (fun _ => C) 0 :=
  Majorized.const C (fun _ => by simp)

theorem base (s : I → ℕ) : Poly s (fun i => s i+2) 1 := by
  simpa only [Poly,Nat.pow_one] using Majorized.refl (fun i => s i+2)

theorem size (s : I → ℕ) : Poly s s 1 :=
  of_le (fun _ => by omega) (base s)

theorem add (hf : Poly s f d) (hg : Poly s g e) : Poly s (fun i => f i+g i) (max d e) :=
  Majorized.add (hf.lift (le_max_left _ _)) (hg.lift (le_max_right _ _))

theorem mul (hf : Poly s f d) (hg : Poly s g e) : Poly s (fun i => f i*g i) (d+e) := by
  simpa only [Poly,Nat.pow_add] using Majorized.mul hf hg

theorem pow (hf : Poly s f d) (k : ℕ) : Poly s (fun i => (f i)^k) (d*k) := by
  simpa only [Poly,Nat.pow_mul] using Majorized.pow hf k

theorem sub (hf : Poly s f d) (g : I → ℕ) : Poly s (fun i => f i-g i) d :=
  of_le (fun _ => Nat.sub_le _ _) hf

theorem max (hf : Poly s f d) (hg : Poly s g e) : Poly s (fun i => max (f i) (g i)) (max d e) :=
  of_le (fun _ => by omega) (add hf hg)

end Poly
end ThreeMachine.StackCompiler

open Lean in
def polyRuleNames : Array Name := #[
  `OAI.ThreeMachine.StackCompiler.Uniform.time_comp,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_pair,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_congr,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_id,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_fst,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_snd,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_nil,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_unit,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_zero,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_false,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_reinterpret,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_cons,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_choose,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_constant,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_uniform,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_tail,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_none,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_some_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_ite_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_head?_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_length_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_drop_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_getElem?_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_functionGet_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_functionGetAny_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_functionGetOptionAny_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_reindex,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_test_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_reverse,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_comp,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_pair,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_congr,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_id,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_fst,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_snd,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_reinterpret,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_choose,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_not_le,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_and_le,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_or_le,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_succ_le,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_test_le,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_add_le,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_sub_le,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_le_le,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_eqNat_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_setFilter,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_setEmpty,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_vectorMap,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_vectorMapAny,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_andD_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_noEdges,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_stateEq,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_stateCompatible,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_append_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_getD_le,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_zero,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_nil,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_cons,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_reverse_le,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_range_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_orD_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_states,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_blockAppend,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_blockAppendNamed,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_stateAdvance,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_lookup_setEq_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_lookup_stateEq_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_tryAdvance,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_solve,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_initialMarks,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_markings,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_layerStep,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_getElemD_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_matrixRows,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_paddedMatrix,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_triple_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_cardinalMap,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_cardinalValue,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_makeUniverse,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_restrictTime,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_scheduleMatrix,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_predecessorCount,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_predecessors,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_rankFromCounts,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_natLT_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_finVal,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_thresholdSets,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_triples,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_cones_cons,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_finEmpty,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_finCons,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_assignments_succ,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_atomSets,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_family,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_fullSolveK,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_scheduleMatrixFixed,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_bestMatrixStep,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_functionListOption_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_sndFunctionListOption_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_binaryValueStep_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_binaryLeadingStep_le,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_binaryTakeStep_le,
  `OAI.ThreeMachine.StackCompiler.Realizer.time_pred,
  `OAI.ThreeMachine.StackCompiler.Uniform.time_binaryParse
]

open Lean Meta Elab Tactic in
private def polyCostHead (e : Expr) : Option Name :=
  let as := e.getAppArgs
  if e.getAppFn.isConstOf ``ThreeMachine.StackCompiler.Uniform.time && as.size ≥ 3 then
    as[as.size-3]!.getAppFn.constName?
  else if e.getAppFn.isConstOf ``ThreeMachine.StackCompiler.Realizer.time && as.size ≥ 2 then
    as[as.size-2]!.getAppFn.constName?
  else none

open Lean Meta Elab Tactic in
private def polyExprName (e : Expr) (n : Name) : Bool := e.getAppFn.isConstOf n

open Lean Meta Elab Tactic in
private def synthPoly (fuel : ℕ) (sz x e : Expr) : TacticM (Expr × Expr) := do
  match fuel with
  | 0 => throwError "poly_bound: expression depth exhausted"
  | fuel+1 =>
    let e := e.headBeta
    let fn ← mkLambdaFVars #[x] e
    for decl in (← getLCtx) do
      unless decl.isImplementationDetail do
        let tp ← instantiateMVars decl.type
        if polyExprName tp ``ThreeMachine.StackCompiler.Poly then
          let as := tp.getAppArgs
          if as.size == 4 then
            if ← isDefEq as[1]! sz then
              if ← isDefEq as[2]! fn then
                return (as[3]!,decl.toExpr)
    if !(e.containsFVar x.fvarId!) then
      return (mkNatLit 0, ← mkAppM ``ThreeMachine.StackCompiler.Poly.const #[sz,e])
    let se := mkApp sz x
    if ← isDefEq e se then
      return (mkNatLit 1, ← mkAppM ``ThreeMachine.StackCompiler.Poly.size #[sz])
    let be ← mkAppM ``Nat.add #[se,mkNatLit 2]
    if ← isDefEq e be then
      return (mkNatLit 1, ← mkAppM ``ThreeMachine.StackCompiler.Poly.base #[sz])
    let es := e.getAppArgs
    let isadd := polyExprName e ``Nat.add || polyExprName e ``HAdd.hAdd
    let ismul := polyExprName e ``Nat.mul || polyExprName e ``HMul.hMul
    let issub := polyExprName e ``Nat.sub || polyExprName e ``HSub.hSub
    let ismax := polyExprName e ``Nat.max || polyExprName e ``Max.max
    if es.size ≥ 2 && (isadd || ismul || issub || ismax) then
      let a := es[es.size-2]!
      let b := es[es.size-1]!
      let (d,h) ← synthPoly fuel sz x a
      if issub then
        let g ← mkLambdaFVars #[x] b
        return (d, ← mkAppM ``ThreeMachine.StackCompiler.Poly.sub #[h,g])
      let (_,h') ← synthPoly fuel sz x b
      let nm := if isadd then ``ThreeMachine.StackCompiler.Poly.add
        else if ismul then ``ThreeMachine.StackCompiler.Poly.mul
        else ``ThreeMachine.StackCompiler.Poly.max
      let proof ← mkAppM nm #[h,h']
      let tp ← inferType proof
      return (tp.getAppArgs[3]!,proof)
    if es.size ≥ 2 && (polyExprName e ``Nat.pow || polyExprName e ``HPow.hPow) &&
        !(es[es.size-1]!.containsFVar x.fvarId!) then
      let (_,h) ← synthPoly fuel sz x es[es.size-2]!
      let proof ← mkAppM ``ThreeMachine.StackCompiler.Poly.pow #[h,es[es.size-1]!]
      let tp ← inferType proof
      return (tp.getAppArgs[3]!,proof)
    if es.size == 1 && polyExprName e ``Nat.succ then
      let (_,h) ← synthPoly fuel sz x es[0]!
      let hc ← mkAppM ``ThreeMachine.StackCompiler.Poly.const #[sz,mkNatLit 1]
      let proof ← mkAppM ``ThreeMachine.StackCompiler.Poly.add #[h,hc]
      let tp ← inferType proof
      return (tp.getAppArgs[3]!,proof)
    if polyExprName e ``ThreeMachine.StackCompiler.volume then
      let val := es[es.size-1]!
      let vf ← mkLambdaFVars #[x] val
      let tp ← inferType val
      let ts := tp.getAppArgs
      let finish (pr : Expr) : TacticM (Expr × Expr) := do
        let tp ← inferType pr
        return (tp.getAppArgs[3]!,pr)
      if polyExprName tp ``Nat then
        let (_,h) ← synthPoly fuel sz x val
        return ← finish (← mkAppM `OAI.ThreeMachine.StackCompiler.Poly.volumeNat #[h])
      if polyExprName tp ``Bool then
        return ← finish (← mkAppM `OAI.ThreeMachine.StackCompiler.Poly.volumeBool #[sz,vf])
      if polyExprName tp ``Unit then
        return ← finish (← mkAppM `OAI.ThreeMachine.StackCompiler.Poly.volumeUnit #[sz,vf])
      if polyExprName tp ``Prod then
        let a ← mkAppM ``Prod.fst #[val]
        let b ← mkAppM ``Prod.snd #[val]
        let (_,h) ← synthPoly fuel sz x (← mkAppM ``ThreeMachine.StackCompiler.volume #[a])
        let (_,h') ← synthPoly fuel sz x (← mkAppM ``ThreeMachine.StackCompiler.volume #[b])
        return ← finish (← mkAppM `OAI.ThreeMachine.StackCompiler.Poly.volumeProd #[vf,h,h'])
      let mut dim? : Option Expr := none
      let mut nm := Name.anonymous
      if polyExprName tp ``Fin then
        dim? := some ts[0]!; nm := `OAI.ThreeMachine.StackCompiler.Poly.volumeFin
      if polyExprName tp ``Finset && polyExprName ts[0]! ``Fin then
        dim? := some ts[0]!.getAppArgs[0]!; nm := `OAI.ThreeMachine.StackCompiler.Poly.volumeFinset
      if polyExprName tp ``ThreeMachine.StackCompiler.Universe then
        dim? := some ts[0]!; nm := `OAI.ThreeMachine.StackCompiler.Poly.volumeUniverse
      if polyExprName tp ``ThreeMachine.StackCompiler.Cardinal then
        dim? := some ts[0]!; nm := `OAI.ThreeMachine.StackCompiler.Poly.volumeCardinal
      if polyExprName tp ``ThreeMachine.StackCompiler.Matrix then
        dim? := some ts[0]!; nm := `OAI.ThreeMachine.StackCompiler.Poly.volumeMatrix
      if polyExprName tp ``ThreeMachine.Algorithm.State && polyExprName ts[0]! ``Fin then
        dim? := some ts[0]!.getAppArgs[0]!; nm := `OAI.ThreeMachine.StackCompiler.Poly.volumeState
      if let .forallE _ dom body _ := (← whnf tp) then
        if polyExprName dom ``Fin && polyExprName body ``Bool then
          dim? := some dom.getAppArgs[0]!; nm := `OAI.ThreeMachine.StackCompiler.Poly.volumeBoolVector
        if polyExprName dom ``Fin then
          if let .forallE _ dom₂ body₂ _ := body then
            if polyExprName dom₂ ``Fin && polyExprName body₂ ``Bool then
              if ← isDefEq dom dom₂ then
                dim? := some dom.getAppArgs[0]!; nm := `OAI.ThreeMachine.StackCompiler.Poly.volumeMatrix
      if let some dim := dim? then
        let (_,h) ← synthPoly fuel sz x dim
        return ← finish (← mkAppM nm #[h,vf])
    if let some rh := polyCostHead e then
      for nm in polyRuleNames do
        let saved ← saveState
        try
          let c ← mkConstWithFreshMVarLevels nm
          let (xs,bs,tp) ← forallMetaTelescope (← inferType c)
          let args := tp.getAppArgs
          if args.size < 2 then throwError "not a bound"
          let lhs := args[args.size-2]!
          let rhs := args[args.size-1]!
          unless polyCostHead lhs == some rh do throwError "different routine"
          unless ← isDefEq lhs e do throwError "bound does not match"
          for k in [:xs.size] do
            if ← xs[k]!.mvarId!.isAssigned then pure ()
            else if bs[k]! == BinderInfo.instImplicit then
              let v ← synthInstance (← inferType xs[k]!)
              xs[k]!.mvarId!.assign v
          let p ← instantiateMVars (mkAppN c xs)
          if p.hasMVar then throwError "bound has unresolved hypotheses"
          let rhs ← instantiateMVars rhs
          let (_,h) ← synthPoly fuel sz x rhs
          let p ← if tp.getAppFn.isConstOf ``Eq then mkAppM ``le_of_eq #[p] else pure p
          let p ← mkLambdaFVars #[x] p
          let proof ← mkAppM ``ThreeMachine.StackCompiler.Poly.of_le #[p,h]
          let tp ← inferType proof
          return (tp.getAppArgs[3]!,proof)
        catch _ => saved.restore
      let pos := if e.getAppFn.isConstOf ``ThreeMachine.StackCompiler.Uniform.time then es.size-3 else es.size-2
      let r := es[pos]!
      if let some r' ← unfoldDefinition? r then
        let args := es.set! pos r'.headBeta
        return ← synthPoly fuel sz x (mkAppN e.getAppFn args)
    throwError "poly_bound: no bound for {e}"

open Lean Meta Elab Tactic in
elab "poly_bound" : tactic => do
  let goal ← getMainGoal
  goal.withContext do
    let tp := (← instantiateMVars (← goal.getType)).consumeMData
    unless polyExprName tp ``ThreeMachine.StackCompiler.Poly do
      throwError "poly_bound expects Poly"
    let args := tp.getAppArgs
    let sz := args[1]!
    let fn := args[2]!
    let wanted := args[3]!
    let fty ← inferType fn
    forallTelescopeReducing fty fun xs _ => do
      unless xs.size == 1 do throwError "poly_bound expects unary function"
      let body := (mkApp fn xs[0]!).headBeta
      let (d,h) ← synthPoly 1000 sz xs[0]! body
      let le ← mkFreshExprMVar (← mkAppM ``LE.le #[d,wanted])
      let proof ← mkAppM ``ThreeMachine.StackCompiler.Poly.lift #[h,le]
      goal.assign proof
      replaceMainGoal [le.mvarId!]
  evalTactic (← `(tactic| omega))
end

end OAI