src/HOL/Tools/Predicate_Compile/predicate_compile_proof.ML
changeset 50056 72efd6b4038d
parent 46460 68cf3d3550b5
child 51552 c713c9505f68
equal deleted inserted replaced
50055:94041d602ecb 50056:72efd6b4038d
    70     val  (f, args) = strip_comb (Envir.eta_contract t)
    70     val  (f, args) = strip_comb (Envir.eta_contract t)
    71     val mode = head_mode_of deriv
    71     val mode = head_mode_of deriv
    72     val param_derivations = param_derivations_of deriv
    72     val param_derivations = param_derivations_of deriv
    73     val ho_args = ho_args_of mode args
    73     val ho_args = ho_args_of mode args
    74     val f_tac = case f of
    74     val f_tac = case f of
    75       Const (name, T) => simp_tac (HOL_basic_ss addsimps 
    75       Const (name, _) => simp_tac (HOL_basic_ss addsimps 
    76          [@{thm eval_pred}, predfun_definition_of ctxt name mode,
    76          [@{thm eval_pred}, predfun_definition_of ctxt name mode,
    77          @{thm split_eta}, @{thm split_beta}, @{thm fst_conv},
    77          @{thm split_eta}, @{thm split_beta}, @{thm fst_conv},
    78          @{thm snd_conv}, @{thm pair_collapse}, @{thm Product_Type.split_conv}]) 1
    78          @{thm snd_conv}, @{thm pair_collapse}, @{thm Product_Type.split_conv}]) 1
    79     | Free _ =>
    79     | Free _ =>
    80       Subgoal.FOCUS_PREMS (fn {context = ctxt, params = params, prems, asms, concl, schematics} =>
    80       Subgoal.FOCUS_PREMS (fn {context, params = params, prems, asms, concl, schematics} =>
    81         let
    81         let
    82           val prems' = maps dest_conjunct_prem (take nargs prems)
    82           val prems' = maps dest_conjunct_prem (take nargs prems)
    83         in
    83         in
    84           Simplifier.rewrite_goal_tac
    84           Simplifier.rewrite_goal_tac
    85             (map (fn th => th RS @{thm sym} RS @{thm eq_reflection}) prems') 1
    85             (map (fn th => th RS @{thm sym} RS @{thm eq_reflection}) prems') 1
    95     THEN REPEAT_DETERM (rtac @{thm refl} 1)
    95     THEN REPEAT_DETERM (rtac @{thm refl} 1)
    96   end
    96   end
    97 
    97 
    98 fun prove_expr options ctxt nargs (premposition : int) (t, deriv) =
    98 fun prove_expr options ctxt nargs (premposition : int) (t, deriv) =
    99   case strip_comb t of
    99   case strip_comb t of
   100     (Const (name, T), args) =>
   100     (Const (name, _), args) =>
   101       let
   101       let
   102         val mode = head_mode_of deriv
   102         val mode = head_mode_of deriv
   103         val introrule = predfun_intro_of ctxt name mode
   103         val introrule = predfun_intro_of ctxt name mode
   104         val param_derivations = param_derivations_of deriv
   104         val param_derivations = param_derivations_of deriv
   105         val ho_args = ho_args_of mode args
   105         val ho_args = ho_args_of mode args
   115         THEN (EVERY (map2 (prove_param options ctxt nargs) ho_args param_derivations))
   115         THEN (EVERY (map2 (prove_param options ctxt nargs) ho_args param_derivations))
   116         THEN (REPEAT_DETERM (atac 1))
   116         THEN (REPEAT_DETERM (atac 1))
   117       end
   117       end
   118   | (Free _, _) =>
   118   | (Free _, _) =>
   119     print_tac options "proving parameter call.."
   119     print_tac options "proving parameter call.."
   120     THEN Subgoal.FOCUS_PREMS (fn {context = ctxt, params, prems, asms, concl, schematics} =>
   120     THEN Subgoal.FOCUS_PREMS (fn {context, params, prems, asms, concl, schematics} =>
   121         let
   121         let
   122           val param_prem = nth prems premposition
   122           val param_prem = nth prems premposition
   123           val (param, _) = strip_comb (HOLogic.dest_Trueprop (prop_of param_prem))
   123           val (param, _) = strip_comb (HOLogic.dest_Trueprop (prop_of param_prem))
   124           val prems' = maps dest_conjunct_prem (take nargs prems)
   124           val prems' = maps dest_conjunct_prem (take nargs prems)
   125           fun param_rewrite prem =
   125           fun param_rewrite prem =
   134         end) ctxt 1
   134         end) ctxt 1
   135     THEN print_tac options "after prove parameter call"
   135     THEN print_tac options "after prove parameter call"
   136 
   136 
   137 fun SOLVED tac st = FILTER (fn st' => nprems_of st' = nprems_of st - 1) tac st;
   137 fun SOLVED tac st = FILTER (fn st' => nprems_of st' = nprems_of st - 1) tac st;
   138 
   138 
   139 fun SOLVEDALL tac st = FILTER (fn st' => nprems_of st' = 0) tac st
       
   140 
       
   141 fun check_format ctxt st =
       
   142   let
       
   143     val concl' = Logic.strip_assums_concl (hd (prems_of st))
       
   144     val concl = HOLogic.dest_Trueprop concl'
       
   145     val expr = fst (strip_comb (fst (Predicate_Comp_Funs.dest_Eval concl)))
       
   146     fun valid_expr (Const (@{const_name Predicate.bind}, _)) = true
       
   147       | valid_expr (Const (@{const_name Predicate.single}, _)) = true
       
   148       | valid_expr _ = false
       
   149   in
       
   150     if valid_expr expr then
       
   151       ((*tracing "expression is valid";*) Seq.single st)
       
   152     else
       
   153       ((*tracing "expression is not valid";*) Seq.empty) (* error "check_format: wrong format" *)
       
   154   end
       
   155 
       
   156 fun prove_match options ctxt nargs out_ts =
   139 fun prove_match options ctxt nargs out_ts =
   157   let
   140   let
   158     val thy = Proof_Context.theory_of ctxt
   141     val thy = Proof_Context.theory_of ctxt
   159     val eval_if_P =
   142     val eval_if_P =
   160       @{lemma "P ==> Predicate.eval x z ==> Predicate.eval (if P then x else y) z" by simp} 
   143       @{lemma "P ==> Predicate.eval x z ==> Predicate.eval (if P then x else y) z" by simp} 
   173      (* make this simpset better! *)
   156      (* make this simpset better! *)
   174     asm_full_simp_tac (HOL_basic_ss' addsimps simprules) 1
   157     asm_full_simp_tac (HOL_basic_ss' addsimps simprules) 1
   175     THEN print_tac options "after prove_match:"
   158     THEN print_tac options "after prove_match:"
   176     THEN (DETERM (TRY 
   159     THEN (DETERM (TRY 
   177            (rtac eval_if_P 1
   160            (rtac eval_if_P 1
   178            THEN (SUBPROOF (fn {context = ctxt, params, prems, asms, concl, schematics} =>
   161            THEN (SUBPROOF (fn {context, params, prems, asms, concl, schematics} =>
   179              (REPEAT_DETERM (rtac @{thm conjI} 1
   162              (REPEAT_DETERM (rtac @{thm conjI} 1
   180              THEN (SOLVED (asm_simp_tac HOL_basic_ss' 1))))
   163              THEN (SOLVED (asm_simp_tac HOL_basic_ss' 1))))
   181              THEN print_tac options "if condition to be solved:"
   164              THEN print_tac options "if condition to be solved:"
   182              THEN asm_simp_tac HOL_basic_ss' 1
   165              THEN asm_simp_tac HOL_basic_ss' 1
   183              THEN TRY (
   166              THEN TRY (
   195 (* corresponds to compile_fun -- maybe call that also compile_sidecond? *)
   178 (* corresponds to compile_fun -- maybe call that also compile_sidecond? *)
   196 
   179 
   197 fun prove_sidecond ctxt t =
   180 fun prove_sidecond ctxt t =
   198   let
   181   let
   199     fun preds_of t nameTs = case strip_comb t of 
   182     fun preds_of t nameTs = case strip_comb t of 
   200       (f as Const (name, T), args) =>
   183       (Const (name, T), args) =>
   201         if is_registered ctxt name then (name, T) :: nameTs
   184         if is_registered ctxt name then (name, T) :: nameTs
   202           else fold preds_of args nameTs
   185           else fold preds_of args nameTs
   203       | _ => nameTs
   186       | _ => nameTs
   204     val preds = preds_of t []
   187     val preds = preds_of t []
   205     val defs = map
   188     val defs = map
   219       (prove_match options ctxt nargs out_ts)
   202       (prove_match options ctxt nargs out_ts)
   220       THEN print_tac options "before simplifying assumptions"
   203       THEN print_tac options "before simplifying assumptions"
   221       THEN asm_full_simp_tac HOL_basic_ss' 1
   204       THEN asm_full_simp_tac HOL_basic_ss' 1
   222       THEN print_tac options "before single intro rule"
   205       THEN print_tac options "before single intro rule"
   223       THEN Subgoal.FOCUS_PREMS
   206       THEN Subgoal.FOCUS_PREMS
   224              (fn {context = ctxt, params = params, prems, asms, concl, schematics} =>
   207              (fn {context, params, prems, asms, concl, schematics} =>
   225               let
   208               let
   226                 val prems' = maps dest_conjunct_prem (take nargs prems)
   209                 val prems' = maps dest_conjunct_prem (take nargs prems)
   227               in
   210               in
   228                 Simplifier.rewrite_goal_tac
   211                 Simplifier.rewrite_goal_tac
   229                   (map (fn th => th RS @{thm sym} RS @{thm eq_reflection}) prems') 1
   212                   (map (fn th => th RS @{thm sym} RS @{thm eq_reflection}) prems') 1
   265                 [@{thm split_eta}, @{thm split_beta}, @{thm fst_conv},
   248                 [@{thm split_eta}, @{thm split_beta}, @{thm fst_conv},
   266                  @{thm snd_conv}, @{thm pair_collapse}, @{thm Product_Type.split_conv}]) 1
   249                  @{thm snd_conv}, @{thm pair_collapse}, @{thm Product_Type.split_conv}]) 1
   267               THEN (if (is_some name) then
   250               THEN (if (is_some name) then
   268                   print_tac options "before applying not introduction rule"
   251                   print_tac options "before applying not introduction rule"
   269                   THEN Subgoal.FOCUS_PREMS
   252                   THEN Subgoal.FOCUS_PREMS
   270                     (fn {context = ctxt, params = params, prems, asms, concl, schematics} =>
   253                     (fn {context, params = params, prems, asms, concl, schematics} =>
   271                       rtac (the neg_intro_rule) 1
   254                       rtac (the neg_intro_rule) 1
   272                       THEN rtac (nth prems premposition) 1) ctxt 1
   255                       THEN rtac (nth prems premposition) 1) ctxt 1
   273                   THEN print_tac options "after applying not introduction rule"
   256                   THEN print_tac options "after applying not introduction rule"
   274                   THEN (EVERY (map2 (prove_param options ctxt nargs) params param_derivations))
   257                   THEN (EVERY (map2 (prove_param options ctxt nargs) params param_derivations))
   275                   THEN (REPEAT_DETERM (atac 1))
   258                   THEN (REPEAT_DETERM (atac 1))
   362     val (f, args) = strip_comb (Envir.eta_contract t)
   345     val (f, args) = strip_comb (Envir.eta_contract t)
   363     val mode = head_mode_of deriv
   346     val mode = head_mode_of deriv
   364     val param_derivations = param_derivations_of deriv
   347     val param_derivations = param_derivations_of deriv
   365     val ho_args = ho_args_of mode args
   348     val ho_args = ho_args_of mode args
   366     val f_tac = case f of
   349     val f_tac = case f of
   367         Const (name, T) => full_simp_tac (HOL_basic_ss addsimps 
   350         Const (name, _) => full_simp_tac (HOL_basic_ss addsimps 
   368            (@{thm eval_pred}::(predfun_definition_of ctxt name mode)
   351            (@{thm eval_pred}::(predfun_definition_of ctxt name mode)
   369            :: @{thm "Product_Type.split_conv"}::[])) 1
   352            :: @{thm "Product_Type.split_conv"}::[])) 1
   370       | Free _ => all_tac
   353       | Free _ => all_tac
   371       | _ => error "prove_param2: illegal parameter term"
   354       | _ => error "prove_param2: illegal parameter term"
   372   in
   355   in
   376     THEN EVERY (map2 (prove_param2 options ctxt) ho_args param_derivations)
   359     THEN EVERY (map2 (prove_param2 options ctxt) ho_args param_derivations)
   377   end
   360   end
   378 
   361 
   379 fun prove_expr2 options ctxt (t, deriv) = 
   362 fun prove_expr2 options ctxt (t, deriv) = 
   380   (case strip_comb t of
   363   (case strip_comb t of
   381       (Const (name, T), args) =>
   364       (Const (name, _), args) =>
   382         let
   365         let
   383           val mode = head_mode_of deriv
   366           val mode = head_mode_of deriv
   384           val param_derivations = param_derivations_of deriv
   367           val param_derivations = param_derivations_of deriv
   385           val ho_args = ho_args_of mode args
   368           val ho_args = ho_args_of mode args
   386         in
   369         in
   394         end
   377         end
   395       | _ => etac @{thm bindE} 1)
   378       | _ => etac @{thm bindE} 1)
   396 
   379 
   397 fun prove_sidecond2 options ctxt t = let
   380 fun prove_sidecond2 options ctxt t = let
   398   fun preds_of t nameTs = case strip_comb t of 
   381   fun preds_of t nameTs = case strip_comb t of 
   399     (f as Const (name, T), args) =>
   382     (Const (name, T), args) =>
   400       if is_registered ctxt name then (name, T) :: nameTs
   383       if is_registered ctxt name then (name, T) :: nameTs
   401         else fold preds_of args nameTs
   384         else fold preds_of args nameTs
   402     | _ => nameTs
   385     | _ => nameTs
   403   val preds = preds_of t []
   386   val preds = preds_of t []
   404   val defs = map
   387   val defs = map
   413    end
   396    end
   414   
   397   
   415 fun prove_clause2 options ctxt pred mode (ts, ps) i =
   398 fun prove_clause2 options ctxt pred mode (ts, ps) i =
   416   let
   399   let
   417     val pred_intro_rule = nth (intros_of ctxt pred) (i - 1)
   400     val pred_intro_rule = nth (intros_of ctxt pred) (i - 1)
   418     val (in_ts, clause_out_ts) = split_mode mode ts;
   401     val (in_ts, _) = split_mode mode ts;
   419     val split_ss = HOL_basic_ss' addsimps [@{thm split_eta}, @{thm split_beta},
   402     val split_ss = HOL_basic_ss' addsimps [@{thm split_eta}, @{thm split_beta},
   420       @{thm fst_conv}, @{thm snd_conv}, @{thm pair_collapse}]
   403       @{thm fst_conv}, @{thm snd_conv}, @{thm pair_collapse}]
   421     fun prove_prems2 out_ts [] =
   404     fun prove_prems2 out_ts [] =
   422       print_tac options "before prove_match2 - last call:"
   405       print_tac options "before prove_match2 - last call:"
   423       THEN prove_match2 options ctxt out_ts
   406       THEN prove_match2 options ctxt out_ts
   504        else EVERY (map2 prove_clause moded_clauses (1 upto (length moded_clauses))))
   487        else EVERY (map2 prove_clause moded_clauses (1 upto (length moded_clauses))))
   505   end;
   488   end;
   506 
   489 
   507 (** proof procedure **)
   490 (** proof procedure **)
   508 
   491 
   509 fun prove_pred options thy clauses preds pred (pol, mode) (moded_clauses, compiled_term) =
   492 fun prove_pred options thy clauses preds pred (_, mode) (moded_clauses, compiled_term) =
   510   let
   493   let
   511     val ctxt = Proof_Context.init_global thy
   494     val ctxt = Proof_Context.init_global thy
   512     val clauses = case AList.lookup (op =) clauses pred of SOME rs => rs | NONE => []
   495     val clauses = case AList.lookup (op =) clauses pred of SOME rs => rs | NONE => []
   513   in
   496   in
   514     Goal.prove ctxt (Term.add_free_names compiled_term []) [] compiled_term
   497     Goal.prove ctxt (Term.add_free_names compiled_term []) [] compiled_term