src/HOLCF/domain/library.ML
author wenzelm
Sat Nov 03 01:41:26 2001 +0100 (2001-11-03)
changeset 12030 46d57d0290a2
parent 11531 d038246a62f2
child 14643 130076a81b84
permissions -rw-r--r--
GPLed;
oheimb@2276
     1
(*  Title:      HOLCF/domain/library.ML
oheimb@2445
     2
    ID:         $Id$
wenzelm@12030
     3
    Author:     David von Oheimb
wenzelm@12030
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
oheimb@2445
     5
wenzelm@12030
     6
Library for domain section.
regensbu@1274
     7
*)
regensbu@1274
     8
oheimb@2446
     9
oheimb@1637
    10
(* ----- general support ---------------------------------------------------- *)
regensbu@1274
    11
regensbu@1274
    12
fun Id x = x;
regensbu@1274
    13
regensbu@1274
    14
fun mapn f n []      = []
regensbu@1274
    15
|   mapn f n (x::xs) = (f n x) :: mapn f (n+1) xs;
regensbu@1274
    16
oheimb@1637
    17
fun foldr'' f (l,f2) = let fun itr []  = raise LIST "foldr''"
oheimb@1637
    18
			     | itr [a] = f2 a
oheimb@1637
    19
			     | itr (a::l) = f(a, itr l)
oheimb@1637
    20
in  itr l  end;
regensbu@1274
    21
fun foldr' f l = foldr'' f (l,Id);
oheimb@1637
    22
fun map_cumulr f start xs = foldr (fn (x,(ys,res))=>case f(x,res) of (y,res2) =>
oheimb@1637
    23
						  (y::ys,res2)) (xs,([],start));
regensbu@1274
    24
regensbu@1274
    25
regensbu@1274
    26
fun first  (x,_,_) = x; fun second (_,x,_) = x; fun third  (_,_,x) = x;
regensbu@1274
    27
fun upd_first  f (x,y,z) = (f x,   y,   z);
regensbu@1274
    28
fun upd_second f (x,y,z) = (  x, f y,   z);
regensbu@1274
    29
fun upd_third  f (x,y,z) = (  x,   y, f z);
regensbu@1274
    30
oheimb@1637
    31
fun atomize thm = let val r_inst = read_instantiate;
oheimb@1637
    32
    fun at  thm = case concl_of thm of
oheimb@1637
    33
      _$(Const("op &",_)$_$_)       => at(thm RS conjunct1)@at(thm RS conjunct2)
oheimb@1637
    34
    | _$(Const("All" ,_)$Abs(s,_,_))=> at(thm RS (r_inst [("x","?"^s)] spec))
oheimb@1637
    35
    | _				    => [thm];
oheimb@1637
    36
in map zero_var_indexes (at thm) end;
regensbu@1274
    37
oheimb@1637
    38
(* ----- specific support for domain ---------------------------------------- *)
regensbu@1274
    39
regensbu@1274
    40
structure Domain_Library = struct
regensbu@1274
    41
oheimb@4122
    42
open HOLCFLogic;
oheimb@4122
    43
regensbu@1274
    44
exception Impossible of string;
regensbu@1274
    45
fun Imposs msg = raise Impossible ("Domain:"^msg);
regensbu@1274
    46
regensbu@1274
    47
(* ----- name handling ----- *)
regensbu@1274
    48
oheimb@1637
    49
val strip_esc = let fun strip ("'" :: c :: cs) = c :: strip cs
oheimb@1637
    50
		    |   strip ["'"] = []
oheimb@1637
    51
		    |   strip (c :: cs) = c :: strip cs
oheimb@1637
    52
		    |   strip [] = [];
wenzelm@4709
    53
in implode o strip o Symbol.explode end;
regensbu@1274
    54
wenzelm@4709
    55
fun extern_name con = case Symbol.explode con of 
oheimb@1637
    56
		   ("o"::"p"::" "::rest) => implode rest
oheimb@1637
    57
		   | _ => con;
regensbu@1274
    58
fun dis_name  con = "is_"^ (extern_name con);
regensbu@1274
    59
