src/HOL/Tools/function_package/fundef_proof.ML
author wenzelm
Thu, 30 Nov 2006 14:17:25 +0100
changeset 21602 cb13024d0e36
parent 21255 617fdb08abe9
permissions -rw-r--r--
Goal.norm/close_result;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     1
(*  Title:      HOL/Tools/function_package/fundef_proof.ML
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     2
    ID:         $Id$
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     3
    Author:     Alexander Krauss, TU Muenchen
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     4
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     5
A package for general recursive function definitions. 
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     6
Internal proofs.
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     7
*)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     8
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     9
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    10
signature FUNDEF_PROOF =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    11
sig
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    12
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19876
diff changeset
    13
    val mk_partial_rules : theory -> FundefCommon.prep_result 
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    14
         -> thm -> FundefCommon.fundef_result
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    15
end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    16
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    17
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    18
structure FundefProof : FUNDEF_PROOF = 
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    19
struct
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    20
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20523
diff changeset
    21
open FundefLib
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    22
open FundefCommon
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    23
open FundefAbbrev
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    24
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    25
(* Theory dependencies *)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    26
val subsetD = thm "subsetD"
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    27
val split_apply = thm "Product_Type.split"
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20523
diff changeset
    28
val wf_induct_rule = thm "FunDef.wfP_induct_rule";
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    29
val Pair_inject = thm "Product_Type.Pair_inject";
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    30
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20523
diff changeset
    31
val wf_in_rel = thm "FunDef.wf_in_rel";
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20523
diff changeset
    32
val in_rel_def = thm "FunDef.in_rel_def";
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    33
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20523
diff changeset
    34
val acc_induct_rule = thm "FunDef.accP_induct_rule"
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20523
diff changeset
    35
val acc_downward = thm "FunDef.accP_downward"
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20523
diff changeset
    36
val accI = thm "FunDef.accPI"
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20523
diff changeset
    37
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20523
diff changeset
    38
val acc_subset_induct = thm "FunDef.accP_subset_induct"
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    39
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19876
diff changeset
    40
val conjunctionD1 = thm "conjunctionD1"
984ae977f7aa Fixed name clash.
krauss
parents: 19876
diff changeset
    41
val conjunctionD2 = thm "conjunctionD2"
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    42
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    43
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19930
diff changeset
    44
fun mk_psimp thy globals R f_iff graph_is_function clause valthm =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    45
    let
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    46
      val Globals {domT, z, ...} = globals
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    47
                                   
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    48
      val ClauseInfo {qglr = (oqs, _, _, _), cdata = ClauseContext {qs, cqs, gs, lhs, rhs, ags, ...}, ...} = clause
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    49
      val lhs_acc = cterm_of thy (Trueprop (mk_acc domT R $ lhs)) (* "acc R lhs" *)
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    50
      val z_smaller = cterm_of thy (Trueprop (R $ z $ lhs)) (* "R z lhs" *)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    51
    in
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    52
      ((assume z_smaller) RS ((assume lhs_acc) RS acc_downward))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    53
        |> (fn it => it COMP graph_is_function)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    54
        |> implies_intr z_smaller
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    55
        |> forall_intr (cterm_of thy z)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    56
        |> (fn it => it COMP valthm)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    57
        |> implies_intr lhs_acc 
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    58
        |> asm_simplify (HOL_basic_ss addsimps [f_iff])
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    59
        |> fold_rev (implies_intr o cprop_of) ags
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    60
        |> fold_rev forall_intr_rename (map fst oqs ~~ cqs)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    61
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    62
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    63
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    64
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19930
diff changeset
    65
