src/HOLCF/Tools/holcf_library.ML
author huffman
Tue, 02 Mar 2010 17:21:10 -0800
changeset 35525 fa231b86cb1e
parent 35489 dd02201d95b6
child 35555 ec01c27bf580
permissions -rw-r--r--
proper names for types cfun, sprod, ssum
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;
35489
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
    16
val natT = HOLogic.natT;
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    17
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    18
val mk_equals = Logic.mk_equals;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    19
val mk_eq = HOLogic.mk_eq;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    20
val mk_trp = HOLogic.mk_Trueprop;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    21
val mk_fst = HOLogic.mk_fst;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    22
val mk_snd = HOLogic.mk_snd;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    23
val mk_not = HOLogic.mk_not;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    24
val mk_conj = HOLogic.mk_conj;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    25
val mk_disj = HOLogic.mk_disj;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    26
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    27
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
    28
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
(*** Basic HOLCF concepts ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    31
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    32
fun mk_bottom T = Const (@{const_name UU}, T);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    33
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    34
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
    35
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
    36
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    37
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
    38
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    39
fun mk_defined t = mk_not (mk_undef t);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    40
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    41
fun mk_compact t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    42
  Const (@{const_name compact}, fastype_of t --> boolT) $ t;
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
fun mk_cont t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    45
  Const (@{const_name cont}, fastype_of t --> boolT) $ t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    46
35489
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
    47
fun mk_chain t =
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
    48
  Const (@{const_name chain}, Term.fastype_of t --> boolT) $ t;
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
    49
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    50
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    51
(*** Continuous function space ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    52
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    53
(* ->> is taken from holcf_logic.ML *)
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 35489
diff changeset
    54
fun mk_cfunT (T, U) = Type(@{type_name cfun}, [T, U]);
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    55
35476
8e5eb497b042 fix infix declarations
huffman
parents: 35475
diff changeset
    56
val (op ->>) = mk_cfunT;
8e5eb497b042 fix infix declarations
huffman
parents: 35475
diff changeset
    57
val (op -->>) = Library.foldr mk_cfunT;
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    58
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 35489
diff changeset
    59
fun dest_cfunT (Type(@{type_name cfun}, [T, U])) = (T, U)
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    60
  | dest_cfunT T = raise TYPE ("dest_cfunT", [T], []);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    61
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    62
fun capply_const (S, T) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    63
  Const(@{const_name Rep_CFun}, (S ->> T) --> (S --> T));
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
fun cabs_const (S, T) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    66
  Const(@{const_name Abs_CFun}, (S --> T) --> (S ->> T));
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
fun mk_cabs t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    69
  let val T = fastype_of t
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    70
  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
    71
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    72
(* builds the expression (% v1 v2 .. vn. rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    73
fun lambdas [] rhs = rhs
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    74
  | lambdas (v::vs) rhs = Term.lambda v (lambdas vs 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 v. rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    77
fun big_lambda v rhs =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    78
  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
    79
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    80
(* builds the expression (LAM v1 v2 .. vn. rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    81
fun big_lambdas [] rhs = rhs
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    82
  | 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
    83
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    84
fun mk_capply (t, u) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    85
  let val (S, T) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    86
    case fastype_of t of
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 35489
diff changeset
    87
        Type(@{type_name cfun}, [S, T]) => (S, T)
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    88
      | _ => 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
    89
  in capply_const (S, T) $ t $ u end;
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
infix 9 ` ; val (op `) = mk_capply;
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
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
    94
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    95
fun mk_ID T = Const (@{const_name ID}, T ->> T);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    96
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    97
fun cfcomp_const (T, U, V) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    98
  Const (@{const_name cfcomp}, (U ->> V) ->> (T ->> U) ->> (T ->> V));
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    99
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   100
fun mk_cfcomp (f, g) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   101
  let
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   102
    val (U, V) = dest_cfunT (fastype_of f);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   103
    val (T, U') = dest_cfunT (fastype_of g);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   104
  in
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   105
    if U = U'
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   106
    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
   107
    else raise TYPE ("mk_cfcomp", [U, U'], [f, g])
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   108
  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
fun mk_strict t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   111
  let val (T, U) = dest_cfunT (fastype_of t);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   112
  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
   113
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
(*** Product type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   116
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   117
val mk_prodT = HOLogic.mk_prodT
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
fun mk_tupleT [] = HOLogic.unitT
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   120
  | mk_tupleT [T] = T
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   121
  | mk_tupleT (T :: Ts) = mk_prodT (T, mk_tupleT Ts);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   122
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   123
(* builds the expression (v1,v2,..,vn) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   124
fun mk_tuple [] = HOLogic.unit
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   125
  | mk_tuple (t::[]) = t
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   126
  | 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
   127
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   128
(* builds the expression (%(v1,v2,..,vn). rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   129
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
   130
  | lambda_tuple (v::[]) rhs = Term.lambda v rhs
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   131
  | lambda_tuple (v::vs) rhs =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   132
      HOLogic.mk_split (Term.lambda v (lambda_tuple vs rhs));
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   133
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
(*** Lifted cpo type ***)
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_upT T = Type(@{type_name "u"}, [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 dest_upT (Type(@{type_name "u"}, [T])) = T
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   140
  | dest_upT T = raise TYPE ("dest_upT", [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 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
   143
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   144
fun mk_up t = up_const (fastype_of t) ` t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   145
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   146
fun fup_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   147
  Const(@{const_name fup}, (T ->> U) ->> mk_upT T ->> U);
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 from_up T = fup_const (T, T) ` mk_ID T;
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
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   152
(*** Strict product type ***)
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
val oneT = @{typ "one"};
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   155
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 35489
diff changeset
   156
fun mk_sprodT (T, U) = Type(@{type_name sprod}, [T, U]);
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   157
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 35489
diff changeset
   158
fun dest_sprodT (Type(@{type_name sprod}, [T, U])) = (T, U)
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   159
  | dest_sprodT T = raise TYPE ("dest_sprodT", [T], []);
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
fun spair_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   162
  Const(@{const_name spair}, T ->> U ->> mk_sprodT (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 (:t, u:) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   165
fun mk_spair (t, u) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   166
  spair_const (fastype_of t, fastype_of u) ` t ` u;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   167
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   168
(* builds the expression (:t1,t2,..,tn:) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   169
fun mk_stuple [] = @{term "ONE"}
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   170
  | mk_stuple (t::[]) = t
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   171
  | mk_stuple (t::ts) = mk_spair (t, mk_stuple ts);
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
fun sfst_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   174
  Const(@{const_name sfst}, mk_sprodT (T, U) ->> T);
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
fun ssnd_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   177
  Const(@{const_name ssnd}, mk_sprodT (T, U) ->> U);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   178
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
(*** Strict sum type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   181
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 35489
diff changeset
   182
fun mk_ssumT (T, U) = Type(@{type_name ssum}, [T, U]);
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   183
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 35489
diff changeset
   184
fun dest_ssumT (Type(@{type_name ssum}, [T, U])) = (T, U)
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   185
  | dest_ssumT T = raise TYPE ("dest_ssumT", [T], []);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   186
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   187
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
   188
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
   189
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   190
(* 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
   191
fun mk_sinjects ts =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   192
  let
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   193
    val Ts = map fastype_of ts;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   194
    fun combine (t, T) (us, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   195
      let
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   196
        val v = sinl_const (T, U) ` t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   197
        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
   198
      in
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   199
        (v::vs, mk_ssumT (T, U))
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   200
      end
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   201
    fun inj [] = error "mk_sinjects: empty list"
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   202
      | inj ((t, T)::[]) = ([t], T)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   203
      | inj ((t, T)::ts) = combine (t, T) (inj ts);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   204
  in
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   205
    fst (inj (ts ~~ Ts))
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   206
  end;
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 sscase_const (T, U, V) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   209
  Const(@{const_name sscase},
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   210
    (T ->> V) ->> (U ->> V) ->> mk_ssumT (T, U) ->> V);
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
fun from_sinl (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   213
  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
   214
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   215
fun from_sinr (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   216
  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
   217
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
(*** pattern match monad type ***)
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 mk_matchT T = Type (@{type_name "maybe"}, [T]);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   222
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   223
fun dest_matchT (Type(@{type_name "maybe"}, [T])) = T
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   224
  | dest_matchT T = raise TYPE ("dest_matchT", [T], []);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   225
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   226
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
   227
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   228
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
   229
fun mk_return t = return_const (fastype_of t) ` t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   230
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
(*** lifted boolean type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   233
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   234
val trT = @{typ "tr"};
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
(*** theory of fixed points ***)
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
fun mk_fix t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   240
  let val (T, _) = dest_cfunT (fastype_of t)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   241
  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
   242
35489
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
   243
fun iterate_const T =
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
   244
  Const (@{const_name iterate}, natT --> (T ->> T) ->> (T ->> T));
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
   245
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
   246
fun mk_iterate (n, f) =
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
   247
  let val (T, _) = dest_cfunT (Term.fastype_of f);
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
   248
  in (iterate_const T $ n) ` f ` mk_bottom T end;
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   249
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   250
end;