]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/tactics/fwdSimplTactic.ml
ocaml 3.09 transition
[helm.git] / helm / ocaml / tactics / fwdSimplTactic.ml
index 5160ea929c678e967d7abd872828546c0f2eb5f7..a5c7878c7341336a822e39587adcaa5947b9d018 100644 (file)
  * http://cs.unibo.it/helm/.
  *)
 
-module MI = CicMkImplicit
-module TC = CicTypeChecker 
-module PET = ProofEngineTypes 
+
 module PEH = ProofEngineHelpers 
 module U  = CicUniv
+module TC = CicTypeChecker 
+module PET = ProofEngineTypes 
 module S = CicSubstitution
 module PT = PrimitiveTactics
 module T = Tacticals
+module FNG = FreshNamesGenerator
+module MI = CicMkImplicit
+module PESR = ProofEngineStructuralRules
 
-let fail_msg1 = "no applicable simplification"
+let fail_msg0 = "unexported clearbody: invalid argument"
+let fail_msg2 = "fwd: no applicable simplification"
 
-let error msg = raise (PET.Fail msg)
+let error msg = raise (PET.Fail (lazy msg))
 
-(* lapply *******************************************************************)
+(* unexported tactics *******************************************************)
 
-let strip_dependent_prods metasenv context t =
-   let irl = MI.identity_relocation_list_for_metavariable context in
-   let rec aux metasenv p xcontext = function
-      | Cic.Prod (name, t1, t2) when not (TC.does_not_occur xcontext 0 1 t2) ->  
-         let index = MI.new_meta metasenv [] in
-         let metasenv = [index, context, t1] @ metasenv in
-         let e, s = Some (name, Cic.Decl t1), Cic.Meta (index, irl) in    
-        aux metasenv (succ p) (e :: xcontext) (S.subst s t2)
-      | Cic.Prod (name, t1, t2) -> metasenv, p, Some t1, (S.subst (Cic.Rel 1) t2)
-      | t                       -> metasenv, p, None, t 
-   in
-   aux metasenv 0 context t
+let id_tac = 
+   let id_tac (proof,goal) = 
+      try
+         let _, metasenv, _, _ = proof in
+         let _, _, _ = CicUtil.lookup_meta goal metasenv in
+         (proof,[goal])
+      with CicUtil.Meta_not_found _ -> (proof, [])
+   in 
+   PET.mk_tactic id_tac
 
-let skip_metas p =
-   let rec aux conts p =
-      if p <= 0 then conts else aux (T.id_tac :: conts) (pred p) 
+let clearbody ~index =
+   let rec find_name index = function
+      | Some (Cic.Name name, _) :: _ when index = 1 -> name
+      | _ :: tail when index > 1 -> find_name (pred index) tail
+      | _ -> error fail_msg0
    in
-   aux [] p
-   
-let get_conclusion context t =
-   let rec aux p context = function 
-      | Cic.Prod (name, t1, t2)  -> 
-         aux (succ p) (Some (name, Cic.Decl t1) :: context) t2
-      | Cic.LetIn (name, u1, t2) -> 
-         aux (succ p) (Some (name, Cic.Def (u1, None)) :: context) t2
-     | Cic.Cast (t2, t1)         -> aux p context t2
-     | t                         -> p, context, t
-   in aux 0 context t
-
-let get_conclusion_dependences context t =
-   let p, context, conclusion = get_conclusion context t in
-   let rec aux l q = 
-      if q <= 0 then l else 
-      let b = TC.does_not_occur context (pred q) q conclusion in
-      aux (b :: l) (pred q)
+   let clearbody status =
+      let (proof, goal) = status in
+      let _, metasenv, _, _ = proof in
+      let _, context, _ = CicUtil.lookup_meta goal metasenv in
+      PET.apply_tactic (PESR.clearbody ~hyp:(find_name index context)) status
    in
-   aux [] p
+   PET.mk_tactic clearbody
+
+(* lapply *******************************************************************)
 
-let solve_independents ?with_what deps =
-  let rec aux p conts = function
-     | []          -> p, conts
-     | true :: tl  -> 
-        let cont = PT.apply_tac ~term:(Cic.Rel (succ p)) in
-       aux (succ p) (cont :: conts) tl
-     | false :: tl -> aux (succ p) conts tl
+let strip_prods metasenv context ?how_many to_what term =
+   let irl = MI.identity_relocation_list_for_metavariable context in
+   let mk_meta metasenv its_type =  
+      let index = MI.new_meta metasenv [] in
+      let metasenv = [index, context, its_type] @ metasenv in
+      metasenv, Cic.Meta (index, irl), index
+   in
+   let update_counters = function
+      | None, []                 -> None, false, id_tac, []
+      | None, to_what :: tail    -> None, true, PT.apply_tac ~term:to_what, tail
+      | Some hm, []              -> Some (pred hm), false, id_tac, []
+      | Some hm, to_what :: tail -> Some (pred hm), true, PT.apply_tac ~term:to_what, tail
    in 
