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