src/HOL/Tools/Predicate_Compile/predicate_compile.ML
changeset 33124 5378e61add1a
parent 33123 3c7c4372f9ad
child 33125 2fef4f9429f7
equal deleted inserted replaced
33123:3c7c4372f9ad 33124:5378e61add1a
    14 val fail_safe_mode = false
    14 val fail_safe_mode = false
    15 
    15 
    16 open Predicate_Compile_Aux;
    16 open Predicate_Compile_Aux;
    17 
    17 
    18 val priority = tracing;
    18 val priority = tracing;
    19 
       
    20 (* tuple processing *)
       
    21 
       
    22 fun expand_tuples thy intro =
       
    23   let
       
    24     fun rewrite_args [] (pats, intro_t, ctxt) = (pats, intro_t, ctxt)
       
    25       | rewrite_args (arg::args) (pats, intro_t, ctxt) = 
       
    26       (case HOLogic.strip_tupleT (fastype_of arg) of
       
    27         (Ts as _ :: _ :: _) =>
       
    28         let
       
    29           fun rewrite_arg' (Const ("Pair", _) $ _ $ t2, Type ("*", [_, T2]))
       
    30             (args, (pats, intro_t, ctxt)) = rewrite_arg' (t2, T2) (args, (pats, intro_t, ctxt))
       
    31             | rewrite_arg' (t, Type ("*", [T1, T2])) (args, (pats, intro_t, ctxt)) =
       
    32               let
       
    33                 val ([x, y], ctxt') = Variable.variant_fixes ["x", "y"] ctxt
       
    34                 val pat = (t, HOLogic.mk_prod (Free (x, T1), Free (y, T2)))
       
    35                   (*val _ = tracing ("Rewriting term " ^
       
    36                     (Syntax.string_of_term_global thy (fst pat)) ^ " to " ^
       
    37                     (Syntax.string_of_term_global thy (snd pat)) ^ " in " ^
       
    38                   (Syntax.string_of_term_global thy intro_t))*)
       
    39                 val intro_t' = Pattern.rewrite_term thy [pat] [] intro_t
       
    40                 val args' = map (Pattern.rewrite_term thy [pat] []) args
       
    41               in
       
    42                 rewrite_arg' (Free (y, T2), T2) (args', (pat::pats, intro_t', ctxt'))
       
    43               end
       
    44             | rewrite_arg' _ (args, (pats, intro_t, ctxt)) = (args, (pats, intro_t, ctxt))
       
    45           val (args', (pats, intro_t', ctxt')) = rewrite_arg' (arg, fastype_of arg)
       
    46             (args, (pats, intro_t, ctxt))
       
    47         in
       
    48           rewrite_args args' (pats, intro_t', ctxt')
       
    49         end
       
    50       | _ => rewrite_args args (pats, intro_t, ctxt))
       
    51     fun rewrite_prem atom =
       
    52       let
       
    53         val (_, args) = strip_comb atom
       
    54       in rewrite_args args end
       
    55     val ctxt = ProofContext.init thy
       
    56     val (((T_insts, t_insts), [intro']), ctxt1) = Variable.import false [intro] ctxt
       
    57     val intro_t = prop_of intro'
       
    58     val concl = Logic.strip_imp_concl intro_t
       
    59     val (p, args) = strip_comb (HOLogic.dest_Trueprop concl)
       
    60     val (pats', intro_t', ctxt2) = rewrite_args args ([], intro_t, ctxt1)
       
    61     val (pats', intro_t', ctxt3) = 
       
    62       fold_atoms rewrite_prem intro_t' (pats', intro_t', ctxt2)
       
    63     (*val _ = Output.tracing ("intro_t': " ^ (Syntax.string_of_term_global thy intro_t'))
       
    64     val _ = Output.tracing ("pats : " ^ (commas (map
       
    65       (fn (t1, t2) => (Syntax.string_of_term_global thy t1) ^ " -> " ^
       
    66       Syntax.string_of_term_global thy t2) pats'))*)
       
    67     fun rewrite_pat (ct1, ct2) =
       
    68       (ct1, cterm_of thy (Pattern.rewrite_term thy pats' [] (term_of ct2)))
       
    69     val t_insts' = map rewrite_pat t_insts
       
    70       (*val _ = Output.tracing ("t_insts':" ^ (commas (map
       
    71       (fn (ct1, ct2) => (Syntax.string_of_term_global thy (term_of ct1) ^ " -> " ^
       
    72     Syntax.string_of_term_global thy (term_of ct2))) t_insts')))*)
       
    73     val intro'' = Thm.instantiate (T_insts, t_insts') intro
       
    74       (*val _ = Output.tracing ("intro'':" ^ (Display.string_of_thm_global thy intro''))*)
       
    75     val [intro'''] = Variable.export ctxt3 ctxt [intro'']
       
    76     (*val _ = Output.tracing ("intro''':" ^ (Display.string_of_thm_global thy intro'''))*)
       
    77   in
       
    78     intro'''
       
    79   end 
       
    80 
       
    81   (* eliminating fst/snd functions *)
       
    82 val simplify_fst_snd = Simplifier.full_simplify
       
    83   (HOL_basic_ss addsimps [@{thm fst_conv}, @{thm snd_conv}, @{thm Pair_eq}])
       
    84 
    19 
    85 (* Some last processing *)
    20 (* Some last processing *)
    86 
    21 
    87 fun remove_pointless_clauses intro =
    22 fun remove_pointless_clauses intro =
    88   if Logic.strip_imp_prems (prop_of intro) = [@{prop "False"}] then
    23   if Logic.strip_imp_prems (prop_of intro) = [@{prop "False"}] then
   143     val specs = (get_specs prednames) @ fun_pred_specs
    78     val specs = (get_specs prednames) @ fun_pred_specs
   144     val (intross3, thy''') = process_specification options specs thy'
    79     val (intross3, thy''') = process_specification options specs thy'
   145     val _ = print_intross thy''' "Introduction rules with new constants: " intross3
    80     val _ = print_intross thy''' "Introduction rules with new constants: " intross3
   146     val intross4 = map (maps remove_pointless_clauses) intross3
    81     val intross4 = map (maps remove_pointless_clauses) intross3
   147     val _ = print_intross thy''' "After removing pointless clauses: " intross4
    82     val _ = print_intross thy''' "After removing pointless clauses: " intross4
   148     val intross5 = burrow (map (AxClass.overload thy''')) intross4
    83     val intross5 = map (map (AxClass.overload thy''')) intross4
   149     val intross6 = burrow (map (simplify_fst_snd o expand_tuples thy''')) intross5
    84     val intross6 = burrow (map (expand_tuples thy''')) intross5
   150     val _ = print_intross thy''' "introduction rules before registering: " intross6
    85     val _ = print_intross thy''' "introduction rules before registering: " intross6
   151     val _ = print_step options "Registering introduction rules..."
    86     val _ = print_step options "Registering introduction rules..."
   152     val thy'''' = fold Predicate_Compile_Core.register_intros intross6 thy'''
    87     val thy'''' = fold Predicate_Compile_Core.register_intros intross6 thy'''
   153   in
    88   in
   154     thy''''
    89     thy''''