-   let p, conts = aux 0 [] deps in
-   match with_what with
-      | None   -> conts
-      | Some t -> PT.apply_tac ~term:(S.lift p t) :: conts
-        
-let lapply_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) 
-               (* ?(substs = []) *) ?to_what what =
-   let cut_tac term = PT.cut_tac ~mk_fresh_name_callback term in
-   let intros_tac () = PT.intros_tac ~mk_fresh_name_callback () in
-   let solve_conclusion_tac ?with_what p deps = 
-      T.then_ ~start:(intros_tac ())
-             ~continuation:(  
-         T.thens ~start:(PT.apply_tac what)
-                ~continuations:( 
-           skip_metas p @ solve_independents ?with_what deps 
-        )
-      )
+   let rec aux metasenv metas conts tw = function
+      | Some hm, _ when hm <= 0               -> metasenv, metas, conts 
+      | xhm, Cic.Prod (Cic.Name _, t1, t2)    ->
+         let metasenv, meta, index = mk_meta metasenv t1 in    
+        aux metasenv (meta :: metas) (conts @ [id_tac, index]) tw (xhm, (S.subst meta t2))      
+      | xhm, Cic.Prod (Cic.Anonymous, t1, t2) ->
+         let xhm, pos, tac, tw = update_counters (xhm, tw) in 
+         let metasenv, meta, index = mk_meta metasenv t1 in    
+        let conts = if pos then (tac, index) :: conts else conts @ [tac, index] in 
+        aux metasenv (meta :: metas) conts tw (xhm, (S.subst meta t2))
+      | _, t                                  -> metasenv, metas, conts 
    in
+   aux metasenv [] [] to_what (how_many, term)
+   
+let lapply_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) 
+               (* ?(substs = []) *) ?how_many ?(to_what = []) what =
+   let letin_tac term = PT.letin_tac ~mk_fresh_name_callback term in   
    let lapply_tac (proof, goal) =
       let xuri, metasenv, u, t = proof in
       let _, context, _ = CicUtil.lookup_meta goal metasenv in
       let lemma, _ = TC.type_of_aux' metasenv context what U.empty_ugraph in
-      match strip_dependent_prods metasenv context lemma with
-         | metasenv, p, Some premise, conclusion ->
-           let deps = get_conclusion_dependences context conclusion in
-           let inner_tac = match to_what with
-              | None      -> 
-                 T.thens ~start:(cut_tac premise)
-                         ~continuations:[
-                     solve_conclusion_tac ~with_what:(Cic.Rel 1) p deps;
-                    T.id_tac
-                 ]
-              | Some with_what -> 
-                  solve_conclusion_tac ~with_what p deps
-           in      
-           let outer_tac =
-              T.thens ~start:(cut_tac conclusion)
-                      ~continuations:[T.id_tac; inner_tac]
-           in
-           let status = (xuri, metasenv, u, t), goal in
-           PET.apply_tactic outer_tac status
-         | metasenv, p, None, conclusion         ->
-           failwith "lapply_tac: not implemented"
+      let lemma = FNG.clean_dummy_dependent_types lemma in
+      let metasenv, metas, conts = strip_prods metasenv context ?how_many to_what lemma in      
+      let conclusion =  
+         match metas with [] -> what | _ -> Cic.Appl (what :: List.rev metas)
+      in
+      let tac = T.then_ ~start:(letin_tac conclusion) 
+        ~continuation:(clearbody ~index:1)
+      in
+      let proof = (xuri, metasenv, u, t) in
+      let aux (proof, goals) (tac, goal) = 
+         let proof, new_goals = PET.apply_tactic tac (proof, goal) in
+        proof, goals @ new_goals
+      in
+      List.fold_left aux (proof, []) ((tac, goal) :: conts)
    in
    PET.mk_tactic lapply_tac
-   
+        
 (* fwd **********************************************************************)
 
-let fwd_simpl_tac ~what ~dbd =
+let fwd_simpl_tac
+     ?(mk_fresh_name_callback = FNG.mk_fresh_name ~subst:[])
+     ~dbd hyp =
+   let lapply_tac to_what lemma = 
+      lapply_tac ~mk_fresh_name_callback ~how_many:1 ~to_what:[to_what] lemma
+   in
    let fwd_simpl_tac status =
       let (proof, goal) = status in
       let _, metasenv, _, _ = proof in
       let _, context, ty = CicUtil.lookup_meta goal metasenv in
-      let major, _ = TC.type_of_aux' metasenv context what U.empty_ugraph in 
+      let index, major = PEH.lookup_type metasenv context hyp in 
       match MetadataQuery.fwd_simpl ~dbd major with
-         | []       -> error fail_msg1
-         | uri :: _ -> prerr_endline (UriManager.string_of_uri uri); (proof, [])  
+         | []       -> error fail_msg2
+         | uri :: _ -> 
+           Printf.eprintf "fwd: %s\n" (UriManager.string_of_uri uri); flush stderr;
+           let start = lapply_tac (Cic.Rel index) (Cic.Const (uri, [])) in  
+            let tac = T.then_ ~start ~continuation:(PESR.clear hyp) in
+            PET.apply_tactic tac status
    in
    PET.mk_tactic fwd_simpl_tac