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