src/HOL/Tools/hologic.ML
author krauss
Tue Sep 28 12:34:41 2010 +0200 (2010-09-28)
changeset 39756 6c8e83d94536
parent 39250 548a3e5521ab
child 40627 becf5d5187cc
permissions -rw-r--r--
consolidated tupled_lambda; moved to structure HOLogic
     1 (*  Title:      HOL/Tools/hologic.ML
     2     Author:     Lawrence C Paulson and Markus Wenzel
     3 
     4 Abstract syntax operations for HOL.
     5 *)
     6 
     7 signature HOLOGIC =
     8 sig
     9   val typeS: sort
    10   val typeT: typ
    11   val boolN: string
    12   val boolT: typ
    13   val Trueprop: term
    14   val mk_Trueprop: term -> term
    15   val dest_Trueprop: term -> term
    16   val true_const: term
    17   val false_const: term
    18   val mk_setT: typ -> typ
    19   val dest_setT: typ -> typ
    20   val Collect_const: typ -> term
    21   val mk_Collect: string * typ * term -> term
    22   val mk_mem: term * term -> term
    23   val dest_mem: term -> term * term
    24   val mk_set: typ -> term list -> term
    25   val dest_set: term -> term list
    26   val mk_UNIV: typ -> term
    27   val conj_intr: thm -> thm -> thm
    28   val conj_elim: thm -> thm * thm
    29   val conj_elims: thm -> thm list
    30   val conj: term
    31   val disj: term
    32   val imp: term
    33   val Not: term
    34   val mk_conj: term * term -> term
    35   val mk_disj: term * term -> term
    36   val mk_imp: term * term -> term
    37   val mk_not: term -> term
    38   val dest_conj: term -> term list
    39   val dest_disj: term -> term list
    40   val disjuncts: term -> term list
    41   val dest_imp: term -> term * term
    42   val dest_not: term -> term
    43   val eq_const: typ -> term
    44   val mk_eq: term * term -> term
    45   val dest_eq: term -> term * term
    46   val all_const: typ -> term
    47   val mk_all: string * typ * term -> term
    48   val list_all: (string * typ) list * term -> term
    49   val exists_const: typ -> term
    50   val mk_exists: string * typ * term -> term
    51   val choice_const: typ -> term
    52   val class_equal: string
    53   val mk_binop: string -> term * term -> term
    54   val mk_binrel: string -> term * term -> term
    55   val dest_bin: string -> typ -> term -> term * term
    56   val unitT: typ
    57   val is_unitT: typ -> bool
    58   val unit: term
    59   val is_unit: term -> bool
    60   val mk_prodT: typ * typ -> typ
    61   val dest_prodT: typ -> typ * typ
    62   val pair_const: typ -> typ -> term
    63   val mk_prod: term * term -> term
    64   val dest_prod: term -> term * term
    65   val mk_fst: term -> term
    66   val mk_snd: term -> term
    67   val split_const: typ * typ * typ -> term
    68   val mk_split: term -> term
    69   val flatten_tupleT: typ -> typ list
    70   val tupled_lambda: term -> term -> term
    71   val mk_tupleT: typ list -> typ
    72   val strip_tupleT: typ -> typ list
    73   val mk_tuple: term list -> term
    74   val strip_tuple: term -> term list
    75   val mk_ptupleT: int list list -> typ list -> typ
    76   val strip_ptupleT: int list list -> typ -> typ list
    77   val flat_tupleT_paths: typ -> int list list
    78   val mk_ptuple: int list list -> typ -> term list -> term
    79   val strip_ptuple: int list list -> term -> term list
    80   val flat_tuple_paths: term -> int list list
    81   val mk_psplits: int list list -> typ -> typ -> term -> term
    82   val strip_psplits: term -> term * typ list * int list list
    83   val natT: typ
    84   val zero: term
    85   val is_zero: term -> bool
    86   val mk_Suc: term -> term
    87   val dest_Suc: term -> term
    88   val Suc_zero: term
    89   val mk_nat: int -> term
    90   val dest_nat: term -> int
    91   val class_size: string
    92   val size_const: typ -> term
    93   val code_numeralT: typ
    94   val intT: typ
    95   val pls_const: term
    96   val min_const: term
    97   val bit0_const: term
    98   val bit1_const: term
    99   val mk_bit: int -> term
   100   val dest_bit: term -> int
   101   val mk_numeral: int -> term
   102   val dest_numeral: term -> int
   103   val number_of_const: typ -> term
   104   val add_numerals: term -> (term * typ) list -> (term * typ) list
   105   val mk_number: typ -> int -> term
   106   val dest_number: term -> typ * int
   107   val realT: typ
   108   val nibbleT: typ
   109   val mk_nibble: int -> term
   110   val dest_nibble: term -> int
   111   val charT: typ
   112   val mk_char: int -> term
   113   val dest_char: term -> int
   114   val listT: typ -> typ
   115   val nil_const: typ -> term
   116   val cons_const: typ -> term
   117   val mk_list: typ -> term list -> term
   118   val dest_list: term -> term list
   119   val stringT: typ
   120   val mk_string: string -> term
   121   val dest_string: term -> string
   122   val literalT: typ
   123   val mk_literal: string -> term
   124   val dest_literal: term -> string
   125   val mk_typerep: typ -> term
   126   val termT: typ
   127   val term_of_const: typ -> term
   128   val mk_term_of: typ -> term -> term
   129   val reflect_term: term -> term
   130   val mk_valtermify_app: string -> (string * typ) list -> typ -> term
   131   val mk_return: typ -> typ -> term -> term
   132   val mk_ST: ((term * typ) * (string * typ) option)  list -> term -> typ -> typ option * typ -> term
   133   val mk_random: typ -> term -> term
   134 end;
   135 
   136 structure HOLogic: HOLOGIC =
   137 struct
   138 
   139 (* HOL syntax *)
   140 
   141 val typeS: sort = ["HOL.type"];
   142 val typeT = Type_Infer.anyT typeS;
   143 
   144 
   145 (* bool and set *)
   146 
   147 val boolN = "HOL.bool";
   148 val boolT = Type (boolN, []);
   149 
   150 val true_const =  Const ("HOL.True", boolT);
   151 val false_const = Const ("HOL.False", boolT);
   152 
   153 fun mk_setT T = T --> boolT;
   154 
   155 fun dest_setT (Type ("fun", [T, Type ("HOL.bool", [])])) = T
   156   | dest_setT T = raise TYPE ("dest_setT: set type expected", [T], []);
   157 
   158 fun mk_set T ts =
   159   let
   160     val sT = mk_setT T;
   161     val empty = Const ("Orderings.bot_class.bot", sT);
   162     fun insert t u = Const ("Set.insert", T --> sT --> sT) $ t $ u;
   163   in fold_rev insert ts empty end;
   164 
   165 fun mk_UNIV T = Const ("Orderings.top_class.top", mk_setT T);
   166 
   167 fun dest_set (Const ("Orderings.bot_class.bot", _)) = []
   168   | dest_set (Const ("Set.insert", _) $ t $ u) = t :: dest_set u
   169   | dest_set t = raise TERM ("dest_set", [t]);
   170 
   171 fun Collect_const T = Const ("Set.Collect", (T --> boolT) --> mk_setT T);
   172 fun mk_Collect (a, T, t) = Collect_const T $ absfree (a, T, t);
   173 
   174 fun mk_mem (x, A) =
   175   let val setT = fastype_of A in
   176     Const ("Set.member", dest_setT setT --> setT --> boolT) $ x $ A
   177   end;
   178 
   179 fun dest_mem (Const ("Set.member", _) $ x $ A) = (x, A)
   180   | dest_mem t = raise TERM ("dest_mem", [t]);
   181 
   182 
   183 (* logic *)
   184 
   185 val Trueprop = Const ("HOL.Trueprop", boolT --> propT);
   186 
   187 fun mk_Trueprop P = Trueprop $ P;
   188 
   189 fun dest_Trueprop (Const ("HOL.Trueprop", _) $ P) = P
   190   | dest_Trueprop t = raise TERM ("dest_Trueprop", [t]);
   191 
   192 fun conj_intr thP thQ =
   193   let
   194     val (P, Q) = pairself (Object_Logic.dest_judgment o Thm.cprop_of) (thP, thQ)
   195       handle CTERM (msg, _) => raise THM (msg, 0, [thP, thQ]);
   196     val inst = Thm.instantiate ([], [(@{cpat "?P::bool"}, P), (@{cpat "?Q::bool"}, Q)]);
   197   in Drule.implies_elim_list (inst @{thm conjI}) [thP, thQ] end;
   198 
   199 fun conj_elim thPQ =
   200   let
   201     val (P, Q) = Thm.dest_binop (Object_Logic.dest_judgment (Thm.cprop_of thPQ))
   202       handle CTERM (msg, _) => raise THM (msg, 0, [thPQ]);
   203     val inst = Thm.instantiate ([], [(@{cpat "?P::bool"}, P), (@{cpat "?Q::bool"}, Q)]);
   204     val thP = Thm.implies_elim (inst @{thm conjunct1}) thPQ;
   205     val thQ = Thm.implies_elim (inst @{thm conjunct2}) thPQ;
   206   in (thP, thQ) end;
   207 
   208 fun conj_elims th =
   209   let val (th1, th2) = conj_elim th
   210   in conj_elims th1 @ conj_elims th2 end handle THM _ => [th];
   211 
   212 val conj = @{term HOL.conj}
   213 and disj = @{term HOL.disj}
   214 and imp = @{term implies}
   215 and Not = @{term Not};
   216 
   217 fun mk_conj (t1, t2) = conj $ t1 $ t2
   218 and mk_disj (t1, t2) = disj $ t1 $ t2
   219 and mk_imp (t1, t2) = imp $ t1 $ t2
   220 and mk_not t = Not $ t;
   221 
   222 fun dest_conj (Const ("HOL.conj", _) $ t $ t') = t :: dest_conj t'
   223   | dest_conj t = [t];
   224 
   225 fun dest_disj (Const ("HOL.disj", _) $ t $ t') = t :: dest_disj t'
   226   | dest_disj t = [t];
   227 
   228 (*Like dest_disj, but flattens disjunctions however nested*)
   229 fun disjuncts_aux (Const ("HOL.disj", _) $ t $ t') disjs = disjuncts_aux t (disjuncts_aux t' disjs)
   230   | disjuncts_aux t disjs = t::disjs;
   231 
   232 fun disjuncts t = disjuncts_aux t [];
   233 
   234 fun dest_imp (Const ("HOL.implies", _) $ A $ B) = (A, B)
   235   | dest_imp  t = raise TERM ("dest_imp", [t]);
   236 
   237 fun dest_not (Const ("HOL.Not", _) $ t) = t
   238   | dest_not t = raise TERM ("dest_not", [t]);
   239 
   240 fun eq_const T = Const ("HOL.eq", T --> T --> boolT);
   241 fun mk_eq (t, u) = eq_const (fastype_of t) $ t $ u;
   242 
   243 fun dest_eq (Const ("HOL.eq", _) $ lhs $ rhs) = (lhs, rhs)
   244   | dest_eq t = raise TERM ("dest_eq", [t])
   245 
   246 fun all_const T = Const ("HOL.All", [T --> boolT] ---> boolT);
   247 fun mk_all (x, T, P) = all_const T $ absfree (x, T, P);
   248 fun list_all (xs, t) = fold_rev (fn (x, T) => fn P => all_const T $ Abs (x, T, P)) xs t;
   249 
   250 fun exists_const T = Const ("HOL.Ex", [T --> boolT] ---> boolT);
   251 fun mk_exists (x, T, P) = exists_const T $ absfree (x, T, P);
   252 
   253 fun choice_const T = Const("Hilbert_Choice.Eps", (T --> boolT) --> T);
   254 
   255 val class_equal = "HOL.equal";
   256 
   257 
   258 (* binary operations and relations *)
   259 
   260 fun mk_binop c (t, u) =
   261   let val T = fastype_of t in
   262     Const (c, [T, T] ---> T) $ t $ u
   263   end;
   264 
   265 fun mk_binrel c (t, u) =
   266   let val T = fastype_of t in
   267     Const (c, [T, T] ---> boolT) $ t $ u
   268   end;
   269 
   270 (*destruct the application of a binary operator. The dummyT case is a crude
   271   way of handling polymorphic operators.*)
   272 fun dest_bin c T (tm as Const (c', Type ("fun", [T', _])) $ t $ u) =
   273       if c = c' andalso (T=T' orelse T=dummyT) then (t, u)
   274       else raise TERM ("dest_bin " ^ c, [tm])
   275   | dest_bin c _ tm = raise TERM ("dest_bin " ^ c, [tm]);
   276 
   277 
   278 (* unit *)
   279 
   280 val unitT = Type ("Product_Type.unit", []);
   281 
   282 fun is_unitT (Type ("Product_Type.unit", [])) = true
   283   | is_unitT _ = false;
   284 
   285 val unit = Const ("Product_Type.Unity", unitT);
   286 
   287 fun is_unit (Const ("Product_Type.Unity", _)) = true
   288   | is_unit _ = false;
   289 
   290 
   291 (* prod *)
   292 
   293 fun mk_prodT (T1, T2) = Type ("Product_Type.prod", [T1, T2]);
   294 
   295 fun dest_prodT (Type ("Product_Type.prod", [T1, T2])) = (T1, T2)
   296   | dest_prodT T = raise TYPE ("dest_prodT", [T], []);
   297 
   298 fun pair_const T1 T2 = Const ("Product_Type.Pair", [T1, T2] ---> mk_prodT (T1, T2));
   299 
   300 fun mk_prod (t1, t2) =
   301   let val T1 = fastype_of t1 and T2 = fastype_of t2 in
   302     pair_const T1 T2 $ t1 $ t2
   303   end;
   304 
   305 fun dest_prod (Const ("Product_Type.Pair", _) $ t1 $ t2) = (t1, t2)
   306   | dest_prod t = raise TERM ("dest_prod", [t]);
   307 
   308 fun mk_fst p =
   309   let val pT = fastype_of p in
   310     Const ("Product_Type.fst", pT --> fst (dest_prodT pT)) $ p
   311   end;
   312 
   313 fun mk_snd p =
   314   let val pT = fastype_of p in
   315     Const ("Product_Type.snd", pT --> snd (dest_prodT pT)) $ p
   316   end;
   317 
   318 fun split_const (A, B, C) =
   319   Const ("Product_Type.prod.prod_case", (A --> B --> C) --> mk_prodT (A, B) --> C);
   320 
   321 fun mk_split t =
   322   (case Term.fastype_of t of
   323     T as (Type ("fun", [A, Type ("fun", [B, C])])) =>
   324       Const ("Product_Type.prod.prod_case", T --> mk_prodT (A, B) --> C) $ t
   325   | _ => raise TERM ("mk_split: bad body type", [t]));
   326 
   327 (*Maps the type T1 * ... * Tn to [T1, ..., Tn], however nested*)
   328 fun flatten_tupleT (Type ("Product_Type.prod", [T1, T2])) = flatten_tupleT T1 @ flatten_tupleT T2
   329   | flatten_tupleT T = [T];
   330 
   331 (*abstraction over nested tuples*)
   332 fun tupled_lambda (x as Free _) b = lambda x b
   333   | tupled_lambda (x as Var _) b = lambda x b
   334   | tupled_lambda (Const ("Product_Type.Pair", _) $ u $ v) b =
   335       mk_split (tupled_lambda u (tupled_lambda v b))
   336   | tupled_lambda (Const ("Product_Type.Unity", _)) b =
   337       Abs ("x", unitT, b)
   338   | tupled_lambda t _ = raise TERM ("tupled_lambda: bad tuple", [t]);
   339 
   340 
   341 (* tuples with right-fold structure *)
   342 
   343 fun mk_tupleT [] = unitT
   344   | mk_tupleT Ts = foldr1 mk_prodT Ts;
   345 
   346 fun strip_tupleT (Type ("Product_Type.unit", [])) = []
   347   | strip_tupleT (Type ("Product_Type.prod", [T1, T2])) = T1 :: strip_tupleT T2
   348   | strip_tupleT T = [T];
   349 
   350 fun mk_tuple [] = unit
   351   | mk_tuple ts = foldr1 mk_prod ts;
   352 
   353 fun strip_tuple (Const ("Product_Type.Unity", _)) = []
   354   | strip_tuple (Const ("Product_Type.Pair", _) $ t1 $ t2) = t1 :: strip_tuple t2
   355   | strip_tuple t = [t];
   356 
   357 
   358 (* tuples with specific arities
   359 
   360    an "arity" of a tuple is a list of lists of integers,
   361    denoting paths to subterms that are pairs
   362 *)
   363 
   364 fun ptuple_err s = raise TERM (s ^ ": inconsistent use of nested products", []);
   365 
   366 fun mk_ptupleT ps =
   367   let
   368     fun mk p Ts =
   369       if member (op =) ps p then
   370         let
   371           val (T, Ts') = mk (1::p) Ts;
   372           val (U, Ts'') = mk (2::p) Ts'
   373         in (mk_prodT (T, U), Ts'') end
   374       else (hd Ts, tl Ts)
   375   in fst o mk [] end;
   376 
   377 fun strip_ptupleT ps =
   378   let
   379     fun factors p T = if member (op =) ps p then (case T of
   380         Type ("Product_Type.prod", [T1, T2]) =>
   381           factors (1::p) T1 @ factors (2::p) T2
   382       | _ => ptuple_err "strip_ptupleT") else [T]
   383   in factors [] end;
   384 
   385 val flat_tupleT_paths =
   386   let
   387     fun factors p (Type ("Product_Type.prod", [T1, T2])) =
   388           p :: factors (1::p) T1 @ factors (2::p) T2
   389       | factors p _ = []
   390   in factors [] end;
   391 
   392 fun mk_ptuple ps =
   393   let
   394     fun mk p T ts =
   395       if member (op =) ps p then (case T of
   396           Type ("Product_Type.prod", [T1, T2]) =>
   397             let
   398               val (t, ts') = mk (1::p) T1 ts;
   399               val (u, ts'') = mk (2::p) T2 ts'
   400             in (pair_const T1 T2 $ t $ u, ts'') end
   401         | _ => ptuple_err "mk_ptuple")
   402       else (hd ts, tl ts)
   403   in fst oo mk [] end;
   404 
   405 fun strip_ptuple ps =
   406   let
   407     fun dest p t = if member (op =) ps p then (case t of
   408         Const ("Product_Type.Pair", _) $ t $ u =>
   409           dest (1::p) t @ dest (2::p) u
   410       | _ => ptuple_err "strip_ptuple") else [t]
   411   in dest [] end;
   412 
   413 val flat_tuple_paths =
   414   let
   415     fun factors p (Const ("Product_Type.Pair", _) $ t $ u) =
   416           p :: factors (1::p) t @ factors (2::p) u
   417       | factors p _ = []
   418   in factors [] end;
   419 
   420 (*In mk_psplits ps S T u, term u expects separate arguments for the factors of S,
   421   with result type T.  The call creates a new term expecting one argument
   422   of type S.*)
   423 fun mk_psplits ps T T3 u =
   424   let
   425     fun ap ((p, T) :: pTs) =
   426           if member (op =) ps p then (case T of
   427               Type ("Product_Type.prod", [T1, T2]) =>
   428                 split_const (T1, T2, map snd pTs ---> T3) $
   429                   ap ((1::p, T1) :: (2::p, T2) :: pTs)
   430             | _ => ptuple_err "mk_psplits")
   431           else Abs ("x", T, ap pTs)
   432       | ap [] =
   433           let val k = length ps
   434           in list_comb (incr_boundvars (k + 1) u, map Bound (k downto 0)) end
   435   in ap [([], T)] end;
   436 
   437 val strip_psplits =
   438   let
   439     fun strip [] qs Ts t = (t, rev Ts, qs)
   440       | strip (p :: ps) qs Ts (Const ("Product_Type.prod.prod_case", _) $ t) =
   441           strip ((1 :: p) :: (2 :: p) :: ps) (p :: qs) Ts t
   442       | strip (p :: ps) qs Ts (Abs (s, T, t)) = strip ps qs (T :: Ts) t
   443       | strip (p :: ps) qs Ts t = strip ps qs
   444           (hd (binder_types (fastype_of1 (Ts, t))) :: Ts)
   445           (incr_boundvars 1 t $ Bound 0)
   446   in strip [[]] [] [] end;
   447 
   448 
   449 (* nat *)
   450 
   451 val natT = Type ("Nat.nat", []);
   452 
   453 val zero = Const ("Groups.zero_class.zero", natT);
   454 
   455 fun is_zero (Const ("Groups.zero_class.zero", _)) = true
   456   | is_zero _ = false;
   457 
   458 fun mk_Suc t = Const ("Nat.Suc", natT --> natT) $ t;
   459 
   460 fun dest_Suc (Const ("Nat.Suc", _) $ t) = t
   461   | dest_Suc t = raise TERM ("dest_Suc", [t]);
   462 
   463 val Suc_zero = mk_Suc zero;
   464 
   465 fun mk_nat n =
   466   let
   467     fun mk 0 = zero
   468       | mk n = mk_Suc (mk (n - 1));
   469   in if n < 0 then raise TERM ("mk_nat: negative number", []) else mk n end;
   470 
   471 fun dest_nat (Const ("Groups.zero_class.zero", _)) = 0
   472   | dest_nat (Const ("Nat.Suc", _) $ t) = dest_nat t + 1
   473   | dest_nat t = raise TERM ("dest_nat", [t]);
   474 
   475 val class_size = "Nat.size";
   476 
   477 fun size_const T = Const ("Nat.size_class.size", T --> natT);
   478 
   479 
   480 (* code numeral *)
   481 
   482 val code_numeralT = Type ("Code_Numeral.code_numeral", []);
   483 
   484 
   485 (* binary numerals and int -- non-unique representation due to leading zeros/ones! *)
   486 
   487 val intT = Type ("Int.int", []);
   488 
   489 val pls_const = Const ("Int.Pls", intT)
   490 and min_const = Const ("Int.Min", intT)
   491 and bit0_const = Const ("Int.Bit0", intT --> intT)
   492 and bit1_const = Const ("Int.Bit1", intT --> intT);
   493 
   494 fun mk_bit 0 = bit0_const
   495   | mk_bit 1 = bit1_const
   496   | mk_bit _ = raise TERM ("mk_bit", []);
   497 
   498 fun dest_bit (Const ("Int.Bit0", _)) = 0
   499   | dest_bit (Const ("Int.Bit1", _)) = 1
   500   | dest_bit t = raise TERM ("dest_bit", [t]);
   501 
   502 fun mk_numeral 0 = pls_const
   503   | mk_numeral ~1 = min_const
   504   | mk_numeral i =
   505       let val (q, r) = Integer.div_mod i 2;
   506       in mk_bit r $ mk_numeral q end;
   507 
   508 fun dest_numeral (Const ("Int.Pls", _)) = 0
   509   | dest_numeral (Const ("Int.Min", _)) = ~1
   510   | dest_numeral (Const ("Int.Bit0", _) $ bs) = 2 * dest_numeral bs
   511   | dest_numeral (Const ("Int.Bit1", _) $ bs) = 2 * dest_numeral bs + 1
   512   | dest_numeral t = raise TERM ("dest_numeral", [t]);
   513 
   514 fun number_of_const T = Const ("Int.number_class.number_of", intT --> T);
   515 
   516 fun add_numerals (Const ("Int.number_class.number_of", Type (_, [_, T])) $ t) = cons (t, T)
   517   | add_numerals (t $ u) = add_numerals t #> add_numerals u
   518   | add_numerals (Abs (_, _, t)) = add_numerals t
   519   | add_numerals _ = I;
   520 
   521 fun mk_number T 0 = Const ("Groups.zero_class.zero", T)
   522   | mk_number T 1 = Const ("Groups.one_class.one", T)
   523   | mk_number T i = number_of_const T $ mk_numeral i;
   524 
   525 fun dest_number (Const ("Groups.zero_class.zero", T)) = (T, 0)
   526   | dest_number (Const ("Groups.one_class.one", T)) = (T, 1)
   527   | dest_number (Const ("Int.number_class.number_of", Type ("fun", [_, T])) $ t) =
   528       (T, dest_numeral t)
   529   | dest_number t = raise TERM ("dest_number", [t]);
   530 
   531 
   532 (* real *)
   533 
   534 val realT = Type ("RealDef.real", []);
   535 
   536 
   537 (* list *)
   538 
   539 fun listT T = Type ("List.list", [T]);
   540 
   541 fun nil_const T = Const ("List.list.Nil", listT T);
   542 
   543 fun cons_const T =
   544   let val lT = listT T
   545   in Const ("List.list.Cons", T --> lT --> lT) end;
   546 
   547 fun mk_list T ts =
   548   let
   549     val lT = listT T;
   550     val Nil = Const ("List.list.Nil", lT);
   551     fun Cons t u = Const ("List.list.Cons", T --> lT --> lT) $ t $ u;
   552   in fold_rev Cons ts Nil end;
   553 
   554 fun dest_list (Const ("List.list.Nil", _)) = []
   555   | dest_list (Const ("List.list.Cons", _) $ t $ u) = t :: dest_list u
   556   | dest_list t = raise TERM ("dest_list", [t]);
   557 
   558 
   559 (* nibble *)
   560 
   561 val nibbleT = Type ("String.nibble", []);
   562 
   563 fun mk_nibble n =
   564   let val s =
   565     if 0 <= n andalso n <= 9 then chr (n + ord "0")
   566     else if 10 <= n andalso n <= 15 then chr (n + ord "A" - 10)
   567     else raise TERM ("mk_nibble", [])
   568   in Const ("String.nibble.Nibble" ^ s, nibbleT) end;
   569 
   570 fun dest_nibble t =
   571   let fun err () = raise TERM ("dest_nibble", [t]) in
   572     (case try (unprefix "String.nibble.Nibble" o fst o Term.dest_Const) t of
   573       NONE => err ()
   574     | SOME c =>
   575         if size c <> 1 then err ()
   576         else if "0" <= c andalso c <= "9" then ord c - ord "0"
   577         else if "A" <= c andalso c <= "F" then ord c - ord "A" + 10
   578         else err ())
   579   end;
   580 
   581 
   582 (* char *)
   583 
   584 val charT = Type ("String.char", []);
   585 
   586 fun mk_char n =
   587   if 0 <= n andalso n <= 255 then
   588     Const ("String.char.Char", nibbleT --> nibbleT --> charT) $
   589       mk_nibble (n div 16) $ mk_nibble (n mod 16)
   590   else raise TERM ("mk_char", []);
   591 
   592 fun dest_char (Const ("String.char.Char", _) $ t $ u) =
   593       dest_nibble t * 16 + dest_nibble u
   594   | dest_char t = raise TERM ("dest_char", [t]);
   595 
   596 
   597 (* string *)
   598 
   599 val stringT = listT charT;
   600 
   601 val mk_string = mk_list charT o map (mk_char o ord) o explode;
   602 val dest_string = implode o map (chr o dest_char) o dest_list;
   603 
   604 
   605 (* literal *)
   606 
   607 val literalT = Type ("String.literal", []);
   608 
   609 fun mk_literal s = Const ("String.STR", stringT --> literalT)
   610       $ mk_string s;
   611 fun dest_literal (Const ("String.STR", _) $ t) =
   612       dest_string t
   613   | dest_literal t = raise TERM ("dest_literal", [t]);
   614 
   615 
   616 (* typerep and term *)
   617 
   618 val typerepT = Type ("Typerep.typerep", []);
   619 
   620 fun mk_typerep (Type (tyco, Ts)) = Const ("Typerep.typerep.Typerep",
   621       literalT --> listT typerepT --> typerepT) $ mk_literal tyco
   622         $ mk_list typerepT (map mk_typerep Ts)
   623   | mk_typerep (T as TFree _) = Const ("Typerep.typerep_class.typerep",
   624       Term.itselfT T --> typerepT) $ Logic.mk_type T;
   625 
   626 val termT = Type ("Code_Evaluation.term", []);
   627 
   628 fun term_of_const T = Const ("Code_Evaluation.term_of_class.term_of", T --> termT);
   629 
   630 fun mk_term_of T t = term_of_const T $ t;
   631 
   632 fun reflect_term (Const (c, T)) =
   633       Const ("Code_Evaluation.Const", literalT --> typerepT --> termT)
   634         $ mk_literal c $ mk_typerep T
   635   | reflect_term (t1 $ t2) =
   636       Const ("Code_Evaluation.App", termT --> termT --> termT)
   637         $ reflect_term t1 $ reflect_term t2
   638   | reflect_term (Abs (v, _, t)) = Abs (v, termT, reflect_term t)
   639   | reflect_term t = t;
   640 
   641 fun mk_valtermify_app c vs T =
   642   let
   643     fun termifyT T = mk_prodT (T, unitT --> termT);
   644     fun valapp T T' = Const ("Code_Evaluation.valapp",
   645       termifyT (T --> T') --> termifyT T --> termifyT T');
   646     fun mk_fTs [] _ = []
   647       | mk_fTs (_ :: Ts) T = (Ts ---> T) :: mk_fTs Ts T;
   648     val Ts = map snd vs;
   649     val t = Const (c, Ts ---> T);
   650     val tt = mk_prod (t, Abs ("u", unitT, reflect_term t));
   651     fun app (fT, (v, T)) t = valapp T fT $ t $ Free (v, termifyT T);
   652   in fold app (mk_fTs Ts T ~~ vs) tt end;
   653 
   654 
   655 (* open state monads *)
   656 
   657 fun mk_return T U x = pair_const T U $ x;
   658 
   659 fun mk_ST clauses t U (someT, V) =
   660   let
   661     val R = case someT of SOME T => mk_prodT (T, V) | NONE => V
   662     fun mk_clause ((t, U), SOME (v, T)) (t', U') =
   663           (Const ("Product_Type.scomp", (U --> mk_prodT (T, U')) --> (T --> U' --> R) --> U --> R)
   664             $ t $ lambda (Free (v, T)) t', U)
   665       | mk_clause ((t, U), NONE) (t', U') =
   666           (Const ("Product_Type.fcomp", (U --> U') --> (U' --> R) --> U --> R)
   667             $ t $ t', U)
   668   in fold_rev mk_clause clauses (t, U) |> fst end;
   669 
   670 
   671 (* random seeds *)
   672 
   673 val random_seedT = mk_prodT (code_numeralT, code_numeralT);
   674 
   675 fun mk_random T t = Const ("Quickcheck.random_class.random", code_numeralT
   676   --> random_seedT --> mk_prodT (mk_prodT (T, unitT --> termT), random_seedT)) $ t;
   677 
   678 end;