fun dis_name_ con = "is_"^ (strip_esc   con);
regensbu@1274
    60
oheimb@1637
    61
(* make distinct names out of the type list, 
oheimb@5292
    62
   forbidding "o","n..","x..","f..","P.." as names *)
oheimb@1637
    63
(* a number string is added if necessary *)
oheimb@4122
    64
fun mk_var_names ids : string list = let
oheimb@5292
    65
    fun nonreserved s = if s mem ["n","x","f","P"] then s^"'" else s;
oheimb@1819
    66
    fun index_vnames(vn::vns,occupied) =
oheimb@1819
    67
          (case assoc(occupied,vn) of
regensbu@1274
    68
             None => if vn mem vns
oheimb@1819
    69
                     then (vn^"1") :: index_vnames(vns,(vn,1)  ::occupied)
oheimb@1819
    70
                     else  vn      :: index_vnames(vns,          occupied)
oheimb@1819
    71
           | Some(i) => (vn^(string_of_int (i+1)))
oheimb@1819
    72
				   :: index_vnames(vns,(vn,i+1)::occupied))
oheimb@1819
    73
      | index_vnames([],occupied) = [];
oheimb@4122
    74
in index_vnames(map nonreserved ids, [("O",0),("o",0)]) end;
regensbu@1274
    75
oheimb@2445
    76
fun rep_Type  (Type  x) = x | rep_Type  _ = Imposs "library:rep_Type";
oheimb@2445
    77
fun rep_TFree (TFree x) = x | rep_TFree _ = Imposs "library:rep_TFree";
oheimb@4008
    78
val tsig_of = #tsig o Sign.rep_sg;
oheimb@4122
    79
wenzelm@7652
    80
fun pcpo_type sg t = Sign.of_sort sg (Sign.certify_typ sg t, pcpoS);
oheimb@4008
    81
fun string_of_typ sg = Sign.string_of_typ sg o Sign.certify_typ sg;
oheimb@4008
    82
fun str2typ sg = typ_of o read_ctyp sg;
regensbu@1274
    83
regensbu@1274
    84
(* ----- constructor list handling ----- *)
regensbu@1274
    85
oheimb@1637
    86
type cons = (string *			(* operator name of constr *)
oheimb@1637
    87
	    ((bool*int)*		(*  (lazy,recursive element or ~1) *)
oheimb@1637
    88
	      string*			(*   selector name    *)
oheimb@1637
    89
	      string)			(*   argument name    *)
oheimb@1637
    90
	    list);			(* argument list      *)
oheimb@1637
    91
type eq = (string *		(* name      of abstracted type *)
oheimb@1637
    92
	   typ list) *		(* arguments of abstracted type *)
oheimb@1637
    93
	  cons list;		(* represented type, as a constructor list *)
regensbu@1274
    94
paulson@2238
    95
fun rec_of arg  = snd (first arg);
paulson@2238
    96
fun is_lazy arg = fst (first arg);
regensbu@1274
    97
val sel_of    =       second;
regensbu@1274
    98
val     vname =       third;
regensbu@1274
    99
val upd_vname =   upd_third;
regensbu@1274
   100
fun is_rec         arg = rec_of arg >=0;
regensbu@1274
   101
fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg);
oheimb@1637
   102
fun nonlazy     args   = map vname (filter_out is_lazy    args);
oheimb@1637
   103
fun nonlazy_rec args   = map vname (filter is_nonlazy_rec args);
oheimb@1637
   104
oheimb@1637
   105
(* ----- support for type and mixfix expressions ----- *)
oheimb@1637
   106
oheimb@1637
   107
infixr 5 -->;
regensbu@1274
   108
regensbu@1274
   109
(* ----- support for term expressions ----- *)
regensbu@1274
   110
berghofe@11531
   111
fun %: s = Free(s,dummyT);
berghofe@11531
   112
fun %# arg = %:(vname arg);
berghofe@11531
   113
fun %%: s = Const(s,dummyT);
regensbu@1274
   114
regensbu@1274
   115
local open HOLogic in
regensbu@1274
   116
val mk_trp = mk_Trueprop;
regensbu@1274
   117
