|
1 | 1 | import Lean |
| 2 | +import Algorithm.TimeCost.Def |
2 | 3 |
|
3 | | -open Lean Meta Elab Term |
4 | | - |
5 | | -syntax (name := time_cost_term)(priority := 0) "time_cost" term : term |
6 | | - |
7 | | -mutual |
8 | | - |
9 | | - partial def time_cost_for_syntax: Syntax → TermElabM Expr := fun stx => |
10 | | - withIncRecDepth do |
11 | | - match stx with |
12 | | - | `($_:num) => return mkNatLit 0 |
13 | | - | `($_:ident) => return mkNatLit 0 |
14 | | - | `($a + $b) => |
15 | | - let acost ← time_cost_for_syntax a |
16 | | - let bcost ← time_cost_for_syntax b |
17 | | - return ← mkAdd (mkNatLit 1) (← mkAdd acost bcost) |
18 | | - | `($f $args*) => |
19 | | - -- 如果函数本身是计算得出的,也需要考虑合成过程的开销 |
20 | | - let fcost ← time_cost_for_syntax f |
21 | | - -- 函数展开的计算开销 |
22 | | - -- let callcostFun ← time_cost_fun_for_syntax f |
23 | | - -- let argsExpr ← args.mapM (λ stx => elabTerm stx none) |
24 | | - -- let callcost := mkAppN callcostFun argsExpr |
25 | | - -- 参数的计算开销 |
26 | | - let argscost ← args.foldlM (init:=mkNatLit 0) (fun sum arg => do |
27 | | - let argCost ← time_cost_for_syntax arg |
28 | | - mkAdd sum argCost |
| 4 | +open Lean Meta Elab Term Command |
| 5 | + |
| 6 | +open Expr |
| 7 | + |
| 8 | +partial def exprToWithCost(expr prevCost: Expr): MetaM Expr := do |
| 9 | + let type ← inferType expr |
| 10 | + logInfo m!"Working: {expr} with type {type}" |
| 11 | + |
| 12 | + match expr with |
| 13 | + | fvar _ => |
| 14 | + let ret := Expr.app |
| 15 | + (Expr.app (Expr.app (Expr.const `Algorithm.WithCost.mk []) type) prevCost) |
| 16 | + expr |
| 17 | + logInfo m!"Ret: {ret}" |
| 18 | + return ret |
| 19 | + | lam name type body bi => |
| 20 | + logInfo m!"lam {name} {type} {body}" |
| 21 | + return (Expr.lam name type |
| 22 | + (← withLocalDecl name bi type |
| 23 | + (fun fvar => do |
| 24 | + let body := body.instantiate1 fvar |
| 25 | + let ret ← exprToWithCost body prevCost |
| 26 | + let ret := ret.liftLooseBVars 0 1 |
| 27 | + let ret := ret.replaceFVar fvar $ Expr.bvar 0 |
| 28 | + return ret |
| 29 | + ) |
29 | 30 | ) |
| 31 | + bi) |
| 32 | + -- | app a b => |
| 33 | + -- logInfo m!"app {a} {b}" |
| 34 | + -- match a with |
| 35 | + -- | _ => throwError m!"TODO" |
| 36 | + | _ => |
| 37 | + throwError m!"不支持的表达式类型:{expr}" |
| 38 | + |
| 39 | +elab "#autogen_fun_with_cost" declName:ident : command => do |
| 40 | + let decl := declName.getId |
| 41 | + let newName := Name.str decl "withCost" |
30 | 42 |
|
31 | | - -- return ← mkAdd (mkNatLit 1) (← mkAdd fcost (← mkAdd argscost callcost)) |
32 | | - return ← mkAdd (mkNatLit 1) (← mkAdd fcost argscost) |
33 | | - | `(($stx)) => time_cost_for_syntax stx |
34 | | - | _ => throwError "Syntax {stx} not supported for time_cost" |
| 43 | + try |
| 44 | + let env ← getEnv |
| 45 | + let info := env.find? decl |
| 46 | + match info with |
| 47 | + | none => |
| 48 | + throwError m!"失败:定义未找到" |
| 49 | + | some info => |
| 50 | + match info with |
| 51 | + | ConstantInfo.defnInfo val => |
| 52 | + liftTermElabM <| do |
| 53 | + let newDef := ← exprToWithCost val.value (Expr.lit (.natVal 0)) |
| 54 | + let newDefType := ← inferType newDef |
| 55 | + logInfo m!"新定义:{newDef}" |
| 56 | + let decl := .defnDecl { |
| 57 | + name := newName |
| 58 | + levelParams := val.levelParams |
| 59 | + type := newDefType |
| 60 | + value := newDef |
| 61 | + safety := .safe |
| 62 | + hints := val.hints |
| 63 | + } |
| 64 | + addDecl decl |
| 65 | + compileDecl decl |
| 66 | + logInfo m!"定义:{newName}" |
| 67 | + | _ => throwError m!"失败:定义不是函数定义" |
35 | 68 |
|
36 | | - partial def time_cost_fun_for_syntax: Syntax → TermElabM Expr := fun stx => |
37 | | - withIncRecDepth do |
38 | | - match stx with |
39 | | - -- | _ => return mkNatLit 0 |
40 | | - | _ => throwError "Syntax {stx} not supported for time_cost_fun" |
| 69 | + catch e => |
| 70 | + throwError m!"失败:{e.toMessageData}" |
41 | 71 |
|
42 | | -end |
| 72 | +def test(a b: Nat) := a |
43 | 73 |
|
44 | | -@[term_elab time_cost_term] |
45 | | -def time_cost_term_impl : TermElab := fun stx type? => do |
46 | | - match stx with |
47 | | - | `(time_cost $stx1) => return ← time_cost_for_syntax stx1 |
48 | | - | _ => throwError "Bad syntax" |
| 74 | +#eval Algorithm.WithCost.mk 2 2 |
49 | 75 |
|
50 | | -syntax (name := time_cost_fun_term)(priority := 0) "time_cost_fun" term : term |
| 76 | +#autogen_fun_with_cost test |
51 | 77 |
|
| 78 | +#print test.withCost |
52 | 79 |
|
53 | | -@[term_elab time_cost_fun_term] |
54 | | -def time_cost_fun_term_impl : TermElab := fun stx type? => do |
55 | | - match stx with |
56 | | - | `(time_cost_fun $stx1) => return ← time_cost_for_syntax stx1 |
57 | | - | _ => throwError "Bad syntax" |
| 80 | +#eval test.withCost 1 2 |
0 commit comments