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