]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/ng_kernel/nCicReduction.ml
partial implementation of reduction up to a given height
[helm.git] / helm / software / components / ng_kernel / nCicReduction.ml
index 46a81d6205417451d5ed611a35f4cf75f2d52e86..9cae8d4091aa387ef9196cb441ac4b4bb485e576 100644 (file)
@@ -396,12 +396,14 @@ module Reduction(RS : Strategy) =
         in
          aux (k, e, he, tl' @ s)
      | (_, _, NCic.Const
-            (NReference.Ref (_,_,NReference.Def) as refer), s) as config ->
-         let _,_,body,_,_,height = NCicEnvironment.get_checked_def refer in
-         if delta > height then config else aux (0, [], body, s) 
+            (NReference.Ref (height,_,NReference.Def) as refer), s) as config ->
+         if delta > height then config else 
+           let _,_,body,_,_,_ = NCicEnvironment.get_checked_def refer in
+           aux (0, [], body, s) 
+     | (_, _, NCic.Const (NReference.Ref (_,_,
+            (NReference.Decl|NReference.Ind _|NReference.Con _|NReference.CoFix _))), _) as config -> config
      | (_, _, NCic.Const (NReference.Ref 
-               (_,_,NReference.Fix (fixno,recindex)) as refer),s) as config ->
-        let fixes,_, height = NCicEnvironment.get_checked_fixes_or_cofixes refer in
+           (height,_,NReference.Fix (fixno,recindex)) as refer),s) as config ->
         if delta > height then config else
         (match
           try Some (RS.from_stack (List.nth s recindex))
@@ -409,6 +411,7 @@ module Reduction(RS : Strategy) =
         with 
         | None -> config
         | Some recparam ->
+           let fixes,_,_ = NCicEnvironment.get_checked_fixes_or_cofixes refer in
            match reduce ~delta:0 ~subst context recparam with
            | (_,_,NCic.Const (NReference.Ref (_,_,NReference.Con _)), _) as c ->
                let new_s =
@@ -417,7 +420,6 @@ module Reduction(RS : Strategy) =
                let _,_,_,_,body = List.nth fixes fixno in
                aux (0, [], body, new_s)
            | _ -> config)
-     | (_, _, NCic.Const _, _) as config -> config
      | (k, e, NCic.Match (_,_,term,pl),s) as config ->
         let decofix = function
           | (_,_,NCic.Const(NReference.Ref(_,_,NReference.CoFix c)as refer),s)->
@@ -486,7 +488,7 @@ let (===) x y = Pervasives.compare x y = 0 ;;
 module C = NCic
 
 (* t1, t2 must be well-typed *)
-let are_convertible whd ?(subst=[]) ?(metasenv=[])  =
+let are_convertible whd ?(subst=[])  =
  let rec aux test_equality_only context t1 t2 =
    let rec aux2 test_equality_only t1 t2 =
      if t1 === t2 then
@@ -502,7 +504,7 @@ let are_convertible whd ?(subst=[]) ?(metasenv=[])  =
            aux test_equality_only ((name1, C.Decl s1)::context) t1 t2
        | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2)) ->
           aux true context s1 s2 && 
-          aux true ((name1, C.Decl s1)::context) t1 t2
+          aux test_equality_only ((name1, C.Decl s1)::context) t1 t2
        | (C.LetIn (name1,ty1,s1,t1), C.LetIn(_,ty2,s2,t2)) ->
           aux test_equality_only context ty1 ty2 &&
           aux test_equality_only context s1 s2 &&
@@ -518,7 +520,7 @@ let are_convertible whd ?(subst=[]) ?(metasenv=[])  =
               (NCicSubstitution.lift s1 t1) 
               (NCicSubstitution.lift s2 t2))  
             l1 l2
-          with Invalid_argument _ -> false)
+          with Invalid_argument _ -> assert false)
 
        | C.Meta (n1,l1), _ ->
           (try 
@@ -551,26 +553,41 @@ let are_convertible whd ?(subst=[]) ?(metasenv=[])  =
    if aux2 test_equality_only t1 t2 then 
      true
    else
-     let rec convert_machines = function
-      | [] -> true
-      | ((k1,env1,h1,s1),(k2,env2,h2,s2))::tl ->
-          aux2 test_equality_only
-           (R.unwind (k1,env1,h1,[])) (R.unwind (k2,env2,h2,[])) &&
-          let problems =
-           let red_stack =
-             List.map
-               (fun si-> R.reduce ~delta:0 ~subst context(RS.from_stack si))
-           in
-           try Some (List.combine (red_stack s1) (red_stack s2) @ tl)
-           with Invalid_argument _ -> None
-          in
-          match problems with
-           | None -> false
-           | Some problems -> convert_machines problems
+     let height_of = function
+      | NCic.Const (NReference.Ref (h,_,_)) -> h
+      | NCic.Appl (NCic.Const (NReference.Ref (h,_,_))::_) -> h
+      | _ -> 0
      in
-      convert_machines 
-       [R.reduce ~delta:0 ~subst context (0,[],t1,[]),
-        R.reduce ~delta:0 ~subst context (0,[],t2,[])]
+     let min_delta (k1,env1,t1,s1) (k2,env2,t2,s2) = 
+       let h1 = height_of t1 and h2 = height_of t2 in
+       if h1 > h2 then
+         R.reduce ~delta:(h2+1) ~subst context (k1,env1,t1,s1), 
+         (k2,env2,t2,s2), h2+1
+       else if h1 < h2 then
+         (k1,env1,t1,s1), 
+         R.reduce ~delta:(h1+1) ~subst context (k2,env2,t2,s2),
+         h1+1
+       else
+         R.reduce ~delta:(max 0 (h1-1)) ~subst context (k1,env1,t1,s1),
+         R.reduce ~delta:(max 0 (h1-1)) ~subst context (k2,env2,t2,s2),
+         (max 0 (h1-1))
+     in
+     let rec convert_machines ((k1,e1,t1,s1 as m1),(k2,e2,t2,s2 as m2),delta) todo =
+       (aux2 test_equality_only
+         (R.unwind (k1,e1,t1,[])) (R.unwind (k2,e2,t2,[])) &&
+       try
+         match List.combine s1 s2 @ todo with
+         | [] -> true
+         | (t1,t2) :: todo -> 
+             let t1 = RS.from_stack t1 and t2 = RS.from_stack t2 in
+             convert_machines (min_delta t1 t2) todo
+       with Invalid_argument _ -> false) || 
+       (delta > 0 &&
+         let delta = delta - 1 in 
+         let red = R.reduce ~delta ~subst context in
+         convert_machines (red m1,red m2,delta) todo)
+     in
+     convert_machines (min_delta (0,[],t1,[]) (0,[],t2,[])) []
  in
   aux false 
 ;;