src/HOLCF/Tools/Domain/domain_library.ML
author huffman
Mon Mar 22 15:05:20 2010 -0700 (2010-03-22)
changeset 35905 3d699b736ff4
parent 35562 e27550a842b9
child 35912 b0e300bd3a2c
permissions -rw-r--r--
fix ML warning in domain_library.ML
haftmann@32126
     1
(*  Title:      HOLCF/Tools/Domain/domain_library.ML
wenzelm@23152
     2
    Author:     David von Oheimb
wenzelm@23152
     3
wenzelm@23152
     4
Library for domain command.
wenzelm@23152
     5
*)
wenzelm@23152
     6
wenzelm@23152
     7
huffman@31006
     8
(* infix syntax *)
huffman@31006
     9
huffman@31006
    10
infixr 5 -->;
huffman@31006
    11
infixr 6 ->>;
huffman@31006
    12
infixr 0 ===>;
huffman@31006
    13
infixr 0 ==>;
huffman@31006
    14
infix 0 ==;
huffman@31006
    15
infix 1 ===;
huffman@31006
    16
infix 1 ~=;
huffman@31006
    17
huffman@31006
    18
infix 9 `  ;
huffman@31006
    19
infix 9 `% ;
huffman@31006
    20
infix 9 `%%;
huffman@31006
    21
huffman@31006
    22
wenzelm@23152
    23
(* ----- specific support for domain ---------------------------------------- *)
wenzelm@23152
    24
huffman@31006
    25
signature DOMAIN_LIBRARY =
huffman@31006
    26
sig
huffman@35465
    27
  val first  : 'a * 'b * 'c -> 'a
huffman@35465
    28
  val second : 'a * 'b * 'c -> 'b
huffman@35465
    29
  val third  : 'a * 'b * 'c -> 'c
huffman@35465
    30
  val upd_second : ('b -> 'd) -> 'a * 'b * 'c -> 'a * 'd * 'c
huffman@35465
    31
  val upd_third  : ('c -> 'd) -> 'a * 'b * 'c -> 'a * 'b * 'd
huffman@35465
    32
  val mapn : (int -> 'a -> 'b) -> int -> 'a list -> 'b list
huffman@35465
    33
  val atomize : Proof.context -> thm -> thm list
huffman@35465
    34
huffman@31006
    35
  val Imposs : string -> 'a;
huffman@31162
    36
  val cpo_type : theory -> typ -> bool;
huffman@31006
    37
  val pcpo_type : theory -> typ -> bool;
huffman@31006
    38
  val string_of_typ : theory -> typ -> string;
huffman@31006
    39
huffman@31006
    40
  (* Creating HOLCF types *)
huffman@31006
    41
  val mk_ssumT : typ * typ -> typ;
huffman@31006
    42
  val mk_sprodT : typ * typ -> typ;
huffman@31006
    43
  val mk_uT : typ -> typ;
huffman@31006
    44
  val oneT : typ;
huffman@31006
    45
  val pcpoS : sort;
huffman@31006
    46
huffman@31006
    47
  (* Creating HOLCF terms *)
huffman@31006
    48
  val %: : string -> term;
huffman@31006
    49
  val %%: : string -> term;
huffman@31006
    50
  val ` : term * term -> term;
