src/HOL/Library/old_recdef.ML
author wenzelm
Fri, 19 Jun 2015 19:45:01 +0200
changeset 60522 1409b4015671
parent 60521 52e956416fbf
child 60523 be2d9f5ddc76
permissions -rw-r--r--
tuned;
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)
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   296
                   | TVar((s,_),_) => error ("Free variable: " ^ s)
60520
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
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   319
            (_ $ (Pv $ (Dv as Var(_, Dty)))) =>
60520
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? *)
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   345
  | find_term_split (Free _, Var _) = NONE (* keep searching *)
60520
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)
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   350
  | find_term_split (Abs(_,_,t1), Abs(_,_,t2)) =
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   351
    find_term_split (t1, t2)
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   352
  | find_term_split (Const (x,_), Const(x2,_)) =
60520
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
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   425
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   426
(*** basic utilities ***)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   427
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   428
structure Utils: UTILS =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   429
struct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   430
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   431
(*standard exception for TFL*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   432
exception ERR of {module: string, func: string, mesg: string};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   433
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   434
fun UTILS_ERR func mesg = ERR {module = "Utils", func = func, mesg = mesg};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   435
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   436
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   437
fun end_itlist _ [] = raise (UTILS_ERR "end_itlist" "list too short")
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   438
  | end_itlist _ [x] = x
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   439
  | end_itlist f (x :: xs) = f x (end_itlist f xs);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   440
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   441
fun itlist2 f L1 L2 base_value =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   442
 let fun it ([],[]) = base_value
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   443
       | it ((a::rst1),(b::rst2)) = f a b (it (rst1,rst2))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   444
       | it _ = raise UTILS_ERR "itlist2" "different length lists"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   445
 in  it (L1,L2)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   446
 end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   447
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   448
fun pluck p  =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   449
  let fun remv ([],_) = raise UTILS_ERR "pluck" "item not found"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   450
        | remv (h::t, A) = if p h then (h, rev A @ t) else remv (t,h::A)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   451
  in fn L => remv(L,[])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   452
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   453
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   454
fun take f =
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   455
  let fun grab(0, _) = []
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   456
        | grab(n, x::rst) = f x::grab(n-1,rst)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   457
  in grab
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   458
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   459
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   460
fun zip3 [][][] = []
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   461
  | zip3 (x::l1) (y::l2) (z::l3) = (x,y,z)::zip3 l1 l2 l3
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   462
  | zip3 _ _ _ = raise UTILS_ERR "zip3" "different lengths";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   463
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   464
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   465
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   466
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   467
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   468
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   469
(*** emulation of HOL's abstract syntax functions ***)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   470
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   471
structure USyntax: USYNTAX =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   472
struct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   473
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   474
infix 4 ##;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   475
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   476
fun USYN_ERR func mesg = Utils.ERR {module = "USyntax", func = func, mesg = mesg};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   477
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   478
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   479
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   480
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   481
 *                            Types
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   482
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   483
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   484
val mk_prim_vartype = TVar;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   485
fun mk_vartype s = mk_prim_vartype ((s, 0), @{sort type});
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   486
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   487
(* But internally, it's useful *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   488
fun dest_vtype (TVar x) = x
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   489
  | dest_vtype _ = raise USYN_ERR "dest_vtype" "not a flexible type variable";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   490
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   491
val is_vartype = can dest_vtype;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   492
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   493
val type_vars  = map mk_prim_vartype o Misc_Legacy.typ_tvars
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   494
fun type_varsl L = distinct (op =) (fold (curry op @ o type_vars) L []);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   495
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   496
val alpha  = mk_vartype "'a"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   497
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   498
val strip_prod_type = HOLogic.flatten_tupleT;
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
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   504
 *                              Terms
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   505
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   506
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   507
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   508
(* Free variables, in order of occurrence, from left to right in the
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   509
 * syntax tree. *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   510
fun free_vars_lr tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   511
  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
   512
      fun add (t, frees) = case t of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   513
            Free   _ => if (memb t frees) then frees else t::frees
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   514
          | Abs (_,_,body) => add(body,frees)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   515
          | f$t =>  add(t, add(f, frees))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   516
          | _ => frees
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   517
  in rev(add(tm,[]))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   518
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   519
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   521
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   522
val type_vars_in_term = map mk_prim_vartype o Misc_Legacy.term_tvars;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   523
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   524
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   525
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   526
(* Prelogic *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   527
fun dest_tybinding (v,ty) = (#1(dest_vtype v),ty)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   528
fun inst theta = subst_vars (map dest_tybinding theta,[])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   529
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   530
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   531
(* Construction routines *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   532
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   533
fun mk_abs{Bvar as Var((s,_),ty),Body}  = Abs(s,ty,abstract_over(Bvar,Body))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   534
  | mk_abs{Bvar as Free(s,ty),Body}  = Abs(s,ty,abstract_over(Bvar,Body))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   535
  | mk_abs _ = raise USYN_ERR "mk_abs" "Bvar is not a variable";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   536
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   537
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   538
fun mk_imp{ant,conseq} =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   539
   let val c = Const(@{const_name HOL.implies},HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   540
   in list_comb(c,[ant,conseq])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   541
   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   542
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   543
fun mk_select (r as {Bvar,Body}) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   544
  let val ty = type_of Bvar
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   545
      val c = Const(@{const_name Eps},(ty --> HOLogic.boolT) --> ty)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   546
  in list_comb(c,[mk_abs r])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   547
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   548
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   549
fun mk_forall (r as {Bvar,Body}) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   550
  let val ty = type_of Bvar
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   551
      val c = Const(@{const_name All},(ty --> HOLogic.boolT) --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   552
  in list_comb(c,[mk_abs r])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   553
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   554
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   555
fun mk_exists (r as {Bvar,Body}) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   556
  let val ty = type_of Bvar
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   557
      val c = Const(@{const_name Ex},(ty --> HOLogic.boolT) --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   558
  in list_comb(c,[mk_abs r])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   559
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   560
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   561
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   562
fun mk_conj{conj1,conj2} =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   563
   let val c = Const(@{const_name HOL.conj},HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   564
   in list_comb(c,[conj1,conj2])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   565
   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   566
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   567
fun mk_disj{disj1,disj2} =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   568
   let val c = Const(@{const_name HOL.disj},HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   569
   in list_comb(c,[disj1,disj2])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   570
   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   571
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   572
fun prod_ty ty1 ty2 = HOLogic.mk_prodT (ty1,ty2);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   573
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   574
local
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   575
fun mk_uncurry (xt, yt, zt) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   576
    Const(@{const_name case_prod}, (xt --> yt --> zt) --> prod_ty xt yt --> zt)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   577
fun dest_pair(Const(@{const_name Pair},_) $ M $ N) = {fst=M, snd=N}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   578
  | dest_pair _ = raise USYN_ERR "dest_pair" "not a pair"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   579
fun is_var (Var _) = true | is_var (Free _) = true | is_var _ = false
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   580
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   581
fun mk_pabs{varstruct,body} =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   582
 let fun mpa (varstruct, body) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   583
       if is_var varstruct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   584
       then mk_abs {Bvar = varstruct, Body = body}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   585
       else let val {fst, snd} = dest_pair varstruct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   586
            in mk_uncurry (type_of fst, type_of snd, type_of body) $
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   587
               mpa (fst, mpa (snd, body))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   588
            end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   589
 in mpa (varstruct, body) end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   590
 handle TYPE _ => raise USYN_ERR "mk_pabs" "";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   591
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   592
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   593
(* Destruction routines *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   594
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   595
datatype lambda = VAR   of {Name : string, Ty : typ}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   596
                | CONST of {Name : string, Ty : typ}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   597
                | COMB  of {Rator: term, Rand : term}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   598
                | LAMB  of {Bvar : term, Body : term};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   599
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   600
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   601
fun dest_term(Var((s,_),ty)) = VAR{Name = s, Ty = ty}
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   602
  | dest_term(Free(s,ty))    = VAR{Name = s, Ty = ty}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   603
  | dest_term(Const(s,ty))   = CONST{Name = s, Ty = ty}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   604
  | dest_term(M$N)           = COMB{Rator=M,Rand=N}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   605
  | dest_term(Abs(s,ty,M))   = let  val v = Free(s,ty)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   606
                               in LAMB{Bvar = v, Body = Term.betapply (M,v)}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   607
                               end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   608
  | dest_term(Bound _)       = raise USYN_ERR "dest_term" "Bound";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   609
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   610
fun dest_const(Const(s,ty)) = {Name = s, Ty = ty}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   611
  | dest_const _ = raise USYN_ERR "dest_const" "not a constant";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   612
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   613
fun dest_comb(t1 $ t2) = {Rator = t1, Rand = t2}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   614
  | dest_comb _ =  raise USYN_ERR "dest_comb" "not a comb";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   615
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   616
fun dest_abs used (a as Abs(s, ty, M)) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   617
     let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   618
       val s' = singleton (Name.variant_list used) s;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   619
       val v = Free(s', ty);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   620
     in ({Bvar = v, Body = Term.betapply (a,v)}, s'::used)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   621
     end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   622
  | dest_abs _ _ =  raise USYN_ERR "dest_abs" "not an abstraction";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   623
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   624
fun dest_eq(Const(@{const_name HOL.eq},_) $ M $ N) = {lhs=M, rhs=N}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   625
  | dest_eq _ = raise USYN_ERR "dest_eq" "not an equality";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   626
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   627
fun dest_imp(Const(@{const_name HOL.implies},_) $ M $ N) = {ant=M, conseq=N}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   628
  | dest_imp _ = raise USYN_ERR "dest_imp" "not an implication";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   629
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   630
fun dest_forall(Const(@{const_name All},_) $ (a as Abs _)) = fst (dest_abs [] a)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   631
  | dest_forall _ = raise USYN_ERR "dest_forall" "not a forall";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   632
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   633
fun dest_exists(Const(@{const_name Ex},_) $ (a as Abs _)) = fst (dest_abs [] a)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   634
  | dest_exists _ = raise USYN_ERR "dest_exists" "not an existential";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   635
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   636
fun dest_neg(Const(@{const_name Not},_) $ M) = M
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   637
  | dest_neg _ = raise USYN_ERR "dest_neg" "not a negation";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   638
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   639
fun dest_conj(Const(@{const_name HOL.conj},_) $ M $ N) = {conj1=M, conj2=N}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   640
  | dest_conj _ = raise USYN_ERR "dest_conj" "not a conjunction";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   641
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   642
fun dest_disj(Const(@{const_name HOL.disj},_) $ M $ N) = {disj1=M, disj2=N}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   643
  | dest_disj _ = raise USYN_ERR "dest_disj" "not a disjunction";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   644
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   645
fun mk_pair{fst,snd} =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   646
   let val ty1 = type_of fst
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   647
       val ty2 = type_of snd
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   648
       val c = Const(@{const_name Pair},ty1 --> ty2 --> prod_ty ty1 ty2)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   649
   in list_comb(c,[fst,snd])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   650
   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   651
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   652
fun dest_pair(Const(@{const_name Pair},_) $ M $ N) = {fst=M, snd=N}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   653
  | dest_pair _ = raise USYN_ERR "dest_pair" "not a pair";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   654
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   655
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   656
local  fun ucheck t = (if #Name (dest_const t) = @{const_name case_prod} then t
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   657
                       else raise Match)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   658
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   659
fun dest_pabs used tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   660
   let val ({Bvar,Body}, used') = dest_abs used tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   661
   in {varstruct = Bvar, body = Body, used = used'}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   662
   end handle Utils.ERR _ =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   663
          let val {Rator,Rand} = dest_comb tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   664
              val _ = ucheck Rator
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   665
              val {varstruct = lv, body, used = used'} = dest_pabs used Rand
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   666
              val {varstruct = rv, body, used = used''} = dest_pabs used' body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   667
          in {varstruct = mk_pair {fst = lv, snd = rv}, body = body, used = used''}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   668
          end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   669
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   670
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   671
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   672
val lhs   = #lhs o dest_eq
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   673
val rhs   = #rhs o dest_eq
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   674
val rand  = #Rand o dest_comb
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   675
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   676
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   677
(* Query routines *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   678
val is_imp    = can dest_imp
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   679
val is_forall = can dest_forall
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   680
val is_exists = can dest_exists
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   681
val is_neg    = can dest_neg
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   682
val is_conj   = can dest_conj
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   683
val is_disj   = can dest_disj
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   684
val is_pair   = can dest_pair
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   685
val is_pabs   = can (dest_pabs [])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   686
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   687
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   688
(* Construction of a cterm from a list of Terms *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   689
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   690
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
   691
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   692
(* These others are almost never used *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   693
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
   694
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
   695
val list_mk_conj = Utils.end_itlist(fn c1 => fn tm => mk_conj{conj1=c1, conj2=tm})
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   696
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   697
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   698
(* Need to reverse? *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   699
fun gen_all tm = list_mk_forall(Misc_Legacy.term_frees tm, tm);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   700
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   701
(* Destructing a cterm to a list of Terms *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   702
fun strip_comb tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   703
   let fun dest(M$N, A) = dest(M, N::A)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   704
         | dest x = x
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   705
   in dest(tm,[])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   706
   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   707
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   708
fun strip_abs(tm as Abs _) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   709
       let val ({Bvar,Body}, _) = dest_abs [] tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   710
           val (bvs, core) = strip_abs Body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   711
       in (Bvar::bvs, core)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   712
       end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   713
  | strip_abs M = ([],M);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   714
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   715
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   716
fun strip_imp fm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   717
   if (is_imp fm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   718
   then let val {ant,conseq} = dest_imp fm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   719
            val (was,wb) = strip_imp conseq
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   720
        in ((ant::was), wb)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   721
        end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   722
   else ([],fm);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   723
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   724
fun strip_forall fm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   725
   if (is_forall fm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   726
   then let val {Bvar,Body} = dest_forall fm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   727
            val (bvs,core) = strip_forall Body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   728
        in ((Bvar::bvs), core)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   729
        end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   730
   else ([],fm);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   731
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   732
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   733
fun strip_exists fm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   734
   if (is_exists fm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   735
   then let val {Bvar, Body} = dest_exists fm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   736
            val (bvs,core) = strip_exists Body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   737
        in (Bvar::bvs, core)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   738
        end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   739
   else ([],fm);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   740
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   741
fun strip_disj w =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   742
   if (is_disj w)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   743
   then let val {disj1,disj2} = dest_disj w
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   744
        in (strip_disj disj1@strip_disj disj2)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   745
        end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   746
   else [w];
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   747
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   748
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   749
(* Miscellaneous *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   750
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   751
fun mk_vstruct ty V =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   752
  let fun follow_prod_type (Type(@{type_name Product_Type.prod},[ty1,ty2])) vs =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   753
              let val (ltm,vs1) = follow_prod_type ty1 vs
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   754
                  val (rtm,vs2) = follow_prod_type ty2 vs1
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   755
              in (mk_pair{fst=ltm, snd=rtm}, vs2) end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   756
        | follow_prod_type _ (v::vs) = (v,vs)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   757
  in #1 (follow_prod_type ty V)  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   758
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   759
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   760
(* Search a term for a sub-term satisfying the predicate p. *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   761
fun find_term p =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   762
   let fun find tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   763
      if (p tm) then SOME tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   764
      else case tm of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   765
          Abs(_,_,body) => find body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   766
        | (t$u)         => (case find t of NONE => find u | some => some)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   767
        | _             => NONE
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   768
   in find
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   769
   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   770
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   771
fun dest_relation tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   772
   if (type_of tm = HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   773
   then let val (Const(@{const_name Set.member},_) $ (Const(@{const_name Pair},_)$y$x) $ R) = tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   774
        in (R,y,x)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   775
        end handle Bind => raise USYN_ERR "dest_relation" "unexpected term structure"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   776
   else raise USYN_ERR "dest_relation" "not a boolean term";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   777
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   778
fun is_WFR (Const(@{const_name Wellfounded.wf},_)$_) = true
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   779
  | is_WFR _                 = false;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   780
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   781
fun ARB ty = mk_select{Bvar=Free("v",ty),
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   782
                       Body=Const(@{const_name True},HOLogic.boolT)};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   783
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   784
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   785
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   786
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   787
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   788
(*** derived cterm destructors ***)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   789
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   790
structure Dcterm: DCTERM =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   791
struct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   792
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   793
fun ERR func mesg = Utils.ERR {module = "Dcterm", func = func, mesg = mesg};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   794
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   795
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   796
fun dest_comb t = Thm.dest_comb t
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   797
  handle CTERM (msg, _) => raise ERR "dest_comb" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   798
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   799
fun dest_abs a t = Thm.dest_abs a t
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   800
  handle CTERM (msg, _) => raise ERR "dest_abs" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   801
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   802
fun capply t u = Thm.apply t u
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   803
  handle CTERM (msg, _) => raise ERR "capply" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   804
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   805
fun cabs a t = Thm.lambda a t
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   806
  handle CTERM (msg, _) => raise ERR "cabs" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   807
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   808
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   809
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   810
 * Some simple constructor functions.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   811
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   812
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   813
val mk_hol_const = Thm.cterm_of @{theory_context HOL} o Const;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   814
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   815
fun mk_exists (r as (Bvar, Body)) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   816
  let val ty = Thm.typ_of_cterm Bvar
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   817
      val c = mk_hol_const(@{const_name Ex}, (ty --> HOLogic.boolT) --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   818
  in capply c (uncurry cabs r) end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   819
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   820
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   821
local val c = mk_hol_const(@{const_name HOL.conj}, HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   822
in fun mk_conj(conj1,conj2) = capply (capply c conj1) conj2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   823
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   824
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   825
local val c = mk_hol_const(@{const_name HOL.disj}, HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   826
in fun mk_disj(disj1,disj2) = capply (capply c disj1) disj2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   827
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   828
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   829
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   830
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   831
 * The primitives.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   832
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   833
fun dest_const ctm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   834
   (case Thm.term_of ctm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   835
      of Const(s,ty) => {Name = s, Ty = ty}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   836
       | _ => raise ERR "dest_const" "not a constant");
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   837
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   838
fun dest_var ctm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   839
   (case Thm.term_of ctm
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   840
      of Var((s,_),ty) => {Name=s, Ty=ty}
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   841
       | Free(s,ty)    => {Name=s, Ty=ty}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   842
       |             _ => raise ERR "dest_var" "not a variable");
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   843
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   844
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   845
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   846
 * Derived destructor operations.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   847
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   848
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   849
fun dest_monop expected tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   850
 let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   851
   fun err () = raise ERR "dest_monop" ("Not a(n) " ^ quote expected);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   852
   val (c, N) = dest_comb tm handle Utils.ERR _ => err ();
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   853
   val name = #Name (dest_const c handle Utils.ERR _ => err ());
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   854
 in if name = expected then N else err () end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   855
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   856
fun dest_binop expected tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   857
 let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   858
   fun err () = raise ERR "dest_binop" ("Not a(n) " ^ quote expected);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   859
   val (M, N) = dest_comb tm handle Utils.ERR _ => err ()
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   860
 in (dest_monop expected M, N) handle Utils.ERR _ => err () end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   861
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   862
fun dest_binder expected tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   863
  dest_abs NONE (dest_monop expected tm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   864
  handle Utils.ERR _ => raise ERR "dest_binder" ("Not a(n) " ^ quote expected);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   865
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   866
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   867
val dest_neg    = dest_monop @{const_name Not}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   868
val dest_pair   = dest_binop @{const_name Pair}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   869
val dest_eq     = dest_binop @{const_name HOL.eq}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   870
val dest_imp    = dest_binop @{const_name HOL.implies}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   871
val dest_conj   = dest_binop @{const_name HOL.conj}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   872
val dest_disj   = dest_binop @{const_name HOL.disj}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   873
val dest_select = dest_binder @{const_name Eps}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   874
val dest_exists = dest_binder @{const_name Ex}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   875
val dest_forall = dest_binder @{const_name All}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   876
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   877
(* Query routines *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   878
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   879
val is_eq     = can dest_eq
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   880
val is_imp    = can dest_imp
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   881
val is_select = can dest_select
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   882
val is_forall = can dest_forall
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   883
val is_exists = can dest_exists
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   884
val is_neg    = can dest_neg
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   885
val is_conj   = can dest_conj
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   886
val is_disj   = can dest_disj
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   887
val is_pair   = can dest_pair
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   888
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   889
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   890
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   891
 * Iterated creation.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   892
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   893
val list_mk_disj = Utils.end_itlist (fn d1 => fn tm => mk_disj (d1, tm));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   894
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   895
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   896
 * Iterated destruction. (To the "right" in a term.)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   897
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   898
fun strip break tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   899
  let fun dest (p as (ctm,accum)) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   900
        let val (M,N) = break ctm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   901
        in dest (N, M::accum)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   902
        end handle Utils.ERR _ => p
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   903
  in dest (tm,[])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   904
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   905
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   906
fun rev2swap (x,l) = (rev l, x);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   907
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   908
val strip_comb   = strip (Library.swap o dest_comb)  (* Goes to the "left" *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   909
val strip_imp    = rev2swap o strip dest_imp
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   910
val strip_abs    = rev2swap o strip (dest_abs NONE)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   911
val strip_forall = rev2swap o strip dest_forall
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   912
val strip_exists = rev2swap o strip dest_exists
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   913
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   914
val strip_disj   = rev o (op::) o strip dest_disj
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   915
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   916
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   917
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   918
 * Going into and out of prop
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   919
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   920
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   921
fun is_Trueprop ct =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   922
  (case Thm.term_of ct of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   923
    Const (@{const_name Trueprop}, _) $ _ => true
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   924
  | _ => false);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   925
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   926
fun mk_prop ct = if is_Trueprop ct then ct else Thm.apply @{cterm Trueprop} ct;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   927
fun drop_prop ct = if is_Trueprop ct then Thm.dest_arg ct else ct;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   928
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   929
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   930
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   931
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   932
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   933
(*** emulation of HOL inference rules for TFL ***)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   934
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   935
structure Rules: RULES =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   936
struct
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   937
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   938
fun RULES_ERR func mesg = Utils.ERR {module = "Rules", func = func, mesg = mesg};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   939
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   940
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   941
fun cconcl thm = Dcterm.drop_prop (#prop (Thm.crep_thm thm));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   942
fun chyps thm = map Dcterm.drop_prop (#hyps (Thm.crep_thm thm));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   943
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   944
fun dest_thm thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   945
  let val {prop,hyps,...} = Thm.rep_thm thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   946
  in (map HOLogic.dest_Trueprop hyps, HOLogic.dest_Trueprop prop) end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   947
  handle TERM _ => raise RULES_ERR "dest_thm" "missing Trueprop";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   948
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   949
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   950
(* Inference rules *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   951
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   952
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   953
 *        Equality (one step)
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 REFL tm = Thm.reflexive tm RS meta_eq_to_obj_eq
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   957
  handle THM (msg, _, _) => raise RULES_ERR "REFL" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   958
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   959
fun SYM thm = thm RS sym
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   960
  handle THM (msg, _, _) => raise RULES_ERR "SYM" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   961
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   962
fun ALPHA thm ctm1 =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   963
  let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   964
    val ctm2 = Thm.cprop_of thm;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   965
    val ctm2_eq = Thm.reflexive ctm2;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   966
    val ctm1_eq = Thm.reflexive ctm1;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   967
  in Thm.equal_elim (Thm.transitive ctm2_eq ctm1_eq) thm end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   968
  handle THM (msg, _, _) => raise RULES_ERR "ALPHA" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   969
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   970
fun rbeta th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   971
  (case Dcterm.strip_comb (cconcl th) of
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
   972
    (_, [_, r]) => Thm.transitive th (Thm.beta_conversion false r)
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   973
  | _ => raise RULES_ERR "rbeta" "");
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   974
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   975
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   976
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   977
 *        Implication and the assumption list
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   978
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   979
 * Assumptions get stuck on the meta-language assumption list. Implications
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   980
 * are in the object language, so discharging an assumption "A" from theorem
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   981
 * "B" results in something that looks like "A --> B".
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   982
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   983
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   984
fun ASSUME ctm = Thm.assume (Dcterm.mk_prop ctm);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   985
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   986
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   987
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   988
 * Implication in TFL is -->. Meta-language implication (==>) is only used
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   989
 * in the implementation of some of the inference rules below.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   990
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   991
fun MP th1 th2 = th2 RS (th1 RS mp)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   992
  handle THM (msg, _, _) => raise RULES_ERR "MP" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   993
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   994
(*forces the first argument to be a proposition if necessary*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   995
fun DISCH tm thm = Thm.implies_intr (Dcterm.mk_prop tm) thm COMP impI
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   996
  handle THM (msg, _, _) => raise RULES_ERR "DISCH" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   997
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   998
fun DISCH_ALL thm = fold_rev DISCH (#hyps (Thm.crep_thm thm)) thm;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
   999
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1000
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1001
fun FILTER_DISCH_ALL P thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1002
 let fun check tm = P (Thm.term_of tm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1003
 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
  1004
 end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1005
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1006
fun UNDISCH thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1007
   let val tm = Dcterm.mk_prop (#1 (Dcterm.dest_imp (cconcl thm)))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1008
   in Thm.implies_elim (thm RS mp) (ASSUME tm) end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1009
   handle Utils.ERR _ => raise RULES_ERR "UNDISCH" ""
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1010
     | THM _ => raise RULES_ERR "UNDISCH" "";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1011
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1012
fun PROVE_HYP ath bth = MP (DISCH (cconcl ath) bth) ath;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1013
60522
wenzelm
parents: 60521
diff changeset
  1014
fun IMP_TRANS th1 th2 = th2 RS (th1 RS @{thm tfl_imp_trans})
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1015
  handle THM (msg, _, _) => raise RULES_ERR "IMP_TRANS" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1016
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1017
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1018
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1019
 *        Conjunction
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1020
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1021
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1022
fun CONJUNCT1 thm = thm RS conjunct1
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1023
  handle THM (msg, _, _) => raise RULES_ERR "CONJUNCT1" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1024
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1025
fun CONJUNCT2 thm = thm RS conjunct2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1026
  handle THM (msg, _, _) => raise RULES_ERR "CONJUNCT2" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1027
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1028
fun CONJUNCTS th = CONJUNCTS (CONJUNCT1 th) @ CONJUNCTS (CONJUNCT2 th) handle Utils.ERR _ => [th];
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1029
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1030
fun LIST_CONJ [] = raise RULES_ERR "LIST_CONJ" "empty list"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1031
  | LIST_CONJ [th] = th
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1032
  | LIST_CONJ (th :: rst) = MP (MP (conjI COMP (impI RS impI)) th) (LIST_CONJ rst)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1033
      handle THM (msg, _, _) => raise RULES_ERR "LIST_CONJ" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1034
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1035
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1036
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1037
 *        Disjunction
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1038
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1039
local
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1040
  val prop = Thm.prop_of disjI1
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1041
  val [P,Q] = Misc_Legacy.term_vars prop
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1042
  val disj1 = Thm.forall_intr (Thm.cterm_of @{context} Q) disjI1
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1043
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1044
fun DISJ1 thm tm = thm RS (Thm.forall_elim (Dcterm.drop_prop tm) disj1)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1045
  handle THM (msg, _, _) => raise RULES_ERR "DISJ1" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1046
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1047
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1048
local
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1049
  val prop = Thm.prop_of disjI2
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
  1050
  val [P,_] = Misc_Legacy.term_vars prop
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1051
  val disj2 = Thm.forall_intr (Thm.cterm_of @{context} P) disjI2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1052
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1053
fun DISJ2 tm thm = thm RS (Thm.forall_elim (Dcterm.drop_prop tm) disj2)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1054
  handle THM (msg, _, _) => raise RULES_ERR "DISJ2" msg;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1055
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1056
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1057
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1058
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1059
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1060
 *                   A1 |- M1, ..., An |- Mn
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1061
 *     ---------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1062
 *     [A1 |- M1 \/ ... \/ Mn, ..., An |- M1 \/ ... \/ Mn]
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1063
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1064
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1065
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1066
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1067
fun EVEN_ORS thms =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1068
  let fun blue ldisjs [] _ = []
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1069
        | blue ldisjs (th::rst) rdisjs =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1070
            let val tail = tl rdisjs
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1071
                val rdisj_tl = Dcterm.list_mk_disj tail
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1072
            in fold_rev DISJ2 ldisjs (DISJ1 th rdisj_tl)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1073
               :: blue (ldisjs @ [cconcl th]) rst tail
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1074
            end handle Utils.ERR _ => [fold_rev DISJ2 ldisjs th]
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1075
   in blue [] thms (map cconcl thms) end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1076
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1077
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1078
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1079
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1080
 *         A |- P \/ Q   B,P |- R    C,Q |- R
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1081
 *     ---------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1082
 *                     A U B U C |- R
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1083
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1084
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1085
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1086
fun DISJ_CASES th1 th2 th3 =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1087
  let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1088
    val c = Dcterm.drop_prop (cconcl th1);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1089
    val (disj1, disj2) = Dcterm.dest_disj c;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1090
    val th2' = DISCH disj1 th2;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1091
    val th3' = DISCH disj2 th3;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1092
  in
60522
wenzelm
parents: 60521
diff changeset
  1093
    th3' RS (th2' RS (th1 RS @{thm tfl_disjE}))
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1094
      handle THM (msg, _, _) => raise RULES_ERR "DISJ_CASES" msg
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1095
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1096
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1097
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1098
(*-----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1099
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1100
 *       |- A1 \/ ... \/ An     [A1 |- M, ..., An |- M]
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1101
 *     ---------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1102
 *                           |- M
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1103
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1104
 * Note. The list of theorems may be all jumbled up, so we have to
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1105
 * first organize it to align with the first argument (the disjunctive
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1106
 * theorem).
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1107
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1108
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1109
fun organize eq =    (* a bit slow - analogous to insertion sort *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1110
 let fun extract a alist =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1111
     let fun ex (_,[]) = raise RULES_ERR "organize" "not a permutation.1"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1112
           | 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
  1113
     in ex ([],alist)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1114
     end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1115
     fun place [] [] = []
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1116
       | place (a::rst) alist =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1117
           let val (item,next) = extract a alist
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1118
           in item::place rst next
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1119
           end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1120
       | place _ _ = raise RULES_ERR "organize" "not a permutation.2"
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1121
 in place
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1122
 end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1123
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1124
fun DISJ_CASESL disjth thl =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1125
   let val c = cconcl disjth
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1126
       fun eq th atm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1127
        exists (fn t => HOLogic.dest_Trueprop t aconv Thm.term_of atm) (Thm.hyps_of th)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1128
       val tml = Dcterm.strip_disj c
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
  1129
       fun DL _ [] = raise RULES_ERR "DISJ_CASESL" "no cases"
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1130
         | DL th [th1] = PROVE_HYP th th1
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1131
         | DL th [th1,th2] = DISJ_CASES th th1 th2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1132
         | DL th (th1::rst) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1133
            let val tm = #2 (Dcterm.dest_disj (Dcterm.drop_prop(cconcl th)))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1134
             in DISJ_CASES th th1 (DL (ASSUME tm) rst) end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1135
   in DL disjth (organize eq tml thl)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1136
   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1137
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1138
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1139
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1140
 *        Universals
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1141
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1142
local (* this is fragile *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1143
  val prop = Thm.prop_of spec
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1144
  val x = hd (tl (Misc_Legacy.term_vars prop))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1145
  val cTV = Thm.ctyp_of @{context} (type_of x)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1146
  val gspec = Thm.forall_intr (Thm.cterm_of @{context} x) spec
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1147
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1148
fun SPEC tm thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1149
   let val gspec' = Drule.instantiate_normalize ([(cTV, Thm.ctyp_of_cterm tm)], []) gspec
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1150
   in thm RS (Thm.forall_elim tm gspec') end
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
fun SPEC_ALL thm = fold SPEC (#1 (Dcterm.strip_forall(cconcl thm))) thm;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1154
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1155
val ISPEC = SPEC
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1156
val ISPECL = fold ISPEC;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1157
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1158
(* Not optimized! Too complicated. *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1159
local
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1160
  val prop = Thm.prop_of allI
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1161
  val [P] = Misc_Legacy.add_term_vars (prop, [])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1162
  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
  1163
  fun ctm_theta ctxt =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1164
    map (fn (i, (_, tm2)) =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1165
      let val ctm2 = Thm.cterm_of ctxt tm2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1166
      in (Thm.cterm_of ctxt (Var (i, Thm.typ_of_cterm ctm2)), ctm2) end)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1167
  fun certify ctxt (ty_theta,tm_theta) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1168
    (cty_theta ctxt (Vartab.dest ty_theta),
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1169
     ctm_theta ctxt (Vartab.dest tm_theta))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1170
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1171
fun GEN ctxt v th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1172
   let val gth = Thm.forall_intr v th
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1173
       val thy = Proof_Context.theory_of ctxt
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1174
       val Const(@{const_name Pure.all},_)$Abs(x,ty,rst) = Thm.prop_of gth
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1175
       val P' = Abs(x,ty, HOLogic.dest_Trueprop rst)  (* get rid of trueprop *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1176
       val theta = Pattern.match thy (P,P') (Vartab.empty, Vartab.empty);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1177
       val allI2 = Drule.instantiate_normalize (certify ctxt theta) allI
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1178
       val thm = Thm.implies_elim allI2 gth
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1179
       val tp $ (A $ Abs(_,_,M)) = Thm.prop_of thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1180
       val prop' = tp $ (A $ Abs(x,ty,M))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1181
   in ALPHA thm (Thm.cterm_of ctxt prop') end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1182
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1183
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1184
fun GENL ctxt = fold_rev (GEN ctxt);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1185
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1186
fun GEN_ALL ctxt thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1187
  let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1188
    val prop = Thm.prop_of thm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1189
    val vlist = map (Thm.cterm_of ctxt) (Misc_Legacy.add_term_vars (prop, []))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1190
  in GENL ctxt vlist thm end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1191
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1192
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1193
fun MATCH_MP th1 th2 =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1194
   if (Dcterm.is_forall (Dcterm.drop_prop(cconcl th1)))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1195
   then MATCH_MP (th1 RS spec) th2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1196
   else MP th1 th2;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1197
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1198
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1199
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1200
 *        Existentials
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1201
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1202
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1203
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1204
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1205
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1206
 * Existential elimination
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1207
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1208
 *      A1 |- ?x.t[x]   ,   A2, "t[v]" |- t'
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1209
 *      ------------------------------------     (variable v occurs nowhere)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1210
 *                A1 u A2 |- t'
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1211
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1212
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1213
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1214
fun CHOOSE ctxt (fvar, exth) fact =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1215
  let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1216
    val lam = #2 (Dcterm.dest_comb (Dcterm.drop_prop (cconcl exth)))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1217
    val redex = Dcterm.capply lam fvar
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1218
    val t$u = Thm.term_of redex
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1219
    val residue = Thm.cterm_of ctxt (Term.betapply (t, u))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1220
  in
60522
wenzelm
parents: 60521
diff changeset
  1221
    GEN ctxt fvar (DISCH residue fact) RS (exth RS @{thm tfl_exE})
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1222
      handle THM (msg, _, _) => raise RULES_ERR "CHOOSE" msg
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1223
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1224
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1225
local
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1226
  val prop = Thm.prop_of exI
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1227
  val [P, x] = map (Thm.cterm_of @{context}) (Misc_Legacy.term_vars prop)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1228
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1229
fun EXISTS (template,witness) thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1230
  let val abstr = #2 (Dcterm.dest_comb template) in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1231
    thm RS (cterm_instantiate [(P, abstr), (x, witness)] exI)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1232
      handle THM (msg, _, _) => raise RULES_ERR "EXISTS" msg
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1233
  end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1234
end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1235
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1236
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1237
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1238
 *         A |- M
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1239
 *   -------------------   [v_1,...,v_n]
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1240
 *    A |- ?v1...v_n. M
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1241
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1242
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1243
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1244
fun EXISTL vlist th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1245
  fold_rev (fn v => fn thm => EXISTS(Dcterm.mk_exists(v,cconcl thm), v) thm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1246
           vlist th;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1247
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1248
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1249
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1250
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1251
 *       A |- M[x_1,...,x_n]
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1252
 *   ----------------------------   [(x |-> y)_1,...,(x |-> y)_n]
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1253
 *       A |- ?y_1...y_n. M
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1254
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1255
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1256
(* Could be improved, but needs "subst_free" for certified terms *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1257
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1258
fun IT_EXISTS ctxt blist th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1259
  let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1260
    val blist' = map (apply2 Thm.term_of) blist
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1261
    fun ex v M = Thm.cterm_of ctxt (USyntax.mk_exists{Bvar=v,Body = M})
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1262
  in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1263
    fold_rev (fn (b as (r1,r2)) => fn thm =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1264
        EXISTS(ex r2 (subst_free [b]
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1265
                   (HOLogic.dest_Trueprop(Thm.prop_of thm))), Thm.cterm_of ctxt r1)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1266
              thm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1267
       blist' th
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1268
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1269
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1270
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1271
 *  Faster version, that fails for some as yet unknown reason
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1272
 * fun IT_EXISTS blist th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1273
 *    let val {thy,...} = rep_thm th
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1274
 *        val tych = cterm_of thy
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1275
 *        fun detype (x,y) = ((#t o rep_cterm) x, (#t o rep_cterm) y)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1276
 *   in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1277
 *  fold (fn (b as (r1,r2), thm) =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1278
 *  EXISTS(D.mk_exists(r2, tych(subst_free[detype b](#t(rep_cterm(cconcl thm))))),
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1279
 *           r1) thm)  blist th
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1280
 *   end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1281
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1282
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1283
(*----------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1284
 *        Rewriting
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1285
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1286
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1287
fun SUBS ctxt thl =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1288
  rewrite_rule ctxt (map (fn th => th RS eq_reflection handle THM _ => th) thl);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1289
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1290
val rew_conv = Raw_Simplifier.rewrite_cterm (true, false, false) (K (K NONE));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1291
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1292
fun simpl_conv ctxt thl ctm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1293
 rew_conv (ctxt addsimps thl) ctm RS meta_eq_to_obj_eq;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1294
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1295
60522
wenzelm
parents: 60521
diff changeset
  1296
fun RIGHT_ASSOC ctxt = rewrite_rule ctxt @{thms tfl_disj_assoc};
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1297
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1298
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1299
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1300
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1301
 *                  TERMINATION CONDITION EXTRACTION
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1302
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1303
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1304
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1305
(* Object language quantifier, i.e., "!" *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1306
fun Forall v M = USyntax.mk_forall{Bvar=v, Body=M};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1307
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1308
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1309
(* 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
  1310
fun is_cong thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1311
  case (Thm.prop_of thm) of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1312
    (Const(@{const_name Pure.imp},_)$(Const(@{const_name Trueprop},_)$ _) $
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
  1313
      (Const(@{const_name Pure.eq},_) $ (Const (@{const_name Wfrec.cut},_) $ _ $ _ $ a $ x) $ _)) =>
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1314
        false
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1315
  | _ => true;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1316
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1317
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1318
fun dest_equal(Const (@{const_name Pure.eq},_) $
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1319
               (Const (@{const_name Trueprop},_) $ lhs)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1320
               $ (Const (@{const_name Trueprop},_) $ rhs)) = {lhs=lhs, rhs=rhs}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1321
  | dest_equal(Const (@{const_name Pure.eq},_) $ lhs $ rhs) = {lhs=lhs, rhs=rhs}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1322
  | dest_equal tm = USyntax.dest_eq tm;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1323
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1324
fun get_lhs tm = #lhs(dest_equal (HOLogic.dest_Trueprop tm));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1325
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1326
fun dest_all used (Const(@{const_name Pure.all},_) $ (a as Abs _)) = USyntax.dest_abs used a
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1327
  | dest_all _ _ = raise RULES_ERR "dest_all" "not a !!";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1328
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1329
val is_all = can (dest_all []);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1330
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1331
fun strip_all used fm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1332
   if (is_all fm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1333
   then let val ({Bvar, Body}, used') = dest_all used fm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1334
            val (bvs, core, used'') = strip_all used' Body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1335
        in ((Bvar::bvs), core, used'')
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1336
        end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1337
   else ([], fm, used);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1338
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1339
fun break_all(Const(@{const_name Pure.all},_) $ Abs (_,_,body)) = body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1340
  | break_all _ = raise RULES_ERR "break_all" "not a !!";
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1341
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1342
fun list_break_all(Const(@{const_name Pure.all},_) $ Abs (s,ty,body)) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1343
     let val (L,core) = list_break_all body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1344
     in ((s,ty)::L, core)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1345
     end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1346
  | list_break_all tm = ([],tm);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1347
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1348
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1349
 * Rename a term of the form
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1350
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1351
 *      !!x1 ...xn. x1=M1 ==> ... ==> xn=Mn
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1352
 *                  ==> ((%v1...vn. Q) x1 ... xn = g x1 ... xn.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1353
 * to one of
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1354
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1355
 *      !!v1 ... vn. v1=M1 ==> ... ==> vn=Mn
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1356
 *      ==> ((%v1...vn. Q) v1 ... vn = g v1 ... vn.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1357
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1358
 * This prevents name problems in extraction, and helps the result to read
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1359
 * better. There is a problem with varstructs, since they can introduce more
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1360
 * than n variables, and some extra reasoning needs to be done.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1361
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1362
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1363
fun get ([],_,L) = rev L
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1364
  | get (ant::rst,n,L) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1365
      case (list_break_all ant)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1366
        of ([],_) => get (rst, n+1,L)
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
  1367
         | (_,body) =>
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1368
            let val eq = Logic.strip_imp_concl body
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
  1369
                val (f,_) = USyntax.strip_comb (get_lhs eq)
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1370
                val (vstrl,_) = USyntax.strip_abs f
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1371
                val names  =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1372
                  Name.variant_list (Misc_Legacy.add_term_names(body, [])) (map (#1 o dest_Free) vstrl)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1373
            in get (rst, n+1, (names,n)::L) end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1374
            handle TERM _ => get (rst, n+1, L)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1375
              | Utils.ERR _ => get (rst, n+1, L);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1376
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1377
(* Note: Thm.rename_params_rule counts from 1, not 0 *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1378
fun rename thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1379
  let
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1380
    val ants = Logic.strip_imp_prems (Thm.prop_of thm)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1381
    val news = get (ants,1,[])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1382
  in fold Thm.rename_params_rule news thm end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1383
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1384
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1385
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1386
 * Beta-conversion to the rhs of an equation (taken from hol90/drule.sml)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1387
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1388
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1389
fun list_beta_conv tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1390
  let fun rbeta th = Thm.transitive th (Thm.beta_conversion false (#2(Dcterm.dest_eq(cconcl th))))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1391
      fun iter [] = Thm.reflexive tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1392
        | iter (v::rst) = rbeta (Thm.combination(iter rst) (Thm.reflexive v))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1393
  in iter  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1394
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1395
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1396
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1397
 * Trace information for the rewriter
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1398
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1399
val tracing = Unsynchronized.ref false;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1400
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1401
fun say s = if !tracing then writeln s else ();
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1402
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1403
fun print_thms ctxt s L =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1404
  say (cat_lines (s :: map (Display.string_of_thm ctxt) L));
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1405
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1406
fun print_term ctxt s t =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1407
  say (cat_lines [s, Syntax.string_of_term ctxt t]);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1408
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1409
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1410
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1411
 * General abstraction handlers, should probably go in USyntax.
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1412
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1413
fun mk_aabs (vstr, body) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1414
  USyntax.mk_abs {Bvar = vstr, Body = body}
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1415
  handle Utils.ERR _ => USyntax.mk_pabs {varstruct = vstr, body = body};
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1416
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1417
fun list_mk_aabs (vstrl,tm) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1418
    fold_rev (fn vstr => fn tm => mk_aabs(vstr,tm)) vstrl tm;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1419
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1420
fun dest_aabs used tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1421
   let val ({Bvar,Body}, used') = USyntax.dest_abs used tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1422
   in (Bvar, Body, used') end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1423
   handle Utils.ERR _ =>
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1424
     let val {varstruct, body, used} = USyntax.dest_pabs used tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1425
     in (varstruct, body, used) end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1426
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1427
fun strip_aabs used tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1428
   let val (vstr, body, used') = dest_aabs used tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1429
       val (bvs, core, used'') = strip_aabs used' body
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1430
   in (vstr::bvs, core, used'') end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1431
   handle Utils.ERR _ => ([], tm, used);
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1432
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1433
fun dest_combn tm 0 = (tm,[])
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1434
  | dest_combn tm n =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1435
     let val {Rator,Rand} = USyntax.dest_comb tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1436
         val (f,rands) = dest_combn Rator (n-1)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1437
     in (f,Rand::rands)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1438
     end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1439
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1440
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1441
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1442
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1443
local fun dest_pair M = let val {fst,snd} = USyntax.dest_pair M in (fst,snd) end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1444
      fun mk_fst tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1445
          let val ty as Type(@{type_name Product_Type.prod}, [fty,sty]) = type_of tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1446
          in  Const (@{const_name Product_Type.fst}, ty --> fty) $ tm  end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1447
      fun mk_snd tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1448
          let val ty as Type(@{type_name Product_Type.prod}, [fty,sty]) = type_of tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1449
          in  Const (@{const_name Product_Type.snd}, ty --> sty) $ tm  end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1450
in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1451
fun XFILL tych x vstruct =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1452
  let fun traverse p xocc L =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1453
        if (is_Free p)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1454
        then tych xocc::L
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1455
        else let val (p1,p2) = dest_pair p
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1456
             in traverse p1 (mk_fst xocc) (traverse p2  (mk_snd xocc) L)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1457
             end
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1458
  in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1459
  traverse vstruct x []
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1460
end end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1461
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1462
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1463
 * Replace a free tuple (vstr) by a universally quantified variable (a).
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1464
 * Note that the notion of "freeness" for a tuple is different than for a
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1465
 * variable: if variables in the tuple also occur in any other place than
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1466
 * an occurrences of the tuple, they aren't "free" (which is thus probably
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1467
 *  the wrong word to use).
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1468
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1469
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1470
fun VSTRUCT_ELIM ctxt tych a vstr th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1471
  let val L = USyntax.free_vars_lr vstr
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1472
      val bind1 = tych (HOLogic.mk_Trueprop (HOLogic.mk_eq(a,vstr)))
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1473
      val thm1 = Thm.implies_intr bind1 (SUBS ctxt [SYM(Thm.assume bind1)] th)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1474
      val thm2 = forall_intr_list (map tych L) thm1
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1475
      val thm3 = forall_elim_list (XFILL tych a vstr) thm2
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1476
  in refl RS
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1477
     rewrite_rule ctxt [Thm.symmetric (@{thm surjective_pairing} RS eq_reflection)] thm3
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1478
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1479
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1480
fun PGEN ctxt tych a vstr th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1481
  let val a1 = tych a
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1482
      val vstr1 = tych vstr
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1483
  in
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1484
  Thm.forall_intr a1
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1485
     (if (is_Free vstr)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1486
      then cterm_instantiate [(vstr1,a1)] th
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1487
      else VSTRUCT_ELIM ctxt tych a vstr th)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1488
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1489
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1490
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1491
(*---------------------------------------------------------------------------
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1492
 * Takes apart a paired beta-redex, looking like "(\(x,y).N) vstr", into
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1493
 *
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1494
 *     (([x,y],N),vstr)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1495
 *---------------------------------------------------------------------------*)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1496
fun dest_pbeta_redex used M n =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1497
  let val (f,args) = dest_combn M n
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
  1498
      val _ = dest_aabs used f
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1499
  in (strip_aabs used f,args)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1500
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1501
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1502
fun pbeta_redex M n = can (fn t => dest_pbeta_redex [] t n) M;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1503
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1504
fun dest_impl tm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1505
  let val ants = Logic.strip_imp_prems tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1506
      val eq = Logic.strip_imp_concl tm
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1507
  in (ants,get_lhs eq)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1508
  end;
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1509
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1510
fun restricted t = is_some (USyntax.find_term
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1511
                            (fn (Const(@{const_name Wfrec.cut},_)) =>true | _ => false)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1512
                            t)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1513
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1514
fun CONTEXT_REWRITE_RULE main_ctxt (func, G, cut_lemma, congs) th =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1515
 let val globals = func::G
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1516
     val ctxt0 = empty_simpset main_ctxt
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1517
     val pbeta_reduce = simpl_conv ctxt0 [@{thm split_conv} RS eq_reflection];
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1518
     val tc_list = Unsynchronized.ref []: term list Unsynchronized.ref
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1519
     val cut_lemma' = cut_lemma RS eq_reflection
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1520
     fun prover used ctxt thm =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1521
     let fun cong_prover ctxt thm =
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
  1522
         let val _ = say "cong_prover:"
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1523
             val cntxt = Simplifier.prems_of ctxt
60521
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
  1524
             val _ = print_thms ctxt "cntxt:" cntxt
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
  1525
             val _ = say "cong rule:"
52e956416fbf removed dead code;
wenzelm
parents: 60520
diff changeset
  1526
             val _ = say (Display.string_of_thm ctxt thm)
60520
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1527
             (* Unquantified eliminate *)
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1528
             fun uq_eliminate (thm,imp) =
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1529
                 let val tych = Thm.cterm_of ctxt
09fc5eaa21ce moved sources;
wenzelm
parents:
diff changeset
  1530
                     val _ = print_term ctxt "To eliminate:" imp