fun mk_conj (S,T) = conj $ S $ T;
regensbu@1274
   118
fun mk_disj (S,T) = disj $ S $ T;
regensbu@1274
   119
fun mk_imp  (S,T) = imp  $ S $ T;
regensbu@1274
   120
fun mk_lam  (x,T) = Abs(x,dummyT,T);
regensbu@1274
   121
fun mk_all  (x,P) = HOLogic.mk_all (x,dummyT,P);
regensbu@1274
   122
local 
berghofe@11531
   123
		    fun sg [s]     = %:s
berghofe@11531
   124
		    |   sg (s::ss) = %%:"_classes" $ %:s $ sg ss
oheimb@2445
   125
	 	    |   sg _       = Imposs "library:sg";
berghofe@11531
   126
		    fun sf [] = %%:"_emptysort"
berghofe@11531
   127
		    |   sf s  = %%:"_sort" $ sg s
berghofe@11531
   128
		    fun tf (Type (s,args)) = foldl (op $) (%:s,map tf args)
berghofe@11531
   129
		    |   tf (TFree(s,sort)) = %%:"_ofsort" $ %:s $ sf sort
oheimb@2445
   130
		    |   tf _               = Imposs "library:tf";
regensbu@1274
   131
in
berghofe@11531
   132
fun mk_constrain      (typ,T) = %%:"_constrain" $ T $ tf typ;
berghofe@11531
   133
fun mk_constrainall (x,typ,P) = %%:"All" $ (%%:"_constrainAbs" $ mk_lam(x,P) $ tf typ);
regensbu@1274
   134
end;
regensbu@1274
   135
fun mk_ex   (x,P) = mk_exists (x,dummyT,P);
regensbu@1274
   136
end;
regensbu@1274
   137
berghofe@11531
   138
fun mk_All  (x,P) = %%:"all" $ mk_lam(x,P); (* meta universal quantification *)
regensbu@1274
   139
berghofe@11531
   140
infixr 0 ===>;fun S ===> T = %%:"==>" $ S $ T;
regensbu@1274
   141
infixr 0 ==>;fun S ==> T = mk_trp S ===> mk_trp T;
berghofe@11531
   142
infix 0 ==;  fun S ==  T = %%:"==" $ S $ T;
berghofe@11531
   143
infix 1 ===; fun S === T = %%:"op =" $ S $ T;
regensbu@1274
   144
infix 1 ~=;  fun S ~=  T = mk_not (S === T);
berghofe@11531
   145
infix 1 <<;  fun S <<  T = %%:"op <<" $ S $ T;
regensbu@1274
   146
infix 1 ~<<; fun S ~<< T = mk_not (S << T);
regensbu@1274
   147
berghofe@11531
   148
infix 9 `  ; fun f`  x = %%:"Rep_CFun" $ f $ x;
berghofe@11531
   149
infix 9 `% ; fun f`% s = f` %: s;
berghofe@11531
   150
infix 9 `%%; fun f`%%s = f` %%:s;
slotosch@5291
   151