huffman@31006
    51
  val `% : term * string -> term;
huffman@31006
    52
  val UU : term;
huffman@31231
    53
  val ID : term;
huffman@31006
    54
  val list_ccomb : term * term list -> term;
huffman@31006
    55
  val con_app2 : string -> ('a -> term) -> 'a list -> term;
huffman@35497
    56
  val prj : ('a -> 'b -> 'a) -> ('a -> 'b -> 'a) -> 'a -> 'b list -> int -> 'a
huffman@31006
    57
  val proj : term -> 'a list -> int -> term;
huffman@31006
    58
huffman@31006
    59
  (* Creating propositions *)
huffman@31006
    60
  val mk_conj : term * term -> term;
huffman@31006
    61
  val mk_disj : term * term -> term;
huffman@31006
    62
  val mk_imp : term * term -> term;
huffman@31006
    63
  val mk_lam : string * term -> term;
huffman@31006
    64
  val mk_all : string * term -> term;
huffman@31006
    65
  val mk_ex : string * term -> term;
huffman@31006
    66
  val mk_constrainall : string * typ * term -> term;
huffman@31006
    67
  val === : term * term -> term;
huffman@31006
    68
  val defined : term -> term;
huffman@31006
    69
  val mk_adm : term -> term;
huffman@31006
    70
  val lift : ('a -> term) -> 'a list * term -> term;
huffman@31006
    71
  val lift_defined : ('a -> term) -> 'a list * term -> term;
huffman@31006
    72
huffman@31006
    73
  (* Creating meta-propositions *)
huffman@31006
    74
  val mk_trp : term -> term; (* HOLogic.mk_Trueprop *)
huffman@31006
    75
  val == : term * term -> term;
huffman@31006
    76
  val ===> : term * term -> term;
huffman@31006
    77
  val mk_All : string * term -> term;
huffman@31006
    78
huffman@31288
    79
      (* Domain specifications *)
huffman@31288
    80
      eqtype arg;
huffman@35521
    81
  type cons = string * arg list;
huffman@31006
    82
  type eq = (string * typ list) * cons list;
huffman@35519
    83
  val mk_arg : (bool * Datatype.dtyp) * string -> arg;
huffman@31006
    84
  val is_lazy : arg -> bool;
huffman@31006
    85
  val rec_of : arg -> int;
haftmann@31738
    86
  val dtyp_of : arg -> Datatype.dtyp;
huffman@31006
    87
  val vname : arg -> string;
huffman@31006
    88
  val upd_vname : (string -> string) -> arg -> arg;
huffman@31006
    89
  val is_rec : arg -> bool;
huffman@31006
    90
  val is_nonlazy_rec : arg -> bool;
huffman@31006
    91
  val nonlazy : arg list -> string list;
huffman@31006
    92
  val nonlazy_rec : arg list -> string list;
huffman@31006
    93
  val %# : arg -> term;
huffman@31006
    94
  val bound_arg : ''a list -> ''a -> term; (* ''a = arg or string *)
huffman@31006
    95
  val idx_name : 'a list -> string -> int -> string;
huffman@31228
    96
  val con_app : string -> arg list -> term;
huffman@31006
    97
end;
huffman@31006
    98
huffman@31023
    99
structure Domain_Library :> DOMAIN_LIBRARY =
huffman@31006
   100
struct
wenzelm@23152
   101
huffman@35465
   102
fun first  (x,_,_) = x;
huffman@35465
   103
fun second (_,x,_) = x;
huffman@35465
   104
fun third  (_,_,x) = x;
huffman@35465
   105
huffman@35465
   106
fun upd_first  f (x,y,z) = (f x,   y,   z);
huffman@35465
   107
fun upd_second f (x,y,z) = (  x, f y,   z);
huffman@35465
   108
fun upd_third  f (x,y,z) = (  x,   y, f z);
huffman@35465
   109
huffman@35465
   110
fun mapn f n []      = []
huffman@35465
   111
  | mapn f n (x::xs) = (f n x) :: mapn f (n+1) xs;
huffman@35465
   112
huffman@35465
   113
fun foldr'' f (l,f2) =
huffman@35465
   114
    let fun itr []  = raise Fail "foldr''" 
huffman@35465
   115
          | itr [a] = f2 a
huffman@35465
   116
          | itr (a::l) = f(a, itr l)
huffman@35465
   117
    in  itr l  end;
huffman@35465
   118
huffman@35465
   119
fun atomize ctxt thm =
huffman@35465
   120
    let
huffman@35465
   121
      val r_inst = read_instantiate ctxt;
huffman@35465
   122
      fun at thm =
huffman@35465
   123
          case concl_of thm of
huffman@35465
   124
            _$(Const("op &",_)$_$_)       => at(thm RS conjunct1)@at(thm RS conjunct2)
huffman@35465
   125
          | _$(Const("All" ,_)$Abs(s,_,_))=> at(thm RS (r_inst [(("x", 0), "?" ^ s)] spec))
huffman@35465
   126
          | _                             => [thm];
huffman@35465
   127
    in map zero_var_indexes (at thm) end;
huffman@35465
   128
wenzelm@23152
   129
exception Impossible of string;
wenzelm@23152
   130
fun Imposs msg = raise Impossible ("Domain:"^msg);
wenzelm@23152
   131
huffman@31162
   132
fun cpo_type sg t = Sign.of_sort sg (Sign.certify_typ sg t, @{sort cpo});
huffman@30910
   133
fun pcpo_type sg t = Sign.of_sort sg (Sign.certify_typ sg t, @{sort pcpo});
wenzelm@26939
   134
fun string_of_typ sg = Syntax.string_of_typ_global sg o Sign.certify_typ sg;
wenzelm@23152
   135
wenzelm@23152
   136
(* ----- constructor list handling ----- *)
wenzelm@23152
   137
huffman@31006
   138
type arg =
haftmann@31738
   139
     (bool * Datatype.dtyp) *   (*  (lazy, recursive element) *)
huffman@31288
   140
     string;                       (*   argument name    *)
huffman@31006
   141
huffman@31006
   142
type cons =
huffman@31288
   143
     string *         (* operator name of constr *)
huffman@31288
   144
     arg list;        (* argument list      *)
huffman@31006
   145
huffman@31006
   146
type eq =
huffman@31288
   147
     (string *        (* name      of abstracted type *)
huffman@31288
   148
      typ list) *     (* arguments of abstracted type *)
huffman@31288
   149
     cons list;       (* represented type, as a constructor list *)
wenzelm@23152
   150
huffman@31228
   151
val mk_arg = I;
huffman@31229
   152
huffman@35519
   153
fun rec_of ((_,dtyp),_) =
haftmann@33971
   154
    case dtyp of Datatype_Aux.DtRec i => i | _ => ~1;
huffman@31229
   155
(* FIXME: what about indirect recursion? *)
huffman@31229
   156
huffman@35519
   157
fun is_lazy arg = fst (fst arg);
huffman@35519
   158
fun dtyp_of arg = snd (fst arg);
huffman@35519
   159
val     vname =       snd;
huffman@35519
   160
val upd_vname =   apsnd;
wenzelm@23152
   161
fun is_rec         arg = rec_of arg >=0;
wenzelm@23152
   162
fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg);
wenzelm@33317
   163
fun nonlazy     args   = map vname (filter_out is_lazy args);
wenzelm@33317
   164
fun nonlazy_rec args   = map vname (filter is_nonlazy_rec args);
wenzelm@23152
   165
huffman@31231
   166
wenzelm@23152
   167
(* ----- support for type and mixfix expressions ----- *)
wenzelm@23152
   168
huffman@30910
   169
fun mk_uT T = Type(@{type_name "u"}, [T]);
huffman@35525
   170
fun mk_sprodT (T, U) = Type(@{type_name sprod}, [T, U]);
huffman@35525
   171
fun mk_ssumT (T, U) = Type(@{type_name ssum}, [T, U]);
huffman@30910
   172
val oneT = @{typ one};
huffman@30910
   173
wenzelm@23152
   174
(* ----- support for term expressions ----- *)
wenzelm@23152
   175
wenzelm@23152
   176
fun %: s = Free(s,dummyT);
wenzelm@23152
   177
fun %# arg = %:(vname arg);
wenzelm@23152
   178
fun %%: s = Const(s,dummyT);
wenzelm@23152
   179
wenzelm@23152
   180
local open HOLogic in
wenzelm@23152
   181
val mk_trp = mk_Trueprop;
wenzelm@23152
   182
fun mk_conj (S,T) = conj $ S $ T;
wenzelm@23152
   183
fun mk_disj (S,T) = disj $ S $ T;
wenzelm@23152
   184
fun mk_imp  (S,T) = imp  $ S $ T;
wenzelm@23152
   185
fun mk_lam  (x,T) = Abs(x,dummyT,T);
wenzelm@23152
   186
fun mk_all  (x,P) = HOLogic.mk_all (x,dummyT,P);
wenzelm@23152
   187
fun mk_ex   (x,P) = mk_exists (x,dummyT,P);
wenzelm@24680
   188
fun mk_constrainall (x,typ,P) = %%:"All" $ (TypeInfer.constrain (typ --> boolT) (mk_lam(x,P)));
wenzelm@23152
   189
end
wenzelm@23152
   190
wenzelm@23152
   191
fun mk_All  (x,P) = %%:"all" $ mk_lam(x,P); (* meta universal quantification *)
wenzelm@23152
   192
wenzelm@23152
   193
infixr 0 ===>;  fun S ===> T = %%:"==>" $ S $ T;
wenzelm@23152
   194
infix 0 ==;     fun S ==  T = %%:"==" $ S $ T;
wenzelm@23152
   195
infix 1 ===;    fun S === T = %%:"op =" $ S $ T;
wenzelm@23152
   196
infix 1 ~=;     fun S ~=  T = HOLogic.mk_not (S === T);
wenzelm@23152
   197
wenzelm@30595
   198
infix 9 `  ; fun f ` x = %%: @{const_name Rep_CFun} $ f $ x;
wenzelm@23152
   199
infix 9 `% ; fun f`% s = f` %: s;
wenzelm@23152
   200
