From 4924ddb52a836eea45a593a7f4faccca64bac9e7 Mon Sep 17 00:00:00 2001 From: Ferruccio Guidi Date: Tue, 16 Dec 2008 19:32:12 +0000 Subject: [PATCH] we added the implicit coercion for modus tollens --- helm/software/lambda-delta/.depend.opt | 24 +++--- .../software/lambda-delta/automath/autItem.ml | 6 ++ .../lambda-delta/basic_ag/bagReduction.ml | 86 +++++++++++-------- .../lambda-delta/basic_ag/bagReduction.mli | 4 +- .../software/lambda-delta/basic_ag/bagType.ml | 63 ++++++++++++-- helm/software/lambda-delta/lib/cps.ml | 11 ++- 6 files changed, 136 insertions(+), 58 deletions(-) diff --git a/helm/software/lambda-delta/.depend.opt b/helm/software/lambda-delta/.depend.opt index 88f23f93e..b8bf8f28d 100644 --- a/helm/software/lambda-delta/.depend.opt +++ b/helm/software/lambda-delta/.depend.opt @@ -35,22 +35,24 @@ basic_ag/bagSubstitution.cmo: lib/share.cmx basic_ag/bag.cmx \ basic_ag/bagSubstitution.cmi basic_ag/bagSubstitution.cmx: lib/share.cmx basic_ag/bag.cmx \ basic_ag/bagSubstitution.cmi -basic_ag/bagReduction.cmi: basic_ag/bag.cmx -basic_ag/bagReduction.cmo: lib/log.cmi lib/cps.cmx \ +basic_ag/bagReduction.cmi: lib/nUri.cmi basic_ag/bag.cmx +basic_ag/bagReduction.cmo: lib/nUri.cmi lib/log.cmi lib/cps.cmx \ basic_ag/bagSubstitution.cmi basic_ag/bagOutput.cmi \ - basic_ag/bagEnvironment.cmi basic_ag/bag.cmx basic_ag/bagReduction.cmi -basic_ag/bagReduction.cmx: lib/log.cmx lib/cps.cmx \ + basic_ag/bagEnvironment.cmi basic_ag/bag.cmx automath/autItem.cmx \ + basic_ag/bagReduction.cmi +basic_ag/bagReduction.cmx: lib/nUri.cmx lib/log.cmx lib/cps.cmx \ basic_ag/bagSubstitution.cmx basic_ag/bagOutput.cmx \ - basic_ag/bagEnvironment.cmx basic_ag/bag.cmx basic_ag/bagReduction.cmi + basic_ag/bagEnvironment.cmx basic_ag/bag.cmx automath/autItem.cmx \ + basic_ag/bagReduction.cmi basic_ag/bagType.cmi: lib/hierarchy.cmi basic_ag/bag.cmx basic_ag/bagType.cmo: lib/share.cmx lib/nUri.cmi lib/log.cmi \ - lib/hierarchy.cmi basic_ag/bagReduction.cmi basic_ag/bagOutput.cmi \ - basic_ag/bagEnvironment.cmi basic_ag/bag.cmx automath/autItem.cmx \ - basic_ag/bagType.cmi + lib/hierarchy.cmi lib/cps.cmx basic_ag/bagReduction.cmi \ + basic_ag/bagOutput.cmi basic_ag/bagEnvironment.cmi basic_ag/bag.cmx \ + automath/autItem.cmx basic_ag/bagType.cmi basic_ag/bagType.cmx: lib/share.cmx lib/nUri.cmx lib/log.cmx \ - lib/hierarchy.cmx basic_ag/bagReduction.cmx basic_ag/bagOutput.cmx \ - basic_ag/bagEnvironment.cmx basic_ag/bag.cmx automath/autItem.cmx \ - basic_ag/bagType.cmi + lib/hierarchy.cmx lib/cps.cmx basic_ag/bagReduction.cmx \ + basic_ag/bagOutput.cmx basic_ag/bagEnvironment.cmx basic_ag/bag.cmx \ + automath/autItem.cmx basic_ag/bagType.cmi basic_ag/bagUntrusted.cmi: lib/hierarchy.cmi basic_ag/bag.cmx basic_ag/bagUntrusted.cmo: basic_ag/bagType.cmi basic_ag/bagEnvironment.cmi \ basic_ag/bag.cmx basic_ag/bagUntrusted.cmi diff --git a/helm/software/lambda-delta/automath/autItem.ml b/helm/software/lambda-delta/automath/autItem.ml index 0f8b08234..19d4e1af1 100644 --- a/helm/software/lambda-delta/automath/autItem.ml +++ b/helm/software/lambda-delta/automath/autItem.ml @@ -20,3 +20,9 @@ let uri s = U.uri_of_string ("ld:" ^ s) let imp = uri "/l/imp" let mp = uri "/l/mp" + +let mt = uri "/l/mt" + +let con = uri "/l/con" + +let not = uri "/l/not" diff --git a/helm/software/lambda-delta/basic_ag/bagReduction.ml b/helm/software/lambda-delta/basic_ag/bagReduction.ml index b0cd88d6c..0ec3d2c2b 100644 --- a/helm/software/lambda-delta/basic_ag/bagReduction.ml +++ b/helm/software/lambda-delta/basic_ag/bagReduction.ml @@ -9,8 +9,10 @@ \ / This software is distributed as is, NO WARRANTY. V_______________________________________________________________ *) +module U = NUri module C = Cps module L = Log +module I = AutItem module B = Bag module O = BagOutput module E = BagEnvironment @@ -26,13 +28,15 @@ type machine = { type whd_result = | Sort_ of int | LRef_ of int * B.term option - | GRef_ of int * B.bind + | GRef_ of B.obj | Bind_ of int * B.id * B.term * B.term type ho_whd_result = | Sort of int | Abst of B.term +type ac_result = (int * NUri.uri * Bag.term list) list option + (* Internal functions *******************************************************) let level = 5 @@ -52,6 +56,10 @@ let unwind_to_term f m t = let f mc = C.list_fold_left f map t mc in B.contents f m.c +let unwind_stack f m = + let map f v = unwind_to_term f m v in + C.list_map f map m.s + let get f c m i = let f = function | Some (_, b) -> f b @@ -69,7 +77,7 @@ let push f c m l id w = let rec whd f c m x = match x with | B.Sort h -> f m (Sort_ h) | B.GRef uri -> - let f (i, _, b) = f m (GRef_ (i, b)) in + let f obj = f m (GRef_ obj) in E.get_obj f uri | B.LRef i -> let f = function @@ -91,18 +99,22 @@ let rec whd f c m x = match x with let f mc = whd f c {m with c = mc} t in B.push f m.c l id b +let insert f i uri vs = function + | Some l -> f (Some ((i, uri, vs) :: l)) + | None -> assert false + (* Interface functions ******************************************************) let rec ho_whd f c m x = let aux m = function - | Sort_ h -> f c (Sort h) - | Bind_ (_, _, w, _) -> + | Sort_ h -> f c (Sort h) + | Bind_ (_, _, w, _) -> let f c = f c (Abst w) in unwind_to_context f c m - | LRef_ (_, Some w) -> ho_whd f c m w - | GRef_ (_, B.Abst u) -> ho_whd f c m u - | GRef_ (_, B.Abbr t) -> ho_whd f c m t - | LRef_ (_, None) -> assert false - | GRef_ (_, B.Void) -> assert false + | LRef_ (_, Some w) -> ho_whd f c m w + | GRef_ (_, _, B.Abst u) -> ho_whd f c m u + | GRef_ (_, _, B.Abbr t) -> ho_whd f c m t + | LRef_ (_, None) -> assert false + | GRef_ (_, _, B.Void) -> assert false in whd aux c m x @@ -111,44 +123,46 @@ let ho_whd f c t = L.box level; L.log O.specs level (L.ct_items1 "Now scanning" c t); ho_whd f c empty_machine t -let rec are_convertible f c m1 t1 m2 t2 = +let rec are_convertible f xl c m1 t1 m2 t2 = let rec aux m1 r1 m2 r2 = match r1, r2 with - | Sort_ h1, Sort_ h2 -> f (h1 = h2) - | LRef_ (i1, _), LRef_ (i2, _) -> - if i1 = i2 then are_convertible_stacks f c m1 m2 else f false - | GRef_ (a1, B.Abst _), GRef_ (a2, B.Abst _) -> - if a1 = a2 then are_convertible_stacks f c m1 m2 else f false - | GRef_ (a1, B.Abbr v1), GRef_ (a2, B.Abbr v2) -> - if a1 = a2 then are_convertible_stacks f c m1 m2 else + | Sort_ h1, Sort_ h2 -> + if h1 = h2 then f xl else f None + | LRef_ (i1, _), LRef_ (i2, _) -> + if i1 = i2 then are_convertible_stacks f xl c m1 m2 else f None + | GRef_ (a1, _, B.Abst _), GRef_ (a2, _, B.Abst _) -> + if a1 = a2 then are_convertible_stacks f xl c m1 m2 else f None + | GRef_ (a1, _, B.Abbr v1), GRef_ (a2, _, B.Abbr v2) -> + if a1 = a2 then are_convertible_stacks f xl c m1 m2 else if a1 < a2 then whd (aux m1 r1) c m2 v2 else whd (aux_rev m2 r2) c m1 v1 - | _, GRef_ (_, B.Abbr v2) -> + | _, GRef_ (_, _, B.Abbr v2) -> whd (aux m1 r1) c m2 v2 - | GRef_ (_, B.Abbr v1), _ -> + | GRef_ (_, _, B.Abbr v1), _ -> whd (aux_rev m2 r2) c m1 v1 - | Bind_ (l1, id1, w1, t1), Bind_ (l2, id2, w2, t2) -> - let f b = - if b then - let f c = - S.subst (are_convertible f c m1 t1 m2) l1 l2 t2 - in - push f c m1 l1 id1 w1 - else f false + | Bind_ (l1, id1, w1, t1), Bind_ (l2, id2, w2, t2) -> + let f xl = + let h c = S.subst (are_convertible f xl c m1 t1 m2) l1 l2 t2 in + if xl = None then f xl else push h c m1 l1 id1 w1 in - are_convertible f c m1 w1 m2 w2 - | _ -> f false + are_convertible f xl c m1 w1 m2 w2 +(* we detect the AUT-QE reduction rule for type/prop inclusion *) + | GRef_ (_, uri, B.Abst _), Bind_ (l2, _, _, _) -> + let g vs = insert f l2 uri vs xl in + if U.eq uri I.imp then unwind_stack g m1 else + begin L.warn (U.string_of_uri uri); f None end + | _ -> f None and aux_rev m2 r2 m1 r1 = aux m1 r1 m2 r2 in let f m1 r1 = whd (aux m1 r1) c m2 t2 in whd f c m1 t1 -and are_convertible_stacks f c m1 m2 = +and are_convertible_stacks f xl c m1 m2 = let mm1, mm2 = {m1 with s = []}, {m2 with s = []} in - let map f v1 v2 = are_convertible f c mm1 v1 mm2 v2 in - if List.length m1.s <> List.length m2.s then f false else - C.forall2 f map m1.s m2.s + let map f xl v1 v2 = are_convertible f xl c mm1 v1 mm2 v2 in + if List.length m1.s <> List.length m2.s then f None else + C.list_fold_left2 f map xl m1.s m2.s -let are_convertible f c t1 t2 = +let are_convertible f c u t = let f b = L.unbox level; f b in L.box level; - L.log O.specs level (L.ct_items2 "Now converting" c t1 "and" c t2); - are_convertible f c empty_machine t1 empty_machine t2 + L.log O.specs level (L.ct_items2 "Now converting" c u "and" c t); + are_convertible f (Some []) c empty_machine u empty_machine t diff --git a/helm/software/lambda-delta/basic_ag/bagReduction.mli b/helm/software/lambda-delta/basic_ag/bagReduction.mli index 2b5f02e71..ed7ae1792 100644 --- a/helm/software/lambda-delta/basic_ag/bagReduction.mli +++ b/helm/software/lambda-delta/basic_ag/bagReduction.mli @@ -16,8 +16,10 @@ type ho_whd_result = | Sort of int | Abst of Bag.term +type ac_result = (int * NUri.uri * Bag.term list) list option + val ho_whd: (Bag.context -> ho_whd_result -> 'a) -> Bag.context -> Bag.term -> 'a val are_convertible: - (bool -> 'a) -> Bag.context -> Bag.term -> Bag.term -> 'a + (ac_result -> 'a) -> Bag.context -> Bag.term -> Bag.term -> 'a diff --git a/helm/software/lambda-delta/basic_ag/bagType.ml b/helm/software/lambda-delta/basic_ag/bagType.ml index 52c485ae7..80b883ff4 100644 --- a/helm/software/lambda-delta/basic_ag/bagType.ml +++ b/helm/software/lambda-delta/basic_ag/bagType.ml @@ -10,6 +10,7 @@ V_______________________________________________________________ *) module U = NUri +module C = Cps module S = Share module L = Log module H = Hierarchy @@ -35,6 +36,46 @@ let mk_gref u l = let map t v = B.Appl (v, t) in List.fold_left map (B.GRef u) l +let add_coercion f t (i, uri, vs) = + let rec add f x = match x with + | B.Sort _ + | B.LRef _ + | B.GRef _ -> f x + | B.Cast (u, t) -> + let f uu = + let f tt = f (S.sh2 u uu t tt x B.cast) in + add f t + in + add f u + | B.Appl (v, t) -> + let f vv = + let f tt = f (S.sh2 v vv t tt x B.appl) in + add f t + in + add f v + | B.Bind (l, _, _, _) when i = l -> + if U.eq uri I.imp then f (mk_gref I.mt (vs @ [x])) else + assert false + | B.Bind (l, id, B.Abst w, t) -> + let f ww = + let f tt = f (S.sh2 w ww t tt x (B.bind_abst l id)) in + add f t + in + add f w + | B.Bind (l, id, B.Abbr v, t) -> + let f vv = + let f tt = f (S.sh2 v vv t tt x (B.bind_abbr l id)) in + add f t + in + add f v + | B.Bind (l, id, B.Void, t) -> + let f tt = f (S.sh1 t tt x (B.bind l id B.Void)) in + add f t + in + add f t + +let add_coercions f = C.list_fold_left f add_coercion + (* Interface functions ******************************************************) let rec b_type_of f g c x = @@ -93,24 +134,29 @@ let rec b_type_of f g c x = L.box (succ level); L.log O.specs (succ level) (L.ct_items1 "Just scanned" cc w); L.unbox (succ level); - let f b = - if b then f (S.sh2 v xv t xt x B.appl) (B.appl xv tt) else - error2 "the term" cc xv "must be of type" cc w + let f = function + | Some l -> f (S.sh2 v xv t xt x B.appl) (B.appl xv tt) + | None -> error2 "the term" cc xv "must be of type" cc w in R.are_convertible f cc w vv in let f xv vv xt = function (* inserting a missing "modus ponens" *) - | B.Appl (y2, B.Appl (y1, B.GRef u)) when U.eq u I.imp -> + | B.Appl (y2, B.Appl (y1, B.GRef uri)) when U.eq uri I.imp -> b_type_of f g c (mk_gref I.mp [y1; y2; xv; xt]) - | tt -> R.ho_whd (h xv vv xt tt) c tt + | B.Appl (y1, B.GRef uri) when U.eq uri I.not -> + b_type_of f g c (mk_gref I.mp [y1; B.GRef I.con; xv; xt]) + | tt -> R.ho_whd (h xv vv xt tt) c tt in let f xv vv = b_type_of (f xv vv) g c t in type_of f g c v | B.Cast (u, t) -> - let f xu xt b = - if b then f (S.sh2 u xu t xt x B.cast) xu else - error2 "the term" c xt "must be of type" c xu + let f xu xt = function + | Some [] -> f (S.sh2 u xu t xt x B.cast) xu + | Some l -> + let f xt = type_of f g c (S.sh2 u xu t xt x B.cast) in + add_coercions f xt l + | None -> error2 "the term" c xt "must be of type" c xu in let f xu xt tt = R.are_convertible (f xu xt) c xu tt in let f xu _ = b_type_of (f xu) g c t in @@ -119,3 +165,4 @@ let rec b_type_of f g c x = and type_of f g c x = let f t u = L.unbox level; f t u in L.box level; b_type_of f g c x + diff --git a/helm/software/lambda-delta/lib/cps.ml b/helm/software/lambda-delta/lib/cps.ml index 9fc5dc9b2..b61fccfa4 100644 --- a/helm/software/lambda-delta/lib/cps.ml +++ b/helm/software/lambda-delta/lib/cps.ml @@ -30,10 +30,10 @@ let rec list_rev_map_append f map ~tail = function let f hd = list_rev_map_append f map ~tail:(hd :: tail) tl in map f hd -let rec forall2 f map l1 l2 = match l1, l2 with +let rec list_forall2 f map l1 l2 = match l1, l2 with | [], [] -> f true | hd1 :: tl1, hd2 :: tl2 -> - let f b = if b then forall2 f map tl1 tl2 else f false in + let f b = if b then list_forall2 f map tl1 tl2 else f false in map f hd1 hd2 | _ -> f false @@ -56,3 +56,10 @@ let list_map f = let list_iter f map l = let map f () x = map f x in list_fold_left f map () l + +let rec list_fold_left2 f map a l1 l2 = match l1, l2 with + | [], [] -> f a + | hd1 :: tl1, hd2 :: tl2 -> + let f a = if a = None then f a else list_fold_left2 f map a tl1 tl2 in + map f a hd1 hd2 + | _ -> f None -- 2.39.2