src/HOL/Tools/Function/function_core.ML
author wenzelm
Fri Mar 06 15:58:56 2015 +0100 (2015-03-06)
changeset 59621 291934bac95e
parent 59618 e6939796717e
child 59627 bb1e4a35d506
permissions -rw-r--r--
Thm.cterm_of and Thm.ctyp_of operate on local context;
krauss@33099
     1
(*  Title:      HOL/Tools/Function/function_core.ML
krauss@33099
     2
    Author:     Alexander Krauss, TU Muenchen
krauss@33099
     3
krauss@41114
     4
Core of the function package.
krauss@33099
     5
*)
krauss@33099
     6
krauss@33099
     7
signature FUNCTION_CORE =
krauss@33099
     8
sig
krauss@34232
     9
  val trace: bool Unsynchronized.ref
krauss@33099
    10
krauss@34232
    11
  val prepare_function : Function_Common.function_config
krauss@34232
    12
    -> string (* defname *)
krauss@34232
    13
    -> ((bstring * typ) * mixfix) list (* defined symbol *)
krauss@34232
    14
    -> ((bstring * typ) list * term list * term * term) list (* specification *)
krauss@34232
    15
    -> local_theory
krauss@34232
    16
    -> (term   (* f *)
krauss@34232
    17
        * thm  (* goalstate *)
krauss@34232
    18
        * (thm -> Function_Common.function_result) (* continuation *)
krauss@34232
    19
       ) * local_theory
krauss@33099
    20
krauss@33099
    21
end
krauss@33099
    22
krauss@33099
    23
structure Function_Core : FUNCTION_CORE =
krauss@33099
    24
struct
krauss@33099
    25
krauss@34232
    26
val trace = Unsynchronized.ref false
krauss@34232
    27
fun trace_msg msg = if ! trace then tracing (msg ()) else ()
krauss@33099
    28
krauss@33099
    29
val boolT = HOLogic.boolT
krauss@33099
    30
val mk_eq = HOLogic.mk_eq
krauss@33099
    31
krauss@33099
    32
open Function_Lib
krauss@33099
    33
open Function_Common
krauss@33099
    34
krauss@34232
    35
datatype globals = Globals of
krauss@34232
    36
 {fvar: term,
krauss@34232
    37
  domT: typ,
krauss@34232
    38
  ranT: typ,
krauss@34232
    39
  h: term,
krauss@34232
    40
  y: term,
krauss@34232
    41
  x: term,
krauss@34232
    42
  z: term,
krauss@34232
    43
  a: term,
krauss@34232
    44
  P: term,
krauss@34232
    45
  D: term,
krauss@34232
    46
  Pbool:term}
krauss@34232
    47
krauss@34232
    48
datatype rec_call_info = RCInfo of
krauss@34232
    49
 {RIvs: (string * typ) list,  (* Call context: fixes and assumes *)
krauss@34232
    50
  CCas: thm list,
krauss@34232
    51
  rcarg: term,                 (* The recursive argument *)
krauss@34232
    52
  llRI: thm,
krauss@34232
    53
  h_assum: term}
krauss@33099
    54
krauss@33099
    55
krauss@34232
    56
datatype clause_context = ClauseContext of
krauss@34232
    57
 {ctxt : Proof.context,
krauss@34232
    58
  qs : term list,
krauss@34232
    59
  gs : term list,
krauss@34232
    60
  lhs: term,
krauss@34232
    61
  rhs: term,
krauss@34232
    62
  cqs: cterm list,
krauss@34232
    63
  ags: thm list,
krauss@34232
    64
  case_hyp : thm}
krauss@33099
    65
krauss@33099
    66
krauss@33099
    67
fun transfer_clause_ctx thy (ClauseContext { ctxt, qs, gs, lhs, rhs, cqs, ags, case_hyp }) =
wenzelm@42361
    68
  ClauseContext { ctxt = Proof_Context.transfer thy ctxt,
krauss@34232
    69
    qs = qs, gs = gs, lhs = lhs, rhs = rhs, cqs = cqs, ags = ags, case_hyp = case_hyp }
krauss@33099
    70
krauss@33099
    71
krauss@34232
    72
datatype clause_info = ClauseInfo of
krauss@34232
    73
 {no: int,
krauss@34232
    74
  qglr : ((string * typ) list * term list * term * term),
krauss@34232
    75
  cdata : clause_context,
wenzelm@58816
    76
  tree: Function_Context_Tree.ctx_tree,
krauss@34232
    77
  lGI: thm,
krauss@34232
    78
  RCs: rec_call_info list}
krauss@33099
    79
krauss@33099
    80
krauss@33099
    81
(* Theory dependencies. *)
krauss@34232
    82
val acc_induct_rule = @{thm accp_induct_rule}
krauss@33099
    83
blanchet@55085
    84
val ex1_implies_ex = @{thm Fun_Def.fundef_ex1_existence}
blanchet@55085
    85
val ex1_implies_un = @{thm Fun_Def.fundef_ex1_uniqueness}
blanchet@55085
    86
val ex1_implies_iff = @{thm Fun_Def.fundef_ex1_iff}
krauss@33099
    87
krauss@34232
    88
val acc_downward = @{thm accp_downward}
krauss@34232
    89
val accI = @{thm accp.accI}
krauss@34232
    90
val case_split = @{thm HOL.case_split}
blanchet@55085
    91
val fundef_default_value = @{thm Fun_Def.fundef_default_value}
krauss@34232
    92
val not_acc_down = @{thm not_accp_down}
krauss@33099
    93
krauss@33099
    94
krauss@33099
    95
krauss@33099
    96
fun find_calls tree =
krauss@34232
    97
  let
krauss@34232
    98
    fun add_Ri (fixes,assumes) (_ $ arg) _ (_, xs) =
krauss@34232
    99
      ([], (fixes, assumes, arg) :: xs)
krauss@34232
   100
      | add_Ri _ _ _ _ = raise Match
krauss@34232
   101
  in
wenzelm@58816
   102
    rev (Function_Context_Tree.traverse_tree add_Ri tree [])
krauss@34232
   103
  end
krauss@33099
   104
krauss@33099
   105
krauss@33099
   106
(** building proof obligations *)
krauss@33099
   107
krauss@33099
   108
fun mk_compat_proof_obligations domT ranT fvar f glrs =
krauss@34232
   109
  let
krauss@34232
   110
    fun mk_impl ((qs, gs, lhs, rhs),(qs', gs', lhs', rhs')) =
krauss@34232
   111
      let