infix 9 `%%; fun f`%%s = f` %%:s;
huffman@26012
   201
wenzelm@30595
   202
fun mk_adm t = %%: @{const_name adm} $ t;
wenzelm@30595
   203
val ID = %%: @{const_name ID};
wenzelm@30595
   204
fun mk_strictify t = %%: @{const_name strictify}`t;
wenzelm@30595
   205
fun mk_ssplit t = %%: @{const_name ssplit}`t;
wenzelm@30595
   206
fun mk_sscase (x, y) = %%: @{const_name sscase}`x`y;
wenzelm@30595
   207
fun mk_fup (t,u) = %%: @{const_name fup} ` t ` u;
huffman@26012
   208
huffman@26012
   209
val pcpoS = @{sort pcpo};
huffman@26012
   210
wenzelm@23152
   211
val list_ccomb = Library.foldl (op `); (* continuous version of list_comb *)
wenzelm@23152
   212
fun con_app2 con f args = list_ccomb(%%:con,map f args);
wenzelm@23152
   213
fun con_app con = con_app2 con %#;
wenzelm@23152
   214
fun prj _  _  x (   _::[]) _ = x
huffman@35905
   215
  | prj _  _  _ []         _ = error "Domain_Library.prj: empty list"
huffman@31288
   216
  | prj f1 _  x (_::y::ys) 0 = f1 x y
