-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.lean
More file actions
92 lines (74 loc) · 1.84 KB
/
Copy pathGenerator.lean
File metadata and controls
92 lines (74 loc) · 1.84 KB
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
import Lean
open Lean
-- PureScript AST
inductive PSExpr where
| Var : String → PSExpr
| Lam : String → PSExpr → PSExpr
| App : PSExpr → PSExpr → PSExpr
| If : PSExpr → PSExpr → PSExpr → PSExpr
deriving Repr
-- Helper to create variables
def var (name : String) : PSExpr := PSExpr.Var name
-- Helper to create lambdas
def lam (name : String) (body : PSExpr) : PSExpr := PSExpr.Lam name body
-- Helper to create application
def app (f arg : PSExpr) : PSExpr := PSExpr.App f arg
-- Helper to create if
def iff (cond then_ else_ : PSExpr) : PSExpr := PSExpr.If cond then_ else_
-- PureScript code generator
def generatePS : PSExpr → String
| .Var name => name
| .Lam arg body => s!"(\\{arg} -> {generatePS body})"
| .App f arg => s!"({generatePS f} ({generatePS arg}))"
| .If cond then_ else_ => s!"if {generatePS cond} then {generatePS then_} else {generatePS else_}"
-- Examples
def identity := lam "x" (var "x")
#eval generatePS identity
def compose :=
lam "f" (
lam "g" (
lam "x" (
app (app (var "f") (app (app (var "g") (var "x")) (var "x"))) (var "y")
)
)
)
#eval generatePS compose
def conditional :=
lam "x" (
iff (app (var "isZero") (var "x"))
(var "zero")
(var "success")
)
#eval generatePS conditional
-- Church encoding example
def churchTrue :=
lam "t" (
lam "f" (
var "t"
)
)
def churchFalse :=
lam "f" (
lam "t" (
var "t"
)
)
def churchAnd :=
lam "p" (
lam "q" (
app (app (var "p") (var "q")) (churchFalse)
)
)
#eval generatePS churchTrue
#eval generatePS churchFalse
#eval generatePS churchAnd
-- Y combinator
def yCombinator :=
lam "f" (
app (lam "x" (
app (var "f") (app (var "x") (var "x"))
)) (lam "x" (
app (var "f") (app (var "x") (var "x"))
))
)
#eval generatePS yCombinator