TFL/post.sml
author paulson
Fri Oct 18 12:41:04 1996 +0200 (1996-10-18)
changeset 2112 3902e9af752f
child 2467 357adb429fda
permissions -rw-r--r--
Konrad Slind's TFL
paulson@2112
     1
structure Tfl
paulson@2112
     2
 :sig
paulson@2112
     3
   structure Prim : TFL_sig
paulson@2112
     4
paulson@2112
     5
   val tgoalw : theory -> thm list -> thm -> thm list
paulson@2112
     6
   val tgoal: theory -> thm -> thm list
paulson@2112
     7
paulson@2112
     8
   val WF_TAC : thm list -> tactic
paulson@2112
     9
paulson@2112
    10
   val simplifier : thm -> thm
paulson@2112
    11
   val std_postprocessor : theory 
paulson@2112
    12
                           -> {induction:thm, rules:thm, TCs:term list list} 
paulson@2112
    13
                           -> {induction:thm, rules:thm, nested_tcs:thm list}
paulson@2112
    14
paulson@2112
    15
   val rfunction  : theory
paulson@2112
    16
                     -> (thm list -> thm -> thm)
paulson@2112
    17
                        -> term -> term  
paulson@2112
    18
                          -> {induction:thm, rules:thm, 
paulson@2112
    19
                              tcs:term list, theory:theory}
paulson@2112
    20
paulson@2112
    21
   val Rfunction : theory -> term -> term  
paulson@2112
    22
                   -> {induction:thm, rules:thm, 
paulson@2112
    23
                       theory:theory, tcs:term list}
paulson@2112
    24
paulson@2112
    25
   val function : theory -> term -> {theory:theory, eq_ind : thm}
paulson@2112
    26
   val lazyR_def : theory -> term -> {theory:theory, eqns : thm}
paulson@2112
    27
paulson@2112
    28
   val tflcongs : theory -> thm list
paulson@2112
    29
paulson@2112
    30
  end = 
paulson@2112
    31
struct
paulson@2112
    32
 structure Prim = Prim
paulson@2112
    33
paulson@2112
    34
 fun tgoalw thy defs thm = 
paulson@2112
    35
    let val L = Prim.termination_goals thm
paulson@2112
    36
        open USyntax
paulson@2112
    37
        val g = cterm_of (sign_of thy) (mk_prop(list_mk_conj L))
paulson@2112
    38
    in goalw_cterm defs g
paulson@2112
    39
    end;
paulson@2112
    40
paulson@2112
    41
 val tgoal = Utils.C tgoalw [];
paulson@2112
    42
paulson@2112
    43
 fun WF_TAC thms = REPEAT(FIRST1(map rtac thms))
paulson@2112
    44
 val WFtac = WF_TAC[wf_measure, wf_inv_image, wf_lex_prod, 
paulson@2112
    45
                    wf_pred_nat, wf_pred_list, wf_trancl];
paulson@2112
    46
paulson@2112
    47
 val terminator = simp_tac(HOL_ss addsimps[pred_nat_def,pred_list_def,
paulson@2112
    48
                                           fst_conv,snd_conv,
paulson@2112
    49
                                           mem_Collect_eq,lessI]) 1
paulson@2112
    50
                  THEN TRY(fast_tac set_cs 1);
paulson@2112
    51
paulson@2112
    52
 val simpls = [less_eq RS eq_reflection,
paulson@2112
    53
               lex_prod_def, measure_def, inv_image_def, 
paulson@2112
    54
               fst_conv RS eq_reflection, snd_conv RS eq_reflection,
paulson@2112
    55
               mem_Collect_eq RS eq_reflection(*, length_Cons RS eq_reflection*)];
paulson@2112
    56
paulson@2112
    57
 val std_postprocessor = Prim.postprocess{WFtac = WFtac,
paulson@2112
    58
                                    terminator = terminator, 
paulson@2112
    59
                                    simplifier = Prim.Rules.simpl_conv simpls};
paulson@2112
    60
paulson@2112
    61
 val simplifier = rewrite_rule (simpls @ #simps(rep_ss HOL_ss) @ 
paulson@2112
    62
                                [pred_nat_def,pred_list_def]);
