src/HOLCF/Tools/domain/domain_library.ML
author wenzelm
Thu, 31 May 2007 14:01:58 +0200
changeset 23152 9497234a2743
child 23284 07ae93e58fea
permissions -rw-r--r--
moved HOLCF tools to canonical place;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOLCF/Tools/domain/domain_library.ML
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     3
    Author:     David von Oheimb
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     4
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     5
Library for domain command.
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     6
*)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     7
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     8
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     9
(* ----- general support ---------------------------------------------------- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    10
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    11
fun mapn f n []      = []
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    12
|   mapn f n (x::xs) = (f n x) :: mapn f (n+1) xs;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    13
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    14
fun foldr'' f (l,f2) = let fun itr []  = raise Fail "foldr''"
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    15
			     | itr [a] = f2 a
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    16
			     | itr (a::l) = f(a, itr l)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    17
in  itr l  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    18
fun map_cumulr f start xs = foldr (fn (x,(ys,res))=>case f(x,res) of (y,res2) =>
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    19
						  (y::ys,res2)) ([],start) xs;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    20
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    21
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    22
fun first  (x,_,_) = x; fun second (_,x,_) = x; fun third  (_,_,x) = x;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    23
fun upd_first  f (x,y,z) = (f x,   y,   z);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    24
fun upd_second f (x,y,z) = (  x, f y,   z);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    25
fun upd_third  f (x,y,z) = (  x,   y, f z);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    26
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    27
fun atomize thm = let val r_inst = read_instantiate;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    28
    fun at  thm = case concl_of thm of
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    29
      _$(Const("op &",_)$_$_)       => at(thm RS conjunct1)@at(thm RS conjunct2)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    30
    | _$(Const("All" ,_)$Abs(s,_,_))=> at(thm RS (r_inst [("x","?"^s)] spec))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    31
    | _				    => [thm];
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    32
in map zero_var_indexes (at thm) end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    33
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    34
(* ----- specific support for domain ---------------------------------------- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    35
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    36
structure Domain_Library = struct
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    37
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    38
open HOLCFLogic;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    39
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    40
exception Impossible of string;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    41
fun Imposs msg = raise Impossible ("Domain:"^msg);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    42
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    43
(* ----- name handling ----- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    44
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    45
val strip_esc = let fun strip ("'" :: c :: cs) = c :: strip cs
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    46
		    |   strip ["'"] = []
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    47
		    |   strip (c :: cs) = c :: strip cs
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    48
		    |   strip [] = [];
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    49
in implode o strip o Symbol.explode end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    50
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    51
fun extern_name con = case Symbol.explode con of 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    52
		   ("o"::"p"::" "::rest) => implode rest
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    53
		   | _ => con;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    54
fun dis_name  con = "is_"^ (extern_name con);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    55
fun dis_name_ con = "is_"^ (strip_esc   con);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    56
fun mat_name  con = "match_"^ (extern_name con);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    57
fun mat_name_ con = "match_"^ (strip_esc   con);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    58
fun pat_name  con = (extern_name con) ^ "_pat";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    59
fun pat_name_ con = (strip_esc   con) ^ "_pat";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    60
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    61
(* make distinct names out of the type list, 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    62
   forbidding "o","n..","x..","f..","P.." as names *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    63
(* a number string is added if necessary *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    64
fun mk_var_names ids : string list = let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    65
    fun nonreserved s = if s mem ["n","x","f","P"] then s^"'" else s;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    66
    fun index_vnames(vn::vns,occupied) =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    67
          (case AList.lookup (op =) occupied vn of
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    68
             NONE => if vn mem vns
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    69
                     then (vn^"1") :: index_vnames(vns,(vn,1)  ::occupied)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    70
                     else  vn      :: index_vnames(vns,          occupied)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    71
           | SOME(i) => (vn^(string_of_int (i+1)))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    72
				   :: index_vnames(vns,(vn,i+1)::occupied))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    73
      | index_vnames([],occupied) = [];
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    74
in index_vnames(map nonreserved ids, [("O",0),("o",0)]) end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    75
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    76
fun pcpo_type sg t = Sign.of_sort sg (Sign.certify_typ sg t, pcpoS);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    77
fun string_of_typ sg = Sign.string_of_typ sg o Sign.certify_typ sg;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    78
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    79
(* ----- constructor list handling ----- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    80
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    81
type cons = (string *				(* operator name of constr *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    82
	    ((bool*int*DatatypeAux.dtyp)*	(*  (lazy,recursive element or ~1) *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    83
	      string option*			(*   selector name    *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    84
	      string)				(*   argument name    *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    85
	    list);				(* argument list      *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    86
type eq = (string *		(* name      of abstracted type *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    87
	   typ list) *		(* arguments of abstracted type *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    88
	  cons list;		(* represented type, as a constructor list *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    89
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    90
fun rec_of arg  = second (first arg);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    91
fun is_lazy arg = first (first arg);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    92
val sel_of    =       second;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    93
val     vname =       third;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    94
val upd_vname =   upd_third;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    95
fun is_rec         arg = rec_of arg >=0;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    96
fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    97
fun nonlazy     args   = map vname (filter_out is_lazy    args);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    98
fun nonlazy_rec args   = map vname (List.filter is_nonlazy_rec args);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    99
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   100
(* ----- qualified names of HOLCF constants ----- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   101
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   102
val lessN      = "Porder.<<"
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   103
val UU_N       = "Pcpo.UU";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   104
val admN       = "Adm.adm";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   105
val compactN   = "Adm.compact";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   106
val Rep_CFunN  = "Cfun.Rep_CFun";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   107
val Abs_CFunN  = "Cfun.Abs_CFun";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   108
val ID_N       = "Cfun.ID";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   109
val cfcompN    = "Cfun.cfcomp";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   110
val strictifyN = "Cfun.strictify";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   111
val cpairN     = "Cprod.cpair";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   112
val cfstN      = "Cprod.cfst";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   113
val csndN      = "Cprod.csnd";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   114
val csplitN    = "Cprod.csplit";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   115
val spairN     = "Sprod.spair";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   116
val sfstN      = "Sprod.sfst";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   117
val ssndN      = "Sprod.ssnd";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   118
val ssplitN    = "Sprod.ssplit";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   119
val sinlN      = "Ssum.sinl";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   120
val sinrN      = "Ssum.sinr";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   121
val sscaseN    = "Ssum.sscase";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   122
val upN        = "Up.up";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   123
val fupN       = "Up.fup";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   124
val ONE_N      = "One.ONE";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   125
val TT_N       = "Tr.TT";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   126
val FF_N       = "Tr.FF";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   127
val iterateN   = "Fix.iterate";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   128
val fixN       = "Fix.fix";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   129
val returnN    = "Fixrec.return";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   130
val failN      = "Fixrec.fail";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   131
val cpair_patN = "Fixrec.cpair_pat";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   132
val branchN    = "Fixrec.branch";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   133
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   134
val pcpoN      = "Pcpo.pcpo"
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   135
val pcpoS      = [pcpoN];
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   136
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   137
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   138
(* ----- support for type and mixfix expressions ----- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   139
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   140
infixr 5 -->;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   141
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   142
(* ----- support for term expressions ----- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   143
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   144
fun %: s = Free(s,dummyT);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   145
fun %# arg = %:(vname arg);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   146
fun %%: s = Const(s,dummyT);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   147
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   148
local open HOLogic in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   149
val mk_trp = mk_Trueprop;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   150
fun mk_conj (S,T) = conj $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   151
fun mk_disj (S,T) = disj $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   152
fun mk_imp  (S,T) = imp  $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   153
fun mk_lam  (x,T) = Abs(x,dummyT,T);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   154
fun mk_all  (x,P) = HOLogic.mk_all (x,dummyT,P);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   155
fun mk_ex   (x,P) = mk_exists (x,dummyT,P);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   156
fun mk_constrain      (typ,T) = TypeInfer.constrain T typ;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   157
fun mk_constrainall (x,typ,P) = %%:"All" $ (TypeInfer.constrain (mk_lam(x,P)) (typ --> boolT));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   158
end
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   159
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   160
fun mk_All  (x,P) = %%:"all" $ mk_lam(x,P); (* meta universal quantification *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   161
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   162
infixr 0 ===>;  fun S ===> T = %%:"==>" $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   163
infixr 0 ==>;   fun S ==> T = mk_trp S ===> mk_trp T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   164
infix 0 ==;     fun S ==  T = %%:"==" $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   165
infix 1 ===;    fun S === T = %%:"op =" $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   166
infix 1 ~=;     fun S ~=  T = HOLogic.mk_not (S === T);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   167
infix 1 <<;     fun S <<  T = %%:lessN $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   168
infix 1 ~<<;    fun S ~<< T = HOLogic.mk_not (S << T);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   169
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   170
infix 9 `  ; fun f`  x = %%:Rep_CFunN $ f $ x;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   171
infix 9 `% ; fun f`% s = f` %: s;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   172
infix 9 `%%; fun f`%%s = f` %%:s;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   173
val list_ccomb = Library.foldl (op `); (* continuous version of list_comb *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   174
fun con_app2 con f args = list_ccomb(%%:con,map f args);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   175
fun con_app con = con_app2 con %#;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   176
fun if_rec  arg f y   = if is_rec arg then f (rec_of arg) else y;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   177
fun app_rec_arg p arg = if_rec arg (fn n => fn x => (p n)`x) I (%# arg);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   178
fun prj _  _  x (   _::[]) _ = x
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   179
|   prj f1 _  x (_::y::ys) 0 = f1 x y
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   180
|   prj f1 f2 x (y::   ys) j = prj f1 f2 (f2 x y) ys (j-1);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   181
fun  proj x      = prj (fn S => K(%%:"fst" $S)) (fn S => K(%%:"snd" $S)) x;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   182
fun cproj x      = prj (fn S => K(%%:cfstN`S)) (fn S => K(%%:csndN`S)) x;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   183
fun lift tfn = Library.foldr (fn (x,t)=> (mk_trp(tfn x) ===> t));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   184
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   185
fun /\ v T = %%:Abs_CFunN $ mk_lam(v,T);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   186
fun /\# (arg,T) = /\ (vname arg) T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   187
infixr 9 oo; fun S oo T = %%:cfcompN`S`T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   188
val UU = %%:UU_N;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   189
fun strict f = f`UU === UU;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   190
fun defined t = t ~= UU;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   191
fun cpair (t,u) = %%:cpairN`t`u;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   192
fun spair (t,u) = %%:spairN`t`u;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   193
fun mk_ctuple [] = HOLogic.unit (* used in match_defs *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   194
|   mk_ctuple ts = foldr1 cpair ts;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   195
fun mk_stuple [] = %%:ONE_N
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   196
|   mk_stuple ts = foldr1 spair ts;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   197
fun mk_ctupleT [] = HOLogic.unitT   (* used in match_defs *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   198
|   mk_ctupleT Ts = foldr1 HOLogic.mk_prodT Ts;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   199
fun mk_maybeT T = Type ("Fixrec.maybe",[T]);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   200
fun cpair_pat (p1,p2) = %%:cpair_patN $ p1 $ p2;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   201
fun lift_defined f = lift (fn x => defined (f x));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   202
fun bound_arg vns v = Bound(length vns -find_index_eq v vns -1);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   203
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   204
fun cont_eta_contract (Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body)) = 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   205
      (case cont_eta_contract body  of
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   206
        body' as (Const("Cfun.Rep_CFun",Ta) $ f $ Bound 0) => 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   207
	  if not (0 mem loose_bnos f) then incr_boundvars ~1 f 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   208
	  else   Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body')
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   209
      | body' => Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body'))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   210
|   cont_eta_contract(f$t) = cont_eta_contract f $ cont_eta_contract t
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   211
|   cont_eta_contract t    = t;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   212
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   213
fun idx_name dnames s n = s^(if length dnames = 1 then "" else string_of_int n);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   214
fun when_funs cons = if length cons = 1 then ["f"] 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   215
                     else mapn (fn n => K("f"^(string_of_int n))) 1 cons;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   216
fun when_body cons funarg = let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   217
	fun one_fun n (_,[]  ) = /\ "dummy" (funarg(1,n))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   218
	|   one_fun n (_,args) = let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   219
		val l2 = length args;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   220
		fun idxs m arg = (if is_lazy arg then fn x=> %%:fupN` %%:ID_N`x
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   221
					         else I) (Bound(l2-m));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   222
		in cont_eta_contract (foldr'' 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   223
			(fn (a,t) => %%:ssplitN`(/\# (a,t)))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   224
			(args,
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   225
			fn a=> /\#(a,(list_ccomb(funarg(l2,n),mapn idxs 1 args))))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   226
			) end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   227
in (if length cons = 1 andalso length(snd(hd cons)) <= 1
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   228
    then fn t => %%:strictifyN`t else I)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   229
     (foldr1 (fn (x,y)=> %%:sscaseN`x`y) (mapn one_fun 1 cons)) end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   230
end; (* struct *)