]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/num.ml
Problems pass, but still missing computation of arities of fresh applicative vars
[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 rec mk_app (h : nf) (arg : nf) =
161 (*let res =*)
162  match h with
163     `I(v,args) -> `I(v,Listx.append (Listx.Nil arg) args)
164   | `Var v -> `I(v, Listx.Nil arg)
165   | `Lam(_,nf) -> subst true 0 arg (nf : nf) (* AC FIXME sanity check on arity *)
166   | `Match(t,v,lift,bs,args) -> `Match(t,v,lift,bs,List.append args [arg])
167   | `N _ -> assert false (* Numbers cannot be applied *)
168 (*in let l = ["v0";"v1";"v2"] in
169 prerr_endline ("mk_app h:" ^ print ~l h ^ " arg:" ^ print ~l:l arg ^ " res:" ^ print ~l:l res); res*)
170
171 and mk_appl h args =
172  (*prerr_endline ("MK_APPL: " ^ print h ^ " " ^ String.concat " " (List.map print args));*)
173  List.fold_left mk_app h args
174
175 and mk_appx h args = Listx.fold_left mk_app h args
176
177 and mk_match t ar bs_lift bs args =
178  (*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));*)
179  match t with
180     `N m ->
181       (try
182         let h = List.assoc m !bs in
183         let h = lift bs_lift h in
184         mk_appl h args
185        with Not_found ->
186         `Match (t,ar,bs_lift,bs,args))
187   | `I _ | `Var _ | `Match _ -> `Match(t,ar,bs_lift,bs,args)
188
189 and subst delift_by_one what (with_what : nf) (where : nf) =
190  let rec aux_i_num_var l =
191   function
192      `I((n,ar),args) ->
193        if n = what + l then
194         mk_appx (lift l with_what) (Listx.map (aux l) args)
195        else
196         `I (((if delift_by_one && n >= l then n-1 else n), ar), Listx.map (aux l) args)
197    | `Var(n,ar) ->
198        if n = what + l then
199         lift l with_what
200        else
201         `Var((if delift_by_one && n >= l then n-1 else n), ar)
202    | `N _ as x -> x
203    | `Match(t,v,bs_lift,bs,args) ->
204        let bs_lift = bs_lift + if delift_by_one then -1 else 0 in
205        let l' = l - bs_lift  in
206        let with_what' = lift l' with_what in
207        (* The following line should be the identity when delift_by_one = true because we
208           are assuming the ts to not contain lambda-bound variables. *)
209        bs := List.map (fun (n,t) -> n,subst false what with_what' t) !bs ;
210        mk_match (cast_to_i_num_var (aux_i_num_var l t)) v bs_lift bs (List.map (aux l) args)
211  and aux l(*lift*) =
212 (*function iii -> let res = match iii with*)
213   function
214    | #i_num_var as x -> aux_i_num_var l x
215    | `Lam(b, nf) -> `Lam(b, aux (l+1) nf)
216 (*in let ll = ["v0";"v1";"v2"] in
217 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*)
218  in
219   aux 0 where
220 ;;
221
222 (************ Parsing ************************************)
223
224 let parse' strs =
225   let fix_arity = function
226   | `I((n,_),args) -> `I((n,Listx.length args),args)
227   | _ -> assert false in
228   let rec aux = function
229   | Parser.Lam t -> `Lam (true, aux t)
230   | Parser.App (t1, t2) -> fix_arity (mk_app (aux t1) (aux t2))
231   | Parser.Var v -> `Var(v,0) in
232   let (tms, free) = Parser.parse_many strs in
233   List.map aux tms, free
234 ;;
235
236 (************** Algorithm(s) ************************)
237
238 let eta_compare x y =
239  (* let clex a b = let diff = ? a b in if diff = 0 then cont () else 0 in *)
240  let clex aux1 aux2 (a1,a2) (b1,b2) =
241   let diff = aux1 a1 b1 in if diff = 0 then aux2 a2 b2 else diff in
242  let rec lex aux l1 l2 =
243   match l1,l2 with
244  | [], [] -> 0
245  | [], _ -> -1
246  | _, [] -> 1
247  | x::xs, y::ys -> clex aux (lex aux) (x,xs) (y,ys) in
248  let rec aux t1 t2 = match t1, t2 with
249   | `Var(n,_) , `Var(m,_) -> compare n m
250   | `I((n1,_), l1), `I((n2,_), l2) ->
251     clex compare (lex aux) (n1, Listx.to_list l1) (n2, Listx.to_list l2)
252   | `Lam _, `N _ -> -1
253   | `N _, `Lam _ -> 1
254   | `Lam(_,t1), `Lam(_,t2) -> aux t1 t2
255   | `Lam(_,t1), t2 -> - aux t1 (mk_app (lift 1 t2) (`Var(0,-666)))
256   | t2, `Lam(_,t1) ->   aux t1 (mk_app (lift 1 t2) (`Var(0,-666)))
257   | `N n1, `N n2 -> compare n1 n2
258   | `Match(u,_,bs_lift,bs,args), `Match(u',_,bs_lift',bs',args') ->
259     let bs = List.sort (fun (n,_) (m,_) -> compare n m) !bs in
260     let bs' = List.sort (fun (n,_) (m,_) -> compare n m) !bs' in
261     clex aux (clex (lex (clex compare aux)) (lex aux)) ((u :> nf), (bs, args)) ((u' :> nf), (bs', args'))
262   | `Match _, _ -> -1
263   | _, `Match _ -> 1
264   | `N _, _ -> -1
265   | _, `N _ -> 1
266   | `I _, _ -> -1
267   | _, `I _ -> 1
268   in aux x y
269 ;;
270
271 let eta_eq (#nf as x) (#nf as y) = 0 = eta_compare x y ;;
272
273 let rec eta_subterm sub t =
274  if eta_eq sub t then true else
275   match t with
276   | `Lam(_,t') -> eta_subterm (lift 1 sub) t'
277   | `Match(u,ar,liftno,bs,args) ->
278      eta_subterm sub (u :> nf)
279      || List.exists (fun (_, t) -> eta_subterm sub (lift liftno t)) !bs
280      || List.exists (eta_subterm sub) args
281   | `I(v, args) -> List.exists (eta_subterm sub) (Listx.to_list args) || (match sub with
282    | `Var v' -> v = v'
283    | `I(v', args') -> v = v'
284     && Listx.length args' < Listx.length args
285     && 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'))
286    | _ -> false
287    )
288   | `N _ | `Var _ -> false
289 ;;
290
291 let eta_subterm (#nf as x) (#nf as y) = eta_subterm x y;;
292
293
294 let max_arity_tms n =
295  let aux_var l (m,a) = if n + l = m then a else -1 in
296  let rec aux l = function
297   | `Var v -> aux_var l v
298   | `I(v,tms) -> max (aux_var l v) (aux_tms l (Listx.to_list tms))
299   | `Lam(_,t) -> aux (l+1) t
300   | `Match(u,_,_,bs,args) -> max (max (aux l (u :> nf)) (aux_tms l args)) (aux_tms l (List.map snd !bs))
301   | `N _ -> -1
302  and aux_tms l =
303   List.fold_left (fun acc t -> Pervasives.max acc (aux l t)) ~-1 in
304  fun tms -> aux_tms 0 (tms :> nf list)
305 ;;