]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/num.ml
2bc253d0554bbdec893c04d6878e2354cfb31020
[fireball-separation.git] / ocaml / num.ml
1 open Util
2 open Util.Vars
3 open Pure
4
5 (* debug options *)
6 let debug_display_arities = false;;
7
8 (************ Syntax ************************************)
9
10 (* Normal forms*)
11
12 (* Var n = n-th De Bruijn index, 0-based *)
13
14 (*type nf =
15   | Lam of nf
16   | Var of int
17   | i
18 and i =
19   | I of int * nf listx
20 ;;*)
21 type var = int * (* arity of variable*) int;;
22 type 'nf i_var_ = [ `I of var * 'nf Listx.listx | `Var of var ]
23 type 'nf i_n_var_ = [ `N of int | 'nf i_var_ ]
24 type 'nf i_num_var_ = [
25   | 'nf i_n_var_
26   | `Match of 'nf i_num_var_ * (* originating var *) var * (*lift*) int * (*branches*)(int * 'nf) list ref * (*args*)'nf list
27 ]
28 type 'nf nf_ = [ `Lam of (* was_unpacked *) bool * 'nf nf_ | 'nf i_num_var_ ]
29 type nf = nf nf_
30 type i_var = nf i_var_;;
31 type i_n_var = nf i_n_var_;;
32 type i_num_var = nf i_num_var_;;
33
34 let hd_of_i_var =
35  function
36    `I ((v,_),_)
37  | `Var (v,_) -> v
38
39 let hd_of =
40  function
41    `I ((v,_),_)
42  | `Var(v,_) -> Some v
43  | `N _ -> None
44  | `Match _ -> assert false
45
46 let arity_of_hd =
47 function
48   `I ((_,a),_)
49 | `Var(_,a) -> a
50 | _ -> 0 (* FIXME? *)
51
52 let lift m (t : nf) =
53  let aux_var l (n, ar) = (if n < l then n else n+m), ar in
54  let rec aux_i_num_var l =
55   function
56     `I(v,args) -> `I(aux_var l v, Listx.map (aux l) args)
57    | `Var v -> `Var(aux_var l v)
58    | `N _ as x -> x
59    | `Match(t,v,lift,bs,args) ->
60       `Match(aux_i_num_var l t, v, lift + m, bs, List.map (aux l) args)
61  and aux l =
62   function
63      #i_num_var as x -> (aux_i_num_var l x :> nf)
64    | `Lam(b,nf) -> `Lam (b, aux (l+1) nf)
65  in
66   (aux 0 t : nf)
67 ;;
68
69 (* put t under n lambdas, lifting t accordingtly *)
70 let rec make_lams t =
71  function
72    0 -> t
73  | n when n > 0 -> `Lam (false, lift 1 (make_lams t (n-1)))
74  | _ -> assert false
75
76 let free_vars' =
77  let rec aux n = function
78     `N _ -> []
79   | `Var(x,ar) -> if x < n then [] else [(x-n,ar)]
80   | `I((x,ar),args) ->
81       (if x < n then [] else [(x-n,ar)]) @
82       List.concat (List.map (aux n) (Listx.to_list args))
83   | `Lam(_,t) -> aux (n+1) t
84   | `Match(t,_,liftno,bs,args) ->
85       aux n (t :> nf) @
86       List.concat (List.map (fun (_,t) -> aux (n-liftno) t) !bs) @
87       List.concat (List.map (aux n) args)
88   in aux 0
89 ;;
90 let free_vars = (List.map fst) ++ free_vars';;
91
92 module ToScott =
93 struct
94
95 let rec t_of_i_num_var =
96  function
97   | `N n -> Scott.mk_n n
98   | `Var(v,_) -> Pure.V v
99   | `Match(t,_,liftno,bs,args) ->
100       let bs = List.map (fun (n,t) -> n, t_of_nf (lift liftno t)) !bs in
101       let t = t_of_i_num_var t in
102       let m = Scott.mk_match t bs in
103       List.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) m args
104   | `I((v,_), args) -> Listx.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) (Pure.V v) args
105 and t_of_nf =
106  function
107   | #i_num_var as x -> t_of_i_num_var x
108   | `Lam(b,f) -> Pure.L (t_of_nf f)
109
110 end
111
112
113 (************ Pretty-printing ************************************)
114
115 (* let rec string_of_term l  = fun _ -> "";; *)
116
117 let rec string_of_term l  =
118  let rec string_of_term_w_pars l  = function
119   | `Var(n,ar) -> List.nth l n ^ (if debug_display_arities then ":" ^ string_of_int ar else "")
120   | `N n -> string_of_int n
121   | `I _ as t -> "(" ^ string_of_term_no_pars_app l (t :> nf) ^ ")"
122   | `Lam _ as t -> "(" ^ string_of_term_no_pars_lam l t ^ ")"
123   | `Match(t,(v,ar),bs_lift,bs,args) ->
124      "["^ List.nth l v ^ (if debug_display_arities then ":"^ string_of_int ar else "") ^",match " ^ string_of_term_no_pars l (t :> nf) ^
125      " with " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ string_of_term l (lift bs_lift (t :> nf))) !bs) ^ "] " ^
126      String.concat " " (List.map (string_of_term l) (args :> nf list)) ^ ")"
127  and string_of_term_no_pars_app l  = function
128   | `I((n,ar), args) -> List.nth l n ^ (if debug_display_arities then ":" ^ string_of_int ar else "") ^ " " ^ String.concat " " (List.map (string_of_term_w_pars l) (Listx.to_list args :> nf list))
129   | #nf as t -> string_of_term_w_pars l t
130  and string_of_term_no_pars_lam l  = function
131   | `Lam(_,t) -> let name = "x" ^ string_of_int (List.length l) in
132      "λ" ^ name ^ ". " ^ (string_of_term_no_pars_lam (name::l) t)
133   | _ as t -> string_of_term_no_pars l t
134  and string_of_term_no_pars l : nf -> string = function
135   | `Lam _ as t -> string_of_term_no_pars_lam l t
136   | #nf as t -> string_of_term_no_pars_app l t
137  in string_of_term_no_pars l
138 ;;
139
140 let print ?(l=[]) = string_of_term l;;
141 let string_of_nf t = string_of_term [] (t:>nf);;
142
143 (************ Hereditary substitutions ************************************)
144
145 let cast_to_i_var =
146  function
147     #i_var as y -> (y : i_var)
148   | t ->
149     prerr_endline (print (t :> nf));
150     assert false (* algorithm failed *)
151
152 let cast_to_i_n_var =
153  function
154     #i_n_var as y -> (y : i_n_var)
155   | t ->
156     prerr_endline (print (t :> nf));
157     assert false (* algorithm failed *)
158
159 let cast_to_i_num_var =
160  function
161     #i_num_var as y -> (y : i_num_var)
162   | t ->
163     prerr_endline (print (t :> nf));
164     assert false (* algorithm failed *)
165
166 let rec set_arity arity = function
167 (* FIXME because onlt variables should be in branches of matches, one day *)
168 | `Var(n,_) -> `Var(n,arity)
169 | `N _ as t -> t
170 | `Lam(false, t) -> `Lam(false, set_arity arity t)
171 | `Match(t,(n,_),bs_lift,bs,args) -> `Match(t,(n,arity),bs_lift,bs,args)
172 | `I _ | `Lam _ -> assert false
173
174 let minus1 n = if n = min_int then n else n - 1;;
175
176 let rec mk_app (h : nf) (arg : nf) =
177 (*let res =*)
178  match h with
179     `I(v,args) -> `I(v,Listx.append (Listx.Nil arg) args)
180   | `Var v -> `I(v, Listx.Nil arg)
181   | `Lam(truelam,nf) -> subst truelam true 0 arg (nf : nf) (* AC FIXME sanity check on arity *)
182   | `Match(t,v,lift,bs,args) -> `Match(t,v,lift,bs,List.append args [arg])
183   | `N _ -> assert false (* Numbers cannot be applied *)
184 (*in let l = ["v0";"v1";"v2"] in
185 prerr_endline ("mk_app h:" ^ print ~l h ^ " arg:" ^ print ~l:l arg ^ " res:" ^ print ~l:l res); res*)
186
187 and mk_appl h args =
188  (*prerr_endline ("MK_APPL: " ^ print h ^ " " ^ String.concat " " (List.map print args));*)
189  List.fold_left mk_app h args
190
191 and mk_appx h args = Listx.fold_left mk_app h args
192
193 and mk_match t (n,ar) bs_lift bs args =
194  (*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));*)
195  match t with
196     `N m ->
197       (try
198         let h = List.assoc m !bs in
199         let h = set_arity (minus1 ar) h in
200         let h = lift bs_lift h in
201         mk_appl h args
202        with Not_found ->
203         `Match (t,(n,ar),bs_lift,bs,args))
204   | `I _ | `Var _ | `Match _ -> `Match(t,(n,ar),bs_lift,bs,args)
205
206 and subst truelam delift_by_one what (with_what : nf) (where : nf) =
207  let rec aux_propagate_arity ar = function
208  | `Lam(false, t) when not delift_by_one -> `Lam(false, aux_propagate_arity ar t)
209  | `Match(`I(v,args),(x,_),liftno,bs,args') when not delift_by_one ->
210     `Match(`I(v,args),(x,ar),liftno,bs,args')
211  | `Var(i,oldar) -> `Var(i, if truelam then (assert (oldar = min_int); ar) else oldar)
212  | _ as t -> t in
213  let rec aux_i_num_var l =
214   function
215      `I((n,ar),args) ->
216        if n = what + l then
217         mk_appx (lift l (aux_propagate_arity ar with_what)) (Listx.map (aux l) args)
218        else
219         `I (((if delift_by_one && n >= l then n-1 else n), ar), Listx.map (aux l) args)
220    | `Var(n,ar) ->
221        if n = what + l then
222         lift l (aux_propagate_arity ar with_what)
223        else
224         `Var((if delift_by_one && n >= l then n-1 else n), ar)
225    | `N _ as x -> x
226    | `Match(t,v,bs_lift,bs,args) ->
227        let bs_lift = bs_lift + if delift_by_one then -1 else 0 in
228        let l' = l - bs_lift  in
229        let with_what' = lift l' with_what in
230        (* The following line should be the identity when delift_by_one = true because we
231           are assuming the ts to not contain lambda-bound variables. *)
232        bs := List.map (fun (n,t) -> n,subst truelam false what with_what' t) !bs ;
233        mk_match (cast_to_i_num_var (aux_i_num_var l t)) v bs_lift bs (List.map (aux l) args)
234  and aux l(*lift*) =
235 (*function iii -> let res = match iii with*)
236   function
237    | #i_num_var as x -> aux_i_num_var l x
238    | `Lam(b, nf) -> `Lam(b, aux (l+1) nf)
239 (*in let ll = ["v0";"v1";"v2"] in
240 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*)
241  in
242   aux 0 where
243 ;;
244
245 (************** Algorithm(s) ************************)
246
247 let eta_compare x y =
248  (* let clex a b = let diff = ? a b in if diff = 0 then cont () else 0 in *)
249  let clex aux1 aux2 (a1,a2) (b1,b2) =
250   let diff = aux1 a1 b1 in if diff = 0 then aux2 a2 b2 else diff in
251  let rec lex aux l1 l2 =
252   match l1,l2 with
253  | [], [] -> 0
254  | [], _ -> -1
255  | _, [] -> 1
256  | x::xs, y::ys -> clex aux (lex aux) (x,xs) (y,ys) in
257  let rec aux t1 t2 = match t1, t2 with
258   | `Var(n,_) , `Var(m,_) -> compare n m
259   | `I((n1,_), l1), `I((n2,_), l2) ->
260     clex compare (lex aux) (n1, Listx.to_list l1) (n2, Listx.to_list l2)
261   | `Lam _, `N _ -> -1
262   | `N _, `Lam _ -> 1
263   | `Lam(_,t1), `Lam(_,t2) -> aux t1 t2
264   | `Lam(_,t1), t2 -> - aux t1 (mk_app (lift 1 t2) (`Var(0,-666)))
265   | t2, `Lam(_,t1) ->   aux t1 (mk_app (lift 1 t2) (`Var(0,-666)))
266   | `N n1, `N n2 -> compare n1 n2
267   | `Match(u,_,bs_lift,bs,args), `Match(u',_,bs_lift',bs',args') ->
268     let bs = List.sort (fun (n,_) (m,_) -> compare n m) !bs in
269     let bs' = List.sort (fun (n,_) (m,_) -> compare n m) !bs' in
270     clex aux (clex (lex (clex compare aux)) (lex aux)) ((u :> nf), (bs, args)) ((u' :> nf), (bs', args'))
271   | `Match _, _ -> -1
272   | _, `Match _ -> 1
273   | `N _, _ -> -1
274   | _, `N _ -> 1
275   | `I _, _ -> -1
276   | _, `I _ -> 1
277   in aux x y
278 ;;
279
280 let eta_eq (#nf as x) (#nf as y) = 0 = eta_compare x y ;;
281
282 let rec eta_subterm sub t =
283  if eta_eq sub t then true else
284   match t with
285   | `Lam(_,t') -> eta_subterm (lift 1 sub) t'
286   | `Match(u,ar,liftno,bs,args) ->
287      eta_subterm sub (u :> nf)
288      || List.exists (fun (_, t) -> eta_subterm sub (lift liftno t)) !bs
289      || List.exists (eta_subterm sub) (args :> nf list)
290   | `I((v,_), args) -> List.exists (eta_subterm sub) ((Listx.to_list args) :> nf list) || (match sub with
291    | `Var(v',_) -> v = v'
292    | `I((v',_), args') -> v = v'
293     && Listx.length args' < Listx.length args
294     && 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'))
295    | _ -> false
296    )
297   | `N _ | `Var _ -> false
298 ;;
299
300 let eta_subterm (#nf as x) (#nf as y) = eta_subterm x y;;
301
302
303 let max_arity_tms n =
304  let max a b = match a, b with
305   | None, None -> None
306   | None, Some x
307   | Some x, None -> Some x
308   | Some x, Some y -> Some (Pervasives.max x y) in
309  let aux_var l (m,a) = if n + l = m then Some a else None in
310  let rec aux l = function
311   | `Var v -> aux_var l v
312   | `I(v,tms) -> max (aux_var l v) (aux_tms l (Listx.to_list tms))
313   | `Lam(_,t) -> aux (l+1) t
314   | `Match(u,_,_,bs,args) -> max (max (aux l (u :> nf)) (aux_tms l args)) (aux_tms l (List.map snd !bs))
315   | `N _ -> None
316  and aux_tms l =
317   List.fold_left (fun acc t -> max acc (aux l t)) None in
318  fun tms -> aux_tms 0 (tms :> nf list)
319 ;;
320
321 let get_first_args var =
322 let rec aux l = function
323 | `Lam(_,t) -> aux (l+1) t
324 | `Match(u,orig,liftno,bs,args) -> Util.concat_map (aux l) args
325 | `I((n,_), args) -> if n = var + l then [Listx.last args] else []
326 | `N _
327 | `Var _ -> []
328 in aux 0
329 ;;
330
331 let compute_arities m =
332  let rec aux n tms =
333   if n = 0
334    then []
335    else
336     let tms = Util.filter_map (function `Lam(_,t) -> Some t | _ -> None ) tms in
337     let arity = match max_arity_tms (m-n) tms with None -> -666 | Some x -> x in
338      arity :: (aux (n-1) tms)
339  in fun tms -> List.rev (aux m tms)
340 ;;
341
342 let compute_arities var special_k all_tms =
343  let tms = List.fold_left (fun acc t -> acc @ (get_first_args var t)) [] all_tms in
344  compute_arities special_k tms
345 ;;