fun mk_cRep_CFun (F,As) = foldl (op `) (F,As);
berghofe@11531
   152
fun con_app2 con f args = mk_cRep_CFun(%%:con,map f args);
regensbu@1274
   153
fun con_app con = con_app2 con %#;
regensbu@1274
   154
fun if_rec  arg f y   = if is_rec arg then f (rec_of arg) else y;
regensbu@1274
   155
fun app_rec_arg p arg = if_rec arg (fn n => fn x => (p n)`x) Id (%# arg);
oheimb@4754
   156
fun prj _  _  x (   _::[]) _ = x
oheimb@4754
   157
|   prj f1 _  x (_::y::ys) 0 = f1 x y
oheimb@4754
   158
|   prj f1 f2 x (y::   ys) j = prj f1 f2 (f2 x y) ys (j-1);
oheimb@4754
   159
fun fix_tp (tn, args) = (tn, map (K oneT) args); (* instantiate type to
oheimb@4754
   160
						    avoid type varaibles *)
berghofe@11531
   161
fun  proj x      = prj (fn S => K(%%:"fst" $S)) (fn S => K(%%:"snd" $S)) x;
berghofe@11531
   162
fun cproj x      = prj (fn S => K(%%:"cfst"`S)) (fn S => K(%%:"csnd"`S)) x;
oheimb@4754
   163
fun cproj' T eqs = prj 
oheimb@4754
   164
	(fn S => fn t => Const("cfst",mk_prodT(dummyT,t)->>dummyT)`S) 
oheimb@4754
   165
	(fn S => fn t => Const("csnd",mk_prodT(t,dummyT)->>dummyT)`S) 
oheimb@4754
   166
		       T (map ((fn tp => tp ->> tp) o Type o fix_tp o fst) eqs);
regensbu@1274
   167
fun lift tfn = foldr (fn (x,t)=> (mk_trp(tfn x) ===> t));
regensbu@1274
   168
berghofe@11531
   169
fun /\ v T = %%:"Abs_CFun" $ mk_lam(v,T);
regensbu@1274
   170
fun /\# (arg,T) = /\ (vname arg) T;
berghofe@11531
   171
infixr 9 oo; fun S oo T = %%:"cfcomp"`S`T;
berghofe@11531
   172
val UU = %%:"UU";
regensbu@1274
   173
fun strict f = f`UU === UU;
regensbu@1274
   174
fun defined t = t ~= UU;
berghofe@11531
   175
fun cpair (S,T) = %%:"cpair"`S`T;
regensbu@1274
   176
fun lift_defined f = lift (fn x => defined (f x));
oheimb@4188
   177
fun bound_arg vns v = Bound(length vns -find_index_eq v vns -1);
regensbu@1274
   178
slotosch@5291
   179
fun cont_eta_contract (Const("Abs_CFun",TT) $ Abs(a,T,body)) = 
regensbu@1274
   180
      (case cont_eta_contract body  of
slotosch@5291
   181
        body' as (Const("Rep_CFun",Ta) $ f $ Bound 0) => 
oheimb@1637
   182
	  if not (0 mem loose_bnos f) then incr_boundvars ~1 f 
slotosch@5291
   183
	  else   Const("Abs_CFun",TT) $ Abs(a,T,body')
slotosch@5291
   184
      | body' => Const("Abs_CFun",TT) $ Abs(a,T,body'))
regensbu@1274
   185
|   cont_eta_contract(f$t) = cont_eta_contract f $ cont_eta_contract t
regensbu@1274
   186
|   cont_eta_contract t    = t;
regensbu@1274
   187
oheimb@1637
   188
fun idx_name dnames s n = s^(if length dnames = 1 then "" else string_of_int n);
oheimb@1834
   189
fun when_funs cons = if length cons = 1 then ["f"] 
oheimb@1834
   190
                     else mapn (fn n => K("f"^(string_of_int n))) 1 cons;
regensbu@1274
   191
fun when_body cons funarg = let
oheimb@1637
   192
	fun one_fun n (_,[]  ) = /\ "dummy" (funarg(1,n))
oheimb@1637
   193
	|   one_fun n (_,args) = let
oheimb@1637
   194
		val l2 = length args;
berghofe@11531
   195
		fun idxs m arg = (if is_lazy arg then fn x=> %%:"fup"`%%"ID"`x
oheimb@1637
   196
					         else Id) (Bound(l2-m));
oheimb@1637
   197
		in cont_eta_contract (foldr'' 
berghofe@11531
   198
			(fn (a,t) => %%:"ssplit"`(/\# (a,t)))
oheimb@1637
   199
			(args,
slotosch@5291
   200
			fn a=> /\#(a,(mk_cRep_CFun(funarg(l2,n),mapn idxs 1 args))))
oheimb@1637
   201
			) end;
oheimb@1637
   202
in (if length cons = 1 andalso length(snd(hd cons)) <= 1
berghofe@11531
   203
    then fn t => %%:"strictify"`t else Id)
berghofe@11531
   204
     (foldr' (fn (x,y)=> %%:"sscase"`x`y) (mapn one_fun 1 cons)) end;
regensbu@1274
   205
end; (* struct *)