src/HOLCF/Tools/holcf_library.ML
author huffman
Sun, 28 Feb 2010 15:30:44 -0800
changeset 35475 979019ab92eb
child 35476 8e5eb497b042
permissions -rw-r--r--
move common functions into new file holcf_library.ML
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
     1
(*  Title:      HOLCF/Tools/holcf_library.ML
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
     2
    Author:     Brian Huffman
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
     3
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
     4
Functions for constructing HOLCF types and terms.
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
     5
*)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
     6
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
     7
structure HOLCF_Library =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
     8
struct
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
     9
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    10
(*** Operations from Isabelle/HOL ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    11
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    12
val boolT = HOLogic.boolT;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    13
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    14
val mk_equals = Logic.mk_equals;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    15
val mk_eq = HOLogic.mk_eq;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    16
val mk_trp = HOLogic.mk_Trueprop;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    17
val mk_fst = HOLogic.mk_fst;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    18
val mk_snd = HOLogic.mk_snd;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    19
val mk_not = HOLogic.mk_not;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    20
val mk_conj = HOLogic.mk_conj;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    21
val mk_disj = HOLogic.mk_disj;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    22
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    23
fun mk_ex (x, t) = HOLogic.exists_const (fastype_of x) $ Term.lambda x t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    24
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    25
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    26
(*** Basic HOLCF concepts ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    27
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    28
fun mk_bottom T = Const (@{const_name UU}, T);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    29
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    30
fun below_const T = Const (@{const_name below}, [T, T] ---> boolT);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    31
fun mk_below (t, u) = below_const (fastype_of t) $ t $ u;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    32
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    33
fun mk_undef t = mk_eq (t, mk_bottom (fastype_of t));
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    34
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    35
fun mk_defined t = mk_not (mk_undef t);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    36
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    37
fun mk_compact t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    38
  Const (@{const_name compact}, fastype_of t --> boolT) $ t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    39
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    40
fun mk_cont t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    41
  Const (@{const_name cont}, fastype_of t --> boolT) $ t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    42
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    43
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    44
(*** Continuous function space ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    45
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    46
(* ->> is taken from holcf_logic.ML *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    47
fun mk_cfunT (T, U) = Type(@{type_name "->"}, [T, U]);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    48
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    49
infixr 6 ->>; val (op ->>) = mk_cfunT;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    50
infix -->>; val (op -->>) = Library.foldr mk_cfunT;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    51
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    52
fun dest_cfunT (Type(@{type_name "->"}, [T, U])) = (T, U)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    53
  | dest_cfunT T = raise TYPE ("dest_cfunT", [T], []);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    54
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    55
fun capply_const (S, T) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    56
  Const(@{const_name Rep_CFun}, (S ->> T) --> (S --> T));
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    57
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    58
fun cabs_const (S, T) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    59
  Const(@{const_name Abs_CFun}, (S --> T) --> (S ->> T));
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    60
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    61
fun mk_cabs t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    62
  let val T = fastype_of t
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    63
  in cabs_const (Term.domain_type T, Term.range_type T) $ t end
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    64
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    65
(* builds the expression (% v1 v2 .. vn. rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    66
fun lambdas [] rhs = rhs
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    67
  | lambdas (v::vs) rhs = Term.lambda v (lambdas vs rhs);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    68
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    69
(* builds the expression (LAM v. rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    70
fun big_lambda v rhs =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    71
  cabs_const (fastype_of v, fastype_of rhs) $ Term.lambda v rhs;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    72
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    73
(* builds the expression (LAM v1 v2 .. vn. rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    74
fun big_lambdas [] rhs = rhs
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    75
  | big_lambdas (v::vs) rhs = big_lambda v (big_lambdas vs rhs);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    76
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    77
fun mk_capply (t, u) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    78
  let val (S, T) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    79
    case fastype_of t of
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    80
        Type(@{type_name "->"}, [S, T]) => (S, T)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    81
      | _ => raise TERM ("mk_capply " ^ ML_Syntax.print_list ML_Syntax.print_term [t, u], [t, u]);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    82
  in capply_const (S, T) $ t $ u end;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    83
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    84
infix 9 ` ; val (op `) = mk_capply;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    85
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    86
val list_ccomb : term * term list -> term = Library.foldl mk_capply;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    87
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    88
fun mk_ID T = Const (@{const_name ID}, T ->> T);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    89
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    90
fun cfcomp_const (T, U, V) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    91
  Const (@{const_name cfcomp}, (U ->> V) ->> (T ->> U) ->> (T ->> V));
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    92
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    93
fun mk_cfcomp (f, g) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    94
  let
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    95
    val (U, V) = dest_cfunT (fastype_of f);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    96
    val (T, U') = dest_cfunT (fastype_of g);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    97
  in
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    98
    if U = U'
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    99
    then mk_capply (mk_capply (cfcomp_const (T, U, V), f), g)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   100
    else raise TYPE ("mk_cfcomp", [U, U'], [f, g])
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   101
  end;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   102
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   103
fun mk_strict t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   104
  let val (T, U) = dest_cfunT (fastype_of t);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   105
  in mk_eq (t ` mk_bottom T, mk_bottom U) end;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   106
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   107
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   108
(*** Product type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   109
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   110
val mk_prodT = HOLogic.mk_prodT
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   111
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   112
fun mk_tupleT [] = HOLogic.unitT
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   113
  | mk_tupleT [T] = T
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   114
  | mk_tupleT (T :: Ts) = mk_prodT (T, mk_tupleT Ts);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   115
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   116
(* builds the expression (v1,v2,..,vn) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   117
fun mk_tuple [] = HOLogic.unit
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   118
  | mk_tuple (t::[]) = t
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   119
  | mk_tuple (t::ts) = HOLogic.mk_prod (t, mk_tuple ts);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   120
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   121
(* builds the expression (%(v1,v2,..,vn). rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   122
fun lambda_tuple [] rhs = Term.lambda (Free("unit", HOLogic.unitT)) rhs
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   123
  | lambda_tuple (v::[]) rhs = Term.lambda v rhs
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   124
  | lambda_tuple (v::vs) rhs =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   125
      HOLogic.mk_split (Term.lambda v (lambda_tuple vs rhs));
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   126
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   127
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   128
(*** Lifted cpo type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   129
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   130
fun mk_upT T = Type(@{type_name "u"}, [T]);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   131
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   132
fun dest_upT (Type(@{type_name "u"}, [T])) = T
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   133
  | dest_upT T = raise TYPE ("dest_upT", [T], []);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   134
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   135
fun up_const T = Const(@{const_name up}, T ->> mk_upT T);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   136
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   137
fun mk_up t = up_const (fastype_of t) ` t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   138
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   139
fun fup_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   140
  Const(@{const_name fup}, (T ->> U) ->> mk_upT T ->> U);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   141
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   142
fun from_up T = fup_const (T, T) ` mk_ID T;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   143
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   144
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   145
(*** Strict product type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   146
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   147
val oneT = @{typ "one"};
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   148
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   149
fun mk_sprodT (T, U) = Type(@{type_name "**"}, [T, U]);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   150
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   151
fun dest_sprodT (Type(@{type_name "**"}, [T, U])) = (T, U)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   152
  | dest_sprodT T = raise TYPE ("dest_sprodT", [T], []);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   153
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   154
fun spair_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   155
  Const(@{const_name spair}, T ->> U ->> mk_sprodT (T, U));
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   156
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   157
(* builds the expression (:t, u:) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   158
fun mk_spair (t, u) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   159
  spair_const (fastype_of t, fastype_of u) ` t ` u;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   160
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   161
(* builds the expression (:t1,t2,..,tn:) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   162
fun mk_stuple [] = @{term "ONE"}
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   163
  | mk_stuple (t::[]) = t
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   164
  | mk_stuple (t::ts) = mk_spair (t, mk_stuple ts);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   165
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   166
fun sfst_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   167
  Const(@{const_name sfst}, mk_sprodT (T, U) ->> T);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   168
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   169
fun ssnd_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   170
  Const(@{const_name ssnd}, mk_sprodT (T, U) ->> U);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   171
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   172
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   173
(*** Strict sum type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   174
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   175
fun mk_ssumT (T, U) = Type(@{type_name "++"}, [T, U]);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   176
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   177
fun dest_ssumT (Type(@{type_name "++"}, [T, U])) = (T, U)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   178
  | dest_ssumT T = raise TYPE ("dest_ssumT", [T], []);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   179
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   180
fun sinl_const (T, U) = Const(@{const_name sinl}, T ->> mk_ssumT (T, U));
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   181
fun sinr_const (T, U) = Const(@{const_name sinr}, U ->> mk_ssumT (T, U));
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   182
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   183
(* builds the list [sinl(t1), sinl(sinr(t2)), ... sinr(...sinr(tn))] *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   184
fun mk_sinjects ts =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   185
  let
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   186
    val Ts = map fastype_of ts;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   187
    fun combine (t, T) (us, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   188
      let
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   189
        val v = sinl_const (T, U) ` t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   190
        val vs = map (fn u => sinr_const (T, U) ` u) us;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   191
      in
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   192
        (v::vs, mk_ssumT (T, U))
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   193
      end
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   194
    fun inj [] = error "mk_sinjects: empty list"
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   195
      | inj ((t, T)::[]) = ([t], T)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   196
      | inj ((t, T)::ts) = combine (t, T) (inj ts);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   197
  in
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   198
    fst (inj (ts ~~ Ts))
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   199
  end;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   200
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   201
fun sscase_const (T, U, V) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   202
  Const(@{const_name sscase},
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   203
    (T ->> V) ->> (U ->> V) ->> mk_ssumT (T, U) ->> V);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   204
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   205
fun from_sinl (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   206
  sscase_const (T, U, T) ` mk_ID T ` mk_bottom (U ->> T);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   207
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   208
fun from_sinr (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   209
  sscase_const (T, U, U) ` mk_bottom (T ->> U) ` mk_ID U;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   210
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   211
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   212
(*** pattern match monad type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   213
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   214
fun mk_matchT T = Type (@{type_name "maybe"}, [T]);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   215
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   216
fun dest_matchT (Type(@{type_name "maybe"}, [T])) = T
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   217
  | dest_matchT T = raise TYPE ("dest_matchT", [T], []);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   218
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   219
fun mk_fail T = Const (@{const_name "Fixrec.fail"}, mk_matchT T);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   220
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   221
fun return_const T = Const (@{const_name "Fixrec.return"}, T ->> mk_matchT T);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   222
fun mk_return t = return_const (fastype_of t) ` t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   223
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   224
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   225
(*** lifted boolean type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   226
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   227
val trT = @{typ "tr"};
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   228
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   229
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   230
(*** theory of fixed points ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   231
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   232
fun mk_fix t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   233
  let val (T, _) = dest_cfunT (fastype_of t)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   234
  in mk_capply (Const(@{const_name fix}, (T ->> T) ->> T), t) end;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   235
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   236
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   237
end;