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