]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/tactics/equalityTactics.ml
ocaml 3.09 transition
[helm.git] / helm / ocaml / tactics / equalityTactics.ml
index fe566ab74f779a5e8ed27ab8a5f4ed2c020ea2ee..7ed946170b5e36e018304c7062a033f96a4cb20c 100644 (file)
  * For details, see the HELM World-Wide-Web page,
  * http://cs.unibo.it/helm/.
  *)
-
-let rewrite_tac ~term:equality =
- let rewrite_tac ~term:equality (proof,goal) =
+let rec rewrite_tac ~direction ~pattern equality =
+ let _rewrite_tac ~direction ~pattern:(wanted,hyps_pat,concl_pat) equality status
+ =
   let module C = Cic in
   let module U = UriManager in
-   let curi,metasenv,pbo,pty = proof in
-   let metano,context,gty = CicUtil.lookup_meta goal metasenv in
-    let eq_ind_r,ty,t1,t2 =
-     match CicTypeChecker.type_of_aux' metasenv context equality with
-        C.Appl [C.MutInd (uri,0,[]) ; ty ; t1 ; t2]
-         when U.eq uri HelmLibraryObjects.Logic.eq_URI ->
-          let eq_ind_r =
-           C.Const
-            (HelmLibraryObjects.Logic.eq_ind_r_URI,[])
-          in
-           eq_ind_r,ty,t1,t2
-      | _ ->
-        raise
-         (ProofEngineTypes.Fail
-           "Rewrite: the argument is not a proof of an equality")
-    in
-     let pred =
-      let gty' = CicSubstitution.lift 1 gty in
-      let t1' = CicSubstitution.lift 1 t1 in
-      let gty'' =
-       ProofEngineReduction.replace_lifting
-        ~equality:ProofEngineReduction.alpha_equivalence
-        ~what:[t1'] ~with_what:[C.Rel 1] ~where:gty'
-      in
-       C.Lambda
-        (FreshNamesGenerator.mk_fresh_name ~subst:[] metasenv context C.Anonymous ~typ:ty,
-          ty, gty'')
-     in
-     let fresh_meta = ProofEngineHelpers.new_meta_of_proof proof in
-     let irl =CicMkImplicit.identity_relocation_list_for_metavariable context in
-     let metasenv' = (fresh_meta,context,C.Appl [pred ; t2])::metasenv in
-      let (proof',goals) =
-       ProofEngineTypes.apply_tactic 
-        (PrimitiveTactics.exact_tac
-         ~term:(C.Appl
-          [eq_ind_r ; ty ; t2 ; pred ; C.Meta (fresh_meta,irl) ; t1 ;equality]))        ((curi,metasenv',pbo,pty),goal)
+  let module PET = ProofEngineTypes in
+  let module PER = ProofEngineReduction in
+  let module PEH = ProofEngineHelpers in
+  let module PT = PrimitiveTactics in
+  assert (wanted = None);   (* this should be checked syntactically *)
+  let proof,goal = status in
+  let curi, metasenv, pbo, pty = proof in
+  let (metano,context,gty) as conjecture = CicUtil.lookup_meta goal metasenv in
+  match hyps_pat with
+     he::(_::_ as tl) ->
+       PET.apply_tactic
+        (Tacticals.then_
+          (rewrite_tac ~direction
+           ~pattern:(None,[he],Cic.Implicit None) equality)
+          (rewrite_tac ~direction ~pattern:(None,tl,concl_pat) equality)
+        ) status
+   | [_] as hyps_pat when concl_pat <> Cic.Implicit None ->
+       PET.apply_tactic
+        (Tacticals.then_
+          (rewrite_tac ~direction
+           ~pattern:(None,hyps_pat,Cic.Implicit None) equality)
+          (rewrite_tac ~direction ~pattern:(None,[],concl_pat) equality)
+        ) status
+   | _ ->
+  let arg,dir2,tac,concl_pat,gty =
+   match hyps_pat with
+      [] -> None,true,(fun ~term _ -> PT.exact_tac term),concl_pat,gty
+    | [name,pat] ->
+      let rec find_hyp n =
+       function
+          [] -> assert false
+        | Some (Cic.Name s,Cic.Decl ty)::_ when name = s ->
+           Cic.Rel n, CicSubstitution.lift n ty
+        | Some (Cic.Name s,Cic.Def _)::_ -> assert false (*CSC: not implemented yet! But does this make any sense?*)
+        | _::tl -> find_hyp (n+1) tl
       in
-       assert (List.length goals = 0) ;
-       (proof',[fresh_meta])
+       let arg,gty = find_hyp 1 context in
+       let last_hyp_name_of_status (proof,goal) =
+        let curi, metasenv, pbo, pty = proof in
+        let metano,context,gty = CicUtil.lookup_meta goal metasenv in
+         match context with
+            (Some (Cic.Name s,_))::_ -> s
+          | _ -> assert false
+       in
+        let dummy = "dummy" in
+        Some arg,false,
+         (fun ~term typ ->
+           Tacticals.seq
+            ~tactics:
+              [ProofEngineStructuralRules.rename name dummy;
+               PT.letin_tac
+                ~mk_fresh_name_callback:(fun _ _ _ ~typ -> Cic.Name name) term;
+               ProofEngineStructuralRules.clearbody name;
+               ReductionTactics.change_tac
+                ~pattern:
+                  (None,[name,Cic.Implicit (Some `Hole)],Cic.Implicit None)
+                (ProofEngineTypes.const_lazy_term typ);
+               ProofEngineStructuralRules.clear dummy
+              ]),
+         pat,gty
+    | _::_ -> assert false
+  in
+  let if_right_to_left do_not_change a b = 
+    match direction with
+    | `RightToLeft -> if do_not_change then a else b
+    | `LeftToRight -> if do_not_change then b else a
+  in
+  let ty_eq,ugraph = 
+    CicTypeChecker.type_of_aux' metasenv context equality 
+      CicUniv.empty_ugraph in 
+  let (ty_eq,metasenv',arguments,fresh_meta) =
+   ProofEngineHelpers.saturate_term
+    (ProofEngineHelpers.new_meta_of_proof proof) metasenv context ty_eq 0 in
+  let equality =
+   if List.length arguments = 0 then
+    equality
+   else
+    C.Appl (equality :: arguments) in
+  (* t1x is t2 if we are rewriting in an hypothesis *)
+  let eq_ind, ty, t1, t2, t1x =
+    match ty_eq with
+    | C.Appl [C.MutInd (uri, 0, []); ty; t1; t2]
+      when LibraryObjects.is_eq_URI uri ->
+        let ind_uri =
+         if_right_to_left dir2
+          LibraryObjects.eq_ind_URI LibraryObjects.eq_ind_r_URI
+        in
+        let eq_ind = C.Const (ind_uri uri,[]) in
+         if dir2 then
+          if_right_to_left true (eq_ind,ty,t2,t1,t2) (eq_ind,ty,t1,t2,t1)
+         else
+          if_right_to_left true (eq_ind,ty,t1,t2,t2) (eq_ind,ty,t2,t1,t1)
+    | _ -> raise (PET.Fail (lazy "Rewrite: argument is not a proof of an equality")) in
+  (* now we always do as if direction was `LeftToRight *)
+  let fresh_name = 
+    FreshNamesGenerator.mk_fresh_name 
+    ~subst:[] metasenv' context C.Anonymous ~typ:ty in
+  let lifted_t1 = CicSubstitution.lift 1 t1x in
+  let lifted_gty = CicSubstitution.lift 1 gty in
+  let lifted_conjecture =
+    metano,(Some (fresh_name,Cic.Decl ty))::context,lifted_gty in
+  let lifted_pattern =
+    Some (fun _ m u -> lifted_t1, m, u),[],CicSubstitution.lift 1 concl_pat
+  in
+  let subst,metasenv',ugraph,_,selected_terms_with_context =
+   ProofEngineHelpers.select
+    ~metasenv:metasenv' ~ugraph ~conjecture:lifted_conjecture
+     ~pattern:lifted_pattern in
+  let metasenv' = CicMetaSubst.apply_subst_metasenv subst metasenv' in
+  let what,with_what = 
+   (* Note: Rel 1 does not live in the context context_of_t           *)
+   (* The replace_lifting_csc 0 function will take care of lifting it *)
+   (* to context_of_t                                                 *)
+   List.fold_right
+    (fun (context_of_t,t) (l1,l2) -> t::l1, Cic.Rel 1::l2)
+    selected_terms_with_context ([],[]) in
+  let t1 = CicMetaSubst.apply_subst subst t1 in
+  let t2 = CicMetaSubst.apply_subst subst t2 in
+  let equality = CicMetaSubst.apply_subst subst equality in
+  let abstr_gty =
+   ProofEngineReduction.replace_lifting_csc 0
+    ~equality:(==) ~what ~with_what:with_what ~where:lifted_gty in
+  let abstr_gty = CicMetaSubst.apply_subst subst abstr_gty in
+  let pred = C.Lambda (fresh_name, ty, abstr_gty) in
+  (* The argument is either a meta if we are rewriting in the conclusion
+     or the hypothesis if we are rewriting in an hypothesis *)
+  let metasenv',arg,newtyp =
+   match arg with
+      None ->
+       let gty' = CicSubstitution.subst t2 abstr_gty in
+       let irl =
+        CicMkImplicit.identity_relocation_list_for_metavariable context in
+       let metasenv' = (fresh_meta,context,gty')::metasenv' in
+        metasenv', C.Meta (fresh_meta,irl), Cic.Rel (-1) (* dummy term, never used *)
+    | Some arg ->
+       let gty' = CicSubstitution.subst t1 abstr_gty in
+        metasenv,arg,gty'
+  in
+  let exact_proof = 
+    C.Appl [eq_ind ; ty ; t2 ; pred ; arg ; t1 ;equality]
+  in
+  let (proof',goals) =
+    PET.apply_tactic 
+      (tac ~term:exact_proof newtyp) ((curi,metasenv',pbo,pty),goal)
+  in
+  let goals =
+   goals@(ProofEngineHelpers.compare_metasenvs ~oldmetasenv:metasenv
+    ~newmetasenv:metasenv')
+  in
+   (proof',goals)
  in
-  ProofEngineTypes.mk_tactic (rewrite_tac ~term:equality)
-;;
-
-
-let rewrite_simpl_tac ~term =
- let rewrite_simpl_tac ~term status =
+  ProofEngineTypes.mk_tactic (_rewrite_tac ~direction ~pattern equality)
+  
+let rewrite_simpl_tac ~direction ~pattern equality =
+ let rewrite_simpl_tac ~direction ~pattern equality status =
   ProofEngineTypes.apply_tactic
   (Tacticals.then_ 
-   ~start:(rewrite_tac ~term)
+   ~start:(rewrite_tac ~direction ~pattern equality)
    ~continuation:
-    (ReductionTactics.simpl_tac ~also_in_hypotheses:false ~terms:None))
-   status
- in
-  ProofEngineTypes.mk_tactic (rewrite_simpl_tac ~term)
-;;
-
-
-let rewrite_back_tac ~term:equality =
- let rewrite_back_tac equality (proof,goal) =
-  let module C = Cic in
-  let module U = UriManager in
-   let curi,metasenv,pbo,pty = proof in
-   let metano,context,gty = CicUtil.lookup_meta goal metasenv in
-    let eq_ind_r,ty,t1,t2 =
-     match CicTypeChecker.type_of_aux' metasenv context equality with
-        C.Appl [C.MutInd (uri,0,[]) ; ty ; t1 ; t2]
-         when U.eq uri HelmLibraryObjects.Logic.eq_URI ->
-          let eq_ind_r =
-           C.Const (HelmLibraryObjects.Logic.eq_ind_URI,[])
-          in
-           eq_ind_r,ty,t2,t1
-      | _ ->
-        raise
-         (ProofEngineTypes.Fail
-           "Rewrite: the argument is not a proof of an equality")
-    in
-     let pred =
-      let gty' = CicSubstitution.lift 1 gty in
-      let t1' = CicSubstitution.lift 1 t1 in
-      let gty'' =
-       ProofEngineReduction.replace_lifting
-        ~equality:ProofEngineReduction.alpha_equivalence
-        ~what:[t1'] ~with_what:[C.Rel 1] ~where:gty'
-      in
-       C.Lambda
-        (FreshNamesGenerator.mk_fresh_name ~subst:[] metasenv context C.Anonymous ~typ:ty,
-          ty, gty'')
-     in
-     let fresh_meta = ProofEngineHelpers.new_meta_of_proof proof in
-     let irl =
-      CicMkImplicit.identity_relocation_list_for_metavariable context in
-     let metasenv' = (fresh_meta,context,C.Appl [pred ; t2])::metasenv in
-      let (proof',goals) =
-       ProofEngineTypes.apply_tactic
-        (PrimitiveTactics.exact_tac
-         ~term:(C.Appl
-          [eq_ind_r ; ty ; t2 ; pred ; C.Meta (fresh_meta,irl) ; t1 ;equality]))
-         ((curi,metasenv',pbo,pty),goal)
-      in
-       assert (List.length goals = 0) ;
-       (proof',[fresh_meta])
- in
-  ProofEngineTypes.mk_tactic (rewrite_back_tac equality)
-;;
-
-
-let rewrite_back_simpl_tac ~term =
- let rewrite_back_simpl_tac ~term status =
-  ProofEngineTypes.apply_tactic
-   (Tacticals.then_ 
-    ~start:(rewrite_back_tac ~term)
-    ~continuation:
-     (ReductionTactics.simpl_tac ~also_in_hypotheses:false ~terms:None))
+     (ReductionTactics.simpl_tac
+       ~pattern:(ProofEngineTypes.conclusion_pattern None)))
    status
  in
-  ProofEngineTypes.mk_tactic (rewrite_back_simpl_tac ~term)
+   ProofEngineTypes.mk_tactic (rewrite_simpl_tac ~direction ~pattern equality)
 ;;
 
-let replace_tac ~what ~with_what =
- let replace_tac ~what ~with_what status =
+let replace_tac ~pattern ~with_what =
+ let replace_tac ~pattern:(wanted,hyps_pat,concl_pat) ~with_what status =
   let (proof, goal) = status in
   let module C = Cic in
   let module U = UriManager in
   let module P = PrimitiveTactics in
   let module T = Tacticals in
-   let _,metasenv,_,_ = proof in
-    let _,context,_ = CicUtil.lookup_meta goal metasenv in
-     let wty = CicTypeChecker.type_of_aux' metasenv context what in
-      try
-      if (wty = (CicTypeChecker.type_of_aux' metasenv context with_what))
-       then
-        ProofEngineTypes.apply_tactic
+  let uri,metasenv,pbo,pty = proof in
+  let (_,context,ty) as conjecture = CicUtil.lookup_meta goal metasenv in
+  assert (hyps_pat = []); (*CSC: not implemented yet *)
+  let context_len = List.length context in
+  let subst,metasenv,u,_,selected_terms_with_context =
+   ProofEngineHelpers.select ~metasenv ~ugraph:CicUniv.empty_ugraph
+    ~conjecture ~pattern in
+  let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
+  let with_what, metasenv, u = with_what context metasenv u in
+  let with_what = CicMetaSubst.apply_subst subst with_what in
+  let pbo = CicMetaSubst.apply_subst subst pbo in
+  let pty = CicMetaSubst.apply_subst subst pty in
+  let status = (uri,metasenv,pbo,pty),goal in
+  let ty_of_with_what,u =
+   CicTypeChecker.type_of_aux'
+    metasenv context with_what CicUniv.empty_ugraph in
+  let whats =
+   match selected_terms_with_context with
+      [] -> raise (ProofEngineTypes.Fail (lazy "Replace: no term selected"))
+    | l ->
+      List.map
+       (fun (context_of_t,t) ->
+         let t_in_context =
+          try
+           let context_of_t_len = List.length context_of_t in
+           if context_of_t_len = context_len then t
+           else
+            (let t_in_context,subst,metasenv' =
+              CicMetaSubst.delift_rels [] metasenv
+               (context_of_t_len - context_len) t
+             in
+              assert (subst = []);
+              assert (metasenv = metasenv');
+              t_in_context)
+          with
+           CicMetaSubst.DeliftingARelWouldCaptureAFreeVariable ->
+            (*CSC: we could implement something stronger by completely changing
+              the semantics of the tactic *)
+            raise (ProofEngineTypes.Fail
+             (lazy "Replace: one of the selected terms is not closed")) in
+         let ty_of_t_in_context,u = (* TASSI: FIXME *)
+          CicTypeChecker.type_of_aux' metasenv context t_in_context
+           CicUniv.empty_ugraph in
+         let b,u = CicReduction.are_convertible ~metasenv context
+          ty_of_with_what ty_of_t_in_context u in
+         if b then
+          let concl_pat_for_t = ProofEngineHelpers.pattern_of ~term:ty [t] in
+          let pattern_for_t = None,[],concl_pat_for_t in
+           t_in_context,pattern_for_t
+         else
+          raise
+           (ProofEngineTypes.Fail
+             (lazy "Replace: one of the selected terms and the term to be replaced with have not convertible types"))
+       ) l in
+  let rec aux n whats status =
+   match whats with
+      [] -> ProofEngineTypes.apply_tactic T.id_tac status
+    | (what,pattern)::tl ->
+       let what = CicSubstitution.lift n what in
+       let with_what = CicSubstitution.lift n with_what in
+       let ty_of_with_what = CicSubstitution.lift n ty_of_with_what in
+       ProofEngineTypes.apply_tactic
          (T.thens
-          ~start:(
-            P.cut_tac 
-             (C.Appl [
-               (C.MutInd (HelmLibraryObjects.Logic.eq_URI, 0, [])) ;
-                 wty ; 
+            ~start:(
+              P.cut_tac 
+               (C.Appl [
+                 (C.MutInd (LibraryObjects.eq_URI (), 0, [])) ;
+                 ty_of_with_what ; 
                  what ; 
                  with_what]))
-          ~continuations:[
-            T.then_
-               ~start:(rewrite_simpl_tac ~term:(C.Rel 1))
-               ~continuation:(
-                 ProofEngineStructuralRules.clear
-                  ~hyp:(List.hd context)) ;
-            T.id_tac])
-          status
-       else raise (ProofEngineTypes.Fail "Replace: terms not replaceable")
-       with (Failure "hd") -> 
-            raise (ProofEngineTypes.Fail "Replace: empty context")
+            ~continuations:[            
+              T.then_
+                ~start:(
+                  rewrite_tac ~direction:`LeftToRight ~pattern (C.Rel 1))
+                 ~continuation:(
+                   T.then_
+                    ~start:(
+                      ProofEngineTypes.mk_tactic
+                       (function ((proof,goal) as status) ->
+                         let _,metasenv,_,_ = proof in
+                         let _,context,_ = CicUtil.lookup_meta goal metasenv in
+                         let hyp =
+                          try
+                           match List.hd context with
+                              Some (Cic.Name name,_) -> name
+                            | _ -> assert false
+                          with (Failure "hd") -> assert false
+                         in
+                          ProofEngineTypes.apply_tactic
+                           (ProofEngineStructuralRules.clear ~hyp) status))
+                    ~continuation:(aux_tac (n + 1) tl));
+              T.id_tac])
+         status
+  and aux_tac n tl = ProofEngineTypes.mk_tactic (aux n tl) in
+   aux 0 whats status
  in
-  ProofEngineTypes.mk_tactic (replace_tac ~what ~with_what)
+   ProofEngineTypes.mk_tactic (replace_tac ~pattern ~with_what)
 ;;
 
 
@@ -200,13 +317,13 @@ let symmetry_tac =
     let metano,context,ty = CicUtil.lookup_meta goal metasenv in
      match (R.whd context ty) with
         (C.Appl [(C.MutInd (uri, 0, [])); _; _; _])
-        when (U.eq uri HelmLibraryObjects.Logic.eq_URI) ->
+        when LibraryObjects.is_eq_URI uri ->
          ProofEngineTypes.apply_tactic 
            (PrimitiveTactics.apply_tac 
-           ~term: (C.Const (HelmLibraryObjects.Logic.sym_eq_URI, []))) 
+           ~term: (C.Const (LibraryObjects.sym_eq_URI uri, []))) 
           (proof,goal)
 
-      | _ -> raise (ProofEngineTypes.Fail "Symmetry failed")
+      | _ -> raise (ProofEngineTypes.Fail (lazy "Symmetry failed"))
  in
   ProofEngineTypes.mk_tactic symmetry_tac
 ;;
@@ -222,16 +339,16 @@ let transitivity_tac ~term =
     let metano,context,ty = CicUtil.lookup_meta goal metasenv in
      match (R.whd context ty) with
         (C.Appl [(C.MutInd (uri, 0, [])); _; _; _]) 
-       when (uri = HelmLibraryObjects.Logic.eq_URI) ->
+       when LibraryObjects.is_eq_URI uri ->
          ProofEngineTypes.apply_tactic 
         (T.thens
           ~start:(PrimitiveTactics.apply_tac
-            ~term: (C.Const (HelmLibraryObjects.Logic.trans_eq_URI, [])))
+            ~term: (C.Const (LibraryObjects.trans_eq_URI uri, [])))
           ~continuations:
             [PrimitiveTactics.exact_tac ~term ; T.id_tac ; T.id_tac])
           status
 
-      | _ -> raise (ProofEngineTypes.Fail "Transitivity failed")
+      | _ -> raise (ProofEngineTypes.Fail (lazy "Transitivity failed"))
  in
   ProofEngineTypes.mk_tactic (transitivity_tac ~term)
 ;;