src/HOLCF/Tools/domain/domain_library.ML
author wenzelm
Thu, 19 Mar 2009 22:05:00 +0100
changeset 30595 c87a3350f5a9
parent 30190 479806475f3c
child 30910 a7cc0ef93269
permissions -rw-r--r--
proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
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
    Author:     David von Oheimb
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     3
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     4
Library for domain command.
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     5
*)
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
(* ----- general support ---------------------------------------------------- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     9
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    10
fun mapn f n []      = []
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    11
|   mapn f n (x::xs) = (f n x) :: mapn f (n+1) xs;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    12
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    13
fun foldr'' f (l,f2) = let fun itr []  = raise Fail "foldr''"
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    14
			     | itr [a] = f2 a
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    15
			     | itr (a::l) = f(a, itr l)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    16
in  itr l  end;
30190
479806475f3c use long names for old-style fold combinators;
wenzelm
parents: 27239
diff changeset
    17
fun map_cumulr f start xs = List.foldr (fn (x,(ys,res))=>case f(x,res) of (y,res2) =>
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    18
						  (y::ys,res2)) ([],start) xs;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    19
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    20
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    21
fun first  (x,_,_) = x; fun second (_,x,_) = x; fun third  (_,_,x) = x;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    22
fun upd_first  f (x,y,z) = (f x,   y,   z);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    23
fun upd_second f (x,y,z) = (  x, f y,   z);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    24
fun upd_third  f (x,y,z) = (  x,   y, f z);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    25
27239
f2f42f9fa09d pervasive RuleInsts;
wenzelm
parents: 27231
diff changeset
    26
fun atomize ctxt thm = let val r_inst = read_instantiate ctxt;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    27
    fun at  thm = case concl_of thm of
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    28
      _$(Const("op &",_)$_$_)       => at(thm RS conjunct1)@at(thm RS conjunct2)
27231
ef7cc91a0d7f atomize: proper context;
wenzelm
parents: 27153
diff changeset
    29
    | _$(Const("All" ,_)$Abs(s,_,_))=> at(thm RS (r_inst [(("x", 0), "?" ^ s)] spec))
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    30
    | _				    => [thm];
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    31
in map zero_var_indexes (at thm) end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    32
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    33
(* ----- specific support for domain ---------------------------------------- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    34
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    35
structure Domain_Library = struct
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    36
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    37
open HOLCFLogic;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    38
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    39
exception Impossible of string;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    40
fun Imposs msg = raise Impossible ("Domain:"^msg);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    41
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    42
(* ----- name handling ----- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    43
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    44
val strip_esc = let fun strip ("'" :: c :: cs) = c :: strip cs
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    45
		    |   strip ["'"] = []
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    46
		    |   strip (c :: cs) = c :: strip cs
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    47
		    |   strip [] = [];
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    48
in implode o strip o Symbol.explode end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    49
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    50
fun extern_name con = case Symbol.explode con of 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    51
		   ("o"::"p"::" "::rest) => implode rest
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    52
		   | _ => con;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    53
fun dis_name  con = "is_"^ (extern_name con);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    54
fun dis_name_ con = "is_"^ (strip_esc   con);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    55
fun mat_name  con = "match_"^ (extern_name con);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    56
fun mat_name_ con = "match_"^ (strip_esc   con);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    57
fun pat_name  con = (extern_name con) ^ "_pat";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    58
fun pat_name_ con = (strip_esc   con) ^ "_pat";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    59
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    60
(* make distinct names out of the type list, 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    61
   forbidding "o","n..","x..","f..","P.." as names *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    62
(* a number string is added if necessary *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    63
fun mk_var_names ids : string list = let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    64
    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
    65
    fun index_vnames(vn::vns,occupied) =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    66
          (case AList.lookup (op =) occupied vn of
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    67
             NONE => if vn mem vns
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    68
                     then (vn^"1") :: index_vnames(vns,(vn,1)  ::occupied)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    69
                     else  vn      :: index_vnames(vns,          occupied)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    70
           | SOME(i) => (vn^(string_of_int (i+1)))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    71
				   :: index_vnames(vns,(vn,i+1)::occupied))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    72
      | index_vnames([],occupied) = [];
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    73
in index_vnames(map nonreserved ids, [("O",0),("o",0)]) end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    74
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    75
fun pcpo_type sg t = Sign.of_sort sg (Sign.certify_typ sg t, pcpoS);
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26012
diff changeset
    76
fun string_of_typ sg = Syntax.string_of_typ_global sg o Sign.certify_typ sg;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    77
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    78
(* ----- constructor list handling ----- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    79
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    80
type cons = (string *				(* operator name of constr *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    81
	    ((bool*int*DatatypeAux.dtyp)*	(*  (lazy,recursive element or ~1) *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    82
	      string option*			(*   selector name    *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    83
	      string)				(*   argument name    *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    84
	    list);				(* argument list      *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    85
type eq = (string *		(* name      of abstracted type *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    86
	   typ list) *		(* arguments of abstracted type *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    87
	  cons list;		(* represented type, as a constructor list *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    88
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    89
fun rec_of arg  = second (first arg);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    90
fun is_lazy arg = first (first arg);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    91
val sel_of    =       second;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    92
val     vname =       third;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    93
val upd_vname =   upd_third;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    94
fun is_rec         arg = rec_of arg >=0;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    95
fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    96
fun nonlazy     args   = map vname (filter_out is_lazy    args);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    97
fun nonlazy_rec args   = map vname (List.filter is_nonlazy_rec args);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    98
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    99
(* ----- support for type and mixfix expressions ----- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   100
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   101
infixr 5 -->;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   102
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   103
(* ----- support for term expressions ----- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   104
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   105
fun %: s = Free(s,dummyT);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   106
fun %# arg = %:(vname arg);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   107
fun %%: s = Const(s,dummyT);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   108
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   109
local open HOLogic in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   110
val mk_trp = mk_Trueprop;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   111
fun mk_conj (S,T) = conj $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   112
fun mk_disj (S,T) = disj $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   113
fun mk_imp  (S,T) = imp  $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   114
fun mk_lam  (x,T) = Abs(x,dummyT,T);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   115
fun mk_all  (x,P) = HOLogic.mk_all (x,dummyT,P);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   116
fun mk_ex   (x,P) = mk_exists (x,dummyT,P);
24680
0d355aa59e67 TypeInfer.constrain: canonical argument order;
wenzelm
parents: 23284
diff changeset
   117
val mk_constrain = uncurry TypeInfer.constrain;
0d355aa59e67 TypeInfer.constrain: canonical argument order;
wenzelm
parents: 23284
diff changeset
   118
fun mk_constrainall (x,typ,P) = %%:"All" $ (TypeInfer.constrain (typ --> boolT) (mk_lam(x,P)));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   119
end
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   120
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   121
fun mk_All  (x,P) = %%:"all" $ mk_lam(x,P); (* meta universal quantification *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   122
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   123
infixr 0 ===>;  fun S ===> T = %%:"==>" $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   124
infixr 0 ==>;   fun S ==> T = mk_trp S ===> mk_trp T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   125
infix 0 ==;     fun S ==  T = %%:"==" $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   126
infix 1 ===;    fun S === T = %%:"op =" $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   127
infix 1 ~=;     fun S ~=  T = HOLogic.mk_not (S === T);
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   128
infix 1 <<;     fun S <<  T = %%: @{const_name Porder.sq_le} $ S $ T;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   129
infix 1 ~<<;    fun S ~<< T = HOLogic.mk_not (S << T);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   130
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   131
infix 9 `  ; fun f ` x = %%: @{const_name Rep_CFun} $ f $ x;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   132
infix 9 `% ; fun f`% s = f` %: s;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   133
infix 9 `%%; fun f`%%s = f` %%:s;
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   134
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   135
fun mk_adm t = %%: @{const_name adm} $ t;
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   136
fun mk_compact t = %%: @{const_name compact} $ t;
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   137
val ID = %%: @{const_name ID};
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   138
fun mk_strictify t = %%: @{const_name strictify}`t;
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   139
fun mk_cfst t = %%: @{const_name cfst}`t;
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   140
fun mk_csnd t = %%: @{const_name csnd}`t;
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   141
(*val csplitN    = "Cprod.csplit";*)
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   142
(*val sfstN      = "Sprod.sfst";*)
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   143
(*val ssndN      = "Sprod.ssnd";*)
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   144
fun mk_ssplit t = %%: @{const_name ssplit}`t;
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   145
fun mk_sinl t = %%: @{const_name sinl}`t;
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   146
fun mk_sinr t = %%: @{const_name sinr}`t;
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   147
fun mk_sscase (x, y) = %%: @{const_name sscase}`x`y;
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   148
fun mk_up t = %%: @{const_name up}`t;
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   149
fun mk_fup (t,u) = %%: @{const_name fup} ` t ` u;
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   150
val ONE = @{term ONE};
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   151
val TT = @{term TT};
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   152
val FF = @{term FF};
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   153
fun mk_iterate (n,f,z) = %%: @{const_name iterate} $ n ` f ` z;
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   154
fun mk_fix t = %%: @{const_name fix}`t;
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   155
fun mk_return t = %%: @{const_name Fixrec.return}`t;
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   156
val mk_fail = %%: @{const_name Fixrec.fail};
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   157
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   158
fun mk_branch t = %%: @{const_name Fixrec.branch} $ t;
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   159
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   160
val pcpoS = @{sort pcpo};
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   161
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   162
val list_ccomb = Library.foldl (op `); (* continuous version of list_comb *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   163
fun con_app2 con f args = list_ccomb(%%:con,map f args);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   164
fun con_app con = con_app2 con %#;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   165
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
   166
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
   167
fun prj _  _  x (   _::[]) _ = x
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   168
|   prj f1 _  x (_::y::ys) 0 = f1 x y
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   169
|   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
   170
fun  proj x      = prj (fn S => K(%%:"fst" $S)) (fn S => K(%%:"snd" $S)) x;
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   171
fun cproj x      = prj (fn S => K(mk_cfst S)) (fn S => K(mk_csnd S)) x;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   172
fun lift tfn = Library.foldr (fn (x,t)=> (mk_trp(tfn x) ===> t));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   173
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   174
fun /\ v T = %%: @{const_name Abs_CFun} $ mk_lam(v,T);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   175
fun /\# (arg,T) = /\ (vname arg) T;
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   176
infixr 9 oo; fun S oo T = %%: @{const_name cfcomp}`S`T;
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   177
val UU = %%: @{const_name UU};
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   178
fun strict f = f`UU === UU;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   179
fun defined t = t ~= UU;
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   180
fun cpair (t,u) = %%: @{const_name cpair}`t`u;
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   181
fun spair (t,u) = %%: @{const_name spair}`t`u;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   182
fun mk_ctuple [] = HOLogic.unit (* used in match_defs *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   183
|   mk_ctuple ts = foldr1 cpair ts;
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   184
fun mk_stuple [] = ONE
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   185
|   mk_stuple ts = foldr1 spair ts;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   186
fun mk_ctupleT [] = HOLogic.unitT   (* used in match_defs *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   187
|   mk_ctupleT Ts = foldr1 HOLogic.mk_prodT Ts;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   188
fun mk_maybeT T = Type ("Fixrec.maybe",[T]);
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   189
fun cpair_pat (p1,p2) = %%: @{const_name cpair_pat} $ p1 $ p2;
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   190
val mk_ctuple_pat = foldr1 cpair_pat;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   191
fun lift_defined f = lift (fn x => defined (f x));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   192
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
   193
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   194
fun cont_eta_contract (Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body)) = 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   195
      (case cont_eta_contract body  of
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   196
        body' as (Const("Cfun.Rep_CFun",Ta) $ f $ Bound 0) => 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   197
	  if not (0 mem loose_bnos f) then incr_boundvars ~1 f 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   198
	  else   Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body')
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   199
      | body' => Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body'))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   200
|   cont_eta_contract(f$t) = cont_eta_contract f $ cont_eta_contract t
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   201
|   cont_eta_contract t    = t;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   202
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   203
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
   204
fun when_funs cons = if length cons = 1 then ["f"] 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   205
                     else mapn (fn n => K("f"^(string_of_int n))) 1 cons;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   206
fun when_body cons funarg = let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   207
	fun one_fun n (_,[]  ) = /\ "dummy" (funarg(1,n))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   208
	|   one_fun n (_,args) = let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   209
		val l2 = length args;
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   210
		fun idxs m arg = (if is_lazy arg then (fn t => mk_fup (ID, t))
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   211
					         else I) (Bound(l2-m));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   212
		in cont_eta_contract (foldr'' 
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   213
			(fn (a,t) => mk_ssplit (/\# (a,t)))
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   214
			(args,
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   215
			fn a=> /\#(a,(list_ccomb(funarg(l2,n),mapn idxs 1 args))))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   216
			) end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   217
in (if length cons = 1 andalso length(snd(hd cons)) <= 1
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   218
    then mk_strictify else I)
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   219
     (foldr1 mk_sscase (mapn one_fun 1 cons)) end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   220
end; (* struct *)