krauss@34232
   112
        val shift = incr_boundvars (length qs')
krauss@34232
   113
      in
krauss@34232
   114
        Logic.mk_implies
krauss@34232
   115
          (HOLogic.mk_Trueprop (HOLogic.eq_const domT $ shift lhs $ lhs'),
krauss@34232
   116
            HOLogic.mk_Trueprop (HOLogic.eq_const ranT $ shift rhs $ rhs'))
krauss@34232
   117
        |> fold_rev (curry Logic.mk_implies) (map shift gs @ gs')
wenzelm@46217
   118
        |> fold_rev (fn (n,T) => fn b => Logic.all_const T $ Abs(n,T,b)) (qs @ qs')
krauss@34232
   119
        |> curry abstract_over fvar
krauss@34232
   120
        |> curry subst_bound f
krauss@34232
   121
      end
krauss@34232
   122
  in
krauss@34232
   123
    map mk_impl (unordered_pairs glrs)
krauss@34232
   124
  end
krauss@33099
   125
krauss@33099
   126
krauss@33099
   127
fun mk_completeness (Globals {x, Pbool, ...}) clauses qglrs =
krauss@34232
   128
  let
krauss@34232
   129
    fun mk_case (ClauseContext {qs, gs, lhs, ...}, (oqs, _, _, _)) =
krauss@34232
   130
      HOLogic.mk_Trueprop Pbool
krauss@34232
   131
      |> curry Logic.mk_implies (HOLogic.mk_Trueprop (mk_eq (x, lhs)))
krauss@34232
   132
      |> fold_rev (curry Logic.mk_implies) gs
krauss@34232
   133
      |> fold_rev mk_forall_rename (map fst oqs ~~ qs)
krauss@34232
   134
  in
krauss@34232
   135
    HOLogic.mk_Trueprop Pbool
krauss@34232
   136
    |> fold_rev (curry Logic.mk_implies o mk_case) (clauses ~~ qglrs)
krauss@34232
   137
    |> mk_forall_rename ("x", x)
krauss@34232
   138
    |> mk_forall_rename ("P", Pbool)
krauss@34232
   139
  end
krauss@33099
   140
krauss@33099
   141
(** making a context with it's own local bindings **)
krauss@33099
   142
krauss@33099
   143
fun mk_clause_context x ctxt (pre_qs,pre_gs,pre_lhs,pre_rhs) =
krauss@34232
   144
  let
krauss@34232
   145
    val (qs, ctxt') = Variable.variant_fixes (map fst pre_qs) ctxt
krauss@34232
   146
      |>> map2 (fn (_, T) => fn n => Free (n, T)) pre_qs
krauss@33099
   147
krauss@34232
   148
    fun inst t = subst_bounds (rev qs, t)
krauss@34232
   149
    val gs = map inst pre_gs
krauss@34232
   150
    val lhs = inst pre_lhs
krauss@34232
   151
    val rhs = inst pre_rhs
krauss@33099
   152
wenzelm@59621
   153
    val cqs = map (Thm.cterm_of ctxt') qs
wenzelm@59621
   154
    val ags = map (Thm.assume o Thm.cterm_of ctxt') gs
krauss@33099
   155
wenzelm@59618
   156
    val case_hyp =
wenzelm@59621
   157
      Thm.assume (Thm.cterm_of ctxt' (HOLogic.mk_Trueprop (mk_eq (x, lhs))))
krauss@34232
   158
  in
krauss@34232
   159
    ClauseContext { ctxt = ctxt', qs = qs, gs = gs, lhs = lhs, rhs = rhs,
krauss@34232
   160
      cqs = cqs, ags = ags, case_hyp = case_hyp }
krauss@34232
   161
  end
krauss@33099
   162
krauss@33099
   163
krauss@33099
   164
(* lowlevel term function. FIXME: remove *)
krauss@33099
   165
fun abstract_over_list vs body =
krauss@33099
   166
  let
krauss@33099
   167
    fun abs lev v tm =
krauss@33099
   168
      if v aconv tm then Bound lev
krauss@33099
   169
      else
krauss@33099
   170
        (case tm of
krauss@33099
   171
          Abs (a, T, t) => Abs (a, T, abs (lev + 1) v t)
krauss@33099
   172
        | t $ u => abs lev v t $ abs lev v u
krauss@34232
   173
        | t => t)
krauss@33099
   174
  in
krauss@33099
   175
    fold_index (fn (i, v) => fn t => abs i v t) vs body
krauss@33099
   176
  end
krauss@33099
   177
krauss@33099
   178
krauss@33099
   179
krauss@33099
   180
fun mk_clause_info globals G f no cdata qglr tree RCs GIntro_thm RIntro_thms =
krauss@34232
   181
  let
krauss@34232
   182
    val Globals {h, ...} = globals
krauss@33099
   183
krauss@34232
   184
    val ClauseContext { ctxt, qs, cqs, ags, ... } = cdata
krauss@33099
   185
krauss@34232
   186
    (* Instantiate the GIntro thm with "f" and import into the clause context. *)
krauss@34232
   187
    val lGI = GIntro_thm
wenzelm@59621
   188
      |> Thm.forall_elim (Thm.cterm_of ctxt f)
wenzelm@36945
   189
      |> fold Thm.forall_elim cqs
krauss@34232
   190
      |> fold Thm.elim_implies ags
krauss@33099
   191
krauss@34232
   192
    fun mk_call_info (rcfix, rcassm, rcarg) RI =
krauss@34232
   193
      let
krauss@34232
   194
        val llRI = RI
wenzelm@36945
   195
          |> fold Thm.forall_elim cqs
wenzelm@59621
   196
          |> fold (Thm.forall_elim o Thm.cterm_of ctxt o Free) rcfix
krauss@34232
   197
          |> fold Thm.elim_implies ags
krauss@34232
   198
          |> fold Thm.elim_implies rcassm
krauss@33099
   199
krauss@34232
   200
        val h_assum =
krauss@34232
   201
          HOLogic.mk_Trueprop (G $ rcarg $ (h $ rcarg))
wenzelm@59582
   202
          |> fold_rev (curry Logic.mk_implies o Thm.prop_of) rcassm
krauss@34232
   203
          |> fold_rev (Logic.all o Free) rcfix
wenzelm@42361
   204
          |> Pattern.rewrite_term (Proof_Context.theory_of ctxt) [(f, h)] []
krauss@34232
   205
          |> abstract_over_list (rev qs)
krauss@34232
   206
      in
krauss@34232
   207
        RCInfo {RIvs=rcfix, rcarg=rcarg, CCas=rcassm, llRI=llRI, h_assum=h_assum}
krauss@34232
   208
      end
krauss@33099
   209
krauss@34232
   210
    val RC_infos = map2 mk_call_info RCs RIntro_thms
krauss@34232
   211
  in
krauss@34232
   212
    ClauseInfo {no=no, cdata=cdata, qglr=qglr, lGI=lGI, RCs=RC_infos,
krauss@34232
   213
      tree=tree}
krauss@34232
   214
  end
krauss@33099
   215
krauss@33099
   216
krauss@33099
   217
fun store_compat_thms 0 thms = []
krauss@33099
   218
  | store_compat_thms n thms =
krauss@34232
   219
  let
krauss@34232
   220
    val (thms1, thms2) = chop n thms
krauss@34232
   221
  in
krauss@34232
   222
    (thms1 :: store_compat_thms (n - 1) thms2)
krauss@34232
   223
  end
krauss@33099
   224
krauss@33099
   225
(* expects i <= j *)
krauss@33099
   226
fun lookup_compat_thm i j cts =
krauss@34232
   227
  nth (nth cts (i - 1)) (j - i)
krauss@33099
   228
krauss@33099
   229
(* Returns "Gsi, Gsj, lhs_i = lhs_j |-- rhs_j_f = rhs_i_f" *)
krauss@33099
   230
(* if j < i, then turn around *)
krauss@33099
   231
fun get_compat_thm thy cts i j ctxi ctxj =
krauss@34232
   232
  let
krauss@34232
   233
    val ClauseContext {cqs=cqsi,ags=agsi,lhs=lhsi,...} = ctxi
krauss@34232
   234
    val ClauseContext {cqs=cqsj,ags=agsj,lhs=lhsj,...} = ctxj
krauss@33099
   235
wenzelm@59621
   236
    val lhsi_eq_lhsj = Thm.global_cterm_of thy (HOLogic.mk_Trueprop (mk_eq (lhsi, lhsj)))
krauss@34232
   237
  in if j < i then
krauss@34232
   238
    let
krauss@34232
   239
      val compat = lookup_compat_thm j i cts
krauss@34232
   240
    in
krauss@34232
   241
      compat         (* "!!qj qi. Gsj => Gsi => lhsj = lhsi ==> rhsj = rhsi" *)
wenzelm@36945
   242
      |> fold Thm.forall_elim (cqsj @ cqsi) (* "Gsj => Gsi => lhsj = lhsi ==> rhsj = rhsi" *)
krauss@34232
   243
      |> fold Thm.elim_implies agsj
krauss@34232
   244
      |> fold Thm.elim_implies agsi
wenzelm@36945
   245
      |> Thm.elim_implies ((Thm.assume lhsi_eq_lhsj) RS sym) (* "Gsj, Gsi, lhsi = lhsj |-- rhsj = rhsi" *)
krauss@33099
   246
    end
krauss@34232
   247
    else
krauss@34232
   248
    let
krauss@34232
   249
      val compat = lookup_compat_thm i j cts
krauss@34232
   250
    in
krauss@34232
   251
      compat        (* "!!qi qj. Gsi => Gsj => lhsi = lhsj ==> rhsi = rhsj" *)
wenzelm@36945
   252
      |> fold Thm.forall_elim (cqsi @ cqsj) (* "Gsi => Gsj => lhsi = lhsj ==> rhsi = rhsj" *)
krauss@34232
   253
      |> fold Thm.elim_implies agsi
krauss@34232
   254
      |> fold Thm.elim_implies agsj
wenzelm@36945
   255
      |> Thm.elim_implies (Thm.assume lhsi_eq_lhsj)
krauss@34232
   256
      |> (fn thm => thm RS sym) (* "Gsi, Gsj, lhsi = lhsj |-- rhsj = rhsi" *)
krauss@34232
   257
    end
krauss@34232
   258
  end
krauss@33099
   259
krauss@33099
   260
(* Generates the replacement lemma in fully quantified form. *)
wenzelm@51717
   261
fun mk_replacement_lemma ctxt h ih_elim clause =
krauss@34232
   262
  let
krauss@34232
   263
    val ClauseInfo {cdata=ClauseContext {qs, lhs, cqs, ags, case_hyp, ...},
krauss@34232
   264
      RCs, tree, ...} = clause
krauss@34232
   265
    local open Conv in
krauss@34232
   266
      val ih_conv = arg1_conv o arg_conv o arg_conv
krauss@34232
   267
    end
krauss@33099
   268
krauss@34232
   269
    val ih_elim_case =
krauss@34232
   270
      Conv.fconv_rule (ih_conv (K (case_hyp RS eq_reflection))) ih_elim
krauss@33099
   271
krauss@34232
   272
    val Ris = map (fn RCInfo {llRI, ...} => llRI) RCs
krauss@34232
   273
    val h_assums = map (fn RCInfo {h_assum, ...} =>
wenzelm@59621
   274
      Thm.assume (Thm.cterm_of ctxt (subst_bounds (rev qs, h_assum)))) RCs
krauss@34232
   275
krauss@34232
   276
    val (eql, _) =
wenzelm@58816
   277
      Function_Context_Tree.rewrite_by_tree ctxt h ih_elim_case (Ris ~~ h_assums) tree
krauss@33099
   278
krauss@34232
   279
    val replace_lemma = (eql RS meta_eq_to_obj_eq)
wenzelm@59582
   280
      |> Thm.implies_intr (Thm.cprop_of case_hyp)
wenzelm@59582
   281
      |> fold_rev (Thm.implies_intr o Thm.cprop_of) h_assums
wenzelm@59582
   282
      |> fold_rev (Thm.implies_intr o Thm.cprop_of) ags
wenzelm@36945
   283
      |> fold_rev Thm.forall_intr cqs
krauss@34232
   284
      |> Thm.close_derivation
krauss@34232
   285
  in
krauss@34232
   286
    replace_lemma
krauss@34232
   287
  end
krauss@33099
   288
krauss@33099
   289
krauss@33855
   290
fun mk_uniqueness_clause thy globals compat_store clausei clausej RLj =
krauss@34232
   291
  let
krauss@34232
   292
    val Globals {h, y, x, fvar, ...} = globals
wenzelm@59582
   293
    val ClauseInfo {no=i, cdata=cctxi as ClauseContext {ctxt=ctxti, lhs=lhsi, case_hyp, ...}, ...} =
wenzelm@59582
   294
      clausei
krauss@34232
   295
    val ClauseInfo {no=j, qglr=cdescj, RCs=RCsj, ...} = clausej
krauss@34232
   296
krauss@34232
   297
    val cctxj as ClauseContext {ags = agsj', lhs = lhsj', rhs = rhsj', qs = qsj', cqs = cqsj', ...} =
krauss@34232
   298
      mk_clause_context x ctxti cdescj
krauss@33099
   299
krauss@34232
   300
    val rhsj'h = Pattern.rewrite_term thy [(fvar,h)] [] rhsj'
krauss@34232
   301
    val compat = get_compat_thm thy compat_store i j cctxi cctxj
wenzelm@59582
   302
    val Ghsj' =
wenzelm@59582
   303
      map (fn RCInfo {h_assum, ...} =>
wenzelm@59621
   304
        Thm.assume (Thm.global_cterm_of thy (subst_bounds (rev qsj', h_assum)))) RCsj
krauss@33099
   305
krauss@34232
   306
    val RLj_import = RLj
wenzelm@36945
   307
      |> fold Thm.forall_elim cqsj'
krauss@34232
   308
      |> fold Thm.elim_implies agsj'
krauss@34232
   309
      |> fold Thm.elim_implies Ghsj'
krauss@33099
   310
wenzelm@59621
   311
    val y_eq_rhsj'h = Thm.assume (Thm.global_cterm_of thy (HOLogic.mk_Trueprop (mk_eq (y, rhsj'h))))
wenzelm@59621
   312
    val lhsi_eq_lhsj' = Thm.assume (Thm.global_cterm_of thy (HOLogic.mk_Trueprop (mk_eq (lhsi, lhsj'))))
krauss@34232
   313
       (* lhs_i = lhs_j' |-- lhs_i = lhs_j' *)
krauss@34232
   314
  in
krauss@34232
   315
    (trans OF [case_hyp, lhsi_eq_lhsj']) (* lhs_i = lhs_j' |-- x = lhs_j' *)
wenzelm@36945
   316
    |> Thm.implies_elim RLj_import
krauss@34232
   317
      (* Rj1' ... Rjk', lhs_i = lhs_j' |-- rhs_j'_h = rhs_j'_f *)
krauss@34232
   318
    |> (fn it => trans OF [it, compat])
krauss@34232
   319
      (* lhs_i = lhs_j', Gj', Rj1' ... Rjk' |-- rhs_j'_h = rhs_i_f *)
krauss@34232
   320
    |> (fn it => trans OF [y_eq_rhsj'h, it])
krauss@34232
   321
      (* lhs_i = lhs_j', Gj', Rj1' ... Rjk', y = rhs_j_h' |-- y = rhs_i_f *)
wenzelm@59582
   322
    |> fold_rev (Thm.implies_intr o Thm.cprop_of) Ghsj'
wenzelm@59582
   323
    |> fold_rev (Thm.implies_intr o Thm.cprop_of) agsj'
krauss@34232
   324
      (* lhs_i = lhs_j' , y = rhs_j_h' |-- Gj', Rj1'...Rjk' ==> y = rhs_i_f *)
wenzelm@59582
   325
    |> Thm.implies_intr (Thm.cprop_of y_eq_rhsj'h)
wenzelm@59582
   326
    |> Thm.implies_intr (Thm.cprop_of lhsi_eq_lhsj')
wenzelm@59621
   327
    |> fold_rev Thm.forall_intr (Thm.global_cterm_of thy h :: cqsj')
krauss@34232
   328
  end
krauss@33099
   329
krauss@33099
   330
krauss@33099
   331
wenzelm@51717
   332
fun mk_uniqueness_case ctxt globals G f ihyp ih_intro G_cases compat_store clauses rep_lemmas clausei =
krauss@34232
   333
  let
wenzelm@51717
   334
    val thy = Proof_Context.theory_of ctxt
krauss@34232
   335
    val Globals {x, y, ranT, fvar, ...} = globals
krauss@34232
   336
    val ClauseInfo {cdata = ClauseContext {lhs, rhs, cqs, ags, case_hyp, ...}, lGI, RCs, ...} = clausei
krauss@34232
   337
    val rhsC = Pattern.rewrite_term thy [(fvar, f)] [] rhs
krauss@33099
   338
wenzelm@51717
   339
    val ih_intro_case = full_simplify (put_simpset HOL_basic_ss ctxt addsimps [case_hyp]) ih_intro
krauss@33099
   340
krauss@34232
   341
    fun prep_RC (RCInfo {llRI, RIvs, CCas, ...}) = (llRI RS ih_intro_case)
wenzelm@59582
   342
      |> fold_rev (Thm.implies_intr o Thm.cprop_of) CCas
wenzelm@59621
   343
      |> fold_rev (Thm.forall_intr o Thm.cterm_of ctxt o Free) RIvs
krauss@34232
   344
krauss@34232
   345
    val existence = fold (curry op COMP o prep_RC) RCs lGI
krauss@33099
   346
wenzelm@59621
   347
    val P = Thm.cterm_of ctxt (mk_eq (y, rhsC))
wenzelm@59621
   348
    val G_lhs_y = Thm.assume (Thm.cterm_of ctxt (HOLogic.mk_Trueprop (G $ lhs $ y)))
krauss@33099
   349
krauss@34232
   350
    val unique_clauses =
krauss@34232
   351
      map2 (mk_uniqueness_clause thy globals compat_store clausei) clauses rep_lemmas
krauss@33099
   352
krauss@36270
   353
    fun elim_implies_eta A AB =
wenzelm@58950
   354
      Thm.bicompose (SOME ctxt) {flatten = false, match = true, incremented = false}
wenzelm@58950
   355
        (false, A, 0) 1 AB
wenzelm@52223
   356
      |> Seq.list_of |> the_single
krauss@36270
   357
krauss@34232
   358
    val uniqueness = G_cases
wenzelm@59621
   359
      |> Thm.forall_elim (Thm.cterm_of ctxt lhs)
wenzelm@59621
   360
      |> Thm.forall_elim (Thm.cterm_of ctxt y)
wenzelm@36945
   361
      |> Thm.forall_elim P
krauss@34232
   362
      |> Thm.elim_implies G_lhs_y
krauss@36270
   363
      |> fold elim_implies_eta unique_clauses
wenzelm@59582
   364
      |> Thm.implies_intr (Thm.cprop_of G_lhs_y)
wenzelm@59621
   365
      |> Thm.forall_intr (Thm.cterm_of ctxt y)
krauss@33099
   366
wenzelm@59621
   367
    val P2 = Thm.cterm_of ctxt (lambda y (G $ lhs $ y)) (* P2 y := (lhs, y): G *)
krauss@33099
   368
krauss@34232
   369
    val exactly_one =
wenzelm@59582
   370
      @{thm ex1I}
wenzelm@59618
   371
      |> instantiate'
wenzelm@59621
   372
          [SOME (Thm.ctyp_of ctxt ranT)]
wenzelm@59621
   373
          [SOME P2, SOME (Thm.cterm_of ctxt rhsC)]
krauss@34232
   374
      |> curry (op COMP) existence
krauss@34232
   375
      |> curry (op COMP) uniqueness
wenzelm@51717
   376
      |> simplify (put_simpset HOL_basic_ss ctxt addsimps [case_hyp RS sym])
wenzelm@59582
   377
      |> Thm.implies_intr (Thm.cprop_of case_hyp)
wenzelm@59582
   378
      |> fold_rev (Thm.implies_intr o Thm.cprop_of) ags
wenzelm@36945
   379
      |> fold_rev Thm.forall_intr cqs
krauss@33099
   380
krauss@34232
   381
    val function_value =
krauss@34232
   382
      existence
wenzelm@36945
   383
      |> Thm.implies_intr ihyp
wenzelm@59582
   384
      |> Thm.implies_intr (Thm.cprop_of case_hyp)
wenzelm@59621
   385
      |> Thm.forall_intr (Thm.cterm_of ctxt x)
wenzelm@59621
   386
      |> Thm.forall_elim (Thm.cterm_of ctxt lhs)
krauss@34232
   387
      |> curry (op RS) refl
krauss@34232
   388
  in
krauss@34232
   389
    (exactly_one, function_value)
krauss@34232
   390
  end
krauss@33099
   391
krauss@33099
   392
krauss@33099
   393
fun prove_stuff ctxt globals G f R clauses complete compat compat_store G_elim f_def =
krauss@34232
   394
  let
krauss@34232
   395
    val Globals {h, domT, ranT, x, ...} = globals
krauss@33099
   396
krauss@34232
   397
    (* Inductive Hypothesis: !!z. (z,x):R ==> EX!y. (z,y):G *)
wenzelm@46217
   398
    val ihyp = Logic.all_const domT $ Abs ("z", domT,
krauss@34232
   399
      Logic.mk_implies (HOLogic.mk_Trueprop (R $ Bound 0 $ x),
haftmann@38558
   400
        HOLogic.mk_Trueprop (Const (@{const_name Ex1}, (ranT --> boolT) --> boolT) $
krauss@34232
   401
          Abs ("y", ranT, G $ Bound 1 $ Bound 0))))
wenzelm@59621
   402
      |> Thm.cterm_of ctxt
krauss@33099
   403
wenzelm@36945
   404
    val ihyp_thm = Thm.assume ihyp |> Thm.forall_elim_vars 0
krauss@34232
   405
    val ih_intro = ihyp_thm RS (f_def RS ex1_implies_ex)
krauss@34232
   406
    val ih_elim = ihyp_thm RS (f_def RS ex1_implies_un)
wenzelm@59621
   407
      |> instantiate' [] [NONE, SOME (Thm.cterm_of ctxt h)]
krauss@33099
   408
krauss@34232
   409
    val _ = trace_msg (K "Proving Replacement lemmas...")
wenzelm@51717
   410
    val repLemmas = map (mk_replacement_lemma ctxt h ih_elim) clauses
krauss@33099
   411
krauss@34232
   412
    val _ = trace_msg (K "Proving cases for unique existence...")
krauss@34232
   413
    val (ex1s, values) =
wenzelm@51717
   414
      split_list
wenzelm@51717
   415
        (map
wenzelm@51717
   416
          (mk_uniqueness_case ctxt globals G f ihyp ih_intro G_elim compat_store clauses repLemmas)
wenzelm@51717
   417
          clauses)
krauss@33099
   418
krauss@34232
   419
    val _ = trace_msg (K "Proving: Graph is a function")
krauss@34232
   420
    val graph_is_function = complete
krauss@34232
   421
      |> Thm.forall_elim_vars 0
krauss@34232
   422
      |> fold (curry op COMP) ex1s
wenzelm@36945
   423
      |> Thm.implies_intr (ihyp)
wenzelm@59621
   424
      |> Thm.implies_intr (Thm.cterm_of ctxt (HOLogic.mk_Trueprop (mk_acc domT R $ x)))
wenzelm@59621
   425
      |> Thm.forall_intr (Thm.cterm_of ctxt x)
wenzelm@52467
   426
      |> (fn it => Drule.compose (it, 2, acc_induct_rule)) (* "EX! y. (?x,y):G" *)
wenzelm@59582
   427
      |> (fn it =>
wenzelm@59621
   428
          fold (Thm.forall_intr o Thm.cterm_of ctxt o Var)
wenzelm@59618
   429
            (Term.add_vars (Thm.prop_of it) []) it)
krauss@33099
   430
krauss@34232
   431
    val goalstate =  Conjunction.intr graph_is_function complete
krauss@34232
   432
      |> Thm.close_derivation
wenzelm@52456
   433
      |> Goal.protect 0
wenzelm@59582
   434
      |> fold_rev (Thm.implies_intr o Thm.cprop_of) compat
wenzelm@59582
   435
      |> Thm.implies_intr (Thm.cprop_of complete)
krauss@34232
   436
  in
krauss@34232
   437
    (goalstate, values)
krauss@34232
   438
  end
krauss@33099
   439
krauss@33348
   440
(* wrapper -- restores quantifiers in rule specifications *)
krauss@33348
   441
fun inductive_def (binding as ((R, T), _)) intrs lthy =
krauss@33348
   442
  let
krauss@33348
   443
    val ({intrs = intrs_gen, elims = [elim_gen], preds = [ Rdef ], induct, ...}, lthy) =
krauss@33348
   444
      lthy
wenzelm@33671
   445
      |> Local_Theory.conceal
krauss@33348
   446
      |> Inductive.add_inductive_i
krauss@33350
   447
          {quiet_mode = true,
krauss@33350
   448
            verbose = ! trace,
krauss@33348
   449
            alt_name = Binding.empty,
krauss@33348
   450
            coind = false,
krauss@33348
   451
            no_elim = false,
krauss@33348
   452
            no_ind = false,
wenzelm@49170
   453
            skip_mono = true}
krauss@33348
   454
          [binding] (* relation *)
krauss@33348
   455
          [] (* no parameters *)
krauss@33348
   456
          (map (fn t => (Attrib.empty_binding, t)) intrs) (* intro rules *)
krauss@33348
   457
          [] (* no special monos *)
wenzelm@33671
   458
      ||> Local_Theory.restore_naming lthy
krauss@33348
   459
krauss@33348
   460
    fun requantify orig_intro thm =
krauss@33348
   461
      let
krauss@33348
   462
        val (qs, t) = dest_all_all orig_intro
wenzelm@42483
   463
        val frees = Variable.add_frees lthy t [] |> remove (op =) (Binding.name_of R, T)
wenzelm@59582
   464
        val vars = Term.add_vars (Thm.prop_of thm) []
krauss@33348
   465
        val varmap = AList.lookup (op =) (frees ~~ map fst vars)
wenzelm@42483
   466
          #> the_default ("", 0)
krauss@33348
   467
      in
krauss@33348
   468
        fold_rev (fn Free (n, T) =>
wenzelm@59621
   469
          forall_intr_rename (n, Thm.cterm_of lthy (Var (varmap (n, T), T)))) qs thm
krauss@33348
   470
      end
krauss@33348
   471
  in
krauss@34232
   472
    ((Rdef, map2 requantify intrs intrs_gen, forall_intr_vars elim_gen, induct), lthy)
krauss@33348
   473
  end
krauss@33348
   474
krauss@33099
   475
fun define_graph Gname fvar domT ranT clauses RCss lthy =
krauss@33349
   476
  let
krauss@33349
   477
    val GT = domT --> ranT --> boolT
krauss@33349
   478
    val (Gvar as (n, T)) = singleton (Variable.variant_frees lthy []) (Gname, GT)
krauss@33099
   479
krauss@33349
   480
    fun mk_GIntro (ClauseContext {qs, gs, lhs, rhs, ...}) RCs =
krauss@33349
   481
      let
krauss@33349
   482
        fun mk_h_assm (rcfix, rcassm, rcarg) =
krauss@33349
   483
          HOLogic.mk_Trueprop (Free Gvar $ rcarg $ (fvar $ rcarg))
wenzelm@59582
   484
          |> fold_rev (curry Logic.mk_implies o Thm.prop_of) rcassm
krauss@33349
   485
          |> fold_rev (Logic.all o Free) rcfix
krauss@33349
   486
      in
krauss@33349
   487
        HOLogic.mk_Trueprop (Free Gvar $ lhs $ rhs)
krauss@33349
   488
        |> fold_rev (curry Logic.mk_implies o mk_h_assm) RCs
krauss@33349
   489
        |> fold_rev (curry Logic.mk_implies) gs
krauss@33349
   490
        |> fold_rev Logic.all (fvar :: qs)
krauss@33349
   491
      end
krauss@33099
   492
krauss@33349
   493
    val G_intros = map2 mk_GIntro clauses RCss
krauss@33349
   494
  in
krauss@33349
   495
    inductive_def ((Binding.name n, T), NoSyn) G_intros lthy
krauss@33349
   496
  end
krauss@33099
   497
krauss@33099
   498
fun define_function fdefname (fname, mixfix) domT ranT G default lthy =
krauss@33349
   499
  let
krauss@33349
   500
    val f_def =
blanchet@55085
   501
      Abs ("x", domT, Const (@{const_name Fun_Def.THE_default}, ranT --> (ranT --> boolT) --> ranT)
krauss@33349
   502
        $ (default $ Bound 0) $ Abs ("y", ranT, G $ Bound 1 $ Bound 0))
krauss@33349
   503
      |> Syntax.check_term lthy
krauss@33349
   504
  in
wenzelm@33766
   505
    Local_Theory.define
krauss@33349
   506
      ((Binding.name (function_name fname), mixfix),
krauss@33349
   507
        ((Binding.conceal (Binding.name fdefname), []), f_def)) lthy
krauss@33349
   508
  end
krauss@33099
   509
krauss@33855
   510
fun define_recursion_relation Rname domT qglrs clauses RCss lthy =
krauss@33349
   511
  let
krauss@33349
   512
    val RT = domT --> domT --> boolT
krauss@33349
   513
    val (Rvar as (n, T)) = singleton (Variable.variant_frees lthy []) (Rname, RT)
krauss@33099
   514
krauss@33349
   515
    fun mk_RIntro (ClauseContext {qs, gs, lhs, ...}, (oqs, _, _, _)) (rcfix, rcassm, rcarg) =
krauss@33349
   516
      HOLogic.mk_Trueprop (Free Rvar $ rcarg $ lhs)
wenzelm@59582
   517
      |> fold_rev (curry Logic.mk_implies o Thm.prop_of) rcassm
krauss@33349
   518
      |> fold_rev (curry Logic.mk_implies) gs
krauss@33349
   519
      |> fold_rev (Logic.all o Free) rcfix
krauss@33349
   520
      |> fold_rev mk_forall_rename (map fst oqs ~~ qs)
krauss@33349
   521
      (* "!!qs xs. CS ==> G => (r, lhs) : R" *)
krauss@33099
   522
krauss@33349
   523
    val R_intross = map2 (map o mk_RIntro) (clauses ~~ qglrs) RCss
krauss@33099
   524
krauss@33349
   525
    val ((R, RIntro_thms, R_elim, _), lthy) =
krauss@33349
   526
      inductive_def ((Binding.name n, T), NoSyn) (flat R_intross) lthy
krauss@33349
   527
  in
krauss@33349
   528
    ((R, Library.unflat R_intross RIntro_thms, R_elim), lthy)
krauss@33349
   529
  end
krauss@33099
   530
krauss@33099
   531
krauss@33099
   532
fun fix_globals domT ranT fvar ctxt =
krauss@34232
   533
  let
wenzelm@59618
   534
    val ([h, y, x, z, a, D, P, Pbool], ctxt') = Variable.variant_fixes
krauss@34232
   535
      ["h_fd", "y_fd", "x_fd", "z_fd", "a_fd", "D_fd", "P_fd", "Pb_fd"] ctxt
krauss@34232
   536
  in
krauss@34232
   537
    (Globals {h = Free (h, domT --> ranT),
krauss@34232
   538
      y = Free (y, ranT),
krauss@34232
   539
      x = Free (x, domT),
krauss@34232
   540
      z = Free (z, domT),
krauss@34232
   541
      a = Free (a, domT),
krauss@34232
   542
      D = Free (D, domT --> boolT),
krauss@34232
   543
      P = Free (P, domT --> boolT),
krauss@34232
   544
      Pbool = Free (Pbool, boolT),
krauss@34232
   545
      fvar = fvar,
krauss@34232
   546
      domT = domT,
krauss@34232
   547
      ranT = ranT},
krauss@34232
   548
    ctxt')
krauss@34232
   549
  end
krauss@33099
   550
krauss@33099
   551
fun inst_RC thy fvar f (rcfix, rcassm, rcarg) =
krauss@34232
   552
  let
krauss@34232
   553
    fun inst_term t = subst_bound(f, abstract_over (fvar, t))
krauss@34232
   554
  in
wenzelm@59621
   555
    (rcfix, map (Thm.assume o Thm.global_cterm_of thy o inst_term o Thm.prop_of) rcassm, inst_term rcarg)
krauss@34232
   556
  end
krauss@33099
   557
krauss@33099
   558
krauss@33099
   559
krauss@33099
   560
(**********************************************************
krauss@33099
   561
 *                   PROVING THE RULES
krauss@33099
   562
 **********************************************************)
krauss@33099
   563
wenzelm@51717
   564
fun mk_psimps ctxt globals R clauses valthms f_iff graph_is_function =
krauss@34232
   565
  let
krauss@34232
   566
    val Globals {domT, z, ...} = globals
krauss@33099
   567
wenzelm@59582
   568
    fun mk_psimp
wenzelm@59582
   569
      (ClauseInfo {qglr = (oqs, _, _, _), cdata = ClauseContext {cqs, lhs, ags, ...}, ...}) valthm =
krauss@34232
   570
      let
wenzelm@59618
   571
        val lhs_acc =
wenzelm@59621
   572
          Thm.cterm_of ctxt (HOLogic.mk_Trueprop (mk_acc domT R $ lhs)) (* "acc R lhs" *)
wenzelm@59618
   573
        val z_smaller =
wenzelm@59621
   574
          Thm.cterm_of ctxt (HOLogic.mk_Trueprop (R $ z $ lhs)) (* "R z lhs" *)
krauss@34232
   575
      in
wenzelm@36945
   576
        ((Thm.assume z_smaller) RS ((Thm.assume lhs_acc) RS acc_downward))
krauss@34232
   577
        |> (fn it => it COMP graph_is_function)
wenzelm@36945
   578
        |> Thm.implies_intr z_smaller
wenzelm@59621
   579
        |> Thm.forall_intr (Thm.cterm_of ctxt  z)
krauss@34232
   580
        |> (fn it => it COMP valthm)
wenzelm@36945
   581
        |> Thm.implies_intr lhs_acc
wenzelm@51717
   582
        |> asm_simplify (put_simpset HOL_basic_ss ctxt addsimps [f_iff])
wenzelm@59582
   583
        |> fold_rev (Thm.implies_intr o Thm.cprop_of) ags
krauss@34232
   584
        |> fold_rev forall_intr_rename (map fst oqs ~~ cqs)
krauss@34232
   585
      end
krauss@34232
   586
  in
krauss@34232
   587
    map2 mk_psimp clauses valthms
krauss@34232
   588
  end
krauss@33099
   589
krauss@33099
   590
krauss@33099
   591
(** Induction rule **)
krauss@33099
   592
krauss@33099
   593
haftmann@34065
   594
val acc_subset_induct = @{thm predicate1I} RS @{thm accp_subset_induct}
krauss@33099
   595
krauss@33099
   596
krauss@33099
   597
fun mk_partial_induct_rule thy globals R complete_thm clauses =
krauss@34232
   598
  let
krauss@34232
   599
    val Globals {domT, x, z, a, P, D, ...} = globals
krauss@34232
   600
    val acc_R = mk_acc domT R
krauss@33099
   601
wenzelm@59621
   602
    val x_D = Thm.assume (Thm.global_cterm_of thy (HOLogic.mk_Trueprop (D $ x)))
wenzelm@59621
   603
    val a_D = Thm.global_cterm_of thy (HOLogic.mk_Trueprop (D $ a))
krauss@33099
   604
wenzelm@59621
   605
    val D_subset = Thm.global_cterm_of thy (Logic.all x
krauss@34232
   606
      (Logic.mk_implies (HOLogic.mk_Trueprop (D $ x), HOLogic.mk_Trueprop (acc_R $ x))))
krauss@33099
   607
krauss@34232
   608
    val D_dcl = (* "!!x z. [| x: D; (z,x):R |] ==> z:D" *)
krauss@34232
   609
      Logic.all x (Logic.all z (Logic.mk_implies (HOLogic.mk_Trueprop (D $ x),
krauss@34232
   610
        Logic.mk_implies (HOLogic.mk_Trueprop (R $ z $ x),
krauss@34232
   611
          HOLogic.mk_Trueprop (D $ z)))))
wenzelm@59621
   612
      |> Thm.global_cterm_of thy
krauss@33099
   613
krauss@34232
   614
    (* Inductive Hypothesis: !!z. (z,x):R ==> P z *)
wenzelm@46217
   615
    val ihyp = Logic.all_const domT $ Abs ("z", domT,
krauss@34232
   616
      Logic.mk_implies (HOLogic.mk_Trueprop (R $ Bound 0 $ x),
krauss@34232
   617
        HOLogic.mk_Trueprop (P $ Bound 0)))
wenzelm@59621
   618
      |> Thm.global_cterm_of thy
krauss@33099
   619
wenzelm@36945
   620
    val aihyp = Thm.assume ihyp
krauss@33099
   621
krauss@34232
   622
    fun prove_case clause =
krauss@33099
   623
      let
krauss@34232
   624
        val ClauseInfo {cdata = ClauseContext {ctxt, qs, cqs, ags, gs, lhs, case_hyp, ...},
krauss@34232
   625
          RCs, qglr = (oqs, _, _, _), ...} = clause
krauss@33099
   626
krauss@34232
   627
        val case_hyp_conv = K (case_hyp RS eq_reflection)
krauss@34232
   628
        local open Conv in
krauss@34232
   629
          val lhs_D = fconv_rule (arg_conv (arg_conv (case_hyp_conv))) x_D
krauss@34232
   630
          val sih =
wenzelm@36936
   631
            fconv_rule (Conv.binder_conv
krauss@34232
   632
              (K (arg1_conv (arg_conv (arg_conv case_hyp_conv)))) ctxt) aihyp
krauss@34232
   633
        end
krauss@33099
   634
krauss@34232
   635
        fun mk_Prec (RCInfo {llRI, RIvs, CCas, rcarg, ...}) = sih
wenzelm@59621
   636
          |> Thm.forall_elim (Thm.global_cterm_of thy rcarg)
krauss@34232
   637
          |> Thm.elim_implies llRI
wenzelm@59582
   638
          |> fold_rev (Thm.implies_intr o Thm.cprop_of) CCas
wenzelm@59621
   639
          |> fold_rev (Thm.forall_intr o Thm.global_cterm_of thy o Free) RIvs
krauss@33099
   640
krauss@34232
   641
        val P_recs = map mk_Prec RCs   (*  [P rec1, P rec2, ... ]  *)
krauss@33099
   642
krauss@34232
   643
        val step = HOLogic.mk_Trueprop (P $ lhs)
wenzelm@59582
   644
          |> fold_rev (curry Logic.mk_implies o Thm.prop_of) P_recs
krauss@34232
   645
          |> fold_rev (curry Logic.mk_implies) gs
krauss@34232
   646
          |> curry Logic.mk_implies (HOLogic.mk_Trueprop (D $ lhs))
krauss@34232
   647
          |> fold_rev mk_forall_rename (map fst oqs ~~ qs)
wenzelm@59621
   648
          |> Thm.global_cterm_of thy
krauss@33099
   649
wenzelm@36945
   650
        val P_lhs = Thm.assume step
wenzelm@36945
   651
          |> fold Thm.forall_elim cqs
krauss@34232
   652
          |> Thm.elim_implies lhs_D
krauss@34232
   653
          |> fold Thm.elim_implies ags
krauss@34232
   654
          |> fold Thm.elim_implies P_recs
krauss@33099
   655
wenzelm@59621
   656
        val res = Thm.global_cterm_of thy (HOLogic.mk_Trueprop (P $ x))
krauss@34232
   657
          |> Conv.arg_conv (Conv.arg_conv case_hyp_conv)
wenzelm@36945
   658
          |> Thm.symmetric (* P lhs == P x *)
wenzelm@36945
   659
          |> (fn eql => Thm.equal_elim eql P_lhs) (* "P x" *)
wenzelm@59582
   660
          |> Thm.implies_intr (Thm.cprop_of case_hyp)
wenzelm@59582
   661
          |> fold_rev (Thm.implies_intr o Thm.cprop_of) ags
wenzelm@36945
   662
          |> fold_rev Thm.forall_intr cqs
krauss@33099
   663
      in
krauss@33099
   664
        (res, step)
krauss@33099
   665
      end
krauss@33099
   666
krauss@34232
   667
    val (cases, steps) = split_list (map prove_case clauses)
krauss@33099
   668
krauss@34232
   669
    val istep = complete_thm
krauss@34232
   670
      |> Thm.forall_elim_vars 0
krauss@34232
   671
      |> fold (curry op COMP) cases (*  P x  *)
wenzelm@36945
   672
      |> Thm.implies_intr ihyp
wenzelm@59582
   673
      |> Thm.implies_intr (Thm.cprop_of x_D)
wenzelm@59621
   674
      |> Thm.forall_intr (Thm.global_cterm_of thy x)
krauss@33099
   675
krauss@34232
   676
    val subset_induct_rule =
krauss@33099
   677
      acc_subset_induct
wenzelm@36945
   678
      |> (curry op COMP) (Thm.assume D_subset)
wenzelm@36945
   679
      |> (curry op COMP) (Thm.assume D_dcl)
wenzelm@36945
   680
      |> (curry op COMP) (Thm.assume a_D)
krauss@34232
   681
      |> (curry op COMP) istep
wenzelm@36945
   682
      |> fold_rev Thm.implies_intr steps
wenzelm@36945
   683
      |> Thm.implies_intr a_D
wenzelm@36945
   684
      |> Thm.implies_intr D_dcl
wenzelm@36945
   685
      |> Thm.implies_intr D_subset
krauss@33099
   686
krauss@34232
   687
    val simple_induct_rule =
krauss@33099
   688
      subset_induct_rule
wenzelm@59621
   689
      |> Thm.forall_intr (Thm.global_cterm_of thy D)
wenzelm@59621
   690
      |> Thm.forall_elim (Thm.global_cterm_of thy acc_R)
wenzelm@58963
   691
      |> atac 1 |> Seq.hd
krauss@34232
   692
      |> (curry op COMP) (acc_downward
wenzelm@59621
   693
        |> (instantiate' [SOME (Thm.global_ctyp_of thy domT)]
wenzelm@59621
   694
             (map (SOME o Thm.global_cterm_of thy) [R, x, z]))
wenzelm@59621
   695
        |> Thm.forall_intr (Thm.global_cterm_of thy z)
wenzelm@59621
   696
        |> Thm.forall_intr (Thm.global_cterm_of thy x))
wenzelm@59621
   697
      |> Thm.forall_intr (Thm.global_cterm_of thy a)
wenzelm@59621
   698
      |> Thm.forall_intr (Thm.global_cterm_of thy P)
krauss@34232
   699
  in
krauss@34232
   700
    simple_induct_rule
krauss@34232
   701
  end
krauss@33099
   702
krauss@33099
   703
krauss@34232
   704
(* FIXME: broken by design *)
krauss@33099
   705
fun mk_domain_intro ctxt (Globals {domT, ...}) R R_cases clause =
krauss@34232
   706
  let
krauss@34232
   707
    val ClauseInfo {cdata = ClauseContext {gs, lhs, cqs, ...},
krauss@34232
   708
      qglr = (oqs, _, _, _), ...} = clause
krauss@34232
   709
    val goal = HOLogic.mk_Trueprop (mk_acc domT R $ lhs)
krauss@34232
   710
      |> fold_rev (curry Logic.mk_implies) gs
wenzelm@59621
   711
      |> Thm.cterm_of ctxt
krauss@34232
   712
  in
krauss@34232
   713
    Goal.init goal
wenzelm@59498
   714
    |> (SINGLE (resolve_tac ctxt [accI] 1)) |> the
wenzelm@59498
   715
    |> (SINGLE (eresolve_tac ctxt [Thm.forall_elim_vars 0 R_cases] 1))  |> the
wenzelm@42793
   716
    |> (SINGLE (auto_tac ctxt)) |> the
krauss@34232
   717
    |> Goal.conclude
krauss@34232
   718
    |> fold_rev forall_intr_rename (map fst oqs ~~ cqs)
krauss@34232
   719
  end
krauss@33099
   720
krauss@33099
   721
krauss@33099
   722
krauss@33099
   723
(** Termination rule **)
krauss@33099
   724
krauss@34232
   725
val wf_induct_rule = @{thm Wellfounded.wfP_induct_rule}
blanchet@55085
   726
val wf_in_rel = @{thm Fun_Def.wf_in_rel}
blanchet@55085
   727
val in_rel_def = @{thm Fun_Def.in_rel_def}
krauss@33099
   728
wenzelm@51717
   729
fun mk_nest_term_case ctxt globals R' ihyp clause =
krauss@34232
   730
  let
krauss@34232
   731
    val Globals {z, ...} = globals
krauss@34232
   732
    val ClauseInfo {cdata = ClauseContext {qs, cqs, ags, lhs, case_hyp, ...}, tree,
krauss@34232
   733
      qglr=(oqs, _, _, _), ...} = clause
krauss@33099
   734
wenzelm@51717
   735
    val ih_case = full_simplify (put_simpset HOL_basic_ss ctxt addsimps [case_hyp]) ihyp
krauss@33099
   736
krauss@34232
   737
    fun step (fixes, assumes) (_ $ arg) u (sub,(hyps,thms)) =
krauss@34232
   738
      let
krauss@34232
   739
        val used = (u @ sub)
wenzelm@59618
   740
          |> map (fn (ctx, thm) => Function_Context_Tree.export_thm ctxt ctx thm)
krauss@33099
   741
krauss@34232
   742
        val hyp = HOLogic.mk_Trueprop (R' $ arg $ lhs)
wenzelm@59582
   743
          |> fold_rev (curry Logic.mk_implies o Thm.prop_of) used (* additional hyps *)
wenzelm@58816
   744
          |> Function_Context_Tree.export_term (fixes, assumes)
wenzelm@59582
   745
          |> fold_rev (curry Logic.mk_implies o Thm.prop_of) ags
krauss@34232
   746
          |> fold_rev mk_forall_rename (map fst oqs ~~ qs)
wenzelm@59621
   747
          |> Thm.cterm_of ctxt
krauss@33099
   748
wenzelm@36945
   749
        val thm = Thm.assume hyp
wenzelm@36945
   750
          |> fold Thm.forall_elim cqs
krauss@34232
   751
          |> fold Thm.elim_implies ags
wenzelm@59618
   752
          |> Function_Context_Tree.import_thm ctxt (fixes, assumes)
krauss@34232
   753
          |> fold Thm.elim_implies used (*  "(arg, lhs) : R'"  *)
krauss@33099
   754
krauss@34232
   755
        val z_eq_arg = HOLogic.mk_Trueprop (mk_eq (z, arg))
wenzelm@59621
   756
          |> Thm.cterm_of ctxt |> Thm.assume
krauss@33099
   757
krauss@34232
   758
        val acc = thm COMP ih_case
krauss@34232
   759
        val z_acc_local = acc
wenzelm@36945
   760
          |> Conv.fconv_rule
wenzelm@36945
   761
              (Conv.arg_conv (Conv.arg_conv (K (Thm.symmetric (z_eq_arg RS eq_reflection)))))
krauss@33099
   762
krauss@34232
   763
        val ethm = z_acc_local
wenzelm@59618
   764
          |> Function_Context_Tree.export_thm ctxt (fixes,
krauss@34232
   765
               z_eq_arg :: case_hyp :: ags @ assumes)
krauss@34232
   766
          |> fold_rev forall_intr_rename (map fst oqs ~~ cqs)
krauss@33099
   767
krauss@34232
   768
        val sub' = sub @ [(([],[]), acc)]
krauss@34232
   769
      in
krauss@34232
   770
        (sub', (hyp :: hyps, ethm :: thms))
krauss@34232
   771
      end
krauss@34232
   772
      | step _ _ _ _ = raise Match
krauss@34232
   773
  in
wenzelm@58816
   774
    Function_Context_Tree.traverse_tree step tree
krauss@34232
   775
  end
krauss@33099
   776
krauss@33099
   777
wenzelm@51717
   778
fun mk_nest_term_rule ctxt globals R R_cases clauses =
krauss@34232
   779
  let
krauss@34232
   780
    val Globals { domT, x, z, ... } = globals
krauss@34232
   781
    val acc_R = mk_acc domT R
krauss@33099
   782
krauss@34232
   783
    val R' = Free ("R", fastype_of R)
krauss@33099
   784
krauss@34232
   785
    val Rrel = Free ("R", HOLogic.mk_setT (HOLogic.mk_prodT (domT, domT)))
blanchet@55085
   786
    val inrel_R = Const (@{const_name Fun_Def.in_rel},
krauss@34232
   787
      HOLogic.mk_setT (HOLogic.mk_prodT (domT, domT)) --> fastype_of R) $ Rrel
krauss@33099
   788
krauss@34232
   789
    val wfR' = HOLogic.mk_Trueprop (Const (@{const_name Wellfounded.wfP},
krauss@34232
   790
      (domT --> domT --> boolT) --> boolT) $ R')
wenzelm@59621
   791
      |> Thm.cterm_of ctxt (* "wf R'" *)
krauss@33099
   792
krauss@34232
   793
    (* Inductive Hypothesis: !!z. (z,x):R' ==> z : acc R *)
wenzelm@46217
   794
    val ihyp = Logic.all_const domT $ Abs ("z", domT,
krauss@34232
   795
      Logic.mk_implies (HOLogic.mk_Trueprop (R' $ Bound 0 $ x),
krauss@34232
   796
        HOLogic.mk_Trueprop (acc_R $ Bound 0)))
wenzelm@59621
   797
      |> Thm.cterm_of ctxt
krauss@33099
   798
wenzelm@36945
   799
    val ihyp_a = Thm.assume ihyp |> Thm.forall_elim_vars 0
krauss@33099
   800
wenzelm@59621
   801
    val R_z_x = Thm.cterm_of ctxt (HOLogic.mk_Trueprop (R $ z $ x))
krauss@33099
   802
wenzelm@51717
   803
    val (hyps, cases) = fold (mk_nest_term_case ctxt globals R' ihyp_a) clauses ([], [])
krauss@34232
   804
  in
krauss@34232
   805
    R_cases
wenzelm@59621
   806
    |> Thm.forall_elim (Thm.cterm_of ctxt z)
wenzelm@59621
   807
    |> Thm.forall_elim (Thm.cterm_of ctxt x)
wenzelm@59621
   808
    |> Thm.forall_elim (Thm.cterm_of ctxt (acc_R $ z))
wenzelm@36945
   809
    |> curry op COMP (Thm.assume R_z_x)
krauss@34232
   810
    |> fold_rev (curry op COMP) cases
wenzelm@36945
   811
    |> Thm.implies_intr R_z_x
wenzelm@59621
   812
    |> Thm.forall_intr (Thm.cterm_of ctxt z)
krauss@34232
   813
    |> (fn it => it COMP accI)
wenzelm@36945
   814
    |> Thm.implies_intr ihyp
wenzelm@59621
   815
    |> Thm.forall_intr (Thm.cterm_of ctxt x)
wenzelm@52467
   816
    |> (fn it => Drule.compose (it, 2, wf_induct_rule))
wenzelm@36945
   817
    |> curry op RS (Thm.assume wfR')
krauss@34232
   818
    |> forall_intr_vars
krauss@34232
   819
    |> (fn it => it COMP allI)
wenzelm@36945
   820
    |> fold Thm.implies_intr hyps
wenzelm@36945
   821
    |> Thm.implies_intr wfR'
wenzelm@59621
   822
    |> Thm.forall_intr (Thm.cterm_of ctxt R')
wenzelm@59621
   823
    |> Thm.forall_elim (Thm.cterm_of ctxt (inrel_R))
krauss@34232
   824
    |> curry op RS wf_in_rel
wenzelm@51717
   825
    |> full_simplify (put_simpset HOL_basic_ss ctxt addsimps [in_rel_def])
wenzelm@59621
   826
    |> Thm.forall_intr (Thm.cterm_of ctxt Rrel)
krauss@34232
   827
  end
krauss@33099
   828
krauss@33099
   829
krauss@33099
   830
krauss@33099
   831
fun prepare_function config defname [((fname, fT), mixfix)] abstract_qglrs lthy =
krauss@34232
   832
  let
krauss@41846
   833
    val FunctionConfig {domintros, default=default_opt, ...} = config
krauss@33099
   834
blanchet@55396
   835
    val default_str = the_default "%x. HOL.undefined" default_opt
krauss@34232
   836
    val fvar = Free (fname, fT)
krauss@34232
   837
    val domT = domain_type fT
krauss@34232
   838
    val ranT = range_type fT
krauss@33099
   839
krauss@34232
   840
    val default = Syntax.parse_term lthy default_str
wenzelm@39288
   841
      |> Type.constraint fT |> Syntax.check_term lthy
krauss@34232
   842
krauss@34232
   843
    val (globals, ctxt') = fix_globals domT ranT fvar lthy
krauss@33099
   844
krauss@34232
   845
    val Globals { x, h, ... } = globals
krauss@33099
   846
krauss@34232
   847
    val clauses = map (mk_clause_context x ctxt') abstract_qglrs
krauss@34232
   848
krauss@34232
   849
    val n = length abstract_qglrs
krauss@33099
   850
krauss@34232
   851
    fun build_tree (ClauseContext { ctxt, rhs, ...}) =
wenzelm@58816
   852
       Function_Context_Tree.mk_tree (fname, fT) h ctxt rhs
krauss@33099
   853
krauss@34232
   854
    val trees = map build_tree clauses
krauss@34232
   855
    val RCss = map find_calls trees
krauss@33099
   856
krauss@34232
   857
    val ((G, GIntro_thms, G_elim, G_induct), lthy) =
krauss@34232
   858
      PROFILE "def_graph" (define_graph (graph_name defname) fvar domT ranT clauses RCss) lthy
krauss@34232
   859
krauss@34232
   860
    val ((f, (_, f_defthm)), lthy) =
krauss@34232
   861
      PROFILE "def_fun" (define_function (defname ^ "_sumC_def") (fname, mixfix) domT ranT G default) lthy
krauss@33099
   862
wenzelm@42361
   863
    val RCss = map (map (inst_RC (Proof_Context.theory_of lthy) fvar f)) RCss
wenzelm@59618
   864
    val trees = map (Function_Context_Tree.inst_tree lthy fvar f) trees
krauss@33099
   865
krauss@34232
   866
    val ((R, RIntro_thmss, R_elim), lthy) =
krauss@34232
   867
      PROFILE "def_rel" (define_recursion_relation (rel_name defname) domT abstract_qglrs clauses RCss) lthy
krauss@33099
   868
krauss@52384
   869
    val dom = mk_acc domT R
krauss@34232
   870
    val (_, lthy) =
krauss@52384
   871
      Local_Theory.abbrev Syntax.mode_default ((Binding.name (dom_name defname), NoSyn), dom) lthy
krauss@33099
   872
wenzelm@42361
   873
    val newthy = Proof_Context.theory_of lthy
krauss@34232
   874
    val clauses = map (transfer_clause_ctx newthy) clauses
krauss@33099
   875
krauss@34232
   876
    val xclauses = PROFILE "xclauses"
wenzelm@58634
   877
      (@{map 7} (mk_clause_info globals G f) (1 upto n) clauses abstract_qglrs trees
krauss@34232
   878
        RCss GIntro_thms) RIntro_thmss
krauss@33099
   879
krauss@34232
   880
    val complete =
wenzelm@59621
   881
      mk_completeness globals clauses abstract_qglrs |> Thm.cterm_of lthy |> Thm.assume
krauss@33099
   882
krauss@34232
   883
    val compat =
krauss@34232
   884
      mk_compat_proof_obligations domT ranT fvar f abstract_qglrs
wenzelm@59621
   885
      |> map (Thm.cterm_of lthy #> Thm.assume)
krauss@33099
   886
krauss@34232
   887
    val compat_store = store_compat_thms n compat
krauss@33099
   888
krauss@34232
   889
    val (goalstate, values) = PROFILE "prove_stuff"
krauss@34232
   890
      (prove_stuff lthy globals G f R xclauses complete compat
krauss@34232
   891
         compat_store G_elim) f_defthm
krauss@34232
   892
krauss@34232
   893
    fun mk_partial_rules provedgoal =
krauss@34232
   894
      let
wenzelm@59582
   895
        val newthy = Thm.theory_of_thm provedgoal (*FIXME*)
wenzelm@51717
   896
        val newctxt = Proof_Context.init_global newthy (*FIXME*)
krauss@33099
   897
krauss@34232
   898
        val (graph_is_function, complete_thm) =
krauss@34232
   899
          provedgoal
krauss@34232
   900
          |> Conjunction.elim
krauss@34232
   901
          |> apfst (Thm.forall_elim_vars 0)
krauss@33099
   902
krauss@34232
   903
        val f_iff = graph_is_function RS (f_defthm RS ex1_implies_iff)
krauss@34232
   904
krauss@34232
   905
        val psimps = PROFILE "Proving simplification rules"
wenzelm@51717
   906
          (mk_psimps newctxt globals R xclauses values f_iff) graph_is_function
krauss@33099
   907
krauss@34232
   908
        val simple_pinduct = PROFILE "Proving partial induction rule"
krauss@34232
   909
          (mk_partial_induct_rule newthy globals R complete_thm) xclauses
krauss@33099
   910
krauss@34232
   911
        val total_intro = PROFILE "Proving nested termination rule"
wenzelm@51717
   912
          (mk_nest_term_rule newctxt globals R R_elim) xclauses
krauss@33099
   913
krauss@34232
   914
        val dom_intros =
krauss@34232
   915
          if domintros then SOME (PROFILE "Proving domain introduction rules"
krauss@34232
   916
             (map (mk_domain_intro lthy globals R R_elim)) xclauses)
krauss@34232
   917
           else NONE
krauss@34232
   918
      in
Manuel@53603
   919
        FunctionResult {fs=[f], G=G, R=R, dom=dom,
Manuel@53603
   920
          cases=[complete_thm], psimps=psimps, pelims=[],
Manuel@53603
   921
          simple_pinducts=[simple_pinduct],
krauss@41846
   922
          termination=total_intro, domintros=dom_intros}
krauss@34232
   923
      end
krauss@34232
   924
  in
krauss@34232
   925
    ((f, goalstate, mk_partial_rules), lthy)
krauss@34232
   926
  end
krauss@33099
   927
krauss@33099
   928
krauss@33099
   929
end