]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/num.ml
9d803139e4dcafa89e1938c08752cdc5d2ff3110
[fireball-separation.git] / ocaml / num.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 var = int * (* arity of variable*) int;;
19 type 'nf i_var_ = [ `I of var * 'nf Listx.listx | `Var of var ]
20 type 'nf i_n_var_ = [ `N of int | 'nf i_var_ ]
21 type 'nf i_num_var_ = [
22   | 'nf i_n_var_
23   | `Match of 'nf i_num_var_ * (* originating var *) var * (*lift*) int * (*branches*)(int * 'nf) list ref * (*args*)'nf list
24 ]
25 type 'nf nf_ = [ `Lam of (* was_unpacked *) bool * 'nf nf_ | 'nf i_num_var_ ]
26 type nf = nf nf_
27 type i_var = nf i_var_;;
28 type i_n_var = nf i_n_var_;;
29 type i_num_var = nf i_num_var_;;
30
31 let hd_of_i_var =
32  function
33    `I ((v,_),_)
34  | `Var (v,_) -> v
35
36 let hd_of =
37  function
38    `I ((v,_),_)
39  | `Var(v,_) -> Some v
40  | `N _ -> None
41  | `Match _ -> assert false
42
43 let lift m (t : nf) =
44  let aux_var l (n, ar) = (if n < l then n else n+m), ar in
45  let rec aux_i_num_var l =
46   function
47     `I(v,args) -> `I(aux_var l v, Listx.map (aux l) args)
48    | `Var v -> `Var(aux_var l v)
49    | `N _ as x -> x
50    | `Match(t,v,lift,bs,args) ->
51       `Match(aux_i_num_var l t, v, lift + m, bs, List.map (aux l) args)
52  and aux l =
53   function
54      #i_num_var as x -> (aux_i_num_var l x :> nf)
55    | `Lam(b,nf) -> `Lam (b, aux (l+1) nf)
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   in aux 0
80 ;;
81
82 module ToScott =
83 struct
84
85 let rec t_of_i_num_var =
86  function
87   | `N n -> Scott.mk_n n
88   | `Var(v,_) -> Pure.V v
89   | `Match(t,_,liftno,bs,args) ->
90       let bs = List.map (fun (n,t) -> n, t_of_nf (lift liftno t)) !bs in
91       let t = t_of_i_num_var t in
92       let m = Scott.mk_match t bs in
93       List.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) m args
94   | `I((v,_), args) -> Listx.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) (Pure.V v) args
95 and t_of_nf =
96  function
97   | #i_num_var as x -> t_of_i_num_var x
98   | `Lam(b,f) -> Pure.L (t_of_nf f)
99
100 end
101
102
103 (************ Pretty-printing ************************************)
104
105 let rec print ?(l=[]) =
106  function
107     `Var(n,_) -> print_name l n
108   | `N n -> string_of_int n
109   | `Match(t,_,bs_lift,bs,args) ->
110      "([" ^ print ~l (t :> nf) ^
111      " ? " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ print ~l (lift bs_lift t)) !bs) ^ "] " ^
112      String.concat " " (List.map (print ~l) args) ^ ")"
113   | `I((n,_),args) -> "(" ^ print_name l n ^ " " ^ String.concat " " (Listx.to_list (Listx.map (print ~l) args)) ^ ")"
114   | `Lam(_,nf) ->
115      let name = string_of_var (List.length l) in
116      "λ" ^ name ^ "." ^ print ~l:(name::l) (nf : nf)
117 ;;
118
119 let rec string_of_term l =
120  let rec string_of_term_w_pars l = function
121   | `Var(n,_) -> print_name l n
122   | `N n -> string_of_int n
123   | `I _ as t -> "(" ^ string_of_term_no_pars_app l (t :> nf) ^ ")"
124   | `Lam _ as t -> "(" ^ string_of_term_no_pars_lam l t ^ ")"
125   | `Match(t,_,bs_lift,bs,args) ->
126      "(match " ^ string_of_term_no_pars l (t :> nf) ^
127      " with " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ string_of_term l (lift bs_lift t)) !bs) ^ "] " ^
128      String.concat " " (List.map (string_of_term l) args) ^ ")"
129  and string_of_term_no_pars_app l = function
130   | `I((n,_), args) -> print_name l n ^ " " ^ String.concat " " (List.map (string_of_term_w_pars l) (Listx.to_list args))
131   | #nf as t -> string_of_term_w_pars l t
132  and string_of_term_no_pars_lam l  = function
133   | `Lam(_,t) -> let name = string_of_var (List.length l) in
134    "λ" ^ name ^ ". " ^ (string_of_term_no_pars_lam (name::l) t)
135   | _ as t -> string_of_term_no_pars l t
136  and string_of_term_no_pars l : nf -> string = function
137   | `Lam _ as t -> string_of_term_no_pars_lam l t
138   | #nf as t -> string_of_term_no_pars_app l t
139  in string_of_term_no_pars l
140 ;;
141
142 let print ?(l=[]) = string_of_term l;;
143 let string_of_nf t = string_of_term [] (t:>nf);;
144
145 (************ Hereditary substitutions ************************************)
146
147 let cast_to_i_var =
148  function
149     #i_var as y -> (y : i_var)
150   | t ->
151     prerr_endline (print (t :> nf));
152     assert false (* algorithm failed *)
153
154 let cast_to_i_n_var =
155  function
156     #i_n_var as y -> (y : i_n_var)
157   | t ->
158     prerr_endline (print (t :> nf));
159     assert false (* algorithm failed *)
160
161 let cast_to_i_num_var =
162  function
163     #i_num_var as y -> (y : i_num_var)
164   | t ->
165     prerr_endline (print (t :> nf));
166     assert false (* algorithm failed *)
167
168 let rec mk_app (h : nf) (arg : nf) =
169 (*let res =*)
170  match h with
171     `I(v,args) -> `I(v,Listx.append (Listx.Nil arg) args)
172   | `Var v -> `I(v, Listx.Nil arg)
173   | `Lam(_,nf) -> subst true 0 arg (nf : nf) (* AC FIXME sanity check on arity *)
174   | `Match(t,v,lift,bs,args) -> `Match(t,v,lift,bs,List.append args [arg])
175   | `N _ -> assert false (* Numbers cannot be applied *)
176 (*in let l = ["v0";"v1";"v2"] in
177 prerr_endline ("mk_app h:" ^ print ~l h ^ " arg:" ^ print ~l:l arg ^ " res:" ^ print ~l:l res); res*)
178
179 and mk_appl h args =
180  (*prerr_endline ("MK_APPL: " ^ print h ^ " " ^ String.concat " " (List.map print args));*)
181  List.fold_left mk_app h args
182
183 and mk_appx h args = Listx.fold_left mk_app h args
184
185 and mk_match t ar bs_lift bs args =
186  (*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));*)
187  match t with
188     `N m ->
189       (try
190         let h = List.assoc m !bs in
191         let h = lift bs_lift h in
192         mk_appl h args
193        with Not_found ->
194         `Match (t,ar,bs_lift,bs,args))
195   | `I _ | `Var _ | `Match _ -> `Match(t,ar,bs_lift,bs,args)
196
197 and subst delift_by_one what (with_what : nf) (where : nf) =
198  let rec aux_i_num_var l =
199   function
200      `I((n,ar),args) ->
201        if n = what + l then
202         mk_appx (lift l with_what) (Listx.map (aux l) args)
203        else
204         `I (((if delift_by_one && n >= l then n-1 else n), ar), Listx.map (aux l) args)
205    | `Var(n,ar) ->
206        if n = what + l then
207         lift l with_what
208        else
209         `Var((if delift_by_one && n >= l then n-1 else n), ar)
210    | `N _ as x -> x
211    | `Match(t,v,bs_lift,bs,args) ->
212        let bs_lift = bs_lift + if delift_by_one then -1 else 0 in
213        let l' = l - bs_lift  in
214        let with_what' = lift l' with_what in
215        (* The following line should be the identity when delift_by_one = true because we
216           are assuming the ts to not contain lambda-bound variables. *)
217        bs := List.map (fun (n,t) -> n,subst false what with_what' t) !bs ;
218        mk_match (cast_to_i_num_var (aux_i_num_var l t)) v bs_lift bs (List.map (aux l) args)
219  and aux l(*lift*) =
220 (*function iii -> let res = match iii with*)
221   function
222    | #i_num_var as x -> aux_i_num_var l x
223    | `Lam(b, nf) -> `Lam(b, aux (l+1) nf)
224 (*in let ll = ["v0";"v1";"v2"] in
225 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*)
226  in
227   aux 0 where
228 ;;
229
230 (************ Parsing ************************************)
231
232 let parse' strs =
233   let rec aux = function
234   | Parser.Lam t -> `Lam (true, aux t)
235   | Parser.App (t1, t2) -> mk_app (aux t1) (aux t2)
236   | Parser.Var v -> `Var(v,-666)
237   in let (tms, free) = Parser.parse_many strs
238   in (List.map aux tms, free)
239 ;;
240
241 (************** Algorithm(s) ************************)
242
243 let eta_compare x y =
244  (* let clex a b = let diff = ? a b in if diff = 0 then cont () else 0 in *)
245  let clex aux1 aux2 (a1,a2) (b1,b2) =
246   let diff = aux1 a1 b1 in if diff = 0 then aux2 a2 b2 else diff in
247  let rec lex aux l1 l2 =
248   match l1,l2 with
249  | [], [] -> 0
250  | [], _ -> -1
251  | _, [] -> 1
252  | x::xs, y::ys -> clex aux (lex aux) (x,xs) (y,ys) in
253  let rec aux t1 t2 = match t1, t2 with
254   | `Var(n,_) , `Var(m,_) -> compare n m
255   | `I((n1,_), l1), `I((n2,_), l2) ->
256     clex compare (lex aux) (n1, Listx.to_list l1) (n2, Listx.to_list l2)
257   | `Lam _, `N _ -> -1
258   | `N _, `Lam _ -> 1
259   | `Lam(_,t1), `Lam(_,t2) -> aux t1 t2
260   | `Lam(_,t1), t2 -> - aux t1 (mk_app (lift 1 t2) (`Var(0,-666)))
261   | t2, `Lam(_,t1) ->   aux t1 (mk_app (lift 1 t2) (`Var(0,-666)))
262   | `N n1, `N n2 -> compare n1 n2
263   | `Match(u,_,bs_lift,bs,args), `Match(u',_,bs_lift',bs',args') ->
264     let bs = List.sort (fun (n,_) (m,_) -> compare n m) !bs in
265     let bs' = List.sort (fun (n,_) (m,_) -> compare n m) !bs' in
266     clex aux (clex (lex (clex compare aux)) (lex aux)) ((u :> nf), (bs, args)) ((u' :> nf), (bs', args'))
267   | `Match _, _ -> -1
268   | _, `Match _ -> 1
269   | `N _, _ -> -1
270   | _, `N _ -> 1
271   | `I _, _ -> -1
272   | _, `I _ -> 1
273   in aux x y
274 ;;
275
276 let eta_eq (#nf as x) (#nf as y) = 0 = eta_compare x y ;;
277
278 let rec eta_subterm sub t =
279  if eta_eq sub t then true else
280   match t with
281   | `Lam(_,t') -> eta_subterm (lift 1 sub) t'
282   | `Match(u,ar,liftno,bs,args) ->
283      eta_subterm sub (u :> nf)
284      || List.exists (fun (_, t) -> eta_subterm sub (lift liftno t)) !bs
285      || List.exists (eta_subterm sub) args
286   | `I(v, args) -> List.exists (eta_subterm sub) (Listx.to_list args) || (match sub with
287    | `Var v' -> v = v'
288    | `I(v', args') -> v = v'
289     && Listx.length args' < Listx.length args
290     && 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'))
291    | _ -> false
292    )
293   | `N _ | `Var _ -> false
294 ;;
295
296 let eta_subterm (#nf as x) (#nf as y) = eta_subterm x y;;