src/HOL/hologic.ML
author paulson
Fri Jul 23 17:25:27 1999 +0200 (1999-07-23)
changeset 7073 a959b4391fd8
parent 6380 32fda1090a13
child 7163 3a2f8fdf4dab
permissions -rw-r--r--
added boolean and binary constants
clasohm@923
     1
(*  Title:      HOL/hologic.ML
clasohm@923
     2
    ID:         $Id$
clasohm@923
     3
    Author:     Lawrence C Paulson and Markus Wenzel
clasohm@923
     4
clasohm@923
     5
Abstract syntax operations for HOL.
clasohm@923
     6
*)
clasohm@923
     7
clasohm@923
     8
signature HOLOGIC =
clasohm@923
     9
sig
clasohm@923
    10
  val termC: class
clasohm@923
    11
  val termS: sort
clasohm@923
    12
  val termTVar: typ
clasohm@923
    13
  val boolT: typ
paulson@7073
    14
  val false_const: term
paulson@7073
    15
  val true_const: term
clasohm@923
    16
  val mk_setT: typ -> typ
clasohm@923
    17
  val dest_setT: typ -> typ
clasohm@923
    18
  val mk_Trueprop: term -> term
clasohm@923
    19
  val dest_Trueprop: term -> term
clasohm@923
    20
  val conj: term
clasohm@923
    21
  val disj: term
clasohm@923
    22
  val imp: term
wenzelm@4571
    23
  val dest_imp: term -> term * term
clasohm@923
    24
  val eq_const: typ -> term
clasohm@923
    25
  val all_const: typ -> term
clasohm@923
    26
  val exists_const: typ -> term
clasohm@923
    27
  val Collect_const: typ -> term
clasohm@923
    28
  val mk_eq: term * term -> term
paulson@6031
    29
  val dest_eq: term -> term * term
clasohm@923
    30
  val mk_all: string * typ * term -> term
clasohm@923
    31
  val mk_exists: string * typ * term -> term
clasohm@923
    32
  val mk_Collect: string * typ * term -> term
clasohm@923
    33
  val mk_mem: term * term -> term
wenzelm@6380
    34
  val dest_mem: term -> term * term
wenzelm@2510
    35
  val mk_binop: string -> term * term -> term
wenzelm@2510
    36
  val mk_binrel: string -> term * term -> term
wenzelm@2510
    37
  val dest_bin: string -> typ -> term -> term * term
wenzelm@4571
    38
  val unitT: typ
wenzelm@4571
    39
  val unit: term
wenzelm@4571
    40
  val is_unit: term -> bool
wenzelm@4571
    41
  val mk_prodT: typ * typ -> typ
wenzelm@4571
    42
  val dest_prodT: typ -> typ * typ
wenzelm@4571
    43
  val mk_prod: term * term -> term
wenzelm@4571
    44
  val dest_prod: term -> term * term
wenzelm@4571
    45
  val mk_fst: term -> term
wenzelm@4571
    46
  val mk_snd: term -> term
berghofe@5096
    47
  val prodT_factors: typ -> typ list
berghofe@5096
    48
  val split_const: typ * typ * typ -> term
berghofe@5096
    49
  val mk_tuple: typ -> term list -> term
wenzelm@5207
    50
  val natT: typ
wenzelm@5207
    51
  val zero: term
wenzelm@5207
    52
  val is_zero: term -> bool
wenzelm@5207
    53
  val mk_Suc: term -> term
wenzelm@5207
    54
  val dest_Suc: term -> term
wenzelm@5207
    55
  val mk_nat: int -> term
wenzelm@5207
    56
  val dest_nat: term -> int
paulson@7073
    57
  val intT: typ
paulson@7073
    58
  val binT: typ
paulson@7073
    59
  val pls_const: term
paulson@7073
    60
  val min_const: term
paulson@7073
    61
  val bit_const: term
clasohm@923
    62
end;
clasohm@923
    63
wenzelm@2510
    64
clasohm@923
    65
structure HOLogic: HOLOGIC =
clasohm@923
    66
struct
clasohm@923
    67
wenzelm@2510
    68
(* basics *)
clasohm@923
    69
clasohm@923
    70
val termC: class = "term";
clasohm@923
    71
val termS: sort = [termC];
clasohm@923
    72
wenzelm@2510
    73
val termTVar = TVar (("'a", 0), termS);
clasohm@923
    74
clasohm@923
    75
wenzelm@2510
    76
(* bool and set *)
clasohm@923
    77
clasohm@923
    78
val boolT = Type ("bool", []);
clasohm@923
    79
paulson@7073
    80
val true_const =  Const ("True", boolT)
paulson@7073
    81
and false_const = Const ("False", boolT);
paulson@7073
    82
clasohm@923
    83
