Skip to content

Commit a621b04

Browse files
author
DengYun
committed
wip: Working on recursive function
1 parent 4502e82 commit a621b04

4 files changed

Lines changed: 36 additions & 17 deletions

File tree

‎Algorithm/TimeCost/Elab.lean‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ partial def isPropOrTypeFuncType(t: Expr): Bool :=
2626
partial def exprToWithCost(expr: Expr)(prevCost: Option Expr): MetaM Expr := do
2727
let type ← inferType expr
2828
logInfo m!"Working: {expr} with type {type}"
29-
if type.isType then
30-
return expr
31-
if type.isProp then
32-
return expr
3329

3430
let withPrevCost(expr: Expr): MetaM Expr := do
3531
match prevCost with
@@ -81,7 +77,13 @@ partial def exprToWithCost(expr: Expr)(prevCost: Option Expr): MetaM Expr := do
8177
bi)
8278
withPrevCost newExpr
8379
| proj typeName idx struct =>
84-
return expr
80+
let newStruct ← exprToWithCost struct none
81+
let newStructType ← inferType newStruct
82+
logInfo m!"newStruct={newStruct} newStructType={newStructType}"
83+
if isWithCostType newStructType then
84+
throwError m!"TODO"
85+
else
86+
return expr
8587
| app f a =>
8688
let newF ← exprToWithCost f none
8789
let newA ← exprToWithCost a none
@@ -100,18 +102,18 @@ partial def exprToWithCost(expr: Expr)(prevCost: Option Expr): MetaM Expr := do
100102
-- 最内层的调用
101103
let genApply(f: Expr)(a: Expr): Expr :=
102104
let app := Expr.app f a
103-
if isPropOrTypeFuncType newAType then
105+
if isPropOrTypeFuncType newAType ∨ isPropOrTypeFuncType retType then
104106
app
105107
else
106108
if isWithCostType retType then
107-
-- 带类型调用,增加消耗 ret.addCost 1
109+
-- 返回值带Cost,增加消耗 ret.addCost 1
108110
let retValueType := getValueType retType
109111
let addCost := Expr.const `Algorithm.WithCost.addCost []
110112
let addCost := Expr.app addCost retValueType
111113
let addCost := Expr.app addCost app
112114
Expr.app addCost (Expr.lit (.natVal 1))
113115
else
114-
-- 不带类型调用,构造消耗 {cost:= 1, val:= ret}
116+
-- 返回值不带Cost,构造消耗 {cost:= 1, val:= ret}
115117
let mkCost := Expr.const `Algorithm.WithCost.mk []
116118
let mkCost := Expr.app mkCost retType
117119
let mkCost := Expr.app mkCost (Expr.lit (.natVal 1))
@@ -153,7 +155,10 @@ partial def exprToWithCost(expr: Expr)(prevCost: Option Expr): MetaM Expr := do
153155
else
154156
-- 直接调用
155157
return genApply1 newF newA
156-
158+
| forallE binderName binderType body binderInfo =>
159+
let binderType ← exprToWithCost binderType none
160+
let binderBody ← exprToWithCost body none
161+
return .forallE binderName binderType binderBody binderInfo
157162
| _ =>
158163
throwError m!"不支持的表达式类型:{expr}"
159164

‎Algorithm/TimeCost/Lib.lean‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2-
def Nat.succ.withCost := Nat.succ
2+
def Nat.succ.withCost := @Nat.succ
33

4-
def Nat.add.withCost := Nat.add
4+
def Nat.add.withCost := @Nat.add
5+
6+
def Nat.below.withCost := @Nat.below

‎Examples/Basic/Factorial.lean‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Mathlib
2+
import Algorithm
3+
4+
open Asymptotics Algorithm
5+
6+
-- 斐波那契函数的定义
7+
def fac: ℕ → ℕ
8+
| 0 => 1
9+
| n+1 => (n+1) * fac n
10+
11+
-- 期待通过Meta Programming自动生成这个函数:
12+
#autogen_fun_with_cost fac
13+
14+
#check Nat.brecOn
15+
#check fac.match_1
16+
#check Nat.below

‎Examples/Basic/Fibonacci.lean‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Mathlib
22
import Algorithm
33

4-
open Asymptotics
4+
open Asymptotics Algorithm
55

66
-- 斐波那契函数的定义
77
def fib: ℕ → ℕ
@@ -10,11 +10,7 @@ def fib: ℕ → ℕ
1010
| n+2 => fib (n+1) + fib n
1111

1212
-- 期待通过Meta Programming自动生成这个函数:
13-
def fib.withCost : ℕ → WithCost ℕ
14-
| 0 => ⟨1, 0⟩
15-
| 1 => ⟨1, 1⟩
16-
| n+2 => fib.withCost (n+1) + fib.withCost n
17-
13+
#autogen_fun_with_cost fib
1814

1915
-- 正确性证明:
2016
-- f_0 = 0, f_1 = 1, f_n = f_{n-1}+f_{n-2} for n ≥ 2

0 commit comments

Comments
 (0)