fun mk_partial_induct_rule thy globals R complete_thm clauses =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    66
    let
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    67
      val Globals {domT, x, z, a, P, D, ...} = globals
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    68
      val acc_R = mk_acc domT R
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    69
                  
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    70
      val x_D = assume (cterm_of thy (Trueprop (D $ x)))
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    71
      val a_D = cterm_of thy (Trueprop (D $ a))
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    72
                
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    73
      val D_subset = cterm_of thy (mk_forall x (implies $ Trueprop (D $ x) $ Trueprop (acc_R $ x)))
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    74
                     
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    75
      val D_dcl = (* "!!x z. [| x: D; (z,x):R |] ==> z:D" *)
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    76
                    mk_forall x
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    77
                    (mk_forall z (Logic.mk_implies (Trueprop (D $ x),
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    78
                                                    Logic.mk_implies (Trueprop (R $ z $ x),
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    79
                                                                      Trueprop (D $ z)))))
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    80
                    |> cterm_of thy
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    81
                    
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    82
                    
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    83
  (* Inductive Hypothesis: !!z. (z,x):R ==> P z *)
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    84
      val ihyp = all domT $ Abs ("z", domT, 
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    85
               implies $ Trueprop (R $ Bound 0 $ x)
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    86
             $ Trueprop (P $ Bound 0))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    87
           |> cterm_of thy
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    88
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    89
      val aihyp = assume ihyp
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19876
diff changeset
    90
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    91
  fun prove_case clause =
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    92
      let
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    93
    val ClauseInfo {cdata = ClauseContext {qs, cqs, ags, gs, lhs, rhs, case_hyp, ...}, RCs, 
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    94
                    qglr = (oqs, _, _, _), ...} = clause
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    95
                       
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    96
    val replace_x_ss = HOL_basic_ss addsimps [case_hyp]
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    97
    val lhs_D = simplify replace_x_ss x_D (* lhs : D *)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    98
    val sih = full_simplify replace_x_ss aihyp
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    99
          
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   100
    fun mk_Prec (RCInfo {llRI, RIvs, CCas, rcarg, ...}) =
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   101
        sih |> forall_elim (cterm_of thy rcarg)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   102
            |> implies_elim_swp llRI
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   103
            |> fold_rev (implies_intr o cprop_of) CCas
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   104
            |> fold_rev (forall_intr o cterm_of thy o Free) RIvs
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   105
        
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   106
    val P_recs = map mk_Prec RCs   (*  [P rec1, P rec2, ... ]  *)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   107
                 
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   108
    val step = Trueprop (P $ lhs)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   109
            |> fold_rev (curry Logic.mk_implies o prop_of) P_recs
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   110
            |> fold_rev (curry Logic.mk_implies) gs
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   111
            |> curry Logic.mk_implies (Trueprop (D $ lhs))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   112
            |> fold_rev mk_forall_rename (map fst oqs ~~ qs)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   113
            |> cterm_of thy
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   114
         
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   115
    val P_lhs = assume step
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   116
           |> fold forall_elim cqs
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   117
           |> implies_elim_swp lhs_D 
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   118
           |> fold_rev implies_elim_swp ags
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   119
           |> fold implies_elim_swp P_recs
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   120
          
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   121
    val res = cterm_of thy (Trueprop (P $ x))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   122
           |> Simplifier.rewrite replace_x_ss
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   123
           |> symmetric (* P lhs == P x *)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   124
           |> (fn eql => equal_elim eql P_lhs) (* "P x" *)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   125
           |> implies_intr (cprop_of case_hyp)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   126
           |> fold_rev (implies_intr o cprop_of) ags
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   127
           |> fold_rev forall_intr cqs
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   128
      in
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   129
    (res, step)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   130
      end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   131
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   132
  val (cases, steps) = split_list (map prove_case clauses)
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19583
diff changeset
   133
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   134
  val istep = complete_thm
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   135
                |> forall_elim_vars 0
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   136
                |> fold (curry op COMP) cases (*  P x  *)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   137
                |> implies_intr ihyp
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   138
                |> implies_intr (cprop_of x_D)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   139
                |> forall_intr (cterm_of thy x)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   140
         
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   141
  val subset_induct_rule = 
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   142
      acc_subset_induct
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   143
        |> (curry op COMP) (assume D_subset)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   144
        |> (curry op COMP) (assume D_dcl)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   145
        |> (curry op COMP) (assume a_D)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   146
        |> (curry op COMP) istep
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   147
        |> fold_rev implies_intr steps
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   148
        |> implies_intr a_D
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   149
        |> implies_intr D_dcl
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   150
        |> implies_intr D_subset
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   151
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   152
  val subset_induct_all = fold_rev (forall_intr o cterm_of thy) [P, a, D] subset_induct_rule
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   153
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   154
  val simple_induct_rule =
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   155
      subset_induct_rule
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   156
        |> forall_intr (cterm_of thy D)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   157
        |> forall_elim (cterm_of thy acc_R)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   158
        |> assume_tac 1 |> Seq.hd
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   159
        |> (curry op COMP) (acc_downward
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   160
                              |> (instantiate' [SOME (ctyp_of thy domT)]
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   161
                                               (map (SOME o cterm_of thy) [R, x, z]))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   162
                              |> forall_intr (cterm_of thy z)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   163
                              |> forall_intr (cterm_of thy x))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   164
        |> forall_intr (cterm_of thy a)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   165
        |> forall_intr (cterm_of thy P)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   166
    in
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   167
      (subset_induct_all, simple_induct_rule)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   168
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   169
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   170
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   171
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19930
diff changeset
   172