fun mk_setT T = Type ("set", [T]);
clasohm@923
    84
clasohm@923
    85
fun dest_setT (Type ("set", [T])) = T
wenzelm@3794
    86
  | dest_setT T = raise TYPE ("dest_setT: set type expected", [T], []);
clasohm@923
    87
paulson@7073
    88
(* logic *)
clasohm@923
    89
clasohm@923
    90
val Trueprop = Const ("Trueprop", boolT --> propT);
clasohm@923
    91
clasohm@923
    92
fun mk_Trueprop P = Trueprop $ P;
clasohm@923
    93
clasohm@923
    94
fun dest_Trueprop (Const ("Trueprop", _) $ P) = P
wenzelm@3794
    95
  | dest_Trueprop t = raise TERM ("dest_Trueprop", [t]);
clasohm@923
    96
clasohm@923
    97
clasohm@923
    98
val conj = Const ("op &", [boolT, boolT] ---> boolT)
clasohm@923
    99
and disj = Const ("op |", [boolT, boolT] ---> boolT)
clasohm@923
   100
and imp = Const ("op -->", [boolT, boolT] ---> boolT);
clasohm@923
   101
paulson@4466
   102
fun dest_imp (Const("op -->",_) $ A $ B) = (A, B)
paulson@4466
   103
  | dest_imp  t = raise TERM ("dest_imp", [t]);
paulson@4466
   104
clasohm@923
   105
fun eq_const T = Const ("op =", [T, T] ---> boolT);
clasohm@923
   106
fun mk_eq (t, u) = eq_const (fastype_of t) $ t $ u;
clasohm@923
   107
paulson@6031
   108
fun dest_eq (Const ("op =", _) $ lhs $ rhs) = (lhs, rhs)
paulson@6031
   109
  | dest_eq t = raise TERM ("dest_eq", [t])
paulson@6031
   110
clasohm@923
   111
fun all_const T = Const ("All", [T --> boolT] ---> boolT);
clasohm@923
   112
fun mk_all (x, T, P) = all_const T $ absfree (x, T, P);
clasohm@923
   113
clasohm@923
   114
fun exists_const T = Const ("Ex", [T --> boolT] ---> boolT);
clasohm@923
   115
fun mk_exists (x, T, P) = exists_const T $ absfree (x, T, P);
clasohm@923
   116
clasohm@923
   117
fun Collect_const T = Const ("Collect", [T --> boolT] ---> mk_setT T);
clasohm@923
   118
fun mk_Collect (a, T, t) = Collect_const T $ absfree (a, T, t);
clasohm@923
   119
clasohm@923
   120
fun mk_mem (x, A) =
clasohm@923
   121
  let val setT = fastype_of A in
clasohm@923
   122
    Const ("op :", [dest_setT setT, setT] ---> boolT) $ x $ A
clasohm@923
   123
  end;
clasohm@923
   124
wenzelm@6380
   125
fun dest_mem (Const ("op :", _) $ x $ A) = (x, A)
wenzelm@6380
   126
  | dest_mem t = raise TERM ("dest_mem", [t]);
wenzelm@6380
   127
clasohm@923
   128
wenzelm@2510
   129
(* binary oprations and relations *)
wenzelm@2510
   130
wenzelm@2510
   131
fun mk_binop c (t, u) =
wenzelm@2510
   132
  let val T = fastype_of t in
wenzelm@2510
   133
    Const (c, [T, T] ---> T) $ t $ u
wenzelm@2510
   134
  end;
wenzelm@2510
   135
wenzelm@2510
   136
fun mk_binrel c (t, u) =
wenzelm@2510
   137
  let val T = fastype_of t in
wenzelm@2510
   138
    Const (c, [T, T] ---> boolT) $ t $ u
wenzelm@2510
   139
  end;
wenzelm@2510
   140
wenzelm@2510
   141
fun dest_bin c T (tm as Const (c', Type ("fun", [T', _])) $ t $ u) =
wenzelm@2510
   142
      if c = c' andalso T = T' then (t, u)
wenzelm@3794
   143
      else raise TERM ("dest_bin " ^ c, [tm])
wenzelm@3794
   144
  | dest_bin c _ tm = raise TERM ("dest_bin " ^ c, [tm]);
wenzelm@2510
   145
wenzelm@2510
   146
wenzelm@4571
   147
(* unit *)
wenzelm@4571
   148
wenzelm@4571
   149
val unitT = Type ("unit", []);
wenzelm@4571
   150
wenzelm@4571
   151
val unit = Const ("()", unitT);
wenzelm@4571
   152
wenzelm@4571
   153
fun is_unit (Const ("()", _)) = true
wenzelm@4571
   154
  | is_unit _ = false;
wenzelm@4571
   155
wenzelm@4571
   156
wenzelm@4571
   157