paulson@2112
    63
 fun tflcongs thy = Prim.Context.read() @ (#case_congs(Thry.extract_info thy));
paulson@2112
    64
paulson@2112
    65
paulson@2112
    66
local structure S = Prim.USyntax
paulson@2112
    67
in
paulson@2112
    68
fun func_of_cond_eqn tm =
paulson@2112
    69
  #1(S.strip_comb(#lhs(S.dest_eq(#2(S.strip_forall(#2(S.strip_imp tm)))))))
paulson@2112
    70
end;
paulson@2112
    71
paulson@2112
    72
paulson@2112
    73
val concl = #2 o Prim.Rules.dest_thm;
paulson@2112
    74
paulson@2112
    75
(*---------------------------------------------------------------------------
paulson@2112
    76
 * Defining a function with an associated termination relation. Lots of
paulson@2112
    77
 * postprocessing takes place.
paulson@2112
    78
 *---------------------------------------------------------------------------*)
paulson@2112
    79
local 
paulson@2112
    80
structure S = Prim.USyntax
paulson@2112
    81
structure R = Prim.Rules
paulson@2112
    82
structure U = Utils
paulson@2112
    83
paulson@2112
    84
val solved = not o U.can S.dest_eq o #2 o S.strip_forall o concl
paulson@2112
    85
paulson@2112
    86
fun id_thm th = 
paulson@2112
    87
   let val {lhs,rhs} = S.dest_eq(#2(S.strip_forall(#2 (R.dest_thm th))))
paulson@2112
    88
   in S.aconv lhs rhs
paulson@2112
    89
   end handle _ => false
paulson@2112
    90
paulson@2112
    91
fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);
paulson@2112
    92
val P_imp_P_iff_True = prover "P --> (P= True)" RS mp;
paulson@2112
    93
val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
paulson@2112
    94
fun mk_meta_eq r = case concl_of r of
paulson@2112
    95
     Const("==",_)$_$_ => r
paulson@2112
    96
  |   _$(Const("op =",_)$_$_) => r RS eq_reflection
paulson@2112
    97
  |   _ => r RS P_imp_P_eq_True
paulson@2112
    98
fun rewrite L = rewrite_rule (map mk_meta_eq (Utils.filter(not o id_thm) L))
paulson@2112
    99
paulson@2112
   100
fun join_assums th = 
paulson@2112
   101
  let val {sign,...} = rep_thm th
paulson@2112
   102
      val tych = cterm_of sign
paulson@2112
   103
      val {lhs,rhs} = S.dest_eq(#2 (S.strip_forall (concl th)))
paulson@2112
   104
      val cntxtl = (#1 o S.strip_imp) lhs  (* cntxtl should = cntxtr *)
paulson@2112
   105
      val cntxtr = (#1 o S.strip_imp) rhs  (* but union is solider *)
paulson@2112
   106
      val cntxt = U.union S.aconv cntxtl cntxtr
paulson@2112
   107
  in 
paulson@2112
   108
  R.GEN_ALL 
paulson@2112
   109
  (R.DISCH_ALL 
paulson@2112
   110
    (rewrite (map (R.ASSUME o tych) cntxt) (R.SPEC_ALL th)))
paulson@2112
   111
  end
paulson@2112
   112
  val gen_all = S.gen_all
paulson@2112
   113
in
paulson@2112
   114
fun rfunction theory reducer R eqs = 
paulson@2112
   115
 let val _ = output(std_out, "Making definition..  ")
paulson@2112
   116
     val _ = flush_out std_out
paulson@2112
   117
     val {rules,theory, full_pats_TCs,
paulson@2112
   118
          TCs,...} = Prim.gen_wfrec_definition theory {R=R,eqs=eqs} 
paulson@2112
   119
     val f = func_of_cond_eqn(concl(R.CONJUNCT1 rules handle _ => rules))
paulson@2112
   120
     val _ = output(std_out, "Definition made.\n")
paulson@2112
   121
     val _ = output(std_out, "Proving induction theorem..  ")
paulson@2112
   122
     val _ = flush_out std_out
paulson@2112
   123
     val ind = Prim.mk_induction theory f R full_pats_TCs
paulson@2112
   124
     val _ = output(std_out, "Proved induction theorem.\n")
paulson@2112
   125
     val pp = std_postprocessor theory
paulson@2112
   126
     val _ = output(std_out, "Postprocessing..  ")
paulson@2112
   127
     val _ = flush_out std_out
paulson@2112
   128
     val {rules,induction,nested_tcs} = pp{rules=rules,induction=ind,TCs=TCs}
paulson@2112
   129
     val normal_tcs = Prim.termination_goals rules
paulson@2112
   130
 in
paulson@2112
   131
 case nested_tcs
paulson@2112
   132
 of [] => (output(std_out, "Postprocessing done.\n");
paulson@2112
   133
           {theory=theory, induction=induction, rules=rules,tcs=normal_tcs})
paulson@2112
   134
  | L  => let val _ = output(std_out, "Simplifying nested TCs..  ")
paulson@2112
   135
              val (solved,simplified,stubborn) =
paulson@2112
   136
               U.itlist (fn th => fn (So,Si,St) =>
paulson@2112
   137
                     if (id_thm th) then (So, Si, th::St) else
paulson@2112
   138
                     if (solved th) then (th::So, Si, St) 
paulson@2112
   139
                     else (So, th::Si, St)) nested_tcs ([],[],[])
paulson@2112
   140
              val simplified' = map join_assums simplified
paulson@2112
   141
              val induction' = reducer (solved@simplified') induction
paulson@2112
   142
              val rules' = reducer (solved@simplified') rules
paulson@2112
   143
              val _ = output(std_out, "Postprocessing done.\n")
paulson@2112
   144
          in
paulson@2112
   145
          {induction = induction',
paulson@2112
   146
               rules = rules',
paulson@2112
   147
                 tcs = normal_tcs @
paulson@2112
   148
                      map (gen_all o S.rhs o #2 o S.strip_forall o concl)
paulson@2112
   149
                           (simplified@stubborn),
paulson@2112
   150
              theory = theory}
paulson@2112
   151
          end
paulson@2112
   152
 end
paulson@2112
   153
 handle (e as Utils.ERR _) => Utils.Raise e
paulson@2112
   154
     |     e               => print_exn e
paulson@2112
   155
paulson@2112
   156
paulson@2112
   157
fun Rfunction thry = 
paulson@2112
   158
     rfunction thry 
paulson@2112
   159
       (fn thl => rewrite (map standard thl @ #simps(rep_ss HOL_ss)));
paulson@2112
   160
paulson@2112
   161
paulson@2112
   162
end;
paulson@2112
   163
paulson@2112
   164
paulson@2112
   165
local structure R = Prim.Rules
paulson@2112
   166
in
paulson@2112
   167
fun function theory eqs = 
paulson@2112
   168
 let val _ = output(std_out, "Making definition..  ")
paulson@2112
   169
     val {rules,R,theory,full_pats_TCs,...} = Prim.lazyR_def theory eqs
paulson@2112
   170
     val f = func_of_cond_eqn (concl(R.CONJUNCT1 rules handle _ => rules))
paulson@2112
   171
     val _ = output(std_out, "Definition made.\n")
paulson@2112
   172
     val _ = output(std_out, "Proving induction theorem..  ")
paulson@2112
   173
     val induction = Prim.mk_induction theory f R full_pats_TCs
paulson@2112
   174
      val _ = output(std_out, "Induction theorem proved.\n")
paulson@2112
   175
 in {theory = theory, 
paulson@2112
   176
     eq_ind = standard (induction RS (rules RS conjI))}
paulson@2112
   177
 end
paulson@2112
   178
 handle (e as Utils.ERR _) => Utils.Raise e
paulson@2112
   179
      |     e              => print_exn e
paulson@2112
   180
end;
paulson@2112
   181
paulson@2112
   182
paulson@2112
   183
fun lazyR_def theory eqs = 
paulson@2112
   184
   let val {rules,theory, ...} = Prim.lazyR_def theory eqs
paulson@2112
   185
   in {eqns=rules, theory=theory}
paulson@2112
   186
   end
paulson@2112
   187
   handle (e as Utils.ERR _) => Utils.Raise e
paulson@2112
   188
        |     e              => print_exn e;
paulson@2112
   189
paulson@2112
   190
paulson@2112
   191
 val () = Prim.Context.write[Thms.LET_CONG, Thms.COND_CONG];
paulson@2112
   192
paulson@2112
   193
end;