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