-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathterm.ml
210 lines (199 loc) · 6.36 KB
/
term.ml
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
type pnterm =
| PNVarName of string
| PNArrow of pnterm * pnterm
| PNOr of pnterm * pnterm
| PNAnd of pnterm * pnterm
| PNTop
| PNBot
let rec pp_print_pnterm_pr pr ppf = function
| PNVarName s ->
Format.fprintf ppf "%s" s
| PNArrow (t,PNBot) ->
Format.fprintf ppf "@[~%a@]"
(pp_print_pnterm_pr 1) t
| PNArrow (t1,t2) ->
Format.fprintf ppf "@[";
if pr < 4 then Format.fprintf ppf "(";
Format.fprintf ppf "%a@ ->@ %a"
(pp_print_pnterm_pr 3) t1
(pp_print_pnterm_pr 4) t2;
if pr < 4 then Format.fprintf ppf ")";
Format.fprintf ppf "@]"
| PNAnd (PNArrow (t1,t2),PNArrow (t2t,t1t))
when t1=t1t && t2=t2t ->
Format.fprintf ppf "@[";
if pr < 5 then Format.fprintf ppf "(";
Format.fprintf ppf "%a@ <->@ %a"
(pp_print_pnterm_pr 4) t1
(pp_print_pnterm_pr 4) t2;
if pr < 5 then Format.fprintf ppf ")";
Format.fprintf ppf "@]"
| PNAnd (t1,t2) ->
Format.fprintf ppf "@[";
if pr < 2 then Format.fprintf ppf "(";
Format.fprintf ppf "%a@ /\\@ %a"
(pp_print_pnterm_pr 1) t1
(pp_print_pnterm_pr 2) t2;
if pr < 2 then Format.fprintf ppf ")";
Format.fprintf ppf "@]"
| PNOr (t1,t2) ->
Format.fprintf ppf "@[";
if pr < 3 then Format.fprintf ppf "(";
Format.fprintf ppf "%a@ \\/@ %a"
(pp_print_pnterm_pr 2) t1
(pp_print_pnterm_pr 3) t2;
if pr < 3 then Format.fprintf ppf ")";
Format.fprintf ppf "@]"
| PNTop ->
Format.fprintf ppf "True"
| PNBot ->
Format.fprintf ppf "False"
let pp_print_pnterm = pp_print_pnterm_pr 5
type pterm =
| PVar of int
| PArrow of pterm * pterm
| POr of pterm * pterm
| PAnd of pterm * pterm
| PTop
| PBot
type name_env = {
mutable maxvar : int;
dict : (string,int) Hashtbl.t;
reverse_dict : (int,string) Hashtbl.t
}
let new_name_env () = {
maxvar = 0;
dict = Hashtbl.create 10;
reverse_dict = Hashtbl.create 100
}
let empty_env = new_name_env ()
let rec convert_name_impl env = function
| PNVarName s ->
begin try
PVar (Hashtbl.find env.dict s)
with Not_found ->
let t = PVar env.maxvar in
Hashtbl.add env.dict s env.maxvar;
Hashtbl.add env.reverse_dict env.maxvar s;
env.maxvar <- env.maxvar + 1;
t
end
| PNArrow (t1,t2) ->
PArrow (convert_name_impl env t1,
convert_name_impl env t2)
| PNAnd (PNArrow (t1,t2),PNArrow(t2t,t1t))
when t1=t1t && t2=t2t ->
let ct1 = convert_name_impl env t1 in
let ct2 = convert_name_impl env t2 in
PAnd (PArrow (ct1,ct2),PArrow(ct2,ct1))
| PNAnd (t1,t2) ->
PAnd (convert_name_impl env t1,
convert_name_impl env t2)
| PNOr (t1,t2) ->
POr (convert_name_impl env t1,
convert_name_impl env t2)
| PNTop -> PTop
| PNBot -> PBot
let convert_name tn =
let env = new_name_env () in
let t = convert_name_impl env tn in
t, env
let rec pp_print_pterm_pr env pr ppf = function
| PVar n ->
begin try
Format.fprintf ppf "%s" (Hashtbl.find env.reverse_dict n)
with Not_found ->
Format.fprintf ppf "?%d" n
end
| PArrow (t,PBot) ->
Format.fprintf ppf "@[~%a@]"
(pp_print_pterm_pr env 1) t
| PArrow (t1,t2) ->
Format.fprintf ppf "@[";
if pr < 4 then Format.fprintf ppf "(";
Format.fprintf ppf "%a@ ->@ %a"
(pp_print_pterm_pr env 3) t1
(pp_print_pterm_pr env 4) t2;
if pr < 4 then Format.fprintf ppf ")";
Format.fprintf ppf "@]"
| PAnd (PArrow (t1,t2),PArrow (t2t,t1t))
when t1=t1t && t2=t2t ->
Format.fprintf ppf "@[";
if pr < 5 then Format.fprintf ppf "(";
Format.fprintf ppf "%a@ <->@ %a"
(pp_print_pterm_pr env 4) t1
(pp_print_pterm_pr env 4) t2;
if pr < 5 then Format.fprintf ppf ")";
Format.fprintf ppf "@]"
| PAnd (t1,t2) ->
Format.fprintf ppf "@[";
if pr < 2 then Format.fprintf ppf "(";
Format.fprintf ppf "%a@ /\\@ %a"
(pp_print_pterm_pr env 1) t1
(pp_print_pterm_pr env 2) t2;
if pr < 2 then Format.fprintf ppf ")";
Format.fprintf ppf "@]"
| POr (t1,t2) ->
Format.fprintf ppf "@[";
if pr < 3 then Format.fprintf ppf "(";
Format.fprintf ppf "%a@ \\/@ %a"
(pp_print_pterm_pr env 2) t1
(pp_print_pterm_pr env 3) t2;
if pr < 3 then Format.fprintf ppf ")";
Format.fprintf ppf "@]"
| PTop ->
Format.fprintf ppf "True"
| PBot ->
Format.fprintf ppf "False"
let pp_print_pterm env = pp_print_pterm_pr env 5
let rec pp_print_pterm_latex_internal env pr ppf = function
| PVar n ->
begin try
Format.fprintf ppf "%s" (Hashtbl.find env.reverse_dict n)
with Not_found ->
Format.fprintf ppf "?%d" n
end
| PArrow (t,PBot) ->
Format.fprintf ppf "@[\\lnot@ %a@]"
(pp_print_pterm_latex_internal env 1) t
| PArrow (t1,t2) ->
Format.fprintf ppf "@[";
if pr < 4 then Format.fprintf ppf "(";
Format.fprintf ppf "%a@ \\to@ %a"
(pp_print_pterm_latex_internal env 3) t1
(pp_print_pterm_latex_internal env 4) t2;
if pr < 4 then Format.fprintf ppf ")";
Format.fprintf ppf "@]"
| PAnd (PArrow (t1,t2),PArrow (t2t,t1t))
when t1=t1t && t2=t2t ->
Format.fprintf ppf "@[";
if pr < 5 then Format.fprintf ppf "(";
Format.fprintf ppf "%a@ \\leftrightarrow@ %a"
(pp_print_pterm_latex_internal env 4) t1
(pp_print_pterm_latex_internal env 4) t2;
if pr < 5 then Format.fprintf ppf ")";
Format.fprintf ppf "@]"
| PAnd (t1,t2) ->
Format.fprintf ppf "@[";
if pr < 2 then Format.fprintf ppf "(";
Format.fprintf ppf "%a@ \\land@ %a"
(pp_print_pterm_latex_internal env 1) t1
(pp_print_pterm_latex_internal env 2) t2;
if pr < 2 then Format.fprintf ppf ")";
Format.fprintf ppf "@]"
| POr (t1,t2) ->
Format.fprintf ppf "@[";
if pr < 3 then Format.fprintf ppf "(";
Format.fprintf ppf "%a@ \\lor@ %a"
(pp_print_pterm_latex_internal env 2) t1
(pp_print_pterm_latex_internal env 3) t2;
if pr < 3 then Format.fprintf ppf ")";
Format.fprintf ppf "@]"
| PTop ->
Format.fprintf ppf "\\top"
| PBot ->
Format.fprintf ppf "\\bot"
let pp_print_pterm_latex env pr ppf t =
Format.fprintf ppf "@[$";
pp_print_pterm_latex_internal env pr ppf t;
Format.fprintf ppf "$@]"