-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.sml
180 lines (146 loc) · 4.05 KB
/
example.sml
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
structure Sort : ABT_SORT =
struct
type t = unit
val eq : t * t -> bool = op=
fun toString () = "exp"
end
structure Vl = AbtValence (structure S = Sort)
structure Ar = AbtArity (Vl)
structure L =
struct
structure Ar = Ar
datatype t =
UNIT
| SIGMA
| AX
| PAIR
| FOO (* a dummy proposition to demonstrate dependency *)
val eq = op=
fun arity UNIT = ([], ())
| arity SIGMA = ([([],()), ([()], ())], ())
| arity AX = ([], ())
| arity PAIR = ([([],()), ([], ())], ())
| arity FOO = ([([],()), ([], ())], ())
fun toString UNIT = "Unit"
| toString SIGMA = "Σ"
| toString AX = "Ax"
| toString PAIR = "Pair"
| toString FOO = "Foo"
end
structure Term = SimpleAbt (L)
structure ShowTm = DebugShowAbt (Term)
structure Language = LcfAbtLanguage (Term)
structure Judgment =
struct
structure Tm = Term
type sort = Language.sort
type env = Language.env
type ren = Language.ren
datatype jdg = TRUE of Tm.abt
fun toString (TRUE p) =
ShowTm.toString p ^ " true"
fun sort _ =
([], ())
fun eq (TRUE m, TRUE n) = Tm.eq (m, n)
fun subst env (TRUE m) = TRUE (Tm.substMetaenv env m)
fun ren env (TRUE m) = TRUE (Tm.renameMetavars env m)
end
structure Lcf = TracedLcf (structure L = Language and Tr = LcfListTrace (type e = string))
structure Tac = LcfTactic (structure Lcf = Lcf and J = Judgment and M = LcfMonadBT)
signature REFINER =
sig
val UnitIntro : Tac.jdg Tac.rule
val SigmaIntro : Tac.jdg Tac.rule
val FooIntro : Tac.jdg Tac.rule
end
structure Refiner :> REFINER =
struct
open Judgment Term
infix $ $$ $# \
structure Tl = Lcf.Tl and V = Term.Metavar
val |> = Lcf.|>
val ::@ = Lcf.::@
infix 2 ::@
infix 3 |>
local structure Notation = TelescopeNotation (Tl) in open Notation infix 4 >: end
local
val i = ref 0
fun newMeta () =
(i := !i + 1;
V.named (Int.toString (!i)))
in
fun makeGoal jdg =
let
val x = newMeta ()
in
((x, jdg), fn ms => check (x $# ms, ()))
end
end
fun UnitIntro (TRUE P) =
let
val L.UNIT $ [] = out P
val ax = L.AX $$ []
in
Tl.empty |> abtToAbs ax
end
fun SigmaIntro (TRUE P) =
let
val L.SIGMA $ [_ \ A, [x] \ B] = out P
val (goalA, holeA) = makeGoal (["SigmaI/proj1"] ::@ TRUE A)
val (goalB, holeB) = makeGoal (["SigmaI/proj2"] ::@ TRUE (substVar (holeA [], x) B))
val pair = L.PAIR $$ [[] \ holeA [], [] \ holeB []]
in
Tl.empty >: goalA >: goalB
|> abtToAbs pair
end
fun FooIntro (TRUE P) =
let
val L.FOO $ [_ \ A, _] = out P
val (goalA, holeA) = makeGoal (["FooI"] ::@ TRUE A)
in
Tl.empty >: goalA |> abtToAbs (holeA [])
end
end
structure Example =
struct
open L Refiner Judgment
open Tac Lcf Term
structure ShowTm = PlainShowAbt (Term)
structure ShowTel = TelescopeUtil (Tl)
infix 5 $ \ then_ orelse_
val x = Var.named "x"
val subgoalsToString =
ShowTel.toString (fn (TRUE p) => ShowTm.toString p ^ " true")
fun run goal (tac : jdg tactic) =
let
val Lcf.|> (psi, vld) = Tac.M.run (tac goal, fn _ => true)
val xs \ m = outb vld
fun prettyGoal (Lcf.::@ (i, jdg)) =
"{[" ^ List.foldr (fn (x, s) => x ^ "." ^ s) "" i ^ "] @ " ^
Judgment.toString jdg
^ "}"
in
print "\n\n";
print (ShowTel.toString prettyGoal psi);
print "\n--------------------------------\n";
print (ShowTm.toString m);
print "\n\n"
end
val mkUnit = check (UNIT $ [], ())
fun mkSigma x a b = check (SIGMA $ [[] \ a, [x] \ b], ())
fun mkFoo a b = check (FOO $ [[] \ a, [] \ b], ())
val x = Var.named "x"
val y = Var.named "y"
val goal =
mkSigma y
(mkSigma x mkUnit mkUnit)
(mkFoo mkUnit (check (`y, ())))
(* to interact with the refiner, try commenting out some of the following lines *)
val script =
Tac.rule SigmaIntro
then_ try (Tac.rule SigmaIntro)
then_ try (Tac.rule UnitIntro)
then_ Tac.rule FooIntro
then_ Tac.rule UnitIntro
val _ = run (TRUE goal) script
end