src/HOLCF/Tools/holcf_library.ML
author huffman
Sun, 28 Feb 2010 15:43:09 -0800
changeset 35476 8e5eb497b042
parent 35475 979019ab92eb
child 35489 dd02201d95b6
permissions -rw-r--r--
fix infix declarations
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
35476
8e5eb497b042 fix infix declarations
huffman
parents: 35475
diff changeset
    10
infixr 6 ->>;
8e5eb497b042 fix infix declarations
huffman
parents: 35475
diff changeset
    11
infix -->>;
8e5eb497b042 fix infix declarations
huffman
parents: 35475
diff changeset
    12
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    13
(*** Operations from Isabelle/HOL ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    14
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    15
val boolT = HOLogic.boolT;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    16
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    17
val mk_equals = Logic.mk_equals;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    18
val mk_eq = HOLogic.mk_eq;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    19
val mk_trp = HOLogic.mk_Trueprop;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    20
val mk_fst = HOLogic.mk_fst;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    21
val mk_snd = HOLogic.mk_snd;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    22
val mk_not = HOLogic.mk_not;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    23
val mk_conj = HOLogic.mk_conj;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    24
val mk_disj = HOLogic.mk_disj;
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
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
    27
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    28
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    29
(*** Basic HOLCF concepts ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    30
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    31
fun mk_bottom T = Const (@{const_name UU}, T);
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 below_const T = Const (@{const_name below}, [T, T] ---> boolT);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    34
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
    35
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    36
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
    37
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    38
fun mk_defined t = mk_not (mk_undef 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_compact t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    41
  Const (@{const_name compact}, 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
fun mk_cont t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    44
  Const (@{const_name cont}, fastype_of t --> boolT) $ t;
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
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    47
(*** Continuous function space ***)
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
(* ->> is taken from holcf_logic.ML *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    50
fun mk_cfunT (T, U) = Type(@{type_name "->"}, [T, U]);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    51
35476
8e5eb497b042 fix infix declarations
huffman
parents: 35475
diff changeset
    52
val (op ->>) = mk_cfunT;
8e5eb497b042 fix infix declarations
huffman
parents: 35475
diff changeset
    53
val (op -->>) = Library.foldr mk_cfunT;
35475
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 dest_cfunT (Type(@{type_name "->"}, [T, U])) = (T, U)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    56
  | dest_cfunT T = raise TYPE ("dest_cfunT", [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 capply_const (S, T) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    59
  Const(@{const_name Rep_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 cabs_const (S, T) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    62
  Const(@{const_name Abs_CFun}, (S --> T) --> (S ->> T));
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    63
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    64
fun mk_cabs t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    65
  let val T = fastype_of t
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    66
  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
    67
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    68
(* builds the expression (% v1 v2 .. vn. rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    69
fun lambdas [] rhs = rhs
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    70
  | lambdas (v::vs) rhs = Term.lambda v (lambdas vs rhs);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    71
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    72
(* builds the expression (LAM v. rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    73
fun big_lambda v rhs =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    74
  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
    75
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    76
(* builds the expression (LAM v1 v2 .. vn. rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    77
fun big_lambdas [] rhs = rhs
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    78
  | 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
    79
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    80
fun mk_capply (t, u) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    81
  let val (S, T) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    82
    case fastype_of t of
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    83
        Type(@{type_name "->"}, [S, T]) => (S, T)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    84
      | _ => 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
    85
  in capply_const (S, T) $ t $ u end;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    86
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    87
infix 9 ` ; val (op `) = mk_capply;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    88
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    89
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
    90
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    91
fun mk_ID T = Const (@{const_name ID}, T ->> T);
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 cfcomp_const (T, U, V) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    94
  Const (@{const_name cfcomp}, (U ->> V) ->> (T ->> U) ->> (T ->> V));
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    95
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    96
fun mk_cfcomp (f, g) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    97
  let
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    98
    val (U, V) = dest_cfunT (fastype_of f);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    99
    val (T, U') = dest_cfunT (fastype_of g);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   100
  in
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   101
    if U = U'
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   102
    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
   103
    else raise TYPE ("mk_cfcomp", [U, U'], [f, g])
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   104
  end;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   105
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   106
fun mk_strict t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   107
  let val (T, U) = dest_cfunT (fastype_of t);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   108
  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
   109
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   110
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   111
(*** Product type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   112
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   113
val mk_prodT = HOLogic.mk_prodT
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   114
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   115
fun mk_tupleT [] = HOLogic.unitT
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   116
  | mk_tupleT [T] = T
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   117
  | mk_tupleT (T :: Ts) = mk_prodT (T, mk_tupleT Ts);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   118
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   119
(* builds the expression (v1,v2,..,vn) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   120
fun mk_tuple [] = HOLogic.unit
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   121
  | mk_tuple (t::[]) = t
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   122
  | 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
   123
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   124
(* builds the expression (%(v1,v2,..,vn). rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   125
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
   126
  | lambda_tuple (v::[]) rhs = Term.lambda v rhs
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   127
  | lambda_tuple (v::vs) rhs =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   128
      HOLogic.mk_split (Term.lambda v (lambda_tuple vs rhs));
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
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   131
(*** Lifted cpo type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   132
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   133
fun mk_upT T = Type(@{type_name "u"}, [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 dest_upT (Type(@{type_name "u"}, [T])) = T
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   136
  | dest_upT T = raise TYPE ("dest_upT", [T], []);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   137
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   138
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
   139
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   140
fun mk_up t = up_const (fastype_of t) ` t;
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 fup_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   143
  Const(@{const_name fup}, (T ->> U) ->> mk_upT T ->> U);
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
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
   146
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   147
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   148
(*** Strict product type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   149
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   150
val oneT = @{typ "one"};
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   151
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   152
fun mk_sprodT (T, U) = Type(@{type_name "**"}, [T, U]);
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 dest_sprodT (Type(@{type_name "**"}, [T, U])) = (T, U)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   155
  | dest_sprodT T = raise TYPE ("dest_sprodT", [T], []);
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
fun spair_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   158
  Const(@{const_name spair}, T ->> U ->> mk_sprodT (T, U));
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   159
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   160
(* builds the expression (:t, u:) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   161
fun mk_spair (t, u) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   162
  spair_const (fastype_of t, fastype_of u) ` t ` u;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   163
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   164
(* builds the expression (:t1,t2,..,tn:) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   165
fun mk_stuple [] = @{term "ONE"}
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   166
  | mk_stuple (t::[]) = t
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   167
  | mk_stuple (t::ts) = mk_spair (t, mk_stuple ts);
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 sfst_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   170
  Const(@{const_name sfst}, mk_sprodT (T, U) ->> T);
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
fun ssnd_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   173
  Const(@{const_name ssnd}, mk_sprodT (T, U) ->> U);
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
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   176
(*** Strict sum type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   177
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   178
fun mk_ssumT (T, U) = Type(@{type_name "++"}, [T, U]);
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 dest_ssumT (Type(@{type_name "++"}, [T, U])) = (T, U)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   181
  | dest_ssumT T = raise TYPE ("dest_ssumT", [T], []);
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
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
   184
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
   185
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   186
(* 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
   187
fun mk_sinjects ts =
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 Ts = map fastype_of ts;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   190
    fun combine (t, T) (us, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   191
      let
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   192
        val v = sinl_const (T, U) ` t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   193
        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
   194
      in
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   195
        (v::vs, mk_ssumT (T, U))
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   196
      end
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   197
    fun inj [] = error "mk_sinjects: empty list"
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   198
      | inj ((t, T)::[]) = ([t], T)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   199
      | inj ((t, T)::ts) = combine (t, T) (inj ts);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   200
  in
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   201
    fst (inj (ts ~~ Ts))
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   202
  end;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   203
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   204
fun sscase_const (T, U, V) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   205
  Const(@{const_name sscase},
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   206
    (T ->> V) ->> (U ->> V) ->> mk_ssumT (T, U) ->> V);
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_sinl (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   209
  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
   210
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   211
fun from_sinr (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   212
  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
   213
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   214
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   215
(*** pattern match monad type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   216
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   217
fun mk_matchT T = Type (@{type_name "maybe"}, [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 dest_matchT (Type(@{type_name "maybe"}, [T])) = T
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   220
  | dest_matchT T = raise TYPE ("dest_matchT", [T], []);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   221
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   222
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
   223
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   224
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
   225
fun mk_return t = return_const (fastype_of t) ` t;
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
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   228
(*** lifted boolean type ***)
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
val trT = @{typ "tr"};
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
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   233
(*** theory of fixed points ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   234
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   235
fun mk_fix t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   236
  let val (T, _) = dest_cfunT (fastype_of t)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   237
  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
   238
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   239
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   240
end;