]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/helena/src/basic_rg/brgReduction.ml
update in helena
[helm.git] / helm / software / helena / src / basic_rg / brgReduction.ml
index 98747ab55a810f10ee463d28cb8270f56dd16a76..ffe223ab5ba02de662a2eea8d134241b8b6fe2b9 100644 (file)
 
 module U  = NUri
 module C  = Cps
-module W  = Share
+module S  = Share
 module L  = Log
+module G  = Options
+module H  = Hierarchy
+module N  = Layer
 module E  = Entity
-module N  = Level
 module O  = Output
-module Q  = Ccs
-module S  = Status
 module B  = Brg
 module BO = BrgOutput
 module BE = BrgEnvironment
 
-type kam = {
-   e: B.lenv;                 (* environment *)
-   s: (B.lenv * B.term) list; (* stack       *)
-   d: int                     (* depth       *)
+type rtm = {
+   e: B.lenv;                 (* environment              *)
+   s: (B.lenv * B.term) list; (* stack                    *)
+   l: int;                    (* level                    *)
+   n: int option;             (* expected type iterations *)
 }
 
+type message = (rtm, B.term) L.message
+
 (* Internal functions *******************************************************)
 
 let level = 5
 
-let log1 s c t =
-   let sc, st = s ^ " in the environment", "the term" in
-   L.log BO.specs level (L.et_items1 sc c st t)
+let sublevel = succ level
+
+let log1 st s c t =
+   let s1, s2 = s ^ " in the environment", "the term" in
+   L.log st BO.specs (pred level) (L.et_items1 s1 c s2 t)
 
-let log2 s cu u ct t =
-   let s1, s2, s3 = s ^ " in the environment", "the term", "and in the environment" in
-   L.log BO.specs level (L.et_items2 s1 cu s2 u ~sc2:s3 ~c2:ct s2 t)
+let log2 st s cu u ct t =
+   let s1, s2, s3 = s ^ " in the environment (expected)", "the term", "and in the environment (inferred)" in
+   L.log st BO.specs (pred level) (L.et_items2 s1 cu s2 u ~sc2:s3 ~c2:ct s2 t)
 
-let rec list_and map = function
+let rec list_and map = function
    | hd1 :: tl1, hd2 :: tl2 ->
-      if map hd1 hd2 then list_and map (tl1, tl2) else false
-   | l1, l2                 -> l1 = l2
+      let f b = f (b && map hd1 hd2) in
+      list_and f map (tl1, tl2)
+   | l1, l2                 -> f (l1 = l2)
+
+let zero = Some 0
 
 (* check closure *)
 let are_alpha_convertible err f t1 t2 =
    let rec aux f = function
-      | B.Sort (_, p1), B.Sort (_, p2)
+      | B.Sort p1, B.Sort p2
       | B.LRef (_, p1), B.LRef (_, p2)         ->
          if p1 = p2 then f () else err ()
       | B.GRef (_, u1), B.GRef (_, u2)         ->
          if U.eq u1 u2 then f () else err ()
-      | B.Cast (_, v1, t1), B.Cast (_, v2, t2)         
+      | B.Cast (v1, t1), B.Cast (v2, t2)         
       | B.Appl (_, v1, t1), B.Appl (_, v2, t2) ->
          let f _ = aux f (t1, t2) in
         aux f (v1, v2)
@@ -62,113 +70,189 @@ let are_alpha_convertible err f t1 t2 =
         aux_bind f (b1, b2)
       | _                                      -> err ()
    and aux_bind f = function
-      | B.Abbr v1, B.Abbr v2                          -> aux f (v1, v2)
-      | B.Abst (n1, v1), B.Abst (n2, v2) when n1 = n2 -> aux f (v1, v2)
-      | B.Void, B.Void                                -> f ()
-      | _                                             -> err ()
+      | B.Abbr v1, B.Abbr v2                                             -> aux f (v1, v2)
+      | B.Abst (r1, n1, v1), B.Abst (r2, n2, v2) when r1 = r2 && n1 = n2 -> aux f (v1, v2)
+      | B.Void, B.Void                                                   -> f ()
+      | _                                                                -> err ()
    in
-   if W.eq t1 t2 then f () else aux f (t1, t2)
+   if S.eq t1 t2 then f () else aux f (t1, t2)
+
+let assert_tstep m vo = match m.n with
+   | Some n -> n > 0
+   | None   -> vo
+
+let tstep m = match m.n with
+   | Some n -> {m with n = Some (pred n)}
+   | None   -> m
+
+let tsteps m = match m.n with
+   | Some n -> n
+   | None   -> 0
 
 let get m i =
-   let _, c, a, b = B.get m.e i in c, a, b
+   let _, c, a, _, b = B.get m.e i in c, a, b
 
 (* to share *)
-let rec step st m x = 
-(*   L.warn "entering R.step"; *)
-   match x with
-   | B.Sort _                     -> m, None, x
-   | B.GRef (_, uri)              ->
-      begin match BE.get_entity uri with
-         | _, _, E.Abbr v when st.S.delta   ->
-           O.add ~gdelta:1 (); step st m v
-         | _, _, E.Abst (_, w) when st.S.rt ->
-            O.add ~grt:1 (); step st m w        
-        | a, _, E.Abbr v                   ->
-           let e = E.apix C.err C.start a in
-           m, Some (e, a, B.Abbr v), x   
-        | a, _, E.Abst (n, w)              ->
-           let e = E.apix C.err C.start a in
-           m, Some (e, a, B.Abst (n, w)), x
-        | _, _, E.Void                     -> assert false
+let rec step st m r =
+IFDEF TRACE THEN
+   if !G.ct >= sublevel then 
+   log1 st (Printf.sprintf "entering R.step: l=%u, n=%s," m.l (match m.n with Some n -> string_of_int n | None -> "infinite")) m.e r
+ELSE () END;
+   match r with
+   | B.Sort k                            ->
+      if assert_tstep m false then
+         step st (tstep m) (B.Sort (H.apply k))      
+      else m, r, None
+   | B.GRef (_, u)                       ->
+      begin match BE.get_entity u with
+         | _, a, _, E.Abbr (_, v) ->
+              m, B.gref a u, Some v
+         | _, _, _, E.Abst (_, w) ->
+            if assert_tstep m true then begin
+IFDEF SUMMARY THEN
+               if !G.summary then O.add ~grt:1 ()
+ELSE () END;
+               step st (tstep m) w
+            end else
+              m, r, None   
+        | _, _, _, E.Void        ->
+            assert false
       end
-   | B.LRef (_, i)                ->
+   | B.LRef (_, i)                       ->
       begin match get m i with
-        | c, _, B.Abbr v                   ->
-           O.add ~ldelta:1 ();
+        | c, _, B.Abbr v         ->
+IFDEF SUMMARY THEN
+           if !G.summary then O.add ~ldelta:1 ()
+ELSE () END; 
            step st {m with e = c} v
-        | c, _, B.Abst (_, w) when st.S.rt ->
-            O.add ~lrt:1 ();
-            step st {m with e = c} w
-        | c, _, B.Void                     ->
+        | c, a, B.Abst (_, _, w) ->
+            if assert_tstep m true then begin
+IFDEF SUMMARY THEN
+               if !G.summary then O.add ~lrt:1 ()
+ELSE () END;
+               step st {(tstep m) with e = c} w
+            end else
+              m, B.lref a i, None
+        | _, _, B.Void           ->
            assert false
-        | c, a, (B.Abst _ as b)            ->
-           let e = E.apix C.err C.start a in
-           {m with e = c}, Some (e, a, b), x
       end
-   | B.Cast (_, _, t)             ->
-      O.add ~tau:1 ();
-      step st m t
-   | B.Appl (_, v, t)             ->
+   | B.Cast (u, t)                       ->
+      if assert_tstep m false then begin
+IFDEF SUMMARY THEN
+         if !G.summary then O.add ~e:1 ()
+ELSE () END;
+         step st (tstep m) u
+      end else begin
+IFDEF SUMMARY THEN
+         if !G.summary then O.add ~epsilon:1 ()
+ELSE () END;
+         step st m t
+      end
+   | B.Appl (_, v, t)                    ->
       step st {m with s = (m.e, v) :: m.s} t   
-   | B.Bind (a, B.Abst (n, w), t) ->
-      begin match m.s with
-         | []          -> m, None, x
+   | B.Bind (y, B.Abst (false, n, w), t) ->
+      let i = tsteps m in
+IFDEF SUMMARY THEN
+      if !G.summary then O.add ~x:i ()
+ELSE () END;
+      let n = if i = 0 then n else N.minus st n i in
+      let r = B.Bind (y, B.Abst (true, n, w), t) in
+      step st m r
+   | B.Bind (y, B.Abst (true, n, w), t)  ->
+      if !G.si || N.is_not_zero st n then begin match m.s with
+         | []          ->
+            m, B.Bind (y, B.Abst (true, n, w), t), None
         | (c, v) :: s ->
-            if N.is_zero n then Q.add_nonzero st.S.cc a;
-           O.add ~beta:1 ~theta:(List.length s) ();
-           let e = B.push m.e c a (B.abbr v) (* (B.Cast ([], w, v)) *) in 
+IFDEF SUMMARY THEN
+           if !G.summary then O.add ~beta:1 ~theta:(List.length s) ()
+ELSE () END;
+           let v = B.Cast (w, v) in
+            let e = B.push m.e c E.empty_node y (B.abbr v) in
            step st {m with e = e; s = s} t
+      end else begin
+IFDEF SUMMARY THEN
+         if !G.summary then O.add ~upsilon:1 ()
+ELSE () END;
+         let e = B.push m.e m.e E.empty_node y B.Void in (**) (* this is wrong in general *) 
+         step st {m with e = e} t
       end
-   | B.Bind (a, b, t)        ->
-      O.add ~theta:(List.length m.s) ();
-      let e = B.push m.e m.e a b in 
+   | B.Bind (y, b, t)        ->
+IFDEF SUMMARY THEN
+      if !G.summary then O.add ~theta:(List.length m.s) ()
+ELSE () END;
+      let e = B.push m.e m.e E.empty_node y b in 
       step st {m with e = e} t
 
-let push m a b = 
-   assert (m.s = []);
-   let a, d = match b with
-      | B.Abst _ -> E.Apix m.d :: a, succ m.d
-      | b        -> a, m.d
+let assert_iterations m1 m2 =
+   m1.n = m2.n
+
+let reset m ?(e=m.e) n =
+   {m with e = e; n = n; s = []} 
+
+let push m y b = 
+   let a, l = match b with
+      | B.Abst _ -> E.node_attrs ~apix:m.l (), succ m.l
+      | _        -> E.empty_node, m.l
    in
-   let e = B.push m.e m.e a b in
-   {m with e = e; d = d}
-
-let rec ac_nfs st (m1, r1, u) (m2, r2, t) =
-   log2 "Now converting nfs" m1.e u m2.e t;
-   match r1, u, r2, t with
-      | _, B.Sort (_, h1), _, B.Sort (_, h2)                   ->
-         h1 = h2  
-      | Some (e1, _, B.Abst _), _, Some (e2, _, B.Abst _), _   ->
+   let e = B.push m.e m.e a y b in
+   {m with e = e; l = l}
+
+let rec ac_nfs st (m1, t1, r1) (m2, t2, r2) =
+IFDEF TRACE THEN
+   if !G.ct >= level then log2 st "Now converting nfs" m1.e t1 m2.e t2
+ELSE () END;
+   match t1, r1, t2, r2 with
+      | B.Sort k1, _, B.Sort k2, _                         ->
+         k1 = k2
+      | B.LRef ({E.n_apix = e1}, _), _, 
+        B.LRef ({E.n_apix = e2}, _), _                     ->
         if e1 = e2 then ac_stacks st m1 m2 else false
-      | Some (e1, _, B.Abbr v1), _, Some (e2, _, B.Abbr v2), _ ->
-         if e1 = e2 then
-           if ac_stacks st m1 m2 then true else begin
-              O.add ~gdelta:2 (); ac st m1 v1 m2 v2
-           end
-        else if e1 < e2 then begin 
-            O.add ~gdelta:1 ();
-           ac_nfs st (m1, r1, u) (step st m2 v2)
-        end else begin
-           O.add ~gdelta:1 ();
-           ac_nfs st (step st m1 v1) (m2, r2, t) 
+      | B.GRef (_, u1), None, B.GRef (_, u2), None   ->
+        if U.eq u1 u2 && assert_iterations m1 m2 then ac_stacks st m1 m2 else false
+      | B.GRef ({E.n_apix = e1}, u1), Some v1, 
+        B.GRef ({E.n_apix = e2}, u2), Some v2              ->
+         if U.eq u1 u2 && assert_iterations m1 m2 && ac_stacks st m1 m2 then true
+         else if e1 < e2 then begin
+IFDEF SUMMARY THEN 
+            if !G.summary then O.add ~gdelta:1 ()
+ELSE () END;
+           ac_nfs st (m1, t1, r1) (step st m2 v2)
+        end else if e2 < e1 then begin
+IFDEF SUMMARY THEN
+           if !G.summary then O.add ~gdelta:1 ()
+ELSE () END;
+           ac_nfs st (step st m1 v1) (m2, t2, r2) 
+         end else begin
+IFDEF SUMMARY THEN
+           if !G.summary then O.add ~gdelta:2 ()
+ELSE () END;
+           ac st m1 v1 m2 v2
          end
-      | _, _, Some (_, _, B.Abbr v2), _                        ->
-         O.add ~gdelta:1 ();
-        ac_nfs st (m1, r1, u) (step st m2 v2)      
-      | Some (_, _, B.Abbr v1), _, _, _                        ->
-         O.add ~gdelta:1 ();
-        ac_nfs st (step st m1 v1) (m2, r2, t)             
-      | _, B.Bind (a1, (B.Abst (n1, w1) as b1), t1), 
-        _, B.Bind (a2, (B.Abst (n2, w2) as b2), t2)            ->
-        if n1 = n2 then () else Q.add_equal st.S.cc a1 a2;
-        if ac {st with S.si = false} m1 w1 m2 w2 then
-           ac st (push m1 a1 b1) t1 (push m2 a2 b2) t2
+      | _, _, B.GRef _, Some v2                            ->
+IFDEF SUMMARY THEN
+         if !G.summary then O.add ~gdelta:1 ()
+ELSE () END;
+        ac_nfs st (m1, t1, r1) (step st m2 v2)
+      | B.GRef _, Some v1, _, _                            ->
+IFDEF SUMMARY THEN
+        if !G.summary then O.add ~gdelta:1 ()
+ELSE () END;
+        ac_nfs st (step st m1 v1) (m2, t2, r2)
+      | B.Bind (y1, (B.Abst (true, n1, w1) as b1), t1), _, 
+        B.Bind (y2, (B.Abst (true, n2, w2) as b2), t2), _  ->
+        if ((!G.cc && N.assert_equal st n1 n2) || N.are_equal st n1 n2) &&
+            ac st (reset m1 zero) w1 (reset m2 zero) w2
+         then ac st (push m1 y1 b1) t1 (push m2 y2 b2) t2
         else false
-      | _, B.Sort _, _, B.Bind (a, (B.Abst (n, _) as b), t)    ->
-         if N.is_zero n then () else Q.add_zero st.S.cc a;
-        O.add ~si:1 ();
-        ac st (push m1 a b) u (push m2 a b) t
-      | _                                                      -> false
+      | B.Sort _, _, B.Bind (y, B.Abst (true, n, _), t), _ ->
+         if !G.si then
+            if !G.cc && not (N.assert_zero st n) then false else begin
+IFDEF SUMMARY THEN
+           if !G.summary then O.add ~upsilon:1 ()
+ELSE () END;
+           ac st (push m1 y B.Void) t1 (push m2 y B.Void) t end
+         else false
+      | _                                                  -> false
 
 and ac st m1 t1 m2 t2 =
 (*   L.warn "entering R.are_convertible"; *)
@@ -176,40 +260,53 @@ and ac st m1 t1 m2 t2 =
 
 and ac_stacks st m1 m2 =
 (*   L.warn "entering R.are_convertible_stacks"; *)
-   if List.length m1.s <> List.length m2.s then false else
    let map (c1, v1) (c2, v2) =
-      let m1, m2 = {m1 with e = c1; s = []}, {m2 with e = c2; s = []} in
-      ac {st with S.si = false} m1 v1 m2 v2
+      let m1, m2 = reset m1 ~e:c1 zero, reset m2 ~e:c2 zero in
+      ac st m1 v1 m2 v2
    in
-   list_and map (m1.s, m2.s)
+   list_and C.start map (m1.s, m2.s)
+
+let rec ih_nfs st (m, t, r) =
+   match t, r with
+      | B.GRef _, Some v ->
+IFDEF SUMMARY THEN
+        if !G.summary then O.add ~gdelta:1 ()
+ELSE () END;
+        ih st m v
+      | _                -> m, t
+
+and ih st m t = ih_nfs st (step st m t)
 
 (* Interface functions ******************************************************)
 
-let empty_kam = { 
-   e = B.empty; s = []; d = 0
+let empty_rtm = { 
+   e = B.empty; s = []; l = 0; n = None
 }
 
 let get m i =
    assert (m.s = []);
-   let _, _, _, b = B.get m.e i in b
+   let _, _, _, _, b = B.get m.e i in b
 
-let xwhd st m t =
-   L.box level; log1 "Now scanning" m.e t;   
-   let m, _, t = step {st with S.delta = true; S.rt = true} m t in
-   L.unbox level; m, t
+let xwhd st m n t =
+IFDEF TRACE THEN
+   if !G.ct >= level then log1 st "Now scanning" m.e t
+ELSE () END;
+   ih st (reset m n) t
 
-let are_convertible st mu u mw w = 
-   L.box level; log2 "Now converting" mu.e u mw.e w;
-   let r = ac {st with S.delta = st.S.expand; S.rt = false} mu u mw w in   
-   L.unbox level; r
+let are_convertible st m1 n1 t1 m2 n2 t2 = 
+IFDEF TRACE THEN
+   if !G.ct >= level then log2 st "Now converting" m1.e t1 m2.e t2
+ELSE () END;   
+   let r = ac st (reset m1 n1) t1 (reset m2 n2) t2 in
+   r
 (*    let err _ = in 
-      if W.eq mu mw then are_alpha_convertible err f u w else err () *)
+      if S.eq mu mw then are_alpha_convertible err f u w else err () *)
 
 (* error reporting **********************************************************)
 
-let pp_term m frm t = BO.specs.L.pp_term m.e frm t
+let pp_term st m och t = BO.specs.L.pp_term st m.e och t
 
-let pp_lenv frm m = BO.specs.L.pp_lenv frm m.e
+let pp_lenv st och m = BO.specs.L.pp_lenv st och m.e
 
 let specs = {
    L.pp_term = pp_term; L.pp_lenv = pp_lenv