src/HOL/Library/old_recdef.ML
author wenzelm
Fri, 19 Jun 2015 19:13:15 +0200
changeset 60520 09fc5eaa21ce
child 60521 52e956416fbf
permissions -rw-r--r--
moved sources;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Tools/old_recdef.ML
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
     2
    Author:     Konrad Slind, Cambridge University Computer Laboratory
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
     3
    Author:     Lucas Dixon, University of Edinburgh
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
     4
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
     5
Old TFL/recdef package.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
     6
*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
     7
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
     8
signature CASE_SPLIT =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
     9
sig
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    10
  (* try to recursively split conjectured thm to given list of thms *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    11
  val splitto : Proof.context -> thm list -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    12
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    13
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    14
signature UTILS =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    15
sig
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    16
  exception ERR of {module: string, func: string, mesg: string}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    17
  val end_itlist: ('a -> 'a -> 'a) -> 'a list -> 'a
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    18
  val itlist2: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    19
  val pluck: ('a -> bool) -> 'a list -> 'a * 'a list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    20
  val zip3: 'a list -> 'b list -> 'c list -> ('a*'b*'c) list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    21
  val take: ('a -> 'b) -> int * 'a list -> 'b list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    22
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    23
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    24
signature USYNTAX =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    25
sig
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    26
  datatype lambda = VAR   of {Name : string, Ty : typ}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    27
                  | CONST of {Name : string, Ty : typ}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    28
                  | COMB  of {Rator: term, Rand : term}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    29
                  | LAMB  of {Bvar : term, Body : term}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    30
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    31
  val alpha : typ
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    32
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    33
  (* Types *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    34
  val type_vars  : typ -> typ list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    35
  val type_varsl : typ list -> typ list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    36
  val mk_vartype : string -> typ
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    37
  val is_vartype : typ -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    38
  val strip_prod_type : typ -> typ list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    39
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    40
  (* Terms *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    41
  val free_vars_lr : term -> term list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    42
  val type_vars_in_term : term -> typ list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    43
  val dest_term  : term -> lambda
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    44
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    45
  (* Prelogic *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    46
  val inst      : (typ*typ) list -> term -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    47
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    48
  (* Construction routines *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    49
  val mk_abs    :{Bvar  : term, Body : term} -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    50
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    51
  val mk_imp    :{ant : term, conseq :  term} -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    52
  val mk_select :{Bvar : term, Body : term} -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    53
  val mk_forall :{Bvar : term, Body : term} -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    54
  val mk_exists :{Bvar : term, Body : term} -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    55
  val mk_conj   :{conj1 : term, conj2 : term} -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    56
  val mk_disj   :{disj1 : term, disj2 : term} -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    57
  val mk_pabs   :{varstruct : term, body : term} -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    58
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    59
  (* Destruction routines *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    60
  val dest_const: term -> {Name : string, Ty : typ}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    61
  val dest_comb : term -> {Rator : term, Rand : term}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    62
  val dest_abs  : string list -> term -> {Bvar : term, Body : term} * string list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    63
  val dest_eq     : term -> {lhs : term, rhs : term}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    64
  val dest_imp    : term -> {ant : term, conseq : term}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    65
  val dest_forall : term -> {Bvar : term, Body : term}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    66
  val dest_exists : term -> {Bvar : term, Body : term}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    67
  val dest_neg    : term -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    68
  val dest_conj   : term -> {conj1 : term, conj2 : term}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    69
  val dest_disj   : term -> {disj1 : term, disj2 : term}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    70
  val dest_pair   : term -> {fst : term, snd : term}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    71
  val dest_pabs   : string list -> term -> {varstruct : term, body : term, used : string list}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    72
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    73
  val lhs   : term -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    74
  val rhs   : term -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    75
  val rand  : term -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    76
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    77
  (* Query routines *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    78
  val is_imp    : term -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    79
  val is_forall : term -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    80
  val is_exists : term -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    81
  val is_neg    : term -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    82
  val is_conj   : term -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    83
  val is_disj   : term -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    84
  val is_pair   : term -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    85
  val is_pabs   : term -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    86
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    87
  (* Construction of a term from a list of Preterms *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    88
  val list_mk_abs    : (term list * term) -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    89
  val list_mk_imp    : (term list * term) -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    90
  val list_mk_forall : (term list * term) -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    91
  val list_mk_conj   : term list -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    92
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    93
  (* Destructing a term to a list of Preterms *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    94
  val strip_comb     : term -> (term * term list)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    95
  val strip_abs      : term -> (term list * term)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    96
  val strip_imp      : term -> (term list * term)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    97
  val strip_forall   : term -> (term list * term)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    98
  val strip_exists   : term -> (term list * term)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
    99
  val strip_disj     : term -> term list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   100
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   101
  (* Miscellaneous *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   102
  val mk_vstruct : typ -> term list -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   103
  val gen_all    : term -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   104
  val find_term  : (term -> bool) -> term -> term option
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   105
  val dest_relation : term -> term * term * term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   106
  val is_WFR : term -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   107
  val ARB : typ -> term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   108
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   109
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   110
signature DCTERM =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   111
sig
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   112
  val dest_comb: cterm -> cterm * cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   113
  val dest_abs: string option -> cterm -> cterm * cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   114
  val capply: cterm -> cterm -> cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   115
  val cabs: cterm -> cterm -> cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   116
  val mk_conj: cterm * cterm -> cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   117
  val mk_disj: cterm * cterm -> cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   118
  val mk_exists: cterm * cterm -> cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   119
  val dest_conj: cterm -> cterm * cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   120
  val dest_const: cterm -> {Name: string, Ty: typ}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   121
  val dest_disj: cterm -> cterm * cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   122
  val dest_eq: cterm -> cterm * cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   123
  val dest_exists: cterm -> cterm * cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   124
  val dest_forall: cterm -> cterm * cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   125
  val dest_imp: cterm -> cterm * cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   126
  val dest_neg: cterm -> cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   127
  val dest_pair: cterm -> cterm * cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   128
  val dest_var: cterm -> {Name:string, Ty:typ}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   129
  val is_conj: cterm -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   130
  val is_disj: cterm -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   131
  val is_eq: cterm -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   132
  val is_exists: cterm -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   133
  val is_forall: cterm -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   134
  val is_imp: cterm -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   135
  val is_neg: cterm -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   136
  val is_pair: cterm -> bool
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   137
  val list_mk_disj: cterm list -> cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   138
  val strip_abs: cterm -> cterm list * cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   139
  val strip_comb: cterm -> cterm * cterm list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   140
  val strip_disj: cterm -> cterm list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   141
  val strip_exists: cterm -> cterm list * cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   142
  val strip_forall: cterm -> cterm list * cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   143
  val strip_imp: cterm -> cterm list * cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   144
  val drop_prop: cterm -> cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   145
  val mk_prop: cterm -> cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   146
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   147
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   148
signature RULES =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   149
sig
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   150
  val dest_thm: thm -> term list * term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   151
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   152
  (* Inference rules *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   153
  val REFL: cterm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   154
  val ASSUME: cterm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   155
  val MP: thm -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   156
  val MATCH_MP: thm -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   157
  val CONJUNCT1: thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   158
  val CONJUNCT2: thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   159
  val CONJUNCTS: thm -> thm list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   160
  val DISCH: cterm -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   161
  val UNDISCH: thm  -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   162
  val SPEC: cterm -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   163
  val ISPEC: cterm -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   164
  val ISPECL: cterm list -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   165
  val GEN: Proof.context -> cterm -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   166
  val GENL: Proof.context -> cterm list -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   167
  val LIST_CONJ: thm list -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   168
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   169
  val SYM: thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   170
  val DISCH_ALL: thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   171
  val FILTER_DISCH_ALL: (term -> bool) -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   172
  val SPEC_ALL: thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   173
  val GEN_ALL: Proof.context -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   174
  val IMP_TRANS: thm -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   175
  val PROVE_HYP: thm -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   176
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   177
  val CHOOSE: Proof.context -> cterm * thm -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   178
  val EXISTS: cterm * cterm -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   179
  val EXISTL: cterm list -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   180
  val IT_EXISTS: Proof.context -> (cterm * cterm) list -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   181
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   182
  val EVEN_ORS: thm list -> thm list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   183
  val DISJ_CASESL: thm -> thm list -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   184
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   185
  val list_beta_conv: cterm -> cterm list -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   186
  val SUBS: Proof.context -> thm list -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   187
  val simpl_conv: Proof.context -> thm list -> cterm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   188
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   189
  val rbeta: thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   190
  val tracing: bool Unsynchronized.ref
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   191
  val CONTEXT_REWRITE_RULE: Proof.context ->
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   192
    term * term list * thm * thm list -> thm -> thm * term list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   193
  val RIGHT_ASSOC: Proof.context -> thm -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   194
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   195
  val prove: Proof.context -> bool -> term * tactic -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   196
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   197
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   198
signature THRY =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   199
sig
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   200
  val match_term: theory -> term -> term -> (term * term) list * (typ * typ) list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   201
  val match_type: theory -> typ -> typ -> (typ * typ) list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   202
  val typecheck: theory -> term -> cterm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   203
  (*datatype facts of various flavours*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   204
  val match_info: theory -> string -> {constructors: term list, case_const: term} option
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   205
  val induct_info: theory -> string -> {constructors: term list, nchotomy: thm} option
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   206
  val extract_info: theory -> {case_congs: thm list, case_rewrites: thm list}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   207
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   208
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   209
signature PRIM =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   210
sig
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   211
  val trace: bool Unsynchronized.ref
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   212
  val trace_thms: Proof.context -> string -> thm list -> unit
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   213
  val trace_cterm: Proof.context -> string -> cterm -> unit
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   214
  type pattern
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   215
  val mk_functional: theory -> term list -> {functional: term, pats: pattern list}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   216
  val wfrec_definition0: string -> term -> term -> theory -> thm * theory
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   217
  val post_definition: Proof.context -> thm list -> thm * pattern list ->
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   218
   {rules: thm,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   219
    rows: int list,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   220
    TCs: term list list,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   221
    full_pats_TCs: (term * term list) list}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   222
  val wfrec_eqns: theory -> xstring -> thm list -> term list ->
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   223
   {WFR: term,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   224
    SV: term list,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   225
    proto_def: term,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   226
    extracta: (thm * term list) list,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   227
    pats: pattern list}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   228
  val lazyR_def: theory -> xstring -> thm list -> term list ->
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   229
   {theory: theory,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   230
    rules: thm,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   231
    R: term,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   232
    SV: term list,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   233
    full_pats_TCs: (term * term list) list,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   234
    patterns : pattern list}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   235
  val mk_induction: theory ->
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   236
    {fconst: term, R: term, SV: term list, pat_TCs_list: (term * term list) list} -> thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   237
  val postprocess: Proof.context -> bool ->
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   238
    {wf_tac: tactic, terminator: tactic, simplifier: cterm -> thm} ->
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   239
    {rules: thm, induction: thm, TCs: term list list} ->
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   240
    {rules: thm, induction: thm, nested_tcs: thm list}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   241
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   242
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   243
signature TFL =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   244
sig
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   245
  val define_i: bool -> thm list -> thm list -> xstring -> term -> term list -> Proof.context ->
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   246
    {lhs: term, rules: (thm * int) list, induct: thm, tcs: term list} * Proof.context
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   247
  val define: bool -> thm list -> thm list -> xstring -> string -> string list -> Proof.context ->
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   248
    {lhs: term, rules: (thm * int) list, induct: thm, tcs: term list} * Proof.context
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   249
  val defer_i: thm list -> xstring -> term list -> theory -> thm * theory
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   250
  val defer: thm list -> xstring -> string list -> theory -> thm * theory
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   251
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   252
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   253
signature OLD_RECDEF =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   254
sig
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   255
  val get_recdef: theory -> string
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   256
    -> {lhs: term, simps: thm list, rules: thm list list, induct: thm, tcs: term list} option
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   257
  val get_hints: Proof.context -> {simps: thm list, congs: (string * thm) list, wfs: thm list}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   258
  val simp_add: attribute
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   259
  val simp_del: attribute
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   260
  val cong_add: attribute
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   261
  val cong_del: attribute
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   262
  val wf_add: attribute
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   263
  val wf_del: attribute
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   264
  val add_recdef: bool -> xstring -> string -> ((binding * string) * Token.src list) list ->
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   265
    Token.src option -> theory -> theory
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   266
      * {lhs: term, simps: thm list, rules: thm list list, induct: thm, tcs: term list}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   267
  val add_recdef_i: bool -> xstring -> term -> ((binding * term) * attribute list) list ->
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   268
    theory -> theory * {lhs: term, simps: thm list, rules: thm list list, induct: thm, tcs: term list}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   269
  val defer_recdef: xstring -> string list -> (Facts.ref * Token.src list) list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   270
    -> theory -> theory * {induct_rules: thm}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   271
  val defer_recdef_i: xstring -> term list -> thm list -> theory -> theory * {induct_rules: thm}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   272
  val recdef_tc: bstring * Token.src list -> xstring -> int option -> bool ->
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   273
    local_theory -> Proof.state
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   274
  val recdef_tc_i: bstring * Token.src list -> string -> int option -> bool ->
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   275
    local_theory -> Proof.state
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   276
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   277
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   278
structure Old_Recdef: OLD_RECDEF =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   279
struct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   280
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   281
(*** extra case splitting for TFL ***)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   282
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   283
structure CaseSplit: CASE_SPLIT =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   284
struct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   285
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   286
(* make a casethm from an induction thm *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   287
val cases_thm_of_induct_thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   288
     Seq.hd o (ALLGOALS (fn i => REPEAT (etac Drule.thin_rl i)));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   289
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   290
(* get the case_thm (my version) from a type *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   291
fun case_thm_of_ty thy ty  =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   292
    let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   293
      val ty_str = case ty of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   294
                     Type(ty_str, _) => ty_str
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   295
                   | TFree(s,_)  => error ("Free type: " ^ s)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   296
                   | TVar((s,i),_) => error ("Free variable: " ^ s)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   297
      val {induct, ...} = BNF_LFP_Compat.the_info thy [BNF_LFP_Compat.Keep_Nesting] ty_str
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   298
    in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   299
      cases_thm_of_induct_thm induct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   300
    end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   301
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   302
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   303
(* for use when there are no prems to the subgoal *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   304
(* does a case split on the given variable *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   305
fun mk_casesplit_goal_thm ctxt (vstr,ty) gt =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   306
    let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   307
      val thy = Proof_Context.theory_of ctxt;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   308
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   309
      val x = Free(vstr,ty);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   310
      val abst = Abs(vstr, ty, Term.abstract_over (x, gt));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   311
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   312
      val case_thm = case_thm_of_ty thy ty;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   313
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   314
      val abs_ct = Thm.cterm_of ctxt abst;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   315
      val free_ct = Thm.cterm_of ctxt x;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   316
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   317
      val (Pv, Dv, type_insts) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   318
          case (Thm.concl_of case_thm) of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   319
            (_ $ (Pv $ (Dv as Var(D, Dty)))) =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   320
            (Pv, Dv,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   321
             Sign.typ_match thy (Dty, ty) Vartab.empty)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   322
          | _ => error "not a valid case thm";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   323
      val type_cinsts = map (fn (ixn, (S, T)) => apply2 (Thm.ctyp_of ctxt) (TVar (ixn, S), T))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   324
        (Vartab.dest type_insts);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   325
      val cPv = Thm.cterm_of ctxt (Envir.subst_term_types type_insts Pv);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   326
      val cDv = Thm.cterm_of ctxt (Envir.subst_term_types type_insts Dv);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   327
    in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   328
      Conv.fconv_rule Drule.beta_eta_conversion
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   329
         (case_thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   330
            |> Thm.instantiate (type_cinsts, [])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   331
            |> Thm.instantiate ([], [(cPv, abs_ct), (cDv, free_ct)]))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   332
    end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   333
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   334
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   335
(* the find_XXX_split functions are simply doing a lightwieght (I
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   336
think) term matching equivalent to find where to do the next split *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   337
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   338
(* assuming two twems are identical except for a free in one at a
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   339
subterm, or constant in another, ie assume that one term is a plit of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   340
another, then gives back the free variable that has been split. *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   341
exception find_split_exp of string
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   342
fun find_term_split (Free v, _ $ _) = SOME v
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   343
  | find_term_split (Free v, Const _) = SOME v
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   344
  | find_term_split (Free v, Abs _) = SOME v (* do we really want this case? *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   345
  | find_term_split (Free v, Var _) = NONE (* keep searching *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   346
  | find_term_split (a $ b, a2 $ b2) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   347
    (case find_term_split (a, a2) of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   348
       NONE => find_term_split (b,b2)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   349
     | vopt => vopt)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   350
  | find_term_split (Abs(_,ty,t1), Abs(_,ty2,t2)) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   351
    find_term_split (t1, t2)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   352
  | find_term_split (Const (x,ty), Const(x2,ty2)) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   353
    if x = x2 then NONE else (* keep searching *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   354
    raise find_split_exp (* stop now *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   355
            "Terms are not identical upto a free varaible! (Consts)"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   356
  | find_term_split (Bound i, Bound j) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   357
    if i = j then NONE else (* keep searching *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   358
    raise find_split_exp (* stop now *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   359
            "Terms are not identical upto a free varaible! (Bound)"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   360
  | find_term_split _ =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   361
    raise find_split_exp (* stop now *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   362
            "Terms are not identical upto a free varaible! (Other)";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   363
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   364
(* assume that "splitth" is a case split form of subgoal i of "genth",
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   365
then look for a free variable to split, breaking the subgoal closer to
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   366
splitth. *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   367
fun find_thm_split splitth i genth =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   368
    find_term_split (Logic.get_goal (Thm.prop_of genth) i,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   369
                     Thm.concl_of splitth) handle find_split_exp _ => NONE;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   370
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   371
(* as above but searches "splitths" for a theorem that suggest a case split *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   372
fun find_thms_split splitths i genth =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   373
    Library.get_first (fn sth => find_thm_split sth i genth) splitths;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   374
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   375
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   376
(* split the subgoal i of "genth" until we get to a member of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   377
splitths. Assumes that genth will be a general form of splitths, that
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   378
can be case-split, as needed. Otherwise fails. Note: We assume that
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   379
all of "splitths" are split to the same level, and thus it doesn't
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   380
matter which one we choose to look for the next split. Simply add
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   381
search on splitthms and split variable, to change this.  *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   382
(* Note: possible efficiency measure: when a case theorem is no longer
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   383
useful, drop it? *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   384
(* Note: This should not be a separate tactic but integrated into the
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   385
case split done during recdef's case analysis, this would avoid us
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   386
having to (re)search for variables to split. *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   387
fun splitto ctxt splitths genth =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   388
    let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   389
      val _ = not (null splitths) orelse error "splitto: no given splitths";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   390
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   391
      (* check if we are a member of splitths - FIXME: quicker and
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   392
      more flexible with discrim net. *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   393
      fun solve_by_splitth th split =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   394
        Thm.biresolution (SOME ctxt) false [(false,split)] 1 th;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   395
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   396
      fun split th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   397
        (case find_thms_split splitths 1 th of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   398
          NONE =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   399
           (writeln (cat_lines
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   400
            (["th:", Display.string_of_thm ctxt th, "split ths:"] @
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   401
              map (Display.string_of_thm ctxt) splitths @ ["\n--"]));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   402
            error "splitto: cannot find variable to split on")
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   403
        | SOME v =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   404
            let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   405
              val gt = HOLogic.dest_Trueprop (#1 (Logic.dest_implies (Thm.prop_of th)));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   406
              val split_thm = mk_casesplit_goal_thm ctxt v gt;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   407
              val (subthms, expf) = IsaND.fixed_subgoal_thms ctxt split_thm;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   408
            in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   409
              expf (map recsplitf subthms)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   410
            end)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   411
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   412
      and recsplitf th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   413
        (* note: multiple unifiers! we only take the first element,
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   414
           probably fine -- there is probably only one anyway. *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   415
        (case get_first (Seq.pull o solve_by_splitth th) splitths of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   416
          NONE => split th
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   417
        | SOME (solved_th, _) => solved_th);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   418
    in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   419
      recsplitf genth
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   420
    end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   421
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   422
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   423
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   424
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   425
(*** basic utilities ***)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   426
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   427
structure Utils: UTILS =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   428
struct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   429
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   430
(*standard exception for TFL*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   431
exception ERR of {module: string, func: string, mesg: string};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   432
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   433
fun UTILS_ERR func mesg = ERR {module = "Utils", func = func, mesg = mesg};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   434
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   435
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   436
fun end_itlist f [] = raise (UTILS_ERR "end_itlist" "list too short")
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   437
  | end_itlist f [x] = x
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   438
  | end_itlist f (x :: xs) = f x (end_itlist f xs);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   439
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   440
fun itlist2 f L1 L2 base_value =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   441
 let fun it ([],[]) = base_value
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   442
       | it ((a::rst1),(b::rst2)) = f a b (it (rst1,rst2))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   443
       | it _ = raise UTILS_ERR "itlist2" "different length lists"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   444
 in  it (L1,L2)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   445
 end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   446
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   447
fun pluck p  =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   448
  let fun remv ([],_) = raise UTILS_ERR "pluck" "item not found"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   449
        | remv (h::t, A) = if p h then (h, rev A @ t) else remv (t,h::A)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   450
  in fn L => remv(L,[])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   451
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   452
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   453
fun take f =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   454
  let fun grab(0,L) = []
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   455
        | grab(n, x::rst) = f x::grab(n-1,rst)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   456
  in grab
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   457
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   458
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   459
fun zip3 [][][] = []
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   460
  | zip3 (x::l1) (y::l2) (z::l3) = (x,y,z)::zip3 l1 l2 l3
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   461
  | zip3 _ _ _ = raise UTILS_ERR "zip3" "different lengths";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   462
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   463
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   464
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   465
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   466
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   467
(*** emulation of HOL's abstract syntax functions ***)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   468
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   469
structure USyntax: USYNTAX =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   470
struct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   471
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   472
infix 4 ##;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   473
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   474
fun USYN_ERR func mesg = Utils.ERR {module = "USyntax", func = func, mesg = mesg};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   475
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   476
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   477
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   478
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   479
 *                            Types
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   480
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   481
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   482
val mk_prim_vartype = TVar;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   483
fun mk_vartype s = mk_prim_vartype ((s, 0), @{sort type});
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   484
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   485
(* But internally, it's useful *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   486
fun dest_vtype (TVar x) = x
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   487
  | dest_vtype _ = raise USYN_ERR "dest_vtype" "not a flexible type variable";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   488
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   489
val is_vartype = can dest_vtype;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   490
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   491
val type_vars  = map mk_prim_vartype o Misc_Legacy.typ_tvars
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   492
fun type_varsl L = distinct (op =) (fold (curry op @ o type_vars) L []);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   493
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   494
val alpha  = mk_vartype "'a"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   495
val beta   = mk_vartype "'b"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   496
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   497
val strip_prod_type = HOLogic.flatten_tupleT;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   498
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   499
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   500
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   501
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   502
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   503
 *                              Terms
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   504
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   505
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   506
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   507
(* Free variables, in order of occurrence, from left to right in the
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   508
 * syntax tree. *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   509
fun free_vars_lr tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   510
  let fun memb x = let fun m[] = false | m(y::rst) = (x=y)orelse m rst in m end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   511
      fun add (t, frees) = case t of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   512
            Free   _ => if (memb t frees) then frees else t::frees
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   513
          | Abs (_,_,body) => add(body,frees)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   514
          | f$t =>  add(t, add(f, frees))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   515
          | _ => frees
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   516
  in rev(add(tm,[]))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   517
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   518
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   519
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   521
val type_vars_in_term = map mk_prim_vartype o Misc_Legacy.term_tvars;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   522
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   523
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   524
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   525
(* Prelogic *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   526
fun dest_tybinding (v,ty) = (#1(dest_vtype v),ty)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   527
fun inst theta = subst_vars (map dest_tybinding theta,[])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   528
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   529
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   530
(* Construction routines *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   531
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   532
fun mk_abs{Bvar as Var((s,_),ty),Body}  = Abs(s,ty,abstract_over(Bvar,Body))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   533
  | mk_abs{Bvar as Free(s,ty),Body}  = Abs(s,ty,abstract_over(Bvar,Body))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   534
  | mk_abs _ = raise USYN_ERR "mk_abs" "Bvar is not a variable";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   535
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   536
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   537
fun mk_imp{ant,conseq} =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   538
   let val c = Const(@{const_name HOL.implies},HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   539
   in list_comb(c,[ant,conseq])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   540
   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   541
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   542
fun mk_select (r as {Bvar,Body}) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   543
  let val ty = type_of Bvar
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   544
      val c = Const(@{const_name Eps},(ty --> HOLogic.boolT) --> ty)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   545
  in list_comb(c,[mk_abs r])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   546
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   547
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   548
fun mk_forall (r as {Bvar,Body}) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   549
  let val ty = type_of Bvar
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   550
      val c = Const(@{const_name All},(ty --> HOLogic.boolT) --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   551
  in list_comb(c,[mk_abs r])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   552
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   553
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   554
fun mk_exists (r as {Bvar,Body}) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   555
  let val ty = type_of Bvar
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   556
      val c = Const(@{const_name Ex},(ty --> HOLogic.boolT) --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   557
  in list_comb(c,[mk_abs r])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   558
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   559
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   560
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   561
fun mk_conj{conj1,conj2} =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   562
   let val c = Const(@{const_name HOL.conj},HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   563
   in list_comb(c,[conj1,conj2])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   564
   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   565
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   566
fun mk_disj{disj1,disj2} =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   567
   let val c = Const(@{const_name HOL.disj},HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   568
   in list_comb(c,[disj1,disj2])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   569
   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   570
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   571
fun prod_ty ty1 ty2 = HOLogic.mk_prodT (ty1,ty2);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   572
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   573
local
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   574
fun mk_uncurry (xt, yt, zt) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   575
    Const(@{const_name case_prod}, (xt --> yt --> zt) --> prod_ty xt yt --> zt)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   576
fun dest_pair(Const(@{const_name Pair},_) $ M $ N) = {fst=M, snd=N}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   577
  | dest_pair _ = raise USYN_ERR "dest_pair" "not a pair"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   578
fun is_var (Var _) = true | is_var (Free _) = true | is_var _ = false
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   579
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   580
fun mk_pabs{varstruct,body} =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   581
 let fun mpa (varstruct, body) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   582
       if is_var varstruct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   583
       then mk_abs {Bvar = varstruct, Body = body}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   584
       else let val {fst, snd} = dest_pair varstruct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   585
            in mk_uncurry (type_of fst, type_of snd, type_of body) $
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   586
               mpa (fst, mpa (snd, body))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   587
            end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   588
 in mpa (varstruct, body) end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   589
 handle TYPE _ => raise USYN_ERR "mk_pabs" "";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   590
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   591
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   592
(* Destruction routines *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   593
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   594
datatype lambda = VAR   of {Name : string, Ty : typ}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   595
                | CONST of {Name : string, Ty : typ}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   596
                | COMB  of {Rator: term, Rand : term}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   597
                | LAMB  of {Bvar : term, Body : term};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   598
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   599
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   600
fun dest_term(Var((s,i),ty)) = VAR{Name = s, Ty = ty}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   601
  | dest_term(Free(s,ty))    = VAR{Name = s, Ty = ty}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   602
  | dest_term(Const(s,ty))   = CONST{Name = s, Ty = ty}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   603
  | dest_term(M$N)           = COMB{Rator=M,Rand=N}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   604
  | dest_term(Abs(s,ty,M))   = let  val v = Free(s,ty)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   605
                               in LAMB{Bvar = v, Body = Term.betapply (M,v)}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   606
                               end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   607
  | dest_term(Bound _)       = raise USYN_ERR "dest_term" "Bound";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   608
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   609
fun dest_const(Const(s,ty)) = {Name = s, Ty = ty}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   610
  | dest_const _ = raise USYN_ERR "dest_const" "not a constant";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   611
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   612
fun dest_comb(t1 $ t2) = {Rator = t1, Rand = t2}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   613
  | dest_comb _ =  raise USYN_ERR "dest_comb" "not a comb";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   614
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   615
fun dest_abs used (a as Abs(s, ty, M)) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   616
     let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   617
       val s' = singleton (Name.variant_list used) s;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   618
       val v = Free(s', ty);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   619
     in ({Bvar = v, Body = Term.betapply (a,v)}, s'::used)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   620
     end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   621
  | dest_abs _ _ =  raise USYN_ERR "dest_abs" "not an abstraction";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   622
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   623
fun dest_eq(Const(@{const_name HOL.eq},_) $ M $ N) = {lhs=M, rhs=N}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   624
  | dest_eq _ = raise USYN_ERR "dest_eq" "not an equality";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   625
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   626
fun dest_imp(Const(@{const_name HOL.implies},_) $ M $ N) = {ant=M, conseq=N}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   627
  | dest_imp _ = raise USYN_ERR "dest_imp" "not an implication";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   628
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   629
fun dest_forall(Const(@{const_name All},_) $ (a as Abs _)) = fst (dest_abs [] a)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   630
  | dest_forall _ = raise USYN_ERR "dest_forall" "not a forall";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   631
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   632
fun dest_exists(Const(@{const_name Ex},_) $ (a as Abs _)) = fst (dest_abs [] a)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   633
  | dest_exists _ = raise USYN_ERR "dest_exists" "not an existential";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   634
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   635
fun dest_neg(Const(@{const_name Not},_) $ M) = M
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   636
  | dest_neg _ = raise USYN_ERR "dest_neg" "not a negation";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   637
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   638
fun dest_conj(Const(@{const_name HOL.conj},_) $ M $ N) = {conj1=M, conj2=N}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   639
  | dest_conj _ = raise USYN_ERR "dest_conj" "not a conjunction";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   640
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   641
fun dest_disj(Const(@{const_name HOL.disj},_) $ M $ N) = {disj1=M, disj2=N}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   642
  | dest_disj _ = raise USYN_ERR "dest_disj" "not a disjunction";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   643
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   644
fun mk_pair{fst,snd} =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   645
   let val ty1 = type_of fst
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   646
       val ty2 = type_of snd
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   647
       val c = Const(@{const_name Pair},ty1 --> ty2 --> prod_ty ty1 ty2)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   648
   in list_comb(c,[fst,snd])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   649
   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   650
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   651
fun dest_pair(Const(@{const_name Pair},_) $ M $ N) = {fst=M, snd=N}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   652
  | dest_pair _ = raise USYN_ERR "dest_pair" "not a pair";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   653
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   654
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   655
local  fun ucheck t = (if #Name (dest_const t) = @{const_name case_prod} then t
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   656
                       else raise Match)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   657
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   658
fun dest_pabs used tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   659
   let val ({Bvar,Body}, used') = dest_abs used tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   660
   in {varstruct = Bvar, body = Body, used = used'}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   661
   end handle Utils.ERR _ =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   662
          let val {Rator,Rand} = dest_comb tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   663
              val _ = ucheck Rator
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   664
              val {varstruct = lv, body, used = used'} = dest_pabs used Rand
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   665
              val {varstruct = rv, body, used = used''} = dest_pabs used' body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   666
          in {varstruct = mk_pair {fst = lv, snd = rv}, body = body, used = used''}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   667
          end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   668
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   669
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   670
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   671
val lhs   = #lhs o dest_eq
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   672
val rhs   = #rhs o dest_eq
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   673
val rand  = #Rand o dest_comb
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   674
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   675
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   676
(* Query routines *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   677
val is_imp    = can dest_imp
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   678
val is_forall = can dest_forall
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   679
val is_exists = can dest_exists
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   680
val is_neg    = can dest_neg
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   681
val is_conj   = can dest_conj
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   682
val is_disj   = can dest_disj
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   683
val is_pair   = can dest_pair
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   684
val is_pabs   = can (dest_pabs [])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   685
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   686
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   687
(* Construction of a cterm from a list of Terms *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   688
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   689
fun list_mk_abs(L,tm) = fold_rev (fn v => fn M => mk_abs{Bvar=v, Body=M}) L tm;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   690
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   691
(* These others are almost never used *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   692
fun list_mk_imp(A,c) = fold_rev (fn a => fn tm => mk_imp{ant=a,conseq=tm}) A c;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   693
fun list_mk_forall(V,t) = fold_rev (fn v => fn b => mk_forall{Bvar=v, Body=b})V t;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   694
val list_mk_conj = Utils.end_itlist(fn c1 => fn tm => mk_conj{conj1=c1, conj2=tm})
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   695
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   696
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   697
(* Need to reverse? *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   698
fun gen_all tm = list_mk_forall(Misc_Legacy.term_frees tm, tm);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   699
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   700
(* Destructing a cterm to a list of Terms *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   701
fun strip_comb tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   702
   let fun dest(M$N, A) = dest(M, N::A)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   703
         | dest x = x
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   704
   in dest(tm,[])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   705
   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   706
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   707
fun strip_abs(tm as Abs _) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   708
       let val ({Bvar,Body}, _) = dest_abs [] tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   709
           val (bvs, core) = strip_abs Body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   710
       in (Bvar::bvs, core)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   711
       end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   712
  | strip_abs M = ([],M);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   713
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   714
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   715
fun strip_imp fm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   716
   if (is_imp fm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   717
   then let val {ant,conseq} = dest_imp fm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   718
            val (was,wb) = strip_imp conseq
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   719
        in ((ant::was), wb)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   720
        end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   721
   else ([],fm);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   722
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   723
fun strip_forall fm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   724
   if (is_forall fm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   725
   then let val {Bvar,Body} = dest_forall fm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   726
            val (bvs,core) = strip_forall Body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   727
        in ((Bvar::bvs), core)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   728
        end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   729
   else ([],fm);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   730
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   731
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   732
fun strip_exists fm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   733
   if (is_exists fm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   734
   then let val {Bvar, Body} = dest_exists fm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   735
            val (bvs,core) = strip_exists Body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   736
        in (Bvar::bvs, core)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   737
        end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   738
   else ([],fm);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   739
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   740
fun strip_disj w =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   741
   if (is_disj w)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   742
   then let val {disj1,disj2} = dest_disj w
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   743
        in (strip_disj disj1@strip_disj disj2)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   744
        end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   745
   else [w];
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   746
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   747
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   748
(* Miscellaneous *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   749
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   750
fun mk_vstruct ty V =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   751
  let fun follow_prod_type (Type(@{type_name Product_Type.prod},[ty1,ty2])) vs =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   752
              let val (ltm,vs1) = follow_prod_type ty1 vs
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   753
                  val (rtm,vs2) = follow_prod_type ty2 vs1
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   754
              in (mk_pair{fst=ltm, snd=rtm}, vs2) end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   755
        | follow_prod_type _ (v::vs) = (v,vs)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   756
  in #1 (follow_prod_type ty V)  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   757
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   758
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   759
(* Search a term for a sub-term satisfying the predicate p. *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   760
fun find_term p =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   761
   let fun find tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   762
      if (p tm) then SOME tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   763
      else case tm of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   764
          Abs(_,_,body) => find body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   765
        | (t$u)         => (case find t of NONE => find u | some => some)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   766
        | _             => NONE
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   767
   in find
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   768
   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   769
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   770
fun dest_relation tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   771
   if (type_of tm = HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   772
   then let val (Const(@{const_name Set.member},_) $ (Const(@{const_name Pair},_)$y$x) $ R) = tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   773
        in (R,y,x)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   774
        end handle Bind => raise USYN_ERR "dest_relation" "unexpected term structure"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   775
   else raise USYN_ERR "dest_relation" "not a boolean term";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   776
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   777
fun is_WFR (Const(@{const_name Wellfounded.wf},_)$_) = true
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   778
  | is_WFR _                 = false;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   779
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   780
fun ARB ty = mk_select{Bvar=Free("v",ty),
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   781
                       Body=Const(@{const_name True},HOLogic.boolT)};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   782
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   783
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   784
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   785
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   786
(*** derived cterm destructors ***)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   787
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   788
structure Dcterm: DCTERM =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   789
struct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   790
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   791
fun ERR func mesg = Utils.ERR {module = "Dcterm", func = func, mesg = mesg};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   792
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   793
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   794
fun dest_comb t = Thm.dest_comb t
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   795
  handle CTERM (msg, _) => raise ERR "dest_comb" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   796
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   797
fun dest_abs a t = Thm.dest_abs a t
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   798
  handle CTERM (msg, _) => raise ERR "dest_abs" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   799
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   800
fun capply t u = Thm.apply t u
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   801
  handle CTERM (msg, _) => raise ERR "capply" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   802
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   803
fun cabs a t = Thm.lambda a t
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   804
  handle CTERM (msg, _) => raise ERR "cabs" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   805
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   806
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   807
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   808
 * Some simple constructor functions.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   809
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   810
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   811
val mk_hol_const = Thm.cterm_of @{theory_context HOL} o Const;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   812
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   813
fun mk_exists (r as (Bvar, Body)) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   814
  let val ty = Thm.typ_of_cterm Bvar
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   815
      val c = mk_hol_const(@{const_name Ex}, (ty --> HOLogic.boolT) --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   816
  in capply c (uncurry cabs r) end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   817
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   818
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   819
local val c = mk_hol_const(@{const_name HOL.conj}, HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   820
in fun mk_conj(conj1,conj2) = capply (capply c conj1) conj2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   821
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   822
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   823
local val c = mk_hol_const(@{const_name HOL.disj}, HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   824
in fun mk_disj(disj1,disj2) = capply (capply c disj1) disj2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   825
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   826
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   827
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   828
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   829
 * The primitives.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   830
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   831
fun dest_const ctm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   832
   (case Thm.term_of ctm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   833
      of Const(s,ty) => {Name = s, Ty = ty}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   834
       | _ => raise ERR "dest_const" "not a constant");
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   835
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   836
fun dest_var ctm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   837
   (case Thm.term_of ctm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   838
      of Var((s,i),ty) => {Name=s, Ty=ty}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   839
       | Free(s,ty)    => {Name=s, Ty=ty}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   840
       |             _ => raise ERR "dest_var" "not a variable");
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   841
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   842
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   843
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   844
 * Derived destructor operations.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   845
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   846
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   847
fun dest_monop expected tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   848
 let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   849
   fun err () = raise ERR "dest_monop" ("Not a(n) " ^ quote expected);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   850
   val (c, N) = dest_comb tm handle Utils.ERR _ => err ();
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   851
   val name = #Name (dest_const c handle Utils.ERR _ => err ());
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   852
 in if name = expected then N else err () end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   853
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   854
fun dest_binop expected tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   855
 let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   856
   fun err () = raise ERR "dest_binop" ("Not a(n) " ^ quote expected);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   857
   val (M, N) = dest_comb tm handle Utils.ERR _ => err ()
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   858
 in (dest_monop expected M, N) handle Utils.ERR _ => err () end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   859
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   860
fun dest_binder expected tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   861
  dest_abs NONE (dest_monop expected tm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   862
  handle Utils.ERR _ => raise ERR "dest_binder" ("Not a(n) " ^ quote expected);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   863
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   864
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   865
val dest_neg    = dest_monop @{const_name Not}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   866
val dest_pair   = dest_binop @{const_name Pair}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   867
val dest_eq     = dest_binop @{const_name HOL.eq}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   868
val dest_imp    = dest_binop @{const_name HOL.implies}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   869
val dest_conj   = dest_binop @{const_name HOL.conj}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   870
val dest_disj   = dest_binop @{const_name HOL.disj}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   871
val dest_select = dest_binder @{const_name Eps}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   872
val dest_exists = dest_binder @{const_name Ex}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   873
val dest_forall = dest_binder @{const_name All}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   874
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   875
(* Query routines *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   876
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   877
val is_eq     = can dest_eq
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   878
val is_imp    = can dest_imp
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   879
val is_select = can dest_select
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   880
val is_forall = can dest_forall
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   881
val is_exists = can dest_exists
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   882
val is_neg    = can dest_neg
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   883
val is_conj   = can dest_conj
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   884
val is_disj   = can dest_disj
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   885
val is_pair   = can dest_pair
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   886
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   887
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   888
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   889
 * Iterated creation.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   890
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   891
val list_mk_disj = Utils.end_itlist (fn d1 => fn tm => mk_disj (d1, tm));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   892
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   893
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   894
 * Iterated destruction. (To the "right" in a term.)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   895
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   896
fun strip break tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   897
  let fun dest (p as (ctm,accum)) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   898
        let val (M,N) = break ctm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   899
        in dest (N, M::accum)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   900
        end handle Utils.ERR _ => p
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   901
  in dest (tm,[])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   902
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   903
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   904
fun rev2swap (x,l) = (rev l, x);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   905
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   906
val strip_comb   = strip (Library.swap o dest_comb)  (* Goes to the "left" *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   907
val strip_imp    = rev2swap o strip dest_imp
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   908
val strip_abs    = rev2swap o strip (dest_abs NONE)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   909
val strip_forall = rev2swap o strip dest_forall
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   910
val strip_exists = rev2swap o strip dest_exists
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   911
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   912
val strip_disj   = rev o (op::) o strip dest_disj
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   913
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   914
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   915
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   916
 * Going into and out of prop
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   917
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   918
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   919
fun is_Trueprop ct =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   920
  (case Thm.term_of ct of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   921
    Const (@{const_name Trueprop}, _) $ _ => true
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   922
  | _ => false);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   923
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   924
fun mk_prop ct = if is_Trueprop ct then ct else Thm.apply @{cterm Trueprop} ct;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   925
fun drop_prop ct = if is_Trueprop ct then Thm.dest_arg ct else ct;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   926
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   927
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   928
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   929
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   930
(*** notable theorems ***)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   931
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   932
structure Thms =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   933
struct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   934
  val WFREC_COROLLARY = @{thm tfl_wfrec};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   935
  val WF_INDUCTION_THM = @{thm tfl_wf_induct};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   936
  val CUT_DEF = @{thm tfl_cut_def};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   937
  val eqT = @{thm tfl_eq_True};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   938
  val rev_eq_mp = @{thm tfl_rev_eq_mp};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   939
  val simp_thm = @{thm tfl_simp_thm};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   940
  val P_imp_P_iff_True = @{thm tfl_P_imp_P_iff_True};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   941
  val imp_trans = @{thm tfl_imp_trans};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   942
  val disj_assoc = @{thm tfl_disj_assoc};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   943
  val tfl_disjE = @{thm tfl_disjE};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   944
  val choose_thm = @{thm tfl_exE};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   945
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   946
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   947
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   948
(*** emulation of HOL inference rules for TFL ***)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   949
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   950
structure Rules: RULES =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   951
struct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   952
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   953
fun RULES_ERR func mesg = Utils.ERR {module = "Rules", func = func, mesg = mesg};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   954
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   955
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   956
fun cconcl thm = Dcterm.drop_prop (#prop (Thm.crep_thm thm));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   957
fun chyps thm = map Dcterm.drop_prop (#hyps (Thm.crep_thm thm));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   958
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   959
fun dest_thm thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   960
  let val {prop,hyps,...} = Thm.rep_thm thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   961
  in (map HOLogic.dest_Trueprop hyps, HOLogic.dest_Trueprop prop) end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   962
  handle TERM _ => raise RULES_ERR "dest_thm" "missing Trueprop";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   963
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   964
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   965
(* Inference rules *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   966
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   967
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   968
 *        Equality (one step)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   969
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   970
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   971
fun REFL tm = Thm.reflexive tm RS meta_eq_to_obj_eq
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   972
  handle THM (msg, _, _) => raise RULES_ERR "REFL" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   973
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   974
fun SYM thm = thm RS sym
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   975
  handle THM (msg, _, _) => raise RULES_ERR "SYM" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   976
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   977
fun ALPHA thm ctm1 =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   978
  let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   979
    val ctm2 = Thm.cprop_of thm;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   980
    val ctm2_eq = Thm.reflexive ctm2;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   981
    val ctm1_eq = Thm.reflexive ctm1;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   982
  in Thm.equal_elim (Thm.transitive ctm2_eq ctm1_eq) thm end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   983
  handle THM (msg, _, _) => raise RULES_ERR "ALPHA" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   984
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   985
fun rbeta th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   986
  (case Dcterm.strip_comb (cconcl th) of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   987
    (_, [l, r]) => Thm.transitive th (Thm.beta_conversion false r)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   988
  | _ => raise RULES_ERR "rbeta" "");
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   989
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   990
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   991
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   992
 *        Implication and the assumption list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   993
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   994
 * Assumptions get stuck on the meta-language assumption list. Implications
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   995
 * are in the object language, so discharging an assumption "A" from theorem
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   996
 * "B" results in something that looks like "A --> B".
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   997
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   998
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   999
fun ASSUME ctm = Thm.assume (Dcterm.mk_prop ctm);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1000
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1001
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1002
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1003
 * Implication in TFL is -->. Meta-language implication (==>) is only used
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1004
 * in the implementation of some of the inference rules below.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1005
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1006
fun MP th1 th2 = th2 RS (th1 RS mp)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1007
  handle THM (msg, _, _) => raise RULES_ERR "MP" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1008
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1009
(*forces the first argument to be a proposition if necessary*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1010
fun DISCH tm thm = Thm.implies_intr (Dcterm.mk_prop tm) thm COMP impI
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1011
  handle THM (msg, _, _) => raise RULES_ERR "DISCH" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1012
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1013
fun DISCH_ALL thm = fold_rev DISCH (#hyps (Thm.crep_thm thm)) thm;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1014
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1015
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1016
fun FILTER_DISCH_ALL P thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1017
 let fun check tm = P (Thm.term_of tm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1018
 in  fold_rev (fn tm => fn th => if check tm then DISCH tm th else th) (chyps thm) thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1019
 end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1020
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1021
fun UNDISCH thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1022
   let val tm = Dcterm.mk_prop (#1 (Dcterm.dest_imp (cconcl thm)))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1023
   in Thm.implies_elim (thm RS mp) (ASSUME tm) end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1024
   handle Utils.ERR _ => raise RULES_ERR "UNDISCH" ""
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1025
     | THM _ => raise RULES_ERR "UNDISCH" "";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1026
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1027
fun PROVE_HYP ath bth = MP (DISCH (cconcl ath) bth) ath;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1028
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1029
fun IMP_TRANS th1 th2 = th2 RS (th1 RS Thms.imp_trans)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1030
  handle THM (msg, _, _) => raise RULES_ERR "IMP_TRANS" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1031
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1032
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1033
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1034
 *        Conjunction
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1035
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1036
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1037
fun CONJUNCT1 thm = thm RS conjunct1
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1038
  handle THM (msg, _, _) => raise RULES_ERR "CONJUNCT1" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1039
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1040
fun CONJUNCT2 thm = thm RS conjunct2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1041
  handle THM (msg, _, _) => raise RULES_ERR "CONJUNCT2" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1042
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1043
fun CONJUNCTS th = CONJUNCTS (CONJUNCT1 th) @ CONJUNCTS (CONJUNCT2 th) handle Utils.ERR _ => [th];
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1044
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1045
fun LIST_CONJ [] = raise RULES_ERR "LIST_CONJ" "empty list"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1046
  | LIST_CONJ [th] = th
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1047
  | LIST_CONJ (th :: rst) = MP (MP (conjI COMP (impI RS impI)) th) (LIST_CONJ rst)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1048
      handle THM (msg, _, _) => raise RULES_ERR "LIST_CONJ" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1049
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1050
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1051
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1052
 *        Disjunction
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1053
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1054
local
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1055
  val prop = Thm.prop_of disjI1
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1056
  val [P,Q] = Misc_Legacy.term_vars prop
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1057
  val disj1 = Thm.forall_intr (Thm.cterm_of @{context} Q) disjI1
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1058
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1059
fun DISJ1 thm tm = thm RS (Thm.forall_elim (Dcterm.drop_prop tm) disj1)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1060
  handle THM (msg, _, _) => raise RULES_ERR "DISJ1" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1061
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1062
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1063
local
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1064
  val prop = Thm.prop_of disjI2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1065
  val [P,Q] = Misc_Legacy.term_vars prop
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1066
  val disj2 = Thm.forall_intr (Thm.cterm_of @{context} P) disjI2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1067
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1068
fun DISJ2 tm thm = thm RS (Thm.forall_elim (Dcterm.drop_prop tm) disj2)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1069
  handle THM (msg, _, _) => raise RULES_ERR "DISJ2" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1070
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1071
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1072
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1073
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1074
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1075
 *                   A1 |- M1, ..., An |- Mn
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1076
 *     ---------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1077
 *     [A1 |- M1 \/ ... \/ Mn, ..., An |- M1 \/ ... \/ Mn]
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1078
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1079
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1080
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1081
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1082
fun EVEN_ORS thms =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1083
  let fun blue ldisjs [] _ = []
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1084
        | blue ldisjs (th::rst) rdisjs =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1085
            let val tail = tl rdisjs
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1086
                val rdisj_tl = Dcterm.list_mk_disj tail
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1087
            in fold_rev DISJ2 ldisjs (DISJ1 th rdisj_tl)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1088
               :: blue (ldisjs @ [cconcl th]) rst tail
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1089
            end handle Utils.ERR _ => [fold_rev DISJ2 ldisjs th]
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1090
   in blue [] thms (map cconcl thms) end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1091
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1092
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1093
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1094
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1095
 *         A |- P \/ Q   B,P |- R    C,Q |- R
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1096
 *     ---------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1097
 *                     A U B U C |- R
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1098
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1099
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1100
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1101
fun DISJ_CASES th1 th2 th3 =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1102
  let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1103
    val c = Dcterm.drop_prop (cconcl th1);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1104
    val (disj1, disj2) = Dcterm.dest_disj c;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1105
    val th2' = DISCH disj1 th2;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1106
    val th3' = DISCH disj2 th3;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1107
  in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1108
    th3' RS (th2' RS (th1 RS Thms.tfl_disjE))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1109
      handle THM (msg, _, _) => raise RULES_ERR "DISJ_CASES" msg
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1110
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1111
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1112
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1113
(*-----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1114
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1115
 *       |- A1 \/ ... \/ An     [A1 |- M, ..., An |- M]
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1116
 *     ---------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1117
 *                           |- M
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1118
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1119
 * Note. The list of theorems may be all jumbled up, so we have to
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1120
 * first organize it to align with the first argument (the disjunctive
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1121
 * theorem).
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1122
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1123
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1124
fun organize eq =    (* a bit slow - analogous to insertion sort *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1125
 let fun extract a alist =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1126
     let fun ex (_,[]) = raise RULES_ERR "organize" "not a permutation.1"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1127
           | ex(left,h::t) = if (eq h a) then (h,rev left@t) else ex(h::left,t)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1128
     in ex ([],alist)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1129
     end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1130
     fun place [] [] = []
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1131
       | place (a::rst) alist =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1132
           let val (item,next) = extract a alist
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1133
           in item::place rst next
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1134
           end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1135
       | place _ _ = raise RULES_ERR "organize" "not a permutation.2"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1136
 in place
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1137
 end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1138
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1139
fun DISJ_CASESL disjth thl =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1140
   let val c = cconcl disjth
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1141
       fun eq th atm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1142
        exists (fn t => HOLogic.dest_Trueprop t aconv Thm.term_of atm) (Thm.hyps_of th)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1143
       val tml = Dcterm.strip_disj c
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1144
       fun DL th [] = raise RULES_ERR "DISJ_CASESL" "no cases"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1145
         | DL th [th1] = PROVE_HYP th th1
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1146
         | DL th [th1,th2] = DISJ_CASES th th1 th2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1147
         | DL th (th1::rst) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1148
            let val tm = #2 (Dcterm.dest_disj (Dcterm.drop_prop(cconcl th)))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1149
             in DISJ_CASES th th1 (DL (ASSUME tm) rst) end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1150
   in DL disjth (organize eq tml thl)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1151
   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1152
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1153
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1154
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1155
 *        Universals
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1156
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1157
local (* this is fragile *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1158
  val prop = Thm.prop_of spec
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1159
  val x = hd (tl (Misc_Legacy.term_vars prop))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1160
  val cTV = Thm.ctyp_of @{context} (type_of x)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1161
  val gspec = Thm.forall_intr (Thm.cterm_of @{context} x) spec
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1162
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1163
fun SPEC tm thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1164
   let val gspec' = Drule.instantiate_normalize ([(cTV, Thm.ctyp_of_cterm tm)], []) gspec
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1165
   in thm RS (Thm.forall_elim tm gspec') end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1166
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1167
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1168
fun SPEC_ALL thm = fold SPEC (#1 (Dcterm.strip_forall(cconcl thm))) thm;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1169
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1170
val ISPEC = SPEC
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1171
val ISPECL = fold ISPEC;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1172
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1173
(* Not optimized! Too complicated. *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1174
local
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1175
  val prop = Thm.prop_of allI
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1176
  val [P] = Misc_Legacy.add_term_vars (prop, [])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1177
  fun cty_theta ctxt = map (fn (i, (S, ty)) => apply2 (Thm.ctyp_of ctxt) (TVar (i, S), ty))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1178
  fun ctm_theta ctxt =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1179
    map (fn (i, (_, tm2)) =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1180
      let val ctm2 = Thm.cterm_of ctxt tm2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1181
      in (Thm.cterm_of ctxt (Var (i, Thm.typ_of_cterm ctm2)), ctm2) end)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1182
  fun certify ctxt (ty_theta,tm_theta) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1183
    (cty_theta ctxt (Vartab.dest ty_theta),
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1184
     ctm_theta ctxt (Vartab.dest tm_theta))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1185
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1186
fun GEN ctxt v th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1187
   let val gth = Thm.forall_intr v th
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1188
       val thy = Proof_Context.theory_of ctxt
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1189
       val Const(@{const_name Pure.all},_)$Abs(x,ty,rst) = Thm.prop_of gth
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1190
       val P' = Abs(x,ty, HOLogic.dest_Trueprop rst)  (* get rid of trueprop *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1191
       val theta = Pattern.match thy (P,P') (Vartab.empty, Vartab.empty);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1192
       val allI2 = Drule.instantiate_normalize (certify ctxt theta) allI
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1193
       val thm = Thm.implies_elim allI2 gth
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1194
       val tp $ (A $ Abs(_,_,M)) = Thm.prop_of thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1195
       val prop' = tp $ (A $ Abs(x,ty,M))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1196
   in ALPHA thm (Thm.cterm_of ctxt prop') end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1197
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1198
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1199
fun GENL ctxt = fold_rev (GEN ctxt);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1200
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1201
fun GEN_ALL ctxt thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1202
  let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1203
    val prop = Thm.prop_of thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1204
    val vlist = map (Thm.cterm_of ctxt) (Misc_Legacy.add_term_vars (prop, []))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1205
  in GENL ctxt vlist thm end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1206
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1207
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1208
fun MATCH_MP th1 th2 =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1209
   if (Dcterm.is_forall (Dcterm.drop_prop(cconcl th1)))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1210
   then MATCH_MP (th1 RS spec) th2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1211
   else MP th1 th2;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1212
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1213
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1214
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1215
 *        Existentials
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1216
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1217
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1218
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1219
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1220
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1221
 * Existential elimination
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1222
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1223
 *      A1 |- ?x.t[x]   ,   A2, "t[v]" |- t'
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1224
 *      ------------------------------------     (variable v occurs nowhere)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1225
 *                A1 u A2 |- t'
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1226
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1227
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1228
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1229
fun CHOOSE ctxt (fvar, exth) fact =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1230
  let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1231
    val lam = #2 (Dcterm.dest_comb (Dcterm.drop_prop (cconcl exth)))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1232
    val redex = Dcterm.capply lam fvar
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1233
    val t$u = Thm.term_of redex
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1234
    val residue = Thm.cterm_of ctxt (Term.betapply (t, u))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1235
  in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1236
    GEN ctxt fvar (DISCH residue fact) RS (exth RS Thms.choose_thm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1237
      handle THM (msg, _, _) => raise RULES_ERR "CHOOSE" msg
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1238
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1239
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1240
local
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1241
  val prop = Thm.prop_of exI
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1242
  val [P, x] = map (Thm.cterm_of @{context}) (Misc_Legacy.term_vars prop)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1243
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1244
fun EXISTS (template,witness) thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1245
  let val abstr = #2 (Dcterm.dest_comb template) in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1246
    thm RS (cterm_instantiate [(P, abstr), (x, witness)] exI)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1247
      handle THM (msg, _, _) => raise RULES_ERR "EXISTS" msg
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1248
  end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1249
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1250
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1251
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1252
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1253
 *         A |- M
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1254
 *   -------------------   [v_1,...,v_n]
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1255
 *    A |- ?v1...v_n. M
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1256
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1257
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1258
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1259
fun EXISTL vlist th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1260
  fold_rev (fn v => fn thm => EXISTS(Dcterm.mk_exists(v,cconcl thm), v) thm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1261
           vlist th;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1262
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1263
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1264
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1265
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1266
 *       A |- M[x_1,...,x_n]
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1267
 *   ----------------------------   [(x |-> y)_1,...,(x |-> y)_n]
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1268
 *       A |- ?y_1...y_n. M
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1269
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1270
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1271
(* Could be improved, but needs "subst_free" for certified terms *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1272
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1273
fun IT_EXISTS ctxt blist th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1274
  let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1275
    val blist' = map (apply2 Thm.term_of) blist
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1276
    fun ex v M = Thm.cterm_of ctxt (USyntax.mk_exists{Bvar=v,Body = M})
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1277
  in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1278
    fold_rev (fn (b as (r1,r2)) => fn thm =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1279
        EXISTS(ex r2 (subst_free [b]
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1280
                   (HOLogic.dest_Trueprop(Thm.prop_of thm))), Thm.cterm_of ctxt r1)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1281
              thm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1282
       blist' th
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1283
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1284
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1285
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1286
 *  Faster version, that fails for some as yet unknown reason
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1287
 * fun IT_EXISTS blist th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1288
 *    let val {thy,...} = rep_thm th
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1289
 *        val tych = cterm_of thy
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1290
 *        fun detype (x,y) = ((#t o rep_cterm) x, (#t o rep_cterm) y)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1291
 *   in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1292
 *  fold (fn (b as (r1,r2), thm) =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1293
 *  EXISTS(D.mk_exists(r2, tych(subst_free[detype b](#t(rep_cterm(cconcl thm))))),
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1294
 *           r1) thm)  blist th
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1295
 *   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1296
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1297
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1298
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1299
 *        Rewriting
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1300
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1301
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1302
fun SUBS ctxt thl =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1303
  rewrite_rule ctxt (map (fn th => th RS eq_reflection handle THM _ => th) thl);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1304
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1305
val rew_conv = Raw_Simplifier.rewrite_cterm (true, false, false) (K (K NONE));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1306
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1307
fun simpl_conv ctxt thl ctm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1308
 rew_conv (ctxt addsimps thl) ctm RS meta_eq_to_obj_eq;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1309
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1310
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1311
fun RIGHT_ASSOC ctxt = rewrite_rule ctxt [Thms.disj_assoc];
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1312
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1313
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1314
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1315
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1316
 *                  TERMINATION CONDITION EXTRACTION
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1317
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1318
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1319
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1320
(* Object language quantifier, i.e., "!" *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1321
fun Forall v M = USyntax.mk_forall{Bvar=v, Body=M};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1322
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1323
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1324
(* Fragile: it's a cong if it is not "R y x ==> cut f R x y = f y" *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1325
fun is_cong thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1326
  case (Thm.prop_of thm) of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1327
    (Const(@{const_name Pure.imp},_)$(Const(@{const_name Trueprop},_)$ _) $
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1328
      (Const(@{const_name Pure.eq},_) $ (Const (@{const_name Wfrec.cut},_) $ f $ R $ a $ x) $ _)) =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1329
        false
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1330
  | _ => true;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1331
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1332
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1333
fun dest_equal(Const (@{const_name Pure.eq},_) $
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1334
               (Const (@{const_name Trueprop},_) $ lhs)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1335
               $ (Const (@{const_name Trueprop},_) $ rhs)) = {lhs=lhs, rhs=rhs}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1336
  | dest_equal(Const (@{const_name Pure.eq},_) $ lhs $ rhs) = {lhs=lhs, rhs=rhs}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1337
  | dest_equal tm = USyntax.dest_eq tm;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1338
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1339
fun get_lhs tm = #lhs(dest_equal (HOLogic.dest_Trueprop tm));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1340
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1341
fun dest_all used (Const(@{const_name Pure.all},_) $ (a as Abs _)) = USyntax.dest_abs used a
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1342
  | dest_all _ _ = raise RULES_ERR "dest_all" "not a !!";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1343
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1344
val is_all = can (dest_all []);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1345
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1346
fun strip_all used fm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1347
   if (is_all fm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1348
   then let val ({Bvar, Body}, used') = dest_all used fm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1349
            val (bvs, core, used'') = strip_all used' Body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1350
        in ((Bvar::bvs), core, used'')
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1351
        end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1352
   else ([], fm, used);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1353
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1354
fun break_all(Const(@{const_name Pure.all},_) $ Abs (_,_,body)) = body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1355
  | break_all _ = raise RULES_ERR "break_all" "not a !!";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1356
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1357
fun list_break_all(Const(@{const_name Pure.all},_) $ Abs (s,ty,body)) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1358
     let val (L,core) = list_break_all body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1359
     in ((s,ty)::L, core)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1360
     end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1361
  | list_break_all tm = ([],tm);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1362
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1363
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1364
 * Rename a term of the form
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1365
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1366
 *      !!x1 ...xn. x1=M1 ==> ... ==> xn=Mn
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1367
 *                  ==> ((%v1...vn. Q) x1 ... xn = g x1 ... xn.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1368
 * to one of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1369
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1370
 *      !!v1 ... vn. v1=M1 ==> ... ==> vn=Mn
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1371
 *      ==> ((%v1...vn. Q) v1 ... vn = g v1 ... vn.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1372
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1373
 * This prevents name problems in extraction, and helps the result to read
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1374
 * better. There is a problem with varstructs, since they can introduce more
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1375
 * than n variables, and some extra reasoning needs to be done.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1376
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1377
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1378
fun get ([],_,L) = rev L
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1379
  | get (ant::rst,n,L) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1380
      case (list_break_all ant)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1381
        of ([],_) => get (rst, n+1,L)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1382
         | (vlist,body) =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1383
            let val eq = Logic.strip_imp_concl body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1384
                val (f,args) = USyntax.strip_comb (get_lhs eq)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1385
                val (vstrl,_) = USyntax.strip_abs f
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1386
                val names  =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1387
                  Name.variant_list (Misc_Legacy.add_term_names(body, [])) (map (#1 o dest_Free) vstrl)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1388
            in get (rst, n+1, (names,n)::L) end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1389
            handle TERM _ => get (rst, n+1, L)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1390
              | Utils.ERR _ => get (rst, n+1, L);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1391
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1392
(* Note: Thm.rename_params_rule counts from 1, not 0 *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1393
fun rename thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1394
  let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1395
    val ants = Logic.strip_imp_prems (Thm.prop_of thm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1396
    val news = get (ants,1,[])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1397
  in fold Thm.rename_params_rule news thm end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1398
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1399
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1400
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1401
 * Beta-conversion to the rhs of an equation (taken from hol90/drule.sml)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1402
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1403
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1404
fun list_beta_conv tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1405
  let fun rbeta th = Thm.transitive th (Thm.beta_conversion false (#2(Dcterm.dest_eq(cconcl th))))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1406
      fun iter [] = Thm.reflexive tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1407
        | iter (v::rst) = rbeta (Thm.combination(iter rst) (Thm.reflexive v))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1408
  in iter  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1409
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1410
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1411
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1412
 * Trace information for the rewriter
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1413
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1414
val tracing = Unsynchronized.ref false;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1415
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1416
fun say s = if !tracing then writeln s else ();
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1417
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1418
fun print_thms ctxt s L =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1419
  say (cat_lines (s :: map (Display.string_of_thm ctxt) L));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1420
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1421
fun print_term ctxt s t =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1422
  say (cat_lines [s, Syntax.string_of_term ctxt t]);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1423
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1424
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1425
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1426
 * General abstraction handlers, should probably go in USyntax.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1427
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1428
fun mk_aabs (vstr, body) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1429
  USyntax.mk_abs {Bvar = vstr, Body = body}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1430
  handle Utils.ERR _ => USyntax.mk_pabs {varstruct = vstr, body = body};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1431
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1432
fun list_mk_aabs (vstrl,tm) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1433
    fold_rev (fn vstr => fn tm => mk_aabs(vstr,tm)) vstrl tm;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1434
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1435
fun dest_aabs used tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1436
   let val ({Bvar,Body}, used') = USyntax.dest_abs used tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1437
   in (Bvar, Body, used') end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1438
   handle Utils.ERR _ =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1439
     let val {varstruct, body, used} = USyntax.dest_pabs used tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1440
     in (varstruct, body, used) end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1441
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1442
fun strip_aabs used tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1443
   let val (vstr, body, used') = dest_aabs used tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1444
       val (bvs, core, used'') = strip_aabs used' body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1445
   in (vstr::bvs, core, used'') end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1446
   handle Utils.ERR _ => ([], tm, used);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1447
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1448
fun dest_combn tm 0 = (tm,[])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1449
  | dest_combn tm n =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1450
     let val {Rator,Rand} = USyntax.dest_comb tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1451
         val (f,rands) = dest_combn Rator (n-1)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1452
     in (f,Rand::rands)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1453
     end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1454
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1455
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1456
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1457
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1458
local fun dest_pair M = let val {fst,snd} = USyntax.dest_pair M in (fst,snd) end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1459
      fun mk_fst tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1460
          let val ty as Type(@{type_name Product_Type.prod}, [fty,sty]) = type_of tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1461
          in  Const (@{const_name Product_Type.fst}, ty --> fty) $ tm  end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1462
      fun mk_snd tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1463
          let val ty as Type(@{type_name Product_Type.prod}, [fty,sty]) = type_of tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1464
          in  Const (@{const_name Product_Type.snd}, ty --> sty) $ tm  end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1465
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1466
fun XFILL tych x vstruct =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1467
  let fun traverse p xocc L =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1468
        if (is_Free p)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1469
        then tych xocc::L
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1470
        else let val (p1,p2) = dest_pair p
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1471
             in traverse p1 (mk_fst xocc) (traverse p2  (mk_snd xocc) L)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1472
             end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1473
  in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1474
  traverse vstruct x []
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1475
end end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1476
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1477
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1478
 * Replace a free tuple (vstr) by a universally quantified variable (a).
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1479
 * Note that the notion of "freeness" for a tuple is different than for a
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1480
 * variable: if variables in the tuple also occur in any other place than
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1481
 * an occurrences of the tuple, they aren't "free" (which is thus probably
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1482
 *  the wrong word to use).
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1483
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1484
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1485
fun VSTRUCT_ELIM ctxt tych a vstr th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1486
  let val L = USyntax.free_vars_lr vstr
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1487
      val bind1 = tych (HOLogic.mk_Trueprop (HOLogic.mk_eq(a,vstr)))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1488
      val thm1 = Thm.implies_intr bind1 (SUBS ctxt [SYM(Thm.assume bind1)] th)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1489
      val thm2 = forall_intr_list (map tych L) thm1
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1490
      val thm3 = forall_elim_list (XFILL tych a vstr) thm2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1491
  in refl RS
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1492
     rewrite_rule ctxt [Thm.symmetric (@{thm surjective_pairing} RS eq_reflection)] thm3
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1493
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1494
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1495
fun PGEN ctxt tych a vstr th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1496
  let val a1 = tych a
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1497
      val vstr1 = tych vstr
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1498
  in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1499
  Thm.forall_intr a1
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1500
     (if (is_Free vstr)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1501
      then cterm_instantiate [(vstr1,a1)] th
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1502
      else VSTRUCT_ELIM ctxt tych a vstr th)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1503
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1504
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1505
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1506
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1507
 * Takes apart a paired beta-redex, looking like "(\(x,y).N) vstr", into
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1508
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1509
 *     (([x,y],N),vstr)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1510
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1511
fun dest_pbeta_redex used M n =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1512
  let val (f,args) = dest_combn M n
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1513
      val dummy = dest_aabs used f
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1514
  in (strip_aabs used f,args)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1515
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1516
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1517
fun pbeta_redex M n = can (fn t => dest_pbeta_redex [] t n) M;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1518
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1519
fun dest_impl tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1520
  let val ants = Logic.strip_imp_prems tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1521
      val eq = Logic.strip_imp_concl tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1522
  in (ants,get_lhs eq)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1523
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1524