src/HOL/HOL.ML
author clasohm
Mon Jan 29 13:48:37 1996 +0100 (1996-01-29)
changeset 1455 52a0271621f3
parent 1338 d2fc3bfaee7f
child 1465 5d7a7e439cec
permissions -rw-r--r--
changed the way simpsets and information about datatypes are stored
clasohm@1455
     1
(*  Title: 	HOL/HOL.ML
clasohm@923
     2
    ID:         $Id$
clasohm@923
     3
    Author: 	Tobias Nipkow
clasohm@923
     4
    Copyright   1991  University of Cambridge
clasohm@923
     5
clasohm@923
     6
For hol.thy
clasohm@923
     7
Derived rules from Appendix of Mike Gordons HOL Report, Cambridge TR 68 
clasohm@923
     8
*)
clasohm@923
     9
clasohm@923
    10
open HOL;
clasohm@923
    11
clasohm@923
    12
clasohm@923
    13
(** Equality **)
clasohm@923
    14
clasohm@923
    15
qed_goal "sym" HOL.thy "s=t ==> t=s"
clasohm@923
    16
 (fn prems => [cut_facts_tac prems 1, etac subst 1, rtac refl 1]);
clasohm@923
    17
clasohm@923
    18
(*calling "standard" reduces maxidx to 0*)
clasohm@923
    19
bind_thm ("ssubst", (sym RS subst));
clasohm@923
    20
clasohm@923
    21
qed_goal "trans" HOL.thy "[| r=s; s=t |] ==> r=t"
clasohm@923
    22
 (fn prems =>
clasohm@923
    23
	[rtac subst 1, resolve_tac prems 1, resolve_tac prems 1]);
clasohm@923
    24
clasohm@923
    25
(*Useful with eresolve_tac for proving equalties from known equalities.
clasohm@923
    26
	a = b
clasohm@923
    27
	|   |
clasohm@923
    28
	c = d	*)
clasohm@923
    29
qed_goal "box_equals" HOL.thy
clasohm@923
    30
    "[| a=b;  a=c;  b=d |] ==> c=d"  
clasohm@923
    31
 (fn prems=>
clasohm@923
    32
  [ (rtac trans 1),
clasohm@923
    33
    (rtac trans 1),
clasohm@923
    34
    (rtac sym 1),
clasohm@923
    35
    (REPEAT (resolve_tac prems 1)) ]);
clasohm@923
    36
clasohm@923
    37
(** Congruence rules for meta-application **)
clasohm@923
    38
clasohm@923
    39
