5 (************ Syntax ************************************)
9 (* Var n = n-th De Bruijn index, 0-based *)
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_ = [
23 | `Match of 'nf i_num_var_ * (* originating var *) var * (*lift*) int * (*branches*)(int * 'nf) list ref * (*args*)'nf list
25 type 'nf nf_ = [ `Lam of (* was_unpacked *) bool * 'nf nf_ | 'nf i_num_var_ ]
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_;;
41 | `Match _ -> assert false
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 =
53 `I(v,args) -> `I(aux_var l v, Listx.map (aux l) args)
54 | `Var v -> `Var(aux_var l v)
56 | `Match(t,v,lift,bs,args) ->
57 `Match(aux_i_num_var l t, v, lift + m, bs, List.map (aux l) args)
60 #i_num_var as x -> (aux_i_num_var l x :> nf)
61 | `Lam(b,nf) -> `Lam (b, aux (l+1) nf)
66 (* put t under n lambdas, lifting t accordingtly *)
70 | n when n > 0 -> `Lam (false, lift 1 (make_lams t (n-1)))
74 let rec aux n = function
76 | `Var(x,ar) -> if x < n then [] else [(x-n,ar)]
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) ->
83 List.concat (List.map (fun (_,t) -> aux (n-liftno) t) !bs) @
84 List.concat (List.map (aux n) args)
87 let free_vars = (List.map fst) ++ free_vars';;
92 let rec t_of_i_num_var =
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
104 | #i_num_var as x -> t_of_i_num_var x
105 | `Lam(b,f) -> Pure.L (t_of_nf f)
110 (************ Pretty-printing ************************************)
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("^print_name l 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
135 let print ?(l=[]) = string_of_term l;;
136 let string_of_nf t = string_of_term [] (t:>nf);;
138 (************ Hereditary substitutions ************************************)
142 #i_var as y -> (y : i_var)
144 prerr_endline (print (t :> nf));
145 assert false (* algorithm failed *)
147 let cast_to_i_n_var =
149 #i_n_var as y -> (y : i_n_var)
151 prerr_endline (print (t :> nf));
152 assert false (* algorithm failed *)
154 let cast_to_i_num_var =
156 #i_num_var as y -> (y : i_num_var)
158 prerr_endline (print (t :> nf));
159 assert false (* algorithm failed *)
161 let set_arity arity = function
162 | `Var(n,_) -> `Var(n,arity)
164 | `Lam(false, `Lam _) as t -> t
165 | `Lam(false, `Match(t,(n,_),bs_lift,bs,args)) -> `Lam(false, `Match(t,(n,arity),bs_lift,bs,args))
168 let minus1 n = if n = min_int then n else n - 1;;
170 let rec mk_app (h : nf) (arg : nf) =
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*)
182 (*prerr_endline ("MK_APPL: " ^ print h ^ " " ^ String.concat " " (List.map print args));*)
183 List.fold_left mk_app h args
185 and mk_appx h args = Listx.fold_left mk_app h args
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));*)
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
197 `Match (t,(n,ar),bs_lift,bs,args))
198 | `I _ | `Var _ | `Match _ -> `Match(t,(n,ar),bs_lift,bs,args)
200 and subst truelam delift_by_one what (with_what : nf) (where : nf) =
201 let aux_propagate_arity ar = function
202 | `Lam(false,`Match(`I(v,args),(x,_),liftno,bs,args')) when not delift_by_one ->
203 `Lam(false,`Match(`I(v,args),(x,ar),liftno,bs,args'))
204 | `Var(i,oldar) -> `Var(i, if truelam then (assert (oldar = min_int); ar) else oldar)
206 let rec aux_i_num_var l =
210 mk_appx (lift l (aux_propagate_arity ar with_what)) (Listx.map (aux l) args)
212 `I (((if delift_by_one && n >= l then n-1 else n), ar), Listx.map (aux l) args)
215 lift l (aux_propagate_arity ar with_what)
217 `Var((if delift_by_one && n >= l then n-1 else n), ar)
219 | `Match(t,v,bs_lift,bs,args) ->
220 let bs_lift = bs_lift + if delift_by_one then -1 else 0 in
221 let l' = l - bs_lift in
222 let with_what' = lift l' with_what in
223 (* The following line should be the identity when delift_by_one = true because we
224 are assuming the ts to not contain lambda-bound variables. *)
225 bs := List.map (fun (n,t) -> n,subst truelam false what with_what' t) !bs ;
226 mk_match (cast_to_i_num_var (aux_i_num_var l t)) v bs_lift bs (List.map (aux l) args)
228 (*function iii -> let res = match iii with*)
230 | #i_num_var as x -> aux_i_num_var l x
231 | `Lam(b, nf) -> `Lam(b, aux (l+1) nf)
232 (*in let ll = ["v0";"v1";"v2"] in
233 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*)
238 (************ Parsing ************************************)
241 let fix_arity = function
242 | `I((n,_),args) -> `I((n,1+Listx.length args),args)
243 | _ -> assert false in
244 let rec aux = function
245 | Parser.Lam t -> `Lam (true, aux t)
246 | Parser.App (t1, t2) -> fix_arity (mk_app (aux t1) (aux t2))
247 | Parser.Var v -> `Var(v,1) in
248 let (tms, free) = Parser.parse_many strs in
249 List.map aux tms, free
252 (************** Algorithm(s) ************************)
254 let eta_compare x y =
255 (* let clex a b = let diff = ? a b in if diff = 0 then cont () else 0 in *)
256 let clex aux1 aux2 (a1,a2) (b1,b2) =
257 let diff = aux1 a1 b1 in if diff = 0 then aux2 a2 b2 else diff in
258 let rec lex aux l1 l2 =
263 | x::xs, y::ys -> clex aux (lex aux) (x,xs) (y,ys) in
264 let rec aux t1 t2 = match t1, t2 with
265 | `Var(n,_) , `Var(m,_) -> compare n m
266 | `I((n1,_), l1), `I((n2,_), l2) ->
267 clex compare (lex aux) (n1, Listx.to_list l1) (n2, Listx.to_list l2)
270 | `Lam(_,t1), `Lam(_,t2) -> aux t1 t2
271 | `Lam(_,t1), t2 -> - aux t1 (mk_app (lift 1 t2) (`Var(0,-666)))
272 | t2, `Lam(_,t1) -> aux t1 (mk_app (lift 1 t2) (`Var(0,-666)))
273 | `N n1, `N n2 -> compare n1 n2
274 | `Match(u,_,bs_lift,bs,args), `Match(u',_,bs_lift',bs',args') ->
275 let bs = List.sort (fun (n,_) (m,_) -> compare n m) !bs in
276 let bs' = List.sort (fun (n,_) (m,_) -> compare n m) !bs' in
277 clex aux (clex (lex (clex compare aux)) (lex aux)) ((u :> nf), (bs, args)) ((u' :> nf), (bs', args'))
287 let eta_eq (#nf as x) (#nf as y) = 0 = eta_compare x y ;;
289 let rec eta_subterm sub t =
290 if eta_eq sub t then true else
292 | `Lam(_,t') -> eta_subterm (lift 1 sub) t'
293 | `Match(u,ar,liftno,bs,args) ->
294 eta_subterm sub (u :> nf)
295 || List.exists (fun (_, t) -> eta_subterm sub (lift liftno t)) !bs
296 || List.exists (eta_subterm sub) args
297 | `I(v, args) -> List.exists (eta_subterm sub) (Listx.to_list args) || (match sub with
299 | `I(v', args') -> v = v'
300 && Listx.length args' < Listx.length args
301 && 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'))
304 | `N _ | `Var _ -> false
307 let eta_subterm (#nf as x) (#nf as y) = eta_subterm x y;;
310 let max_arity_tms n =
311 let max a b = match a, b with
314 | Some x, None -> Some x
315 | Some x, Some y -> Some (Pervasives.max x y) in
316 let aux_var l (m,a) = if n + l = m then Some a else None in
317 let rec aux l = function
318 | `Var v -> aux_var l v
319 | `I(v,tms) -> max (aux_var l v) (aux_tms l (Listx.to_list tms))
320 | `Lam(_,t) -> aux (l+1) t
321 | `Match(u,_,_,bs,args) -> max (max (aux l (u :> nf)) (aux_tms l args)) (aux_tms l (List.map snd !bs))
324 List.fold_left (fun acc t -> Pervasives.max acc (aux l t)) None in
325 fun tms -> aux_tms 0 (tms :> nf list)
328 let get_first_args var =
329 let rec aux l = function
330 | `Lam(_,t) -> aux (l+1) t
331 | `Match(u,orig,liftno,bs,args) -> Util.concat_map (aux l) args
332 | `I((n,_), args) -> if n = var + l then [Listx.last args] else []
338 let compute_arities m =
343 let tms = Util.filter_map (function `Lam(_,t) -> Some t | _ -> None ) tms in
344 let arity = match max_arity_tms (m-n) tms with None -> -666 | Some x -> x in
345 arity :: (aux (n-1) tms)
346 in fun tms -> List.rev (aux m tms)
349 let compute_arities var special_k all_tms =
350 let tms = List.fold_left (fun acc t -> acc @ (get_first_args var t)) [] all_tms in
351 compute_arities special_k tms