(* Does this work with Guards??? *)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19930
diff changeset
   173
fun mk_domain_intro thy globals R R_cases clause =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   174
    let
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   175
      val Globals {z, domT, ...} = globals
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   176
      val ClauseInfo {cdata = ClauseContext {qs, gs, lhs, rhs, cqs, ...}, 
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   177
                      qglr = (oqs, _, _, _), ...} = clause
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   178
      val goal = Trueprop (mk_acc domT R $ lhs)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   179
                          |> fold_rev (curry Logic.mk_implies) gs
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   180
                          |> cterm_of thy
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   181
    in
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   182
      Goal.init goal 
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   183
      |> (SINGLE (resolve_tac [accI] 1)) |> the
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   184
      |> (SINGLE (eresolve_tac [forall_elim_vars 0 R_cases] 1))  |> the
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   185
      |> (SINGLE (CLASIMPSET auto_tac)) |> the
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   186
      |> Goal.conclude
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   187
      |> fold_rev forall_intr_rename (map fst oqs ~~ cqs)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   188
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   189
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   190
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
   191
fun maybe_mk_domain_intro thy =
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
   192
    if !FundefCommon.domintros then mk_domain_intro thy
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
   193
    else K (K (K (K refl)))
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   194
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   195
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19930
diff changeset
   196
