-
Notifications
You must be signed in to change notification settings - Fork 1
/
expression.ml
241 lines (223 loc) · 5.21 KB
/
expression.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
open Syntax
(* Integers *)
(* Divide one decimal digit by 2 *)
let divby2 c n =
let open Stdlib in
n mod 2 = 1, (if c then n / 2 + 5 else n / 2)
(* Divide a list of decimal digits by 2 *)
let get_lsb_div l = List.fold_left_map divby2 false l
let rec trim =
function
| 0 :: l -> trim l
| l -> l
(* Convert a list of decimal digits in binary (reversed) *)
let rec dec_2_bin_rev =
function
| [] -> []
| l -> let b, l = get_lsb_div l in
let l = trim l in
b :: dec_2_bin_rev l
let char2int =
function
| '0' -> 0
| '1' -> 1
| '2' -> 2
| '3' -> 3
| '4' -> 4
| '5' -> 5
| '6' -> 6
| '7' -> 7
| '8' -> 8
| '9' -> 9
| _ -> 0
let rec bin2string =
function
| [true] -> xh
| true :: l -> x1 (bin2string l)
| false :: l -> x0 (bin2string l)
| _ -> assert false
let helper s = String.fold_right (fun c l -> char2int c :: l) s []
let int n =
match String.get n 0 with
| '0' -> zero
| '-' -> zneg (bin2string @@ dec_2_bin_rev @@ List.tl @@ helper n)
| _ -> zpos (bin2string @@ dec_2_bin_rev @@ helper n)
let to_list x = List.fold_right cons x (nil ())
let rec my_fold f o =
function
| [] -> o
| [x] -> x
| x :: l -> f x (my_fold f o l)
let unary_op =
function
| "imax" -> imax
| "rmax" -> rmax
| "imin" -> imin
| "rmin" -> rmin
| "card" -> card
| "dom" -> dom
| "ran" -> ran
| "POW" -> pow
| "POW1" -> pow1
| "FIN" -> fin
| "FIN1" -> fin1
| "union" -> union_gen
| "inter" -> inter_gen
| "seq" -> seq
| "seq1" -> seq1
| "iseq" -> iseq
| "iseq1" -> iseq1
| "-i" -> minusi
| "-r" -> minusr
| "~" -> inversion
| "size" -> size
| "perm" -> perm
| "first" -> first
| "last" -> last
| "id" -> identity
| "closure" -> closure
| "closure1" -> closure1
| "tail" -> tail
| "front" -> front
| "rev" -> rev
| "conc" -> conc
| "succ" -> succ
| "pred" -> pred
| "rel" -> rel
| "fnc" -> fnc
| "real" -> real
| "floor" -> floor
| "ceiling" -> ceiling
| "tree" -> tree
| "btree" -> btree
| "top" -> top
| "sons" -> sons
| "prefix" -> pref
| "postfix" -> post
| "sizet" -> sizet
| "mirror" -> mirror
| "left" -> left_tree
| "right" -> right_tree
| "infix" -> infix_tree
| "bin" -> bin_unary
| x -> failwith ("Invalid unary expression : " ^ x)
let default x y = trueu
let binary_op =
function
| "=>" -> imply
| "<=>" -> equiv
| ":" -> belong
| "/:" -> notbelong
| "<:" -> included
| "/<:" -> notincluded
| "<<:" -> strictincluded
| "/<<:" -> notstrictincluded
| "=" -> equal
| "/=" -> notequal
(* integer comparison *)
| ">=i" -> igeq
| ">i" -> igreater
| "<i" -> ismaller
| "<=i" -> ileq
(* real comparison *)
| ">=r" -> rgeq
| ">r" -> rgreater
| "<r" -> rsmaller
| "<=r" -> rleq
(* float comparison *)
| ">=f" -> fgeq
| ">f" -> fgreater
| "<f" -> fsmaller
| "<=f" -> fleq
| "," -> pair
| "*i" -> iprod
| "*r" -> rprod
| "*f" -> fprod
| "*s" -> sprod
| "**i" -> iexp
| "**r" -> rexp
| "+i" -> iplus
| "+r" -> rplus
| "+f" -> fplus
| "+->" -> partial
| "+->>" -> partialsurj
| "-i" -> iminus
| "-r" -> rminus
| "-f" -> fminus
| "-s" -> sminus
| "-->" -> total
| "-->>" -> totalsurj
| "->" -> headinsert
| ".." -> interval
| "/i" -> idiv
| "/r" -> rdiv
| "/f" -> fdiv
| "/\\" -> inter
| "/|\\" -> headrestrict
| ";" -> compose
| "<+" -> overload
| "<->" -> relation
| "<-" -> tailinsert
| "<<|" -> domsubtract
| "<|" -> domrestrict
| ">+>" -> partialinject
| ">->" -> totalinject
| ">->>" -> totalbiject
| "><" -> directprod
| "||" -> parallelprod
| "\\/" -> union
| "\\|/" -> tailrestrict
| "^" -> concat
| "mod" -> ( mod )
| "|->" -> map
| "|>" -> imagerestrict
| "|>>" -> imagesubtract
| "[" -> image (* image of a relation *)
| "(" -> eval (* image of a function *)
| "prj1" -> prj1
| "prj2" -> prj2
| "iterate" -> iterate
| "const" -> const
| "rank" -> rank
| "father" -> father
| "subtree" -> subtree
| "arity" -> arity
| x -> failwith ("Invalid binary expression : " ^ x)
let ternary_op =
function
| "son" -> son
| "bin" -> bin_ternary
| x -> failwith ("Invalid ternary expression : " ^ x)
let nary_op =
function
| "[" -> fun x -> sequence (to_list x)
| "{" -> fun x -> extension (to_list x)
| "&" -> my_fold conj trueu
| "or" -> my_fold disj falseu
| x -> failwith ("Invalid n-ary expression : " ^ x)
let quantified_pred_op s args body =
match s with
| "!" -> List.fold_left (fun x (i,t) -> forall i t x) body args
| "#" -> List.fold_left (fun x (i,t) -> exists i t x) body args
| x -> failwith ("Invalid predicate quantifier : " ^ x)
let rec helper =
function
| [] -> assert false
| [(_,y)] -> typnil y
| (_,y) :: l -> typcons y (helper l)
let quantified_exp_op s args b1 b2 =
let helper = helper args in
let args = List.map (fun (x,y) -> x, Some (tau y)) args in
let b1 = Lp.Binder(Lp.Uid "λ", args, b1) in
let b2 = Lp.Binder(Lp.Uid "λ", args, b2) in
begin
match s with
| "%" -> lambda helper
| "iSIGMA" -> isigma helper
| "rSIGMA" -> rsigma helper
| "iPI" -> ipi helper
| "rPI" -> rpi helper
| "INTER" -> inter' helper
| "UNION" -> union' helper
| x -> failwith ("Invalid expression quantifier : " ^ x)
end b1 b2