X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fsoftware%2Fcomponents%2Ftactics%2FprimitiveTactics.ml;h=5c7af528f04df92dd4f3df1e8069a72969d5fbf6;hb=025d672d177e34a01c59f188fcf7a13e93bb89c8;hp=16cff94d7796493f52a05658332a8261f564be4f;hpb=041ad23b567b9844ec187ad436595868441802f4;p=helm.git diff --git a/helm/software/components/tactics/primitiveTactics.ml b/helm/software/components/tactics/primitiveTactics.ml index 16cff94d7..5c7af528f 100644 --- a/helm/software/components/tactics/primitiveTactics.ml +++ b/helm/software/components/tactics/primitiveTactics.ml @@ -30,6 +30,7 @@ open ProofEngineTypes exception TheTypeOfTheCurrentGoalIsAMetaICannotChooseTheRightElimiantionPrinciple exception NotAnInductiveTypeToEliminate exception WrongUriToVariable of string +exception NotAnEliminator (* lambda_abstract newmeta ty *) (* returns a triple [bo],[context],[ty'] where *) @@ -40,7 +41,7 @@ exception WrongUriToVariable of string (* howmany = -1 means Intros, howmany > 0 means Intros n *) let lambda_abstract ?(howmany=(-1)) metasenv context newmeta ty mk_fresh_name = let module C = Cic in - let rec collect_context context howmany ty = + let rec collect_context context howmany do_whd ty = match howmany with | 0 -> let irl = @@ -49,16 +50,17 @@ let lambda_abstract ?(howmany=(-1)) metasenv context newmeta ty mk_fresh_name = context, ty, (C.Meta (newmeta,irl)) | _ -> match ty with - C.Cast (te,_) -> collect_context context howmany te + C.Cast (te,_) -> collect_context context howmany do_whd te | C.Prod (n,s,t) -> let n' = mk_fresh_name metasenv context n ~typ:s in let (context',ty,bo) = - collect_context ((Some (n',(C.Decl s)))::context) (howmany - 1) t + let ctx = (Some (n',(C.Decl s)))::context in + collect_context ctx (howmany - 1) do_whd t in (context',ty,C.Lambda(n',s,bo)) | C.LetIn (n,s,t) -> let (context',ty,bo) = - collect_context ((Some (n,(C.Def (s,None))))::context) (howmany - 1) t + collect_context ((Some (n,(C.Def (s,None))))::context) (howmany - 1) do_whd t in (context',ty,C.LetIn(n,s,bo)) | _ as t -> @@ -67,10 +69,13 @@ let lambda_abstract ?(howmany=(-1)) metasenv context newmeta ty mk_fresh_name = CicMkImplicit.identity_relocation_list_for_metavariable context in context, t, (C.Meta (newmeta,irl)) - else + else if do_whd then + let t = CicReduction.whd ~delta:true context t in + collect_context context howmany false t + else raise (Fail (lazy "intro(s): not enough products or let-ins")) in - collect_context context howmany ty + collect_context context howmany true ty let eta_expand metasenv context t arg = let module T = CicTypeChecker in @@ -229,12 +234,13 @@ let new_fresh_meta,newmetasenvfragment,exp_named_subst',exp_named_subst_diff ;; -let new_metasenv_and_unify_and_t newmeta' metasenv' context term' ty termty goal_arity = +let new_metasenv_and_unify_and_t newmeta' metasenv' subst context term' ty termty goal_arity = let (consthead,newmetasenv,arguments,_) = - ProofEngineHelpers.saturate_term newmeta' metasenv' context termty + TermUtil.saturate_term newmeta' metasenv' context termty goal_arity in let subst,newmetasenv',_ = - CicUnification.fo_unif newmetasenv context consthead ty CicUniv.empty_ugraph + CicUnification.fo_unif_subst + subst context newmetasenv consthead ty CicUniv.empty_ugraph in let t = if List.length arguments = 0 then term' else Cic.Appl (term'::arguments) @@ -251,7 +257,7 @@ let apply_with_subst ~term ~subst ~maxmeta (proof, goal) = let module T = CicTypeChecker in let module R = CicReduction in let module C = Cic in - let (_,metasenv,_,_) = proof in + let (_,metasenv,_,_, _) = proof in let metano,context,ty = CicUtil.lookup_meta goal metasenv in let newmeta = max (CicMkImplicit.new_meta metasenv subst) maxmeta in let exp_named_subst_diff,newmeta',newmetasenvfragment,term' = @@ -296,7 +302,7 @@ let apply_with_subst ~term ~subst ~maxmeta (proof, goal) = let subst,newmetasenv',t = let rec add_one_argument n = try - new_metasenv_and_unify_and_t newmeta' metasenv' context term' ty + new_metasenv_and_unify_and_t newmeta' metasenv' subst context term' ty termty n with CicUnification.UnificationFailure _ when n > 0 -> add_one_argument (n - 1) @@ -321,7 +327,7 @@ let apply_with_subst ~term ~subst ~maxmeta (proof, goal) = ProofEngineHelpers.subst_meta_and_metasenv_in_proof proof metano subst_in newmetasenv'' in - let subst = ((metano,(context,bo',Cic.Implicit None))::subst) in + let subst = ((metano,(context,bo',ty))::subst) in subst, (newproof, List.map (function (i,_,_) -> i) new_uninstantiatedmetas), max maxmeta (CicMkImplicit.new_meta newmetasenv''' subst) @@ -335,8 +341,7 @@ let apply_with_subst ~term ?(subst=[]) ?(maxmeta=0) status = (* TODO cacciare anche altre eccezioni? *) with | CicUnification.UnificationFailure msg - | CicTypeChecker.TypeCheckerFailure msg -> - raise (Fail msg) + | CicTypeChecker.TypeCheckerFailure msg -> raise (Fail msg) (* ALB *) let apply_tac_verbose ~term status = @@ -366,7 +371,7 @@ let intros_tac ?howmany ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_ = let module C = Cic in let module R = CicReduction in - let (_,metasenv,_,_) = proof in + let (_,metasenv,_,_, _) = proof in let metano,context,ty = CicUtil.lookup_meta goal metasenv in let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in let (context',ty',bo') = @@ -386,7 +391,7 @@ let cut_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst: term (proof, goal) = let module C = Cic in - let curi,metasenv,pbo,pty = proof in + let curi,metasenv,pbo,pty, attrs = proof in let metano,context,ty = CicUtil.lookup_meta goal metasenv in let newmeta1 = ProofEngineHelpers.new_meta_of_proof ~proof in let newmeta2 = newmeta1 + 1 in @@ -421,7 +426,7 @@ let letin_tac ?(mk_fresh_name_callback=FreshNamesGenerator.mk_fresh_name ~subst: term (proof, goal) = let module C = Cic in - let curi,metasenv,pbo,pty = proof in + let curi,metasenv,pbo,pty, attrs = proof in (* occur check *) let occur i t = let m = CicUtil.metas_of_term t in @@ -457,7 +462,7 @@ let letin_tac ?(mk_fresh_name_callback=FreshNamesGenerator.mk_fresh_name ~subst: let exact_tac ~term = let exact_tac ~term (proof, goal) = (* Assumption: the term bo must be closed in the current context *) - let (_,metasenv,_,_) = proof in + let (_,metasenv,_,_, _) = proof in let metano,context,ty = CicUtil.lookup_meta goal metasenv in let module T = CicTypeChecker in let module R = CicReduction in @@ -475,18 +480,34 @@ let exact_tac ~term = mk_tactic (exact_tac ~term) (* not really "primitive" tactics .... *) -let elim_tac ~term = - let elim_tac ~term (proof, goal) = - let module T = CicTypeChecker in - let module U = UriManager in - let module R = CicReduction in - let module C = Cic in - let (curi,metasenv,proofbo,proofty) = proof in - let metano,context,ty = CicUtil.lookup_meta goal metasenv in - let termty,_ = T.type_of_aux' metasenv context term CicUniv.empty_ugraph in + +module TC = CicTypeChecker +module U = UriManager +module R = CicReduction +module C = Cic +module PET = ProofEngineTypes +module PEH = ProofEngineHelpers +module PER = ProofEngineReduction +module MS = CicMetaSubst +module S = CicSubstitution +module T = Tacticals +module RT = ReductionTactics + +let elim_tac ?using ?(pattern = PET.conclusion_pattern None) term = + let elim_tac (proof, goal) = + let ugraph = CicUniv.empty_ugraph in + let curi, metasenv, proofbo, proofty, attrs = proof in + let conjecture = CicUtil.lookup_meta goal metasenv in + let metano, context, ty = conjecture in +(* let (term, metasenv, _ugraph), cpatt = match pattern with + | Some f, [], Some cpatt -> f context metasenv ugraph, cpatt + | _ -> assert false + in +*) + let termty,_ugraph = TC.type_of_aux' metasenv context term ugraph in let termty = CicReduction.whd context termty in - let (termty,metasenv',arguments,fresh_meta) = - ProofEngineHelpers.saturate_term + let (termty,metasenv',arguments,_fresh_meta) = + TermUtil.saturate_term (ProofEngineHelpers.new_meta_of_proof proof) metasenv context termty 0 in let term = if arguments = [] then term else Cic.Appl (term::arguments) in let uri,exp_named_subst,typeno,args = @@ -499,14 +520,14 @@ let elim_tac ~term = let eliminator_uri = let buri = U.buri_of_uri uri in let name = - let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in + let o,_ugraph = CicEnvironment.get_obj ugraph uri in match o with C.InductiveDefinition (tys,_,_,_) -> let (name,_,_,_) = List.nth tys typeno in name | _ -> assert false in - let ty_ty,_ = T.type_of_aux' metasenv' context ty CicUniv.empty_ugraph in + let ty_ty,_ugraph = TC.type_of_aux' metasenv' context ty ugraph in let ext = match ty_ty with C.Sort C.Prop -> "_ind" @@ -518,59 +539,258 @@ let elim_tac ~term = in U.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con") in - let eliminator_ref = C.Const (eliminator_uri,exp_named_subst) in - let ety,_ = - T.type_of_aux' metasenv' context eliminator_ref CicUniv.empty_ugraph in - let rec find_args_no = - function - C.Prod (_,_,t) -> 1 + find_args_no t - | C.Cast (s,_) -> find_args_no s - | C.LetIn (_,_,t) -> 0 + find_args_no t - | _ -> 0 - in - let args_no = find_args_no ety in - let term_to_refine = - let rec make_tl base_case = - function - 0 -> [base_case] - | n -> (C.Implicit None)::(make_tl base_case (n - 1)) - in - C.Appl (eliminator_ref :: make_tl term (args_no - 1)) + let eliminator_ref = match using with + | None -> C.Const (eliminator_uri,exp_named_subst) + | Some t -> t + in + let ety,_ugraph = + TC.type_of_aux' metasenv' context eliminator_ref ugraph in +(* FG: ADDED PART ***********************************************************) +(* FG: we can not assume eliminator is the default eliminator ***************) +(* + let add_lambdas n t = + let rec aux n t = + if n <= 0 then t + else C.Lambda (C.Anonymous, C.Implicit None, aux (pred n) t) + in + aux n (S.lift n t) + in +*) + let rec args_init n f = + if n <= 0 then [] else f n :: args_init (pred n) f + in + let splits, args_no = PEH.split_with_whd (context, ety) in + let pred_pos = match List.hd splits with + | _, C.Rel i when i > 1 && i <= args_no -> i + | _, C.Appl (C.Rel i :: _) when i > 1 && i <= args_no -> i + | _ -> raise NotAnEliminator + in +(* + let _, lambdas = PEH.split_with_whd (List.nth splits pred_pos) in + let termty_ty = + let termty_ty,_ugraph = TC.type_of_aux' metasenv' context termty ugraph in + CicReduction.whd context termty_ty + in +*) +(* + let metasenv', term, pred, upto = match cpatt, termty_ty with + | C.Implicit (Some `Hole), _ + | _, C.Sort C.Prop when lambdas = 0 -> metasenv', term, C.Implicit None, 0 + | _ -> +(* FG: we find the predicate for the eliminator as in the rewrite tactic ****) + let fresh_name = + FreshNamesGenerator.mk_fresh_name + ~subst:[] metasenv' context C.Anonymous ~typ:termty + in + let lazy_term c m u = + let distance = List.length c - List.length context in + S.lift distance term, m, u in - let refined_term,_,metasenv'',_ = + let pattern = Some lazy_term, [], Some cpatt in + let subst, metasenv', _ugraph, _conjecture, selected_terms = + ProofEngineHelpers.select + ~metasenv:metasenv' ~ugraph ~conjecture ~pattern + in + let metasenv' = MS.apply_subst_metasenv subst metasenv' in + let map (_context_of_t, t) l = t :: l in + let what = List.fold_right map selected_terms [] in + let ty = MS.apply_subst subst ty in + let term = MS.apply_subst subst term in + let termty = MS.apply_subst subst termty in + let abstr_ty = PER.replace_with_rel_1_from ~equality:(==) ~what 1 ty in + let abstr_ty = MS.apply_subst subst abstr_ty in + let pred_body = C.Lambda (fresh_name, termty, abstr_ty) in + metasenv', term, add_lambdas (pred lambdas) pred_body, lambdas + in +(* FG: END OF ADDED PART ****************************************************) +*) + let pred, upto = C.Implicit None, 0 in + + let term_to_refine = + let f n = + if n = pred_pos then pred else + if n = 1 then term else C.Implicit None + in + C.Appl (eliminator_ref :: args_init args_no f) + in + let refined_term,_refined_termty,metasenv'',_ugraph = CicRefine.type_of_aux' metasenv' context term_to_refine - CicUniv.empty_ugraph + ugraph in let new_goals = ProofEngineHelpers.compare_metasenvs ~oldmetasenv:metasenv ~newmetasenv:metasenv'' in - let proof' = curi,metasenv'',proofbo,proofty in + let proof' = curi,metasenv'',proofbo,proofty, attrs in let proof'', new_goals' = - apply_tactic (apply_tac ~term:refined_term) (proof',goal) + apply_tactic (apply_tac ~term:refined_term) (proof',goal) in (* The apply_tactic can have closed some of the new_goals *) let patched_new_goals = - let (_,metasenv''',_,_) = proof'' in + let (_,metasenv''',_,_, _) = proof'' in List.filter (function i -> List.exists (function (j,_,_) -> j=i) metasenv''' ) new_goals @ new_goals' in - proof'', patched_new_goals + let res = proof'', patched_new_goals in + if upto = 0 then res else + let pattern = PET.conclusion_pattern None in + let continuation = + RT.simpl_tac ~pattern + (* RT.head_beta_reduce_tac ~delta:false ~upto ~pattern *) + in + let dummy_status = proof,goal in + PET.apply_tactic + (T.then_ ~start:(PET.mk_tactic (fun _ -> res)) ~continuation) + dummy_status in - mk_tactic (elim_tac ~term) + mk_tactic elim_tac ;; +let cases_intros_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) term = + let cases_tac ~term (proof, goal) = + let module TC = CicTypeChecker in + let module U = UriManager in + let module R = CicReduction in + let module C = Cic in + let (curi,metasenv,proofbo,proofty, attrs) = proof in + let metano,context,ty = CicUtil.lookup_meta goal metasenv in + let termty,_ = TC.type_of_aux' metasenv context term CicUniv.empty_ugraph in + let termty = CicReduction.whd context termty in + let (termty,metasenv',arguments,fresh_meta) = + TermUtil.saturate_term + (ProofEngineHelpers.new_meta_of_proof proof) metasenv context termty 0 in + let term = if arguments = [] then term else Cic.Appl (term::arguments) in + let uri,exp_named_subst,typeno,args = + match termty with + | C.MutInd (uri,typeno,exp_named_subst) -> (uri,exp_named_subst,typeno,[]) + | C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::args) -> + (uri,exp_named_subst,typeno,args) + | _ -> raise NotAnInductiveTypeToEliminate + in + let paramsno,itty,patterns,right_args = + match CicEnvironment.get_obj CicUniv.empty_ugraph uri with + | C.InductiveDefinition (tys,_,paramsno,_),_ -> + let _,left_parameters,right_args = + List.fold_right + (fun x (n,acc1,acc2) -> + if n > 0 then (n-1,acc1,x::acc2) else (n,x::acc1,acc2)) + args (List.length args - paramsno, [],[]) + in + let _,_,itty,cl = List.nth tys typeno in + let rec aux left_parameters context t = + match left_parameters,CicReduction.whd context t with + | [],C.Prod (name,source,target) -> + let fresh_name = + mk_fresh_name_callback metasenv' context name ~typ:source + in + C.Lambda (fresh_name,C.Implicit None, + aux [] (Some (fresh_name,C.Decl source)::context) target) + | hd::tl,C.Prod (name,source,target) -> + (* left parameters instantiation *) + aux tl context (CicSubstitution.subst hd target) + | [],_ -> C.Implicit None + | _ -> assert false + in + paramsno,itty, + List.map (function (_,cty) -> aux left_parameters context cty) cl, + right_args + | _ -> assert false + in + let outtype = + let n_right_args = List.length right_args in + let n_lambdas = n_right_args + 1 in + let lifted_ty = CicSubstitution.lift n_lambdas ty in + let captured_ty = + let what = + List.map (CicSubstitution.lift n_lambdas) (right_args) + in + let with_what meta = + let rec mkargs = function + | 0 -> assert false + | 1 -> [] + | n -> + (if meta then Cic.Implicit None else Cic.Rel n)::(mkargs (n-1)) + in + mkargs n_lambdas + in + let replaced = ref false in + let replace = ProofEngineReduction.replace_lifting + ~equality:(fun _ a b -> let rc = CicUtil.alpha_equivalence a b in + if rc then replaced := true; rc) + ~context:[] + in + let captured = + replace ~what:[CicSubstitution.lift n_lambdas term] + ~with_what:[Cic.Rel 1] ~where:lifted_ty + in + if not !replaced then + (* this means the matched term is not there, + * but maybe right params are: we user rels (to right args lambdas) *) + replace ~what ~with_what:(with_what false) ~where:captured + else + (* since the matched is there, rights should be inferrable *) + replace ~what ~with_what:(with_what true) ~where:captured + in + let captured_term_ty = + let term_ty = CicSubstitution.lift n_right_args termty in + let rec mkrels = function 0 -> []|n -> (Cic.Rel n)::(mkrels (n-1)) in + let rec fstn acc l n = + if n = 0 then acc else fstn (acc@[List.hd l]) (List.tl l) (n-1) + in + match term_ty with + | C.MutInd _ -> term_ty + | C.Appl ((C.MutInd (a,b,c))::args) -> + C.Appl ((C.MutInd (a,b,c)):: + fstn [] args paramsno @ mkrels n_right_args) + | _ -> raise NotAnInductiveTypeToEliminate + in + let rec add_lambdas = function + | 0 -> captured_ty + | 1 -> + C.Lambda (C.Name "matched", captured_term_ty, (add_lambdas 0)) + | n -> + C.Lambda (C.Name ("right_"^(string_of_int (n-1))), + C.Implicit None, (add_lambdas (n-1))) + in + add_lambdas n_lambdas + in + let term_to_refine = C.MutCase (uri,typeno,outtype,term,patterns) in + let refined_term,_,metasenv'',_ = + CicRefine.type_of_aux' metasenv' context term_to_refine + CicUniv.empty_ugraph + in + let new_goals = + ProofEngineHelpers.compare_metasenvs + ~oldmetasenv:metasenv ~newmetasenv:metasenv'' + in + let proof' = curi,metasenv'',proofbo,proofty, attrs in + let proof'', new_goals' = + apply_tactic (apply_tac ~term:refined_term) (proof',goal) + in + (* The apply_tactic can have closed some of the new_goals *) + let patched_new_goals = + let (_,metasenv''',_,_,_) = proof'' in + List.filter + (function i -> List.exists (function (j,_,_) -> j=i) metasenv''') + new_goals @ new_goals' + in + proof'', patched_new_goals + in + mk_tactic (cases_tac ~term) +;; + + let elim_intros_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) - ?depth ?using what = - Tacticals.then_ ~start:(elim_tac ~term:what) + ?depth ?using ?pattern what = + Tacticals.then_ ~start:(elim_tac ?using ?pattern what) ~continuation:(intros_tac ~mk_fresh_name_callback ?howmany:depth ()) ;; (* The simplification is performed only on the conclusion *) let elim_intros_simpl_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) - ?depth ?using what = - Tacticals.then_ ~start:(elim_tac ~term:what) + ?depth ?using ?pattern what = + Tacticals.then_ ~start:(elim_tac ?using ?pattern what) ~continuation: (Tacticals.thens ~start:(intros_tac ~mk_fresh_name_callback ?howmany:depth ()) @@ -581,13 +801,11 @@ let elim_intros_simpl_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fres (* FG: insetrts a "hole" in the context (derived from letin_tac) *) -module C = Cic - let letout_tac = let mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[] in let term = C.Sort C.Set in let letout_tac (proof, goal) = - let curi, metasenv, pbo, pty = proof in + let curi, metasenv, pbo, pty, attrs = proof in let metano, context, ty = CicUtil.lookup_meta goal metasenv in let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in let fresh_name = mk_fresh_name_callback metasenv context (Cic.Name "hole") ~typ:term in