fun mk_nest_term_case thy globals R' ihyp clause =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   197
    let
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   198
      val Globals {x, z, ...} = globals
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   199
      val ClauseInfo {cdata = ClauseContext {qs,cqs,ags,lhs,rhs,case_hyp,...},tree,
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   200
                      qglr=(oqs, _, _, _), ...} = clause
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   201
          
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   202
      val ih_case = full_simplify (HOL_basic_ss addsimps [case_hyp]) ihyp
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   203
                    
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   204
      fun step (fixes, assumes) (_ $ arg) u (sub,(hyps,thms)) = 
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   205
          let
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   206
            val used = map (fn ((f,a),thm) => FundefCtxTree.export_thm thy (f, map prop_of a) thm) (u @ sub)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   207
                       
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   208
            val hyp = Trueprop (R' $ arg $ lhs)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   209
                      |> fold_rev (curry Logic.mk_implies o prop_of) used
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   210
                      |> FundefCtxTree.export_term (fixes, map prop_of assumes) 
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   211
                      |> fold_rev (curry Logic.mk_implies o prop_of) ags
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   212
                      |> fold_rev mk_forall_rename (map fst oqs ~~ qs)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   213
                      |> cterm_of thy
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   214
                      
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   215
            val thm = assume hyp
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   216
                      |> fold forall_elim cqs
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   217
                      |> fold implies_elim_swp ags
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   218
                      |> FundefCtxTree.import_thm thy (fixes, assumes) (*  "(arg, lhs) : R'"  *)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   219
                      |> fold implies_elim_swp used
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   220
                      
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   221
            val acc = thm COMP ih_case
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   222
                      
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   223
            val z_eq_arg = cterm_of thy (Trueprop (HOLogic.mk_eq (z, arg)))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   224
                           
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   225
            val arg_eq_z = (assume z_eq_arg) RS sym
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   226
                           
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   227
            val z_acc = simplify (HOL_basic_ss addsimps [arg_eq_z]) acc (* fragile, slow... *)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   228
                        |> implies_intr (cprop_of case_hyp)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   229
                        |> implies_intr z_eq_arg
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   230
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   231
            val z_eq_arg = assume (cterm_of thy (Trueprop (mk_eq (z, arg))))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   232
            val x_eq_lhs = assume (cterm_of thy (Trueprop (mk_eq (x, lhs))))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   233
                           
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   234
            val ethm = (z_acc OF [z_eq_arg, x_eq_lhs])
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   235
                         |> FundefCtxTree.export_thm thy (fixes, 
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   236
                                                          prop_of z_eq_arg :: prop_of x_eq_lhs :: map prop_of (ags @ assumes))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   237
                         |> fold_rev forall_intr_rename (map fst oqs ~~ cqs)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   238
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   239
            val sub' = sub @ [(([],[]), acc)]
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   240
          in
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   241
            (sub', (hyp :: hyps, ethm :: thms))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   242
          end
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   243
        | step _ _ _ _ = raise Match
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   244
    in
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   245
      FundefCtxTree.traverse_tree step tree
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   246
    end
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   247
    
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   248
    
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19930
diff changeset
   249
fun mk_nest_term_rule thy globals R R_cases clauses =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   250
    let
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   251
      val Globals { domT, x, z, ... } = globals
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   252
      val acc_R = mk_acc domT R
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   253
                  
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   254
      val R' = Free ("R", fastype_of R)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   255
               
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   256
      val Rrel = Free ("R", mk_relT (domT, domT))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   257
      val inrel_R = Const ("FunDef.in_rel", mk_relT (domT, domT) --> fastype_of R) $ Rrel
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   258
                    
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   259
      val wfR' = cterm_of thy (Trueprop (Const ("FunDef.wfP", (domT --> domT --> boolT) --> boolT) $ R')) (* "wf R'" *)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   260
                          
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   261
      (* Inductive Hypothesis: !!z. (z,x):R' ==> z : acc R *)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   262
      val ihyp = all domT $ Abs ("z", domT, 
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   263
                                 implies $ Trueprop (R' $ Bound 0 $ x)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   264
                                         $ Trueprop (acc_R $ Bound 0))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   265
                     |> cterm_of thy
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   266
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   267
      val ihyp_a = assume ihyp |> forall_elim_vars 0
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   268
                   
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   269
      val R_z_x = cterm_of thy (Trueprop (R $ z $ x)) 
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   270
                  
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   271
      val (hyps,cases) = fold (mk_nest_term_case thy globals R' ihyp_a) clauses ([],[])
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   272
    in
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   273
      R_cases
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   274
        |> forall_elim (cterm_of thy z)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   275
        |> forall_elim (cterm_of thy x)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   276
        |> forall_elim (cterm_of thy (acc_R $ z))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   277
        |> curry op COMP (assume R_z_x)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   278
        |> fold_rev (curry op COMP) cases
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   279
        |> implies_intr R_z_x
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   280
        |> forall_intr (cterm_of thy z)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   281
        |> (fn it => it COMP accI)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   282
        |> implies_intr ihyp
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   283
        |> forall_intr (cterm_of thy x)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   284
        |> (fn it => Drule.compose_single(it,2,wf_induct_rule))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   285
        |> curry op RS (assume wfR')
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   286
        |> fold implies_intr hyps
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   287
        |> implies_intr wfR'
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   288
        |> forall_intr (cterm_of thy R')
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   289
        |> forall_elim (cterm_of thy (inrel_R))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   290
        |> curry op RS wf_in_rel
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   291
        |> full_simplify (HOL_basic_ss addsimps [in_rel_def])
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   292
        |> forall_intr (cterm_of thy Rrel)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   293
    end
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   294
    
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   295
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   296
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   297
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19876
diff changeset
   298
fun mk_partial_rules thy data provedgoal =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   299
    let
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   300
      val Prep {globals, G, f, R, clauses, values, R_cases, ex1_iff, ...} = data
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   301
                                                                            
21602
cb13024d0e36 Goal.norm/close_result;
wenzelm
parents: 21255
diff changeset
   302
      val provedgoal = PROFILE "Closing result" Goal.close_result provedgoal
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   303
                       
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
   304
      val graph_is_function = PROFILE "Getting function theorem" (fn x => (x COMP conjunctionD1) |> forall_elim_vars 0) provedgoal
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   305
                              
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
   306
      val complete_thm = PROFILE "Getting cases" (curry op COMP provedgoal) conjunctionD2
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
   307
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
   308
      val f_iff = PROFILE "Making f_iff" (curry op RS graph_is_function) ex1_iff
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   309
                  
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
   310
      val psimps = PROFILE "Proving simplification rules" (map2 (mk_psimp thy globals R f_iff graph_is_function)) clauses values
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   311
                    
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
   312
      val (subset_pinduct, simple_pinduct) = PROFILE "Proving partial induction rule" 
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
   313
                                             (mk_partial_induct_rule thy globals R complete_thm) clauses
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   314
                                             
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
   315
      val total_intro = PROFILE "Proving nested termination rule" (mk_nest_term_rule thy globals R R_cases) clauses
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   316
                        
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
   317
      val dom_intros = PROFILE "Proving domain introduction rules" (map (maybe_mk_domain_intro thy globals R R_cases)) clauses
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   318
    in 
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   319
      FundefResult {f=f, G=G, R=R, completeness=complete_thm, 
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   320
                    psimps=psimps, subset_pinduct=subset_pinduct, simple_pinduct=simple_pinduct, total_intro=total_intro,
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   321
                    dom_intros=dom_intros}
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   322
    end
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   323
    
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   324
    
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   325
end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   326
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   327