]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/numx.ml
Copy ocaml folder from sacerdot's svn repository, rev 4907
[fireball-separation.git] / ocaml / numx.ml
1 open Util
2 open Util.Vars
3 open Pure
4
5 (************ Syntax ************************************)
6
7 (* Normal forms*)
8
9 (* Var n = n-th De Bruijn index, 0-based *)
10
11 (*type nf =
12   | Lam of nf
13   | Var of int
14   | i
15 and i =
16   | I of int * nf listx
17 ;;*)
18 type 'nf i_var_ = [ `I of int * 'nf Listx.listx | `Var of int ]
19 type 'nf i_n_var_ = [ `N of int | 'nf i_var_ ]
20 type 'nf i_num_var_ = [
21   | 'nf i_n_var_
22   | `Match of 'nf i_num_var_ * (*lift*) int * (*branches*)(int * 'nf) list ref * (*args*)'nf list
23 ]
24 type 'nf nf_ = [ `Lam of (* was_unpacked *) bool * 'nf nf_ | `Bomb | `Pacman | 'nf i_num_var_ ]
25 type nf = nf nf_
26 type i_var = nf i_var_;;
27 type i_n_var = nf i_n_var_;;
28 type i_num_var = nf i_num_var_;;
29
30 let hd_of_i_var =
31  function
32    `I (v,_)
33  | `Var v -> v
34
35 let hd_of =
36  function
37    `I (v,_)
38  | `Var v -> Some v
39  | `N _ -> None
40  | `Match _ | `Bomb -> assert false
41
42 let lift m (t : nf) =
43  let rec aux_i_num_var l =
44   function
45     `I(n,args) -> (`I((if n < l then n else n+m),Listx.map (aux l) args) : i_num_var)
46    | `Var n -> `Var (if n < l then n else n+m)
47    | `N _ as x -> x
48    | `Match(t,lift,bs,args) ->
49       `Match(aux_i_num_var l t, lift + m, bs, List.map (aux l) args)
50  and aux l =
51   function
52      #i_num_var as x -> (aux_i_num_var l x :> nf)
53    | `Lam(b,nf) -> `Lam (b,aux (l+1) nf)
54    | `Bomb -> `Bomb
55    | `Pacman -> `Pacman
56  in
57   (aux 0 t : nf)
58 ;;
59
60 (* put t under n lambdas, lifting t accordingtly *)
61 let rec make_lams t =
62  function
63    0 -> t
64  | n when n > 0 -> `Lam (false,lift 1 (make_lams t (n-1)))
65  | _ -> assert false
66
67 let free_vars =
68  let rec aux n = function
69     `N _ -> []
70   | `Var x -> if x < n then [] else [x-n]
71   | `I(x,args) ->
72       (if x < n then [] else [x-n]) @
73       List.concat (List.map (aux n) (Listx.to_list args))
74   | `Lam(_,t) -> aux (n+1) t
75   | `Match(t,liftno,bs,args) ->
76       aux n (t :> nf) @
77       List.concat (List.map (fun (_,t) -> aux (n-liftno) t) !bs) @
78       List.concat (List.map (aux n) args)
79   | `Bomb | `Pacman -> []
80   in aux 0
81 ;;
82
83 module ToScott =
84 struct
85
86 let rec t_of_i_num_var =
87  function
88   | `N n -> Scott.mk_n n
89   | `Var v -> Pure.V v
90   | `Match(t,liftno,bs,args) ->
91       let bs = List.map (fun (n,t) -> n, t_of_nf (lift liftno t)) !bs in
92       let t = t_of_i_num_var t in
93       let m = Scott.mk_match t bs in
94       List.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) m args
95   | `I(v, args) -> Listx.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) (Pure.V v) args
96 and t_of_nf =
97  function
98   | #i_num_var as x -> t_of_i_num_var x
99   | `Lam(b,f) -> Pure.L (t_of_nf f)
100   | `Bomb -> let f x = Pure.A (x,x) in f (Pure.L (f (Pure.V 0)))
101   | `Pacman -> let f x = Pure.A (x,x) in f (Pure.L (Pure.L (f (Pure.V 0))))
102
103 end
104
105
106 (************ Pretty-printing ************************************)
107
108 (* let rec print ?(l=[]) =
109  function
110     `Var n -> print_name l n
111   | `N n -> string_of_int n
112   | `Match(t,bs_lift,bs,args) ->
113      "([" ^ print ~l (t :> nf) ^
114      " ? " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ print ~l (lift bs_lift t)) !bs) ^ "] " ^
115      String.concat " " (List.map (print ~l) args) ^ ")"
116   | `I(n,args) -> "(" ^ print_name l n ^ " " ^ String.concat " " (Listx.to_list (Listx.map (print ~l) args)) ^ ")"
117   | `Lam(_,nf) ->
118      let name = string_of_var (List.length l) in
119      "λ" ^ name ^ "." ^ print ~l:(name::l) (nf : nf)
120 ;; *)
121
122 let rec string_of_term l =
123  let rec string_of_term_w_pars l = function
124   | `Var n -> print_name l n
125   | `N n -> string_of_int n
126   | `I _ as t -> "(" ^ string_of_term_no_pars_app l (t :> nf) ^ ")"
127   | `Lam _ as t -> "(" ^ string_of_term_no_pars_lam l t ^ ")"
128   | `Match(t,bs_lift,bs,args) ->
129      "(match " ^ string_of_term_no_pars l (t :> nf) ^
130      " with " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ string_of_term l (lift bs_lift t)) !bs) ^ "] " ^
131      String.concat " " (List.map (string_of_term l) args) ^ ")"
132   | `Bomb -> "TNT"
133   | `Pacman -> "PAC"
134  and string_of_term_no_pars_app l = function
135   | `I(n, args) -> print_name l n ^ " " ^ String.concat " " (List.map (string_of_term_w_pars l) (Listx.to_list args))
136   | #nf as t -> string_of_term_w_pars l t
137  and string_of_term_no_pars_lam l  = function
138   | `Lam(_,t) -> let name = string_of_var (List.length l) in
139    "λ" ^ name ^ ". " ^ (string_of_term_no_pars_lam (name::l) t)
140   | _ as t -> string_of_term_no_pars l t
141  and string_of_term_no_pars l : nf -> string = function
142   | `Lam _ as t -> string_of_term_no_pars_lam l t
143   | #nf as t -> string_of_term_no_pars_app l t
144  in string_of_term_no_pars l
145 ;;
146
147 let rec print ?(l=[]) = string_of_term l;;
148
149 (************ Hereditary substitutions ************************************)
150
151 let cast_to_i_var =
152  function
153     #i_var as y -> (y : i_var)
154   | t ->
155     prerr_endline (print (t :> nf));
156     assert false (* algorithm failed *)
157
158 let cast_to_i_n_var =
159  function
160     #i_n_var as y -> (y : i_n_var)
161   | t ->
162     prerr_endline (print (t :> nf));
163     assert false (* algorithm failed *)
164
165 let cast_to_i_num_var =
166  function
167     #i_num_var as y -> (y : i_num_var)
168   | t ->
169     prerr_endline (print (t :> nf));
170     assert false (* algorithm failed *)
171
172 let rec mk_app (h : nf) (arg : nf) =
173 (*let res =*)
174  match h with
175     `I(n,args) -> `I(n,Listx.append (Listx.Nil arg) args)
176   | `Var n -> `I(n, Listx.Nil arg)
177   | `Lam(_,nf) -> subst true 0 arg (nf : nf)
178   | `Match(t,lift,bs,args) -> `Match(t,lift,bs,List.append args [arg])
179   | `N _ -> assert false (* Numbers cannot be applied *)
180   | `Bomb | `Pacman -> failwith "mk_app su bomba o pacman"
181 (*in let l = ["v0";"v1";"v2"] in
182 prerr_endline ("mk_app h:" ^ print ~l h ^ " arg:" ^ print ~l:l arg ^ " res:" ^ print ~l:l res); res*)
183
184 and mk_appl h args =
185  (*prerr_endline ("MK_APPL: " ^ print h ^ " " ^ String.concat " " (List.map print args));*)
186  List.fold_left mk_app h args
187
188 and mk_appx h args = Listx.fold_left mk_app h args
189
190 and mk_match t bs_lift bs args =
191  (*prerr_endline ("MK_MATCH: ([" ^ print t ^ "] " ^ String.concat " " (Listx.to_list (Listx.map (fun (n,t) -> string_of_int n ^ " => " ^ print t) bs)) ^ ") " ^ String.concat " " (List.map print args));*)
192  match t with
193     `N m ->
194       (try
195         let h = List.assoc m !bs in
196         let h = lift bs_lift h in
197         mk_appl h args
198        with Not_found ->
199         `Match (t,bs_lift,bs,args))
200   | `I _ | `Var _ | `Match _ -> `Match(t,bs_lift,bs,args)
201
202 and subst delift_by_one what (with_what : nf) (where : nf) =
203  let rec aux_i_num_var l =
204   function
205      `I(n,args) ->
206        if n = what + l then
207         mk_appx (lift l with_what) (Listx.map (aux l) args)
208        else
209         `I ((if delift_by_one && n >= l then n-1 else n), Listx.map (aux l) args)
210    | `Var n ->
211        if n = what + l then
212         lift l with_what
213        else
214         `Var (if delift_by_one && n >= l then n-1 else n)
215    | `N _ as x -> x
216    | `Match(t,bs_lift,bs,args) ->
217        let bs_lift = bs_lift + if delift_by_one then -1 else 0 in
218        let l' = l - bs_lift  in
219        let with_what' = lift l' with_what in
220        (* The following line should be the identity when delift_by_one = true because we
221           are assuming the ts to not contain lambda-bound variables. *)
222        bs := List.map (fun (n,t) -> n,subst false what with_what' t) !bs ;
223        mk_match (cast_to_i_num_var (aux_i_num_var l t)) bs_lift bs (List.map (aux l) args)
224  and aux l(*lift*) =
225 (*function iii -> let res = match iii with*)
226   function
227    | #i_num_var as x -> aux_i_num_var l x
228    | `Lam(b,nf) -> `Lam(b,aux (l+1) nf)
229    | `Bomb -> `Bomb
230    | `Pacman -> `Pacman
231 (*in let ll = ["v0";"v1";"v2"] in
232 prerr_endline ("subst l:" ^ string_of_int l ^ " delift_by_one:" ^ string_of_bool delift_by_one ^ " what:" ^ (List.nth ll what) ^ " with_what:" ^ print ~l:ll with_what ^ " where:" ^ print ~l:ll iii ^ " res:" ^ print ~l:ll res); res*)
233  in
234   aux 0 where
235 ;;
236
237 (************ Parsing ************************************)
238
239 let parse' strs =
240   let rec aux = function
241   | Parser.Lam t -> `Lam (true,aux t)
242   | Parser.App (t1, t2) -> mk_app (aux t1) (aux t2)
243   | Parser.Var v -> `Var v
244   in let (tms, free) = Parser.parse_many strs
245   in (List.map aux tms, free)
246 ;;
247
248 (************** Algorithm(s) ************************)
249
250 let eta_eq x y =
251   let rec aux = function
252   | `Var n , `Var m -> n = m
253   | `I(n1, l1), `I(n2, l2) ->
254     n1 = n2 && Listx.length l1 = Listx.length l2 &&
255     List.for_all aux (List.combine (Listx.to_list l1) (Listx.to_list l2))
256   | `Lam(_,t1), `Lam(_,t2) -> aux (t1,t2)
257   | `Lam(_,t1), t2
258   | t2, `Lam(_,t1) -> aux( t1,  (mk_app (lift 1 t2) (`Var 0)) )
259   | `N n1, `N n2 -> n1 = n2
260   | `Match(u,bs_lift,bs,args), `Match(u',bs_lift',bs',args') ->
261     aux ((u :> nf), (u' :> nf)) && List.length !bs = List.length !bs' &&
262     let bs = List.sort (fun (n,_) (m,_) -> compare n m) !bs in
263     let bs' = List.sort (fun (n,_) (m,_) -> compare n m) !bs' in
264     List.for_all (fun ((n,t),(n',t')) -> n = n' && aux (t, t')) (List.combine bs bs') && List.length args = List.length args' &&
265     List.for_all aux (List.combine args args')
266   | _ -> false
267   in aux (x, y)
268 ;;
269
270 let eta_compare x y =
271   if eta_eq x y then 0 else compare x y
272 ;;
273
274 let eta_eq (#nf as x) (#nf as y) = eta_eq x y;;
275
276 let rec eta_subterm sub t =
277  if eta_eq sub t then true else
278   match t with
279   | `Lam(_,t') -> eta_subterm (lift 1 sub) t'
280   | `Match(u,liftno,bs,args) ->
281      eta_subterm sub (u :> nf)
282      || List.exists (fun (_, t) -> eta_subterm sub (lift liftno t)) !bs
283      || List.exists (eta_subterm sub) args
284   | `I(v, args) -> List.exists (eta_subterm sub) (Listx.to_list args) || (match sub with
285    | `Var v' -> v = v'
286    | `I(v', args') -> v = v'
287     && Listx.length args' < Listx.length args
288     && List.for_all (fun (x,y) -> eta_eq x y) (List.combine (Util.take (Listx.length args') (Listx.to_list args)) (Listx.to_list args'))
289    | _ -> false
290    )
291   | `N _ | `Var _ | `Bomb | `Pacman -> false
292 ;;
293
294 let eta_subterm (#nf as x) (#nf as y) = eta_subterm x y;;