(* prod *)
wenzelm@4571
   158
wenzelm@4571
   159
fun mk_prodT (T1, T2) = Type ("*", [T1, T2]);
wenzelm@4571
   160
wenzelm@4571
   161
fun dest_prodT (Type ("*", [T1, T2])) = (T1, T2)
wenzelm@4571
   162
  | dest_prodT T = raise TYPE ("dest_prodT", [T], []);
wenzelm@4571
   163
wenzelm@4571
   164
fun mk_prod (t1, t2) =
wenzelm@4571
   165
  let val T1 = fastype_of t1 and T2 = fastype_of t2 in
wenzelm@4571
   166
    Const ("Pair", [T1, T2] ---> mk_prodT (T1, T2)) $ t1 $ t2
wenzelm@4571
   167
  end;
wenzelm@4571
   168
wenzelm@4571
   169
fun dest_prod (Const ("Pair", _) $ t1 $ t2) = (t1, t2)
wenzelm@4571
   170
  | dest_prod t = raise TERM ("dest_prod", [t]);
wenzelm@4571
   171
wenzelm@4571
   172
fun mk_fst p =
wenzelm@4571
   173
  let val pT = fastype_of p in
wenzelm@4571
   174
    Const ("fst", pT --> fst (dest_prodT pT)) $ p
wenzelm@4571
   175
  end;
wenzelm@4571
   176
wenzelm@4571
   177
fun mk_snd p =
wenzelm@4571
   178
  let val pT = fastype_of p in
wenzelm@4571
   179
    Const ("snd", pT --> snd (dest_prodT pT)) $ p
wenzelm@4571
   180
  end;
wenzelm@4571
   181
berghofe@5096
   182
(*Maps the type T1 * ... * Tn to [T1, ..., Tn], however nested*)
berghofe@5096
   183
fun prodT_factors (Type ("*", [T1, T2])) = prodT_factors T1 @ prodT_factors T2
berghofe@5096
   184
  | prodT_factors T = [T];
berghofe@5096
   185
berghofe@5096
   186
fun split_const (Ta, Tb, Tc) = 
berghofe@5096
   187
    Const ("split", [[Ta, Tb] ---> Tc, mk_prodT (Ta, Tb)] ---> Tc);
berghofe@5096
   188
berghofe@5096
   189
(*Makes a nested tuple from a list, following the product type structure*)
berghofe@5096
   190
fun mk_tuple (Type ("*", [T1, T2])) tms = 
berghofe@5096
   191
        mk_prod (mk_tuple T1 tms, 
berghofe@5096
   192
                 mk_tuple T2 (drop (length (prodT_factors T1), tms)))
berghofe@5096
   193
  | mk_tuple T (t::_) = t;
wenzelm@4571
   194
wenzelm@5207
   195
wenzelm@5207
   196
wenzelm@5207
   197
(* nat *)
wenzelm@5207
   198
wenzelm@5207
   199
val natT = Type ("nat", []);
wenzelm@5207
   200
wenzelm@5207
   201
val zero = Const ("0", natT);
wenzelm@5207
   202
wenzelm@5207
   203
fun is_zero (Const ("0", _)) = true
wenzelm@5207
   204
  | is_zero _ = false;
wenzelm@5207
   205
wenzelm@5207
   206
fun mk_Suc t = Const ("Suc", natT --> natT) $ t;
wenzelm@5207
   207
wenzelm@5207
   208
fun dest_Suc (Const ("Suc", _) $ t) = t
wenzelm@5207
   209
  | dest_Suc t = raise TERM ("dest_Suc", [t]);
wenzelm@5207
   210
wenzelm@5207
   211
fun mk_nat 0 = zero
wenzelm@5207
   212
  | mk_nat n = mk_Suc (mk_nat (n - 1));
wenzelm@5207
   213
wenzelm@5207
   214
fun dest_nat (Const ("0", _)) = 0
wenzelm@5207
   215
  | dest_nat (Const ("Suc", _) $ t) = dest_nat t + 1
wenzelm@5207
   216
  | dest_nat t = raise TERM ("dest_nat", [t]);
wenzelm@5207
   217
wenzelm@5207
   218
paulson@7073
   219
val intT = Type ("IntDef.int", []);
paulson@7073
   220
paulson@7073
   221
paulson@7073
   222
(* binary numerals *)
paulson@7073
   223
paulson@7073
   224
val binT = Type ("Numeral.bin", []);
paulson@7073
   225
paulson@7073
   226
val pls_const =  Const ("Numeral.bin.Pls", binT)
paulson@7073
   227
and min_const = Const ("Numeral.bin.Min", binT)
paulson@7073
   228
and bit_const = Const ("Numeral.bin.Bit", [binT, boolT] ---> binT);
paulson@7073
   229
clasohm@923
   230
end;