]> matita.cs.unibo.it Git - fireball-separation.git/blobdiff - ocaml/num.ml
Problems pass, but still missing computation of arities of fresh applicative vars
[fireball-separation.git] / ocaml / num.ml
index 9d803139e4dcafa89e1938c08752cdc5d2ff3110..614dde85185147fe812dc37d647300ea8d363651 100644 (file)
@@ -40,6 +40,12 @@ let hd_of =
  | `N _ -> None
  | `Match _ -> assert false
 
+let arity_of_hd =
+function
+  `I ((_,a),_)
+| `Var(_,a) -> a
+| _ -> 0 (* FIXME? *)
+
 let lift m (t : nf) =
  let aux_var l (n, ar) = (if n < l then n else n+m), ar in
  let rec aux_i_num_var l =
@@ -102,23 +108,9 @@ end
 
 (************ Pretty-printing ************************************)
 
-let rec print ?(l=[]) =
- function
-    `Var(n,_) -> print_name l n
-  | `N n -> string_of_int n
-  | `Match(t,_,bs_lift,bs,args) ->
-     "([" ^ print ~l (t :> nf) ^
-     " ? " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ print ~l (lift bs_lift t)) !bs) ^ "] " ^
-     String.concat " " (List.map (print ~l) args) ^ ")"
-  | `I((n,_),args) -> "(" ^ print_name l n ^ " " ^ String.concat " " (Listx.to_list (Listx.map (print ~l) args)) ^ ")"
-  | `Lam(_,nf) ->
-     let name = string_of_var (List.length l) in
-     "λ" ^ name ^ "." ^ print ~l:(name::l) (nf : nf)
-;;
-
 let rec string_of_term l =
  let rec string_of_term_w_pars l = function
-  | `Var(n,_) -> print_name l n
+  | `Var(n,ar) -> print_name l n ^ ":" ^ string_of_int ar
   | `N n -> string_of_int n
   | `I _ as t -> "(" ^ string_of_term_no_pars_app l (t :> nf) ^ ")"
   | `Lam _ as t -> "(" ^ string_of_term_no_pars_lam l t ^ ")"
@@ -127,7 +119,7 @@ let rec string_of_term l =
      " with " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ string_of_term l (lift bs_lift t)) !bs) ^ "] " ^
      String.concat " " (List.map (string_of_term l) args) ^ ")"
  and string_of_term_no_pars_app l = function
-  | `I((n,_), args) -> print_name l n ^ " " ^ String.concat " " (List.map (string_of_term_w_pars l) (Listx.to_list args))
+  | `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 as t -> string_of_term_w_pars l t
  and string_of_term_no_pars_lam l  = function
   | `Lam(_,t) -> let name = string_of_var (List.length l) in
@@ -230,12 +222,15 @@ prerr_endline ("subst l:" ^ string_of_int l ^ " delift_by_one:" ^ string_of_bool
 (************ Parsing ************************************)
 
 let parse' strs =
+  let fix_arity = function
+  | `I((n,_),args) -> `I((n,Listx.length args),args)
+  | _ -> assert false in
   let rec aux = function
   | Parser.Lam t -> `Lam (true, aux t)
-  | Parser.App (t1, t2) -> mk_app (aux t1) (aux t2)
-  | Parser.Var v -> `Var(v,-666)
-  in let (tms, free) = Parser.parse_many strs
-  in (List.map aux tms, free)
+  | Parser.App (t1, t2) -> fix_arity (mk_app (aux t1) (aux t2))
+  | Parser.Var v -> `Var(v,0) in
+  let (tms, free) = Parser.parse_many strs in
+  List.map aux tms, free
 ;;
 
 (************** Algorithm(s) ************************)
@@ -294,3 +289,17 @@ let rec eta_subterm sub t =
 ;;
 
 let eta_subterm (#nf as x) (#nf as y) = eta_subterm x y;;
+
+
+let max_arity_tms n =
+ let aux_var l (m,a) = if n + l = m then a else -1 in
+ let rec aux l = function
+  | `Var v -> aux_var l v
+  | `I(v,tms) -> max (aux_var l v) (aux_tms l (Listx.to_list tms))
+  | `Lam(_,t) -> aux (l+1) t
+  | `Match(u,_,_,bs,args) -> max (max (aux l (u :> nf)) (aux_tms l args)) (aux_tms l (List.map snd !bs))
+  | `N _ -> -1
+ and aux_tms l =
+  List.fold_left (fun acc t -> Pervasives.max acc (aux l t)) ~-1 in
+ fun tms -> aux_tms 0 (tms :> nf list)
+;;