(*similar to AP_THM in Gordon's HOL*)
clasohm@923
    40
qed_goal "fun_cong" HOL.thy "(f::'a=>'b) = g ==> f(x)=g(x)"
clasohm@923
    41
  (fn [prem] => [rtac (prem RS subst) 1, rtac refl 1]);
clasohm@923
    42
clasohm@923
    43
(*similar to AP_TERM in Gordon's HOL and FOL's subst_context*)
clasohm@923
    44
qed_goal "arg_cong" HOL.thy "x=y ==> f(x)=f(y)"
clasohm@923
    45
 (fn [prem] => [rtac (prem RS subst) 1, rtac refl 1]);
clasohm@923
    46
clasohm@923
    47
qed_goal "cong" HOL.thy
clasohm@923
    48
   "[| f = g; (x::'a) = y |] ==> f(x) = g(y)"
clasohm@923
    49
 (fn [prem1,prem2] =>
clasohm@923
    50
   [rtac (prem1 RS subst) 1, rtac (prem2 RS subst) 1, rtac refl 1]);
clasohm@923
    51
clasohm@923
    52
(** Equality of booleans -- iff **)
clasohm@923
    53
clasohm@923
    54
qed_goal "iffI" HOL.thy
clasohm@923
    55
   "[| P ==> Q;  Q ==> P |] ==> P=Q"
clasohm@923
    56
 (fn prems=> [ (REPEAT (ares_tac (prems@[impI, iff RS mp RS mp]) 1)) ]);
clasohm@923
    57
clasohm@923
    58
qed_goal "iffD2" HOL.thy "[| P=Q; Q |] ==> P"
clasohm@923
    59
 (fn prems =>
clasohm@923
    60
	[rtac ssubst 1, resolve_tac prems 1, resolve_tac prems 1]);
clasohm@923
    61
clasohm@923
    62
val iffD1 = sym RS iffD2;
clasohm@923
    63
clasohm@923
    64
qed_goal "iffE" HOL.thy
clasohm@923
    65
    "[| P=Q; [| P --> Q; Q --> P |] ==> R |] ==> R"
clasohm@923
    66
 (fn [p1,p2] => [REPEAT(ares_tac([p1 RS iffD2, p1 RS iffD1, p2, impI])1)]);
clasohm@923
    67
clasohm@923
    68
(** True **)
clasohm@923
    69
clasohm@923
    70
qed_goalw "TrueI" HOL.thy [True_def] "True"
clasohm@923
    71
  (fn _ => [rtac refl 1]);
clasohm@923
    72
clasohm@923
    73
qed_goal "eqTrueI " HOL.thy "P ==> P=True" 
clasohm@923
    74
 (fn prems => [REPEAT(resolve_tac ([iffI,TrueI]@prems) 1)]);
clasohm@923
    75
clasohm@923
    76
qed_goal "eqTrueE" HOL.thy "P=True ==> P" 
clasohm@923
    77
 (fn prems => [REPEAT(resolve_tac (prems@[TrueI,iffD2]) 1)]);
clasohm@923
    78
clasohm@923
    79
(** Universal quantifier **)
clasohm@923
    80
clasohm@923
    81
qed_goalw "allI" HOL.thy [All_def] "(!!x::'a. P(x)) ==> !x. P(x)"
clasohm@923
    82
 (fn prems => [resolve_tac (prems RL [eqTrueI RS ext]) 1]);
clasohm@923
    83
clasohm@923
    84
qed_goalw "spec" HOL.thy [All_def] "! x::'a.P(x) ==> P(x)"
clasohm@923
    85
 (fn prems => [rtac eqTrueE 1, resolve_tac (prems RL [fun_cong]) 1]);
clasohm@923
    86
clasohm@923
    87
qed_goal "allE" HOL.thy "[| !x.P(x);  P(x) ==> R |] ==> R"
clasohm@923
    88
 (fn major::prems=>
clasohm@923
    89
  [ (REPEAT (resolve_tac (prems @ [major RS spec]) 1)) ]);
clasohm@923
    90
clasohm@923
    91
qed_goal "all_dupE" HOL.thy 
clasohm@923
    92
    "[| ! x.P(x);  [| P(x); ! x.P(x) |] ==> R |] ==> R"
clasohm@923
    93
 (fn prems =>
clasohm@923
    94
  [ (REPEAT (resolve_tac (prems @ (prems RL [spec])) 1)) ]);
clasohm@923
    95
clasohm@923
    96
clasohm@923
    97
(** False ** Depends upon spec; it is impossible to do propositional logic
clasohm@923
    98
             before quantifiers! **)
clasohm@923
    99
clasohm@923
   100
qed_goalw "FalseE" HOL.thy [False_def] "False ==> P"
clasohm@923
   101
 (fn [major] => [rtac (major RS spec) 1]);
clasohm@923
   102
clasohm@923
   103
qed_goal "False_neq_True" HOL.thy "False=True ==> P"
clasohm@923
   104
 (fn [prem] => [rtac (prem RS eqTrueE RS FalseE) 1]);
clasohm@923
   105
clasohm@923
   106
clasohm@923
   107
(** Negation **)
clasohm@923
   108
clasohm@923
   109
qed_goalw "notI" HOL.thy [not_def] "(P ==> False) ==> ~P"
clasohm@923
   110
 (fn prems=> [rtac impI 1, eresolve_tac prems 1]);
clasohm@923
   111
clasohm@923
   112
qed_goalw "notE" HOL.thy [not_def] "[| ~P;  P |] ==> R"
clasohm@923
   113
 (fn prems => [rtac (prems MRS mp RS FalseE) 1]);
clasohm@923
   114
clasohm@923
   115
(** Implication **)
clasohm@923
   116
clasohm@923
   117
qed_goal "impE" HOL.thy "[| P-->Q;  P;  Q ==> R |] ==> R"
clasohm@923
   118
 (fn prems=> [ (REPEAT (resolve_tac (prems@[mp]) 1)) ]);
clasohm@923
   119
clasohm@923
   120
(* Reduces Q to P-->Q, allowing substitution in P. *)
clasohm@923
   121
qed_goal "rev_mp" HOL.thy "[| P;  P --> Q |] ==> Q"
clasohm@923
   122
 (fn prems=>  [ (REPEAT (resolve_tac (prems@[mp]) 1)) ]);
clasohm@923
   123
clasohm@923
   124
qed_goal "contrapos" HOL.thy "[| ~Q;  P==>Q |] ==> ~P"
clasohm@923
   125
 (fn [major,minor]=> 
clasohm@923
   126
  [ (rtac (major RS notE RS notI) 1), 
clasohm@923
   127
    (etac minor 1) ]);
clasohm@923
   128
nipkow@1334
   129
qed_goal "rev_contrapos" HOL.thy "[| P==>Q; ~Q |] ==> ~P"
nipkow@1334
   130
 (fn [major,minor]=> 
nipkow@1334
   131
  [ (rtac (minor RS contrapos) 1), (etac major 1) ]);
nipkow@1334
   132
clasohm@923
   133
(* ~(?t = ?s) ==> ~(?s = ?t) *)
nipkow@1334
   134
bind_thm("not_sym", sym COMP rev_contrapos);
clasohm@923
   135
clasohm@923
   136
clasohm@923
   137
(** Existential quantifier **)
clasohm@923
   138
clasohm@923
   139
qed_goalw "exI" HOL.thy [Ex_def] "P(x) ==> ? x::'a.P(x)"
clasohm@923
   140
 (fn prems => [rtac selectI 1, resolve_tac prems 1]);
clasohm@923
   141
clasohm@923
   142
qed_goalw "exE" HOL.thy [Ex_def]
clasohm@923
   143
  "[| ? x::'a.P(x); !!x. P(x) ==> Q |] ==> Q"
clasohm@923
   144
  (fn prems => [REPEAT(resolve_tac prems 1)]);
clasohm@923
   145
clasohm@923
   146
clasohm@923
   147
(** Conjunction **)
clasohm@923
   148
clasohm@923
   149
qed_goalw "conjI" HOL.thy [and_def] "[| P; Q |] ==> P&Q"
clasohm@923
   150
 (fn prems =>
clasohm@923
   151
  [REPEAT (resolve_tac (prems@[allI,impI]) 1 ORELSE etac (mp RS mp) 1)]);
clasohm@923
   152
clasohm@923
   153
qed_goalw "conjunct1" HOL.thy [and_def] "[| P & Q |] ==> P"
clasohm@923
   154
 (fn prems =>
clasohm@923
   155
   [resolve_tac (prems RL [spec] RL [mp]) 1, REPEAT(ares_tac [impI] 1)]);
clasohm@923
   156
clasohm@923
   157
qed_goalw "conjunct2" HOL.thy [and_def] "[| P & Q |] ==> Q"
clasohm@923
   158
 (fn prems =>
clasohm@923
   159
   [resolve_tac (prems RL [spec] RL [mp]) 1, REPEAT(ares_tac [impI] 1)]);
clasohm@923
   160
clasohm@923
   161
qed_goal "conjE" HOL.thy "[| P&Q;  [| P; Q |] ==> R |] ==> R"
clasohm@923
   162
 (fn prems =>
clasohm@923
   163
	 [cut_facts_tac prems 1, resolve_tac prems 1,
clasohm@923
   164
	  etac conjunct1 1, etac conjunct2 1]);
clasohm@923
   165
clasohm@923
   166
(** Disjunction *)
clasohm@923
   167
clasohm@923
   168
qed_goalw "disjI1" HOL.thy [or_def] "P ==> P|Q"
clasohm@923
   169
 (fn [prem] => [REPEAT(ares_tac [allI,impI, prem RSN (2,mp)] 1)]);
clasohm@923
   170
clasohm@923
   171
qed_goalw "disjI2" HOL.thy [or_def] "Q ==> P|Q"
clasohm@923
   172
 (fn [prem] => [REPEAT(ares_tac [allI,impI, prem RSN (2,mp)] 1)]);
clasohm@923
   173
clasohm@923
   174
qed_goalw "disjE" HOL.thy [or_def] "[| P | Q; P ==> R; Q ==> R |] ==> R"
clasohm@923
   175
 (fn [a1,a2,a3] =>
clasohm@923
   176
	[rtac (mp RS mp) 1, rtac spec 1, rtac a1 1,
clasohm@923
   177
	 rtac (a2 RS impI) 1, assume_tac 1, rtac (a3 RS impI) 1, assume_tac 1]);
clasohm@923
   178
clasohm@923
   179
(** CCONTR -- classical logic **)
clasohm@923
   180
clasohm@923
   181
qed_goalw "classical" HOL.thy [not_def]  "(~P ==> P) ==> P"
clasohm@923
   182
 (fn [prem] =>
clasohm@923
   183
   [rtac (True_or_False RS (disjE RS eqTrueE)) 1,  assume_tac 1,
clasohm@923
   184
    rtac (impI RS prem RS eqTrueI) 1,
clasohm@923
   185
    etac subst 1,  assume_tac 1]);
clasohm@923
   186
clasohm@923
   187
val ccontr = FalseE RS classical;
clasohm@923
   188
clasohm@923
   189
(*Double negation law*)
clasohm@923
   190
qed_goal "notnotD" HOL.thy "~~P ==> P"
clasohm@923
   191
 (fn [major]=>
clasohm@923
   192
  [ (rtac classical 1), (eresolve_tac [major RS notE] 1) ]);
clasohm@923
   193
clasohm@923
   194
clasohm@923
   195
(** Unique existence **)
clasohm@923
   196
clasohm@923
   197
qed_goalw "ex1I" HOL.thy [Ex1_def]
clasohm@923
   198
    "[| P(a);  !!x. P(x) ==> x=a |] ==> ?! x. P(x)"
clasohm@923
   199
 (fn prems =>
clasohm@923
   200
  [REPEAT (ares_tac (prems@[exI,conjI,allI,impI]) 1)]);
clasohm@923
   201
clasohm@923
   202
qed_goalw "ex1E" HOL.thy [Ex1_def]
clasohm@923
   203
    "[| ?! x.P(x);  !!x. [| P(x);  ! y. P(y) --> y=x |] ==> R |] ==> R"
clasohm@923
   204
 (fn major::prems =>
clasohm@923
   205
  [rtac (major RS exE) 1, REPEAT (etac conjE 1 ORELSE ares_tac prems 1)]);
clasohm@923
   206
clasohm@923
   207
clasohm@923
   208
(** Select: Hilbert's Epsilon-operator **)
clasohm@923
   209
clasohm@923
   210
(*Easier to apply than selectI: conclusion has only one occurrence of P*)
clasohm@923
   211
qed_goal "selectI2" HOL.thy
clasohm@923
   212
    "[| P(a);  !!x. P(x) ==> Q(x) |] ==> Q(@x.P(x))"
clasohm@923
   213
 (fn prems => [ resolve_tac prems 1, 
clasohm@923
   214
	        rtac selectI 1, 
clasohm@923
   215
		resolve_tac prems 1 ]);
clasohm@923
   216
clasohm@923
   217
qed_goal "select_equality" HOL.thy
clasohm@923
   218
    "[| P(a);  !!x. P(x) ==> x=a |] ==> (@x.P(x)) = a"
clasohm@923
   219
 (fn prems => [ rtac selectI2 1, 
clasohm@923
   220
		REPEAT (ares_tac prems 1) ]);
clasohm@923
   221
clasohm@923
   222
clasohm@923
   223
(** Classical intro rules for disjunction and existential quantifiers *)
clasohm@923
   224
clasohm@923
   225
qed_goal "disjCI" HOL.thy "(~Q ==> P) ==> P|Q"
clasohm@923
   226
 (fn prems=>
clasohm@923
   227
  [ (rtac classical 1),
clasohm@923
   228
    (REPEAT (ares_tac (prems@[disjI1,notI]) 1)),
clasohm@923
   229
    (REPEAT (ares_tac (prems@[disjI2,notE]) 1)) ]);
clasohm@923
   230
clasohm@923
   231
qed_goal "excluded_middle" HOL.thy "~P | P"
clasohm@923
   232
 (fn _ => [ (REPEAT (ares_tac [disjCI] 1)) ]);
clasohm@923
   233
clasohm@923
   234
(*For disjunctive case analysis*)
clasohm@923
   235
fun excluded_middle_tac sP =
clasohm@923
   236
    res_inst_tac [("Q",sP)] (excluded_middle RS disjE);
clasohm@923
   237
clasohm@923
   238
(*Classical implies (-->) elimination. *)
clasohm@923
   239
qed_goal "impCE" HOL.thy "[| P-->Q; ~P ==> R; Q ==> R |] ==> R" 
clasohm@923
   240
 (fn major::prems=>
clasohm@923
   241
  [ rtac (excluded_middle RS disjE) 1,
clasohm@923
   242
    REPEAT (DEPTH_SOLVE_1 (ares_tac (prems @ [major RS mp]) 1))]);
clasohm@923
   243
clasohm@923
   244
(*Classical <-> elimination. *)
clasohm@923
   245
qed_goal "iffCE" HOL.thy
clasohm@923
   246
    "[| P=Q;  [| P; Q |] ==> R;  [| ~P; ~Q |] ==> R |] ==> R"
clasohm@923
   247
 (fn major::prems =>
clasohm@923
   248
  [ (rtac (major RS iffE) 1),
clasohm@923
   249
    (REPEAT (DEPTH_SOLVE_1 
clasohm@923
   250
	(eresolve_tac ([asm_rl,impCE,notE]@prems) 1))) ]);
clasohm@923
   251
clasohm@923
   252
qed_goal "exCI" HOL.thy "(! x. ~P(x) ==> P(a)) ==> ? x.P(x)"
clasohm@923
   253
 (fn prems=>
clasohm@923
   254
  [ (rtac ccontr 1),
clasohm@923
   255
    (REPEAT (ares_tac (prems@[exI,allI,notI,notE]) 1))  ]);
clasohm@923
   256
clasohm@923
   257
clasohm@923
   258
(* case distinction *)
clasohm@923
   259
clasohm@923
   260
qed_goal "case_split_thm" HOL.thy "[| P ==> Q; ~P ==> Q |] ==> Q"
clasohm@923
   261
  (fn [p1,p2] => [cut_facts_tac [excluded_middle] 1, etac disjE 1,
clasohm@923
   262
                  etac p2 1, etac p1 1]);
clasohm@923
   263
clasohm@923
   264
fun case_tac a = res_inst_tac [("P",a)] case_split_thm;
clasohm@923
   265
clasohm@923
   266
(** Standard abbreviations **)
clasohm@923
   267
clasohm@923
   268
fun stac th = rtac(th RS ssubst);
clasohm@923
   269
fun sstac ths = EVERY' (map stac ths);
clasohm@923
   270
fun strip_tac i = REPEAT(resolve_tac [impI,allI] i); 
clasohm@1338
   271
clasohm@1338
   272
clasohm@1338
   273
(*** Load simpdata.ML to be able to initialize HOL's simpset ***)
clasohm@1338
   274
clasohm@1338
   275
(** Applying HypsubstFun to generate hyp_subst_tac **)
clasohm@1338
   276
clasohm@1338
   277
structure Hypsubst_Data =
clasohm@1338
   278
  struct
clasohm@1338
   279
  structure Simplifier = Simplifier
clasohm@1338
   280
  (*Take apart an equality judgement; otherwise raise Match!*)
clasohm@1338
   281
  fun dest_eq (Const("Trueprop",_) $ (Const("op =",_)  $ t $ u)) = (t,u);
clasohm@1338
   282
  val eq_reflection = eq_reflection
clasohm@1338
   283
  val imp_intr = impI
clasohm@1338
   284
  val rev_mp = rev_mp
clasohm@1338
   285
  val subst = subst
clasohm@1338
   286
  val sym = sym
clasohm@1338
   287
  end;
clasohm@1338
   288
clasohm@1338
   289
structure Hypsubst = HypsubstFun(Hypsubst_Data);
clasohm@1338
   290
open Hypsubst;
clasohm@1338
   291
clasohm@1338
   292
(*** Applying ClassicalFun to create a classical prover ***)
clasohm@1338
   293
structure Classical_Data = 
clasohm@1338
   294
  struct
clasohm@1338
   295
  val sizef	= size_of_thm
clasohm@1338
   296
  val mp	= mp
clasohm@1338
   297
  val not_elim	= notE
clasohm@1338
   298
  val classical	= classical
clasohm@1338
   299
  val hyp_subst_tacs=[hyp_subst_tac]
clasohm@1338
   300
  end;
clasohm@1338
   301
clasohm@1338
   302
structure Classical = ClassicalFun(Classical_Data);
clasohm@1338
   303
open Classical;
clasohm@1338
   304
clasohm@1338
   305
(*Propositional rules*)
clasohm@1338
   306
val prop_cs = empty_cs addSIs [refl,TrueI,conjI,disjCI,impI,notI,iffI]
clasohm@1338
   307
                       addSEs [conjE,disjE,impCE,FalseE,iffE];
clasohm@1338
   308
clasohm@1338
   309
(*Quantifier rules*)
clasohm@1338
   310
val HOL_cs = prop_cs addSIs [allI] addIs [exI,ex1I]
clasohm@1338
   311
                     addSEs [exE,ex1E] addEs [allE];
clasohm@1338
   312
clasohm@1338
   313
use     "simpdata.ML";
clasohm@1338
   314
simpset := HOL_ss;
clasohm@1455
   315
clasohm@1455
   316
clasohm@1455
   317
(** Install simpsets and datatypes in theory structure **)
clasohm@1455
   318
exception SS_DATA of simpset;
clasohm@1455
   319
clasohm@1455
   320
let fun merge [] = SS_DATA empty_ss
clasohm@1455
   321
      | merge ss = let val ss = map (fn SS_DATA x => x) ss;
clasohm@1455
   322
                   in SS_DATA (foldl merge_ss (hd ss, tl ss)) end;
clasohm@1455
   323
clasohm@1455
   324
    fun put (SS_DATA ss) = simpset := ss;
clasohm@1455
   325
clasohm@1455
   326
    fun get () = SS_DATA (!simpset);
clasohm@1455
   327
in add_thydata HOL.thy
clasohm@1455
   328
     ("simpset", ThyMethods {merge = merge, put = put, get = get})
clasohm@1455
   329
end;
clasohm@1455
   330
clasohm@1455
   331
exception DT_DATA of string list;
clasohm@1455
   332
val datatypes = ref [] : string list ref;
clasohm@1455
   333
clasohm@1455
   334
let fun merge [] = DT_DATA []
clasohm@1455
   335
      | merge ds = let val ds = map (fn DT_DATA x => x) ds;
clasohm@1455
   336
                   in DT_DATA (foldl (op union) (hd ds, tl ds)) end;
clasohm@1455
   337
clasohm@1455
   338
    fun put (DT_DATA ds) = datatypes := ds;
clasohm@1455
   339
clasohm@1455
   340
    fun get () = DT_DATA (!datatypes);
clasohm@1455
   341
in add_thydata HOL.thy
clasohm@1455
   342
     ("datatypes", ThyMethods {merge = merge, put = put, get = get})
clasohm@1455
   343
end;
clasohm@1455
   344
clasohm@1455
   345
clasohm@1455
   346
add_thy_reader_file "thy_data.ML";