From 74df5ba0526545acae8d23350c41c45c9c480789 Mon Sep 17 00:00:00 2001 From: acondolu Date: Thu, 14 Jun 2018 14:43:26 +0200 Subject: [PATCH] Implementing entanglement of terms FIXME usage of flags b' in subst and mk_app must be understood and fixed! --- ocaml/simple.ml | 65 +++++++++++++++---------------------------------- 1 file changed, 20 insertions(+), 45 deletions(-) diff --git a/ocaml/simple.ml b/ocaml/simple.ml index a7a8d7a..5764c3c 100644 --- a/ocaml/simple.ml +++ b/ocaml/simple.ml @@ -9,14 +9,19 @@ open Pure type var = int;; type t = | V of var - | A of bool * t * t + | A of (bool ref) * t * t | L of (bool * t) ;; -let rec measure_of_t = function - | V _ -> 0 - | A(b,t1,t2) -> (if b then 1 else 0) + measure_of_t t1 + measure_of_t t2 - | L(b,t) -> if b then measure_of_t t else 0 +let measure_of_t = + let rec aux acc = function + | V _ -> acc, 0 + | A(b,t1,t2) -> + let acc, m1 = aux acc t1 in + let acc, m2 = aux acc t2 in + if not (List.memq b acc) && !b then b::acc, 1 + m1 + m2 else acc, m1 + m2 + | L(b,t) -> if b then aux acc t else acc, 0 + in snd ++ (aux []) ;; let string_of_t = @@ -30,7 +35,7 @@ let string_of_t = | A _ | L _ as t -> "(" ^ string_of_term_no_pars level t ^ ")" and string_of_term_no_pars_app level = function - | A(b,t1,t2) -> string_of_term_no_pars_app level t1 ^ (if b then "," else " ") ^ string_of_term_w_pars level t2 + | A(b,t1,t2) -> string_of_term_no_pars_app level t1 ^ (if !b then "," else " ") ^ string_of_term_w_pars level t2 | _ as t -> string_of_term_w_pars level t and string_of_term_no_pars level = function | L(_,t) -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t @@ -39,7 +44,7 @@ let string_of_t = ;; -let delta = L(true,A(true,V 0, V 0));; +let delta = L(true,A(ref true,V 0, V 0));; (* does NOT lift the argument *) let mk_lams = fold_nat (fun x _ -> L(false,x)) ;; @@ -105,13 +110,13 @@ let rec subst b' level delift sub = function | V v -> if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v) | L(b,t) -> L(b, subst b' (level + 1) delift sub t) - | A(_,t1,(V v as t2)) when b' && v = level + fst sub -> + | A(_,t1,(V v as t2)) when !b' && v = level + fst sub -> mk_app b' (subst b' level delift sub t1) (subst b' level delift sub t2) | A(b,t1,t2) -> mk_app b (subst b' level delift sub t1) (subst b' level delift sub t2) and mk_app b' t1 t2 = if t1 = delta && t2 = delta then raise B else match t1 with - | L(b,t1) -> subst (b' && not b) 0 true (0, t2) t1 + | L(b,t1) -> subst (ref (!b' && not b)) 0 true (0, t2) t1 | _ -> A (b', t1, t2) and lift n = let rec aux lev = @@ -121,14 +126,14 @@ and lift n = | A (b,t1, t2) -> A (b,aux lev t1, aux lev t2) in aux 0 ;; -let subst = subst false 0 false;; -let mk_app = mk_app true;; +let subst = subst (ref false) 0 false;; +let mk_app = mk_app (ref true);; let eta_eq = let rec aux t1 t2 = match t1, t2 with | L(_,t1), L(_,t2) -> aux t1 t2 - | L(_,t1), t2 -> aux t1 (A(true,lift 1 t2,V 0)) - | t1, L(_,t2) -> aux (A(true,lift 1 t1,V 0)) t2 + | L(_,t1), t2 -> aux t1 (A(ref true,lift 1 t2,V 0)) + | t1, L(_,t2) -> aux (A(ref true,lift 1 t1,V 0)) t2 | V a, V b -> a = b | A(_,t1,t2), A(_,u1,u2) -> aux t1 u1 && aux t2 u2 | _, _ -> false @@ -244,36 +249,6 @@ let compute_max_lambdas_at hd_var j = let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);; -(* eat the arguments of the divergent and explode. - It does NOT perform any check, may fail if done unsafely *) -let eat p = -print_cmd "EAT" ""; - let var, k = get_inert p.div in - let phase = p.phase in - let p = - match phase with - | `One -> - let n = 1 + max - (compute_max_lambdas_at var (k-1) p.div) - (compute_max_lambdas_at var (k-1) p.conv) in - (* apply fresh vars *) - let p, t = fold_nat (fun (p, t) _ -> - let p, v = freshvar p in - p, A(false, t, V (v + k)) - ) (p, V 0) n in - let p = {p with phase=`Two} in - let t = A(false, t, delta) in - let t = fold_nat (fun t m -> A(false, t, V (k-m))) t (k-1) in - let subst = var, mk_lams t k in - let p = subst_in_problem subst p in - let _, args = get_inert p.div in - {p with div = inert_cut_at (args-k) p.div} - | `Two -> - let subst = var, mk_lams delta k in - subst_in_problem subst p in - sanity p -;; - (* 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 @@ -281,10 +256,10 @@ 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(false, t, V (v + k + 1)) + p, A(ref false, t, V (v + k + 1)) ) (p, V 0) n in let t = (* apply bound variables V_k..V_0 *) - fold_nat (fun t m -> A(false, t, V (k-m+1))) t (k+1) in + fold_nat (fun t m -> A(ref false, t, V (k-m+1))) t (k+1) 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 -- 2.39.2