huffman@31288
   217
  | prj f1 f2 x (y::   ys) j = prj f1 f2 (f2 x y) ys (j-1);
wenzelm@23152
   218
fun  proj x      = prj (fn S => K(%%:"fst" $S)) (fn S => K(%%:"snd" $S)) x;
wenzelm@23152
   219
fun lift tfn = Library.foldr (fn (x,t)=> (mk_trp(tfn x) ===> t));
wenzelm@23152
   220
wenzelm@30595
   221
val UU = %%: @{const_name UU};
wenzelm@23152
   222
fun defined t = t ~= UU;
huffman@33396
   223
fun cpair (t,u) = %%: @{const_name Pair} $ t $ u;
wenzelm@30595
   224
fun spair (t,u) = %%: @{const_name spair}`t`u;
wenzelm@23152
   225
fun lift_defined f = lift (fn x => defined (f x));
haftmann@31986
   226
fun bound_arg vns v = Bound (length vns - find_index (fn v' => v' = v) vns - 1);
wenzelm@23152
   227
wenzelm@23152
   228
fun cont_eta_contract (Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body)) = 
huffman@31288
   229
    (case cont_eta_contract body  of
huffman@31288
   230
       body' as (Const("Cfun.Rep_CFun",Ta) $ f $ Bound 0) => 
huffman@31288
   231
       if not (0 mem loose_bnos f) then incr_boundvars ~1 f 
huffman@31288
   232
       else   Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body')
huffman@31288
   233
     | body' => Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body'))
huffman@31288
   234
  | cont_eta_contract(f$t) = cont_eta_contract f $ cont_eta_contract t
huffman@31288
   235
  | cont_eta_contract t    = t;
wenzelm@23152
   236
wenzelm@23152
   237
fun idx_name dnames s n = s^(if length dnames = 1 then "" else string_of_int n);
huffman@31288
   238
wenzelm@23152
   239
end; (* struct *)