From 11ba0158d9b6947f5d5b9033955b98a6adfe7959 Mon Sep 17 00:00:00 2001 From: acondolu Date: Wed, 30 May 2018 13:49:13 +0200 Subject: [PATCH] Clean-up step function; move code --- ocaml/andrea.ml | 62 ++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/ocaml/andrea.ml b/ocaml/andrea.ml index 93d69a8..bd2ad09 100644 --- a/ocaml/andrea.ml +++ b/ocaml/andrea.ml @@ -1,5 +1,6 @@ let (++) f g x = f (g x);; let id x = x;; +let rec fold_nat f x n = if n = 0 then x else f (fold_nat f x (n-1)) n ;; let print_hline = Console.print_hline;; @@ -22,17 +23,8 @@ let eta_eq = in aux 0 0 ;; -type problem = { - orig_freshno: int - ; freshno : int - ; div : t - ; conv : t - ; sigma : (var * t) list (* substitutions *) - ; stepped : var list -} - -exception Done of (var * t) list (* substitution *);; -exception Fail of int * string;; +(* does NOT lift t *) +let mk_lams = fold_nat (fun x _ -> L x) ;; let string_of_t = let string_of_bvar = @@ -54,6 +46,15 @@ let string_of_t = in string_of_term_no_pars 0 ;; +type problem = { + orig_freshno: int + ; freshno : int + ; div : t + ; conv : t + ; sigma : (var * t) list (* substitutions *) + ; stepped : var list +} + let string_of_problem p = let lines = [ "[stepped] " ^ String.concat " " (List.map string_of_int p.stepped); @@ -63,6 +64,9 @@ let string_of_problem p = String.concat "\n" lines ;; +exception Done of (var * t) list (* substitution *);; +exception Fail of int * string;; + let problem_fail p reason = print_endline "!!!!!!!!!!!!!!! FAIL !!!!!!!!!!!!!!!"; print_endline (string_of_problem p); @@ -150,11 +154,7 @@ let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);; let eat p = print_cmd "EAT" ""; let var, n = get_inert p.div in - let rec aux m t = - if m = 0 - then lift n t - else L (aux (m-1) t) in - let subst = var, aux n B in + let subst = var, mk_lams B n in let p = subst_in_problem subst p in sanity p; p ;; @@ -162,21 +162,15 @@ print_cmd "EAT" ""; (* step on the head of div, on the k-th argument, with n fresh vars *) let step k n p = let var, _ = get_inert p.div in - print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")"); - let rec aux' p m t = - if m < 0 - then p, t - else - let p, v = freshvar p in - let p, t = aux' p (m-1) t in - p, A(t, V (v + k + 1)) in - let p, t = aux' p n (V 0) in - let rec aux' m t = if m < 0 then t else A(aux' (m-1) t, V (k-m)) in - let rec aux m t = - if m < 0 - then aux' (k-1) t - else L (aux (m-1) t) in - let t = aux k t in +print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")"); + let p, t = (* apply fresh vars *) + fold_nat (fun (p, t) _ -> + let p, v = freshvar p in + p, A(t, V (v + k + 1)) + ) (p, V 0) n in + let t = (* apply unused bound variables V_{k-1}..V_1 *) + fold_nat (fun t m -> A(t, V (k-m+1))) t k in + let t = mk_lams t (k+1) in (* make leading lambdas *) let subst = var, t in let p = subst_in_problem subst p in sanity p; p @@ -266,7 +260,7 @@ let rec auto p = (try problem_fail (eat p) "Auto did not complete the problem" with Done _ -> ()) | Some t -> let j = find_eta_difference p t n_args - 1 in - let k = max + let k = 1 + max (compute_max_lambdas_at hd_var j p.div) (compute_max_lambdas_at hd_var j p.conv) in let p = step j k p in @@ -309,11 +303,11 @@ let auto' a b = auto (problem_of a (conv_join b));; exec "x x" (conv_join["x y"; "y y"; "y x"]) - [ step 0 0; eat ] + [ step 0 1; eat ] ;; interactive "x y" - "@ (x x) (y x) (y z)" [step 0 0; step 0 1; eat] + "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat] ;; *) -- 2.39.2