From 587d5b28d74a035b6038d4105a379a6fe4fac37b Mon Sep 17 00:00:00 2001 From: acondolu Date: Fri, 1 Jun 2018 17:12:42 +0200 Subject: [PATCH] Implementing constants auto now iterates the eta differences and backtracks if no more available --- ocaml/simple.ml | 73 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 24 deletions(-) diff --git a/ocaml/simple.ml b/ocaml/simple.ml index a633149..4a69c6e 100644 --- a/ocaml/simple.ml +++ b/ocaml/simple.ml @@ -17,8 +17,15 @@ type t = let delta = L(A(V 0, V 0));; +let rec is_stuck = function + | C -> true + | A(t,_) -> is_stuck t + | _ -> false +;; + let eta_eq' = let rec aux l1 l2 t1 t2 = match t1, t2 with + | _, _ when is_stuck t1 || is_stuck t2 -> true | L t1, L t2 -> aux l1 l2 t1 t2 | L t1, t2 -> aux l1 (l2+1) t1 t2 | t1, L t2 -> aux (l1+1) l2 t1 t2 @@ -30,11 +37,11 @@ let eta_eq = eta_eq' 0 0;; (* is arg1 eta-subterm of arg2 ? *) let eta_subterm u = - let rec aux lev t = eta_eq' lev 0 u t || match t with + let rec aux lev t = if t = C then false else (eta_eq' lev 0 u t || match t with | L t -> aux (lev+1) t | A(t1, t2) -> aux lev t1 || aux lev t2 - | _ -> false - in aux 0 + | _ -> false) in + aux 0 ;; (* does NOT lift the argument *) @@ -102,7 +109,7 @@ let rec is_inert = ;; let rec get_inert = function - | V n -> (n,0) + | V _ | C as t -> (t,0) | A(t, _) -> let hd,args = get_inert t in hd,args+1 | _ -> assert false ;; @@ -111,7 +118,7 @@ let rec get_inert = function after replacing _v_ w/ a term starting with n lambdas *) let rec no_leading_lambdas v n = function | L t -> 1 + no_leading_lambdas (v+1) n t - | A _ as t -> let v', m = get_inert t in if v = v' then max 0 (n - m) else 0 + | A _ as t -> let v', m = get_inert t in if V v = v' then max 0 (n - m) else 0 | V v' -> if v = v' then n else 0 | B | C -> 0 ;; @@ -156,7 +163,7 @@ let get_subterm_with_head_and_args hd_var n_args = | L t -> aux (lev+1) t | A(t1,t2) as t -> let hd_var', n_args' = get_inert t1 in - if hd_var' = hd_var + lev && n_args <= 1 + n_args' + if hd_var' = V (hd_var + lev) && n_args <= 1 + n_args' (* the `+1` above is because of t2 *) then Some (lift ~-lev t) else match aux lev t2 with @@ -218,9 +225,10 @@ let inert_cut_at n t = let find_eta_difference p t argsno = let t = inert_cut_at argsno t in let rec aux t u k = match t, u with - | V _, V _ -> problem_fail p "no eta difference found (div subterm of conv?)" + | V _, V _ -> [] | A(t1,t2), A(u1,u2) -> - if not (eta_eq t2 u2) then (k-1) + print_endline (string_of_t t2 ^ " vs " ^ string_of_t u2); + if not (eta_eq t2 u2) then (k-1)::aux t1 u1 (k-1) else aux t1 u1 (k-1) | _, _ -> assert false in aux p.div t argsno @@ -229,9 +237,9 @@ let find_eta_difference p t argsno = let compute_max_lambdas_at hd_var j = let rec aux hd = function | A(t1,t2) -> - (if get_inert t1 = (hd, j) + (if get_inert t1 = (V hd, j) then max ( (*FIXME*) - if is_inert t2 && let hd', j' = get_inert t2 in hd' = hd + if is_inert t2 && let hd', j' = get_inert t2 in hd' = V hd then let hd', j' = get_inert t2 in j - j' else no_leading_lambdas hd_var j t2) else id) (max (aux hd t1) (aux hd t2)) @@ -278,8 +286,11 @@ 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 hd, _ = get_inert p.div in + match hd with + | C | L _ | B | A _ -> assert false + | V var -> +print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (on " ^ string_of_int (k+1) ^ "th)"); let p, t = (* apply fresh vars *) fold_nat (fun (p, t) _ -> let p, v = freshvar p in @@ -293,24 +304,37 @@ print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")") sanity p ;; -let rec auto p = - let hd_var, n_args = get_inert p.div in +let auto p = + let rec aux p = + let hd, n_args = get_inert p.div in + match hd with + | C | L _ | B | A _ -> assert false + | V hd_var -> match get_subterm_with_head_and_args hd_var n_args p.conv with | None -> - (try let phase = p.phase in let p = eat p in if phase = `Two then problem_fail p "Auto.2 did not complete the problem" - else auto p - with Done sigma -> sigma) + else aux p | Some t -> - let j = find_eta_difference p t n_args in - 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 - auto p + let js = find_eta_difference p t n_args in + (* print_endline (String.concat ", " (List.map string_of_int js)); *) + if js = [] then problem_fail p "no eta difference found (div subterm of conv?)"; + let js = List.rev js in + List.iter + (fun j -> + try + let k = 1 + max + (compute_max_lambdas_at hd_var j p.div) + (compute_max_lambdas_at hd_var j p.conv) in + ignore (aux (step j k p)) + with Fail(_, s) -> + print_endline ("Backtracking because: " ^ s)) js; + raise (Fail(-1, "no eta difference")) in + try + aux p + with Done sigma -> sigma ;; let problem_of (label, div, convs, ps, var_names) = @@ -334,7 +358,8 @@ let problem_of (label, div, convs, ps, var_names) = ;; let solve p = - if eta_subterm p.div p.conv + if is_stuck p.div then print_endline "!!! div is stuck. Problem was not run !!!" + else if eta_subterm p.div p.conv then print_endline "!!! div is subterm of conv. Problem was not run !!!" else check p (auto p) ;; -- 2.39.2