src/HOLCF/Tools/holcf_library.ML
author huffman
Sat, 06 Mar 2010 16:02:22 -0800
changeset 35630 8e562d56d404
parent 35555 ec01c27bf580
child 35785 cdaf99fddd17
permissions -rw-r--r--
add case_names attribute to casedist and ind rules
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 -->>;
35555
ec01c27bf580 move function mk_lub into holcf_library.ML
huffman
parents: 35525
diff changeset
    12
infix 9 `;
35476
8e5eb497b042 fix infix declarations
huffman
parents: 35475
diff changeset
    13
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    14
(*** Operations from Isabelle/HOL ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    15
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    16
val boolT = HOLogic.boolT;
35489
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
    17
val natT = HOLogic.natT;
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    18
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    19
val mk_equals = Logic.mk_equals;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    20
val mk_eq = HOLogic.mk_eq;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    21
val mk_trp = HOLogic.mk_Trueprop;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    22
val mk_fst = HOLogic.mk_fst;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    23
val mk_snd = HOLogic.mk_snd;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    24
val mk_not = HOLogic.mk_not;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    25
val mk_conj = HOLogic.mk_conj;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    26
val mk_disj = HOLogic.mk_disj;
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_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
    29
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
(*** Basic HOLCF concepts ***)
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_bottom T = Const (@{const_name UU}, 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 below_const T = Const (@{const_name below}, [T, T] ---> boolT);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    36
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
    37
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    38
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
    39
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    40
fun mk_defined t = mk_not (mk_undef t);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    41
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    42
fun mk_compact t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    43
  Const (@{const_name compact}, fastype_of t --> boolT) $ t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    44
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    45
fun mk_cont t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    46
  Const (@{const_name cont}, fastype_of t --> boolT) $ t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    47
35489
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
    48
fun mk_chain t =
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
    49
  Const (@{const_name chain}, Term.fastype_of t --> boolT) $ t;
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
    50
35555
ec01c27bf580 move function mk_lub into holcf_library.ML
huffman
parents: 35525
diff changeset
    51
fun mk_lub t =
ec01c27bf580 move function mk_lub into holcf_library.ML
huffman
parents: 35525
diff changeset
    52
  let
ec01c27bf580 move function mk_lub into holcf_library.ML
huffman
parents: 35525
diff changeset
    53
    val T = Term.range_type (Term.fastype_of t);
ec01c27bf580 move function mk_lub into holcf_library.ML
huffman
parents: 35525
diff changeset
    54
    val lub_const = Const (@{const_name lub}, (T --> boolT) --> T);
ec01c27bf580 move function mk_lub into holcf_library.ML
huffman
parents: 35525
diff changeset
    55
    val UNIV_const = @{term "UNIV :: nat set"};
ec01c27bf580 move function mk_lub into holcf_library.ML
huffman
parents: 35525
diff changeset
    56
    val image_type = (natT --> T) --> (natT --> boolT) --> T --> boolT;
ec01c27bf580 move function mk_lub into holcf_library.ML
huffman
parents: 35525
diff changeset
    57
    val image_const = Const (@{const_name image}, image_type);
ec01c27bf580 move function mk_lub into holcf_library.ML
huffman
parents: 35525
diff changeset
    58
  in
ec01c27bf580 move function mk_lub into holcf_library.ML
huffman
parents: 35525
diff changeset
    59
    lub_const $ (image_const $ t $ UNIV_const)
ec01c27bf580 move function mk_lub into holcf_library.ML
huffman
parents: 35525
diff changeset
    60
  end;
ec01c27bf580 move function mk_lub into holcf_library.ML
huffman
parents: 35525
diff changeset
    61
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    62
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    63
(*** Continuous function space ***)
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
(* ->> is taken from holcf_logic.ML *)
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 35489
diff changeset
    66
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
    67
35476
8e5eb497b042 fix infix declarations
huffman
parents: 35475
diff changeset
    68
val (op ->>) = mk_cfunT;
8e5eb497b042 fix infix declarations
huffman
parents: 35475
diff changeset
    69
val (op -->>) = Library.foldr mk_cfunT;
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    70
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 35489
diff changeset
    71
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
    72
  | dest_cfunT T = raise TYPE ("dest_cfunT", [T], []);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    73
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    74
fun capply_const (S, T) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    75
  Const(@{const_name Rep_CFun}, (S ->> T) --> (S --> T));
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 cabs_const (S, T) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    78
  Const(@{const_name Abs_CFun}, (S --> T) --> (S ->> T));
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_cabs t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    81
  let val T = fastype_of t
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    82
  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
    83
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    84
(* builds the expression (% v1 v2 .. vn. rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    85
fun lambdas [] rhs = rhs
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    86
  | lambdas (v::vs) rhs = Term.lambda v (lambdas vs rhs);
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
(* builds the expression (LAM v. rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    89
fun big_lambda v rhs =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    90
  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
    91
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    92
(* builds the expression (LAM v1 v2 .. vn. rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    93
fun big_lambdas [] rhs = rhs
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    94
  | 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
    95
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    96
fun mk_capply (t, u) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    97
  let val (S, T) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
    98
    case fastype_of t of
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 35489
diff changeset
    99
        Type(@{type_name cfun}, [S, T]) => (S, T)
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   100
      | _ => 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
   101
  in capply_const (S, T) $ t $ u end;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   102
35555
ec01c27bf580 move function mk_lub into holcf_library.ML
huffman
parents: 35525
diff changeset
   103
val (op `) = mk_capply;
35475
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   104
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   105
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
   106
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   107
fun mk_ID T = Const (@{const_name ID}, T ->> T);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   108
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   109
fun cfcomp_const (T, U, V) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   110
  Const (@{const_name cfcomp}, (U ->> V) ->> (T ->> U) ->> (T ->> V));
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_cfcomp (f, g) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   113
  let
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   114
    val (U, V) = dest_cfunT (fastype_of f);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   115
    val (T, U') = dest_cfunT (fastype_of g);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   116
  in
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   117
    if U = U'
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   118
    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
   119
    else raise TYPE ("mk_cfcomp", [U, U'], [f, g])
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   120
  end;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   121
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   122
fun mk_strict t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   123
  let val (T, U) = dest_cfunT (fastype_of t);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   124
  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
   125
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
(*** Product type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   128
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   129
val mk_prodT = HOLogic.mk_prodT
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
fun mk_tupleT [] = HOLogic.unitT
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   132
  | mk_tupleT [T] = T
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   133
  | mk_tupleT (T :: Ts) = mk_prodT (T, mk_tupleT Ts);
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
(* builds the expression (v1,v2,..,vn) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   136
fun mk_tuple [] = HOLogic.unit
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   137
  | mk_tuple (t::[]) = t
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   138
  | 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
   139
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   140
(* builds the expression (%(v1,v2,..,vn). rhs) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   141
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
   142
  | lambda_tuple (v::[]) rhs = Term.lambda v rhs
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   143
  | lambda_tuple (v::vs) rhs =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   144
      HOLogic.mk_split (Term.lambda v (lambda_tuple vs rhs));
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
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   147
(*** Lifted cpo type ***)
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_upT T = Type(@{type_name "u"}, [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
fun dest_upT (Type(@{type_name "u"}, [T])) = T
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   152
  | dest_upT T = raise TYPE ("dest_upT", [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 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
   155
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   156
fun mk_up t = up_const (fastype_of t) ` t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   157
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   158
fun fup_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   159
  Const(@{const_name fup}, (T ->> U) ->> mk_upT 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
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
   162
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
(*** Strict product type ***)
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
val oneT = @{typ "one"};
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   167
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 35489
diff changeset
   168
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
   169
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 35489
diff changeset
   170
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
   171
  | dest_sprodT T = raise TYPE ("dest_sprodT", [T], []);
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 spair_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   174
  Const(@{const_name spair}, T ->> U ->> mk_sprodT (T, U));
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
(* builds the expression (:t, u:) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   177
fun mk_spair (t, u) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   178
  spair_const (fastype_of t, fastype_of u) ` 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
(* builds the expression (:t1,t2,..,tn:) *)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   181
fun mk_stuple [] = @{term "ONE"}
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   182
  | mk_stuple (t::[]) = t
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   183
  | mk_stuple (t::ts) = mk_spair (t, mk_stuple ts);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   184
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   185
fun sfst_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   186
  Const(@{const_name sfst}, mk_sprodT (T, U) ->> T);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   187
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   188
fun ssnd_const (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   189
  Const(@{const_name ssnd}, mk_sprodT (T, U) ->> U);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   190
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   191
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   192
(*** Strict sum type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   193
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 35489
diff changeset
   194
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
   195
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 35489
diff changeset
   196
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
   197
  | dest_ssumT T = raise TYPE ("dest_ssumT", [T], []);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   198
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   199
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
   200
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
   201
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   202
(* 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
   203
fun mk_sinjects ts =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   204
  let
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   205
    val Ts = map fastype_of ts;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   206
    fun combine (t, T) (us, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   207
      let
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   208
        val v = sinl_const (T, U) ` t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   209
        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
   210
      in
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   211
        (v::vs, mk_ssumT (T, U))
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   212
      end
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   213
    fun inj [] = error "mk_sinjects: empty list"
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   214
      | inj ((t, T)::[]) = ([t], T)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   215
      | inj ((t, T)::ts) = combine (t, T) (inj ts);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   216
  in
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   217
    fst (inj (ts ~~ Ts))
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   218
  end;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   219
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   220
fun sscase_const (T, U, V) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   221
  Const(@{const_name sscase},
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   222
    (T ->> V) ->> (U ->> V) ->> mk_ssumT (T, U) ->> V);
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 from_sinl (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   225
  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
   226
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   227
fun from_sinr (T, U) =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   228
  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
   229
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
(*** pattern match monad type ***)
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
fun mk_matchT T = Type (@{type_name "maybe"}, [T]);
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 dest_matchT (Type(@{type_name "maybe"}, [T])) = T
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   236
  | dest_matchT T = raise TYPE ("dest_matchT", [T], []);
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   237
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   238
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
   239
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   240
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
   241
fun mk_return t = return_const (fastype_of t) ` t;
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   242
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   243
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   244
(*** lifted boolean type ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   245
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   246
val trT = @{typ "tr"};
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   247
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   248
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   249
(*** theory of fixed points ***)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   250
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   251
fun mk_fix t =
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   252
  let val (T, _) = dest_cfunT (fastype_of t)
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   253
  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
   254
35489
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
   255
fun iterate_const T =
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
   256
  Const (@{const_name iterate}, natT --> (T ->> T) ->> (T ->> T));
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
   257
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
   258
fun mk_iterate (n, f) =
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
   259
  let val (T, _) = dest_cfunT (Term.fastype_of f);
dd02201d95b6 add function define_take_functions
huffman
parents: 35476
diff changeset
   260
  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
   261
979019ab92eb move common functions into new file holcf_library.ML
huffman
parents:
diff changeset
   262
end;