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