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