src/HOLCF/Tools/Domain/domain_library.ML
author haftmann
Fri, 04 Dec 2009 12:17:43 +0100
changeset 33971 9c7fa7f76950
parent 33396 45c5c3c51918
child 35288 aa7da51ae1ef
permissions -rw-r--r--
modernized structure Datatype_Aux
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32126
a5042f260440 obey captialized directory names convention
haftmann
parents: 31986
diff changeset
     1
(*  Title:      HOLCF/Tools/Domain/domain_library.ML
23152
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 []      = []
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    11
  | mapn f n (x::xs) = (f n x) :: mapn f (n+1) xs;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    12
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    13
fun foldr'' f (l,f2) =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    14
    let fun itr []  = raise Fail "foldr''" 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    15
          | itr [a] = f2 a
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    16
          | itr (a::l) = f(a, itr l)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    17
    in  itr l  end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    18
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    19
fun map_cumulr f start xs =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    20
    List.foldr (fn (x,(ys,res))=>case f(x,res) of (y,res2) =>
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    21
                                                  (y::ys,res2)) ([],start) xs;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    22
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    23
fun first  (x,_,_) = x; fun second (_,x,_) = x; fun third  (_,_,x) = x;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    24
fun upd_first  f (x,y,z) = (f x,   y,   z);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    25
fun upd_second f (x,y,z) = (  x, f y,   z);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    26
fun upd_third  f (x,y,z) = (  x,   y, f z);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    27
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    28
fun atomize ctxt thm =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    29
    let
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    30
      val r_inst = read_instantiate ctxt;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    31
      fun at thm =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    32
          case concl_of thm of
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    33
            _$(Const("op &",_)$_$_)       => at(thm RS conjunct1)@at(thm RS conjunct2)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    34
          | _$(Const("All" ,_)$Abs(s,_,_))=> at(thm RS (r_inst [(("x", 0), "?" ^ s)] spec))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    35
          | _                             => [thm];
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    36
    in map zero_var_indexes (at thm) end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    37
31006
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    38
(* infix syntax *)
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    39
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    40
infixr 5 -->;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    41
infixr 6 ->>;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    42
infixr 0 ===>;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    43
infixr 0 ==>;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    44
infix 0 ==;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    45
infix 1 ===;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    46
infix 1 ~=;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    47
infix 1 <<;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    48
infix 1 ~<<;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    49
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    50
infix 9 `  ;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    51
infix 9 `% ;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    52
infix 9 `%%;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    53
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    54
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    55
(* ----- specific support for domain ---------------------------------------- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    56
31006
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    57
signature DOMAIN_LIBRARY =
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    58
sig
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    59
  val Imposs : string -> 'a;
31162
6dc708ca4a8f add cpo_type function
huffman
parents: 31076
diff changeset
    60
  val cpo_type : theory -> typ -> bool;
31006
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    61
  val pcpo_type : theory -> typ -> bool;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    62
  val string_of_typ : theory -> typ -> string;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    63
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    64
  (* Creating HOLCF types *)
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    65
  val mk_cfunT : typ * typ -> typ;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    66
  val ->> : typ * typ -> typ;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    67
  val mk_ssumT : typ * typ -> typ;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    68
  val mk_sprodT : typ * typ -> typ;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    69
  val mk_uT : typ -> typ;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    70
  val oneT : typ;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    71
  val trT : typ;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    72
  val mk_maybeT : typ -> typ;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    73
  val mk_ctupleT : typ list -> typ;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    74
  val mk_TFree : string -> typ;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    75
  val pcpoS : sort;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    76
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    77
  (* Creating HOLCF terms *)
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    78
  val %: : string -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    79
  val %%: : string -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    80
  val ` : term * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    81
  val `% : term * string -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    82
  val /\ : string -> term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    83
  val UU : term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    84
  val TT : term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    85
  val FF : term;
31231
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
    86
  val ID : term;
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
    87
  val oo : term * term -> term;
31006
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    88
  val mk_up : term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    89
  val mk_sinl : term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    90
  val mk_sinr : term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    91
  val mk_stuple : term list -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    92
  val mk_ctuple : term list -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    93
  val mk_fix : term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    94
  val mk_iterate : term * term * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    95
  val mk_fail : term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    96
  val mk_return : term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
    97
  val list_ccomb : term * term list -> term;
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    98
  (*
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
    99
   val con_app : string -> ('a * 'b * string) list -> term;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   100
   *)
31006
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   101
  val con_app2 : string -> ('a -> term) -> 'a list -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   102
  val proj : term -> 'a list -> int -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   103
  val prj : ('a -> 'b -> 'a) -> ('a -> 'b -> 'a) -> 'a -> 'b list -> int -> 'a;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   104
  val mk_ctuple_pat : term list -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   105
  val mk_branch : term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   106
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   107
  (* Creating propositions *)
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   108
  val mk_conj : term * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   109
  val mk_disj : term * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   110
  val mk_imp : term * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   111
  val mk_lam : string * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   112
  val mk_all : string * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   113
  val mk_ex : string * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   114
  val mk_constrain : typ * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   115
  val mk_constrainall : string * typ * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   116
  val === : term * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   117
  val << : term * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   118
  val ~<< : term * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   119
  val strict : term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   120
  val defined : term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   121
  val mk_adm : term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   122
  val mk_compact : term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   123
  val lift : ('a -> term) -> 'a list * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   124
  val lift_defined : ('a -> term) -> 'a list * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   125
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   126
  (* Creating meta-propositions *)
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   127
  val mk_trp : term -> term; (* HOLogic.mk_Trueprop *)
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   128
  val == : term * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   129
  val ===> : term * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   130
  val ==> : term * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   131
  val mk_All : string * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   132
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   133
      (* Domain specifications *)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   134
      eqtype arg;
31006
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   135
  type cons = string * arg list;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   136
  type eq = (string * typ list) * cons list;
31738
7b9b9ba532ca discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31288
diff changeset
   137
  val mk_arg : (bool * Datatype.dtyp) * string option * string -> arg;
31006
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   138
  val is_lazy : arg -> bool;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   139
  val rec_of : arg -> int;
31738
7b9b9ba532ca discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31288
diff changeset
   140
  val dtyp_of : arg -> Datatype.dtyp;
31006
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   141
  val sel_of : arg -> string option;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   142
  val vname : arg -> string;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   143
  val upd_vname : (string -> string) -> arg -> arg;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   144
  val is_rec : arg -> bool;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   145
  val is_nonlazy_rec : arg -> bool;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   146
  val nonlazy : arg list -> string list;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   147
  val nonlazy_rec : arg list -> string list;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   148
  val %# : arg -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   149
  val /\# : arg * term -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   150
  val when_body : cons list -> (int * int -> term) -> term;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   151
  val when_funs : 'a list -> string list;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   152
  val bound_arg : ''a list -> ''a -> term; (* ''a = arg or string *)
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   153
  val idx_name : 'a list -> string -> int -> string;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   154
  val app_rec_arg : (int -> term) -> arg -> term;
31228
bcacfd816d28 make type Domain_Library.arg abstract
huffman
parents: 31162
diff changeset
   155
  val con_app : string -> arg list -> term;
31738
7b9b9ba532ca discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31288
diff changeset
   156
  val dtyp_of_eq : eq -> Datatype.dtyp;
31228
bcacfd816d28 make type Domain_Library.arg abstract
huffman
parents: 31162
diff changeset
   157
31006
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   158
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   159
  (* Name mangling *)
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   160
  val strip_esc : string -> string;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   161
  val extern_name : string -> string;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   162
  val dis_name : string -> string;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   163
  val mat_name : string -> string;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   164
  val pat_name : string -> string;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   165
  val mk_var_names : string list -> string list;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   166
end;
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   167
31023
d027411c9a38 use opaque ascription for all HOLCF code
huffman
parents: 31006
diff changeset
   168
structure Domain_Library :> DOMAIN_LIBRARY =
31006
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   169
struct
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   170
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   171
exception Impossible of string;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   172
fun Imposs msg = raise Impossible ("Domain:"^msg);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   173
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   174
(* ----- name handling ----- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   175
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   176
val strip_esc =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   177
    let fun strip ("'" :: c :: cs) = c :: strip cs
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   178
          | strip ["'"] = []
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   179
          | strip (c :: cs) = c :: strip cs
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   180
          | strip [] = [];
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   181
    in implode o strip o Symbol.explode end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   182
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   183
fun extern_name con =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   184
    case Symbol.explode con of 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   185
      ("o"::"p"::" "::rest) => implode rest
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   186
    | _ => con;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   187
fun dis_name  con = "is_"^ (extern_name con);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   188
fun dis_name_ con = "is_"^ (strip_esc   con);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   189
fun mat_name  con = "match_"^ (extern_name con);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   190
fun mat_name_ con = "match_"^ (strip_esc   con);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   191
fun pat_name  con = (extern_name con) ^ "_pat";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   192
fun pat_name_ con = (strip_esc   con) ^ "_pat";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   193
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   194
(* make distinct names out of the type list, 
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   195
                                       forbidding "o","n..","x..","f..","P.." as names *)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   196
(* a number string is added if necessary *)
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   197
fun mk_var_names ids : string list =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   198
    let
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   199
      fun nonreserved s = if s mem ["n","x","f","P"] then s^"'" else s;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   200
      fun index_vnames(vn::vns,occupied) =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   201
          (case AList.lookup (op =) occupied vn of
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   202
             NONE => if vn mem vns
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   203
                     then (vn^"1") :: index_vnames(vns,(vn,1)  ::occupied)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   204
                     else  vn      :: index_vnames(vns,          occupied)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   205
           | SOME(i) => (vn^(string_of_int (i+1)))
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   206
                        :: index_vnames(vns,(vn,i+1)::occupied))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   207
        | index_vnames([],occupied) = [];
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   208
    in index_vnames(map nonreserved ids, [("O",0),("o",0)]) end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   209
31162
6dc708ca4a8f add cpo_type function
huffman
parents: 31076
diff changeset
   210
fun cpo_type sg t = Sign.of_sort sg (Sign.certify_typ sg t, @{sort cpo});
30910
a7cc0ef93269 set up domain package in Domain.thy
huffman
parents: 30595
diff changeset
   211
fun pcpo_type sg t = Sign.of_sort sg (Sign.certify_typ sg t, @{sort pcpo});
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26012
diff changeset
   212
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
   213
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   214
(* ----- constructor list handling ----- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   215
31006
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   216
type arg =
31738
7b9b9ba532ca discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31288
diff changeset
   217
     (bool * Datatype.dtyp) *   (*  (lazy, recursive element) *)
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   218
     string option *               (*   selector name    *)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   219
     string;                       (*   argument name    *)
31006
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   220
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   221
type cons =
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   222
     string *         (* operator name of constr *)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   223
     arg list;        (* argument list      *)
31006
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   224
644d18da3c77 add module signature to domain_library.ML
huffman
parents: 30910
diff changeset
   225
type eq =
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   226
     (string *        (* name      of abstracted type *)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   227
      typ list) *     (* arguments of abstracted type *)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   228
     cons list;       (* represented type, as a constructor list *)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   229
31228
bcacfd816d28 make type Domain_Library.arg abstract
huffman
parents: 31162
diff changeset
   230
val mk_arg = I;
31229
8a890890d143 change representation of Domain_Library.arg
huffman
parents: 31228
diff changeset
   231
8a890890d143 change representation of Domain_Library.arg
huffman
parents: 31228
diff changeset
   232
fun rec_of ((_,dtyp),_,_) =
33971
9c7fa7f76950 modernized structure Datatype_Aux
haftmann
parents: 33396
diff changeset
   233
    case dtyp of Datatype_Aux.DtRec i => i | _ => ~1;
31229
8a890890d143 change representation of Domain_Library.arg
huffman
parents: 31228
diff changeset
   234
(* FIXME: what about indirect recursion? *)
8a890890d143 change representation of Domain_Library.arg
huffman
parents: 31228
diff changeset
   235
8a890890d143 change representation of Domain_Library.arg
huffman
parents: 31228
diff changeset
   236
fun is_lazy arg = fst (first arg);
31231
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
   237
fun dtyp_of arg = snd (first arg);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   238
val sel_of    =       second;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   239
val     vname =       third;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   240
val upd_vname =   upd_third;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   241
fun is_rec         arg = rec_of arg >=0;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   242
fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg);
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 32126
diff changeset
   243
fun nonlazy     args   = map vname (filter_out is_lazy args);
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 32126
diff changeset
   244
fun nonlazy_rec args   = map vname (filter is_nonlazy_rec args);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   245
31231
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
   246
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
   247
(* ----- combinators for making dtyps ----- *)
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
   248
33971
9c7fa7f76950 modernized structure Datatype_Aux
haftmann
parents: 33396
diff changeset
   249
fun mk_uD T = Datatype_Aux.DtType(@{type_name "u"}, [T]);
9c7fa7f76950 modernized structure Datatype_Aux
haftmann
parents: 33396
diff changeset
   250
fun mk_sprodD (T, U) = Datatype_Aux.DtType(@{type_name "**"}, [T, U]);
9c7fa7f76950 modernized structure Datatype_Aux
haftmann
parents: 33396
diff changeset
   251
fun mk_ssumD (T, U) = Datatype_Aux.DtType(@{type_name "++"}, [T, U]);
9c7fa7f76950 modernized structure Datatype_Aux
haftmann
parents: 33396
diff changeset
   252
fun mk_liftD T = Datatype_Aux.DtType(@{type_name "lift"}, [T]);
9c7fa7f76950 modernized structure Datatype_Aux
haftmann
parents: 33396
diff changeset
   253
val unitD = Datatype_Aux.DtType(@{type_name "unit"}, []);
9c7fa7f76950 modernized structure Datatype_Aux
haftmann
parents: 33396
diff changeset
   254
val boolD = Datatype_Aux.DtType(@{type_name "bool"}, []);
31231
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
   255
val oneD = mk_liftD unitD;
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
   256
val trD = mk_liftD boolD;
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
   257
fun big_sprodD ds = case ds of [] => oneD | _ => foldr1 mk_sprodD ds;
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
   258
fun big_ssumD ds = case ds of [] => unitD | _ => foldr1 mk_ssumD ds;
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
   259
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
   260
fun dtyp_of_arg ((lazy, D), _, _) = if lazy then mk_uD D else D;
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
   261
fun dtyp_of_cons (_, args) = big_sprodD (map dtyp_of_arg args);
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
   262
fun dtyp_of_eq (_, cons) = big_ssumD (map dtyp_of_cons cons);
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
   263
9434cd5ef24a export ID, oo; add more dtyp operations
huffman
parents: 31229
diff changeset
   264
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   265
(* ----- support for type and mixfix expressions ----- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   266
30910
a7cc0ef93269 set up domain package in Domain.thy
huffman
parents: 30595
diff changeset
   267
fun mk_uT T = Type(@{type_name "u"}, [T]);
a7cc0ef93269 set up domain package in Domain.thy
huffman
parents: 30595
diff changeset
   268
fun mk_cfunT (T, U) = Type(@{type_name "->"}, [T, U]);
a7cc0ef93269 set up domain package in Domain.thy
huffman
parents: 30595
diff changeset
   269
fun mk_sprodT (T, U) = Type(@{type_name "**"}, [T, U]);
a7cc0ef93269 set up domain package in Domain.thy
huffman
parents: 30595
diff changeset
   270
fun mk_ssumT (T, U) = Type(@{type_name "++"}, [T, U]);
a7cc0ef93269 set up domain package in Domain.thy
huffman
parents: 30595
diff changeset
   271
val oneT = @{typ one};
a7cc0ef93269 set up domain package in Domain.thy
huffman
parents: 30595
diff changeset
   272
val trT = @{typ tr};
a7cc0ef93269 set up domain package in Domain.thy
huffman
parents: 30595
diff changeset
   273
a7cc0ef93269 set up domain package in Domain.thy
huffman
parents: 30595
diff changeset
   274
val op ->> = mk_cfunT;
a7cc0ef93269 set up domain package in Domain.thy
huffman
parents: 30595
diff changeset
   275
a7cc0ef93269 set up domain package in Domain.thy
huffman
parents: 30595
diff changeset
   276
fun mk_TFree s = TFree ("'" ^ s, @{sort pcpo});
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   277
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   278
(* ----- support for term expressions ----- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   279
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   280
fun %: s = Free(s,dummyT);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   281
fun %# arg = %:(vname arg);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   282
fun %%: s = Const(s,dummyT);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   283
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   284
local open HOLogic in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   285
val mk_trp = mk_Trueprop;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   286
fun mk_conj (S,T) = conj $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   287
fun mk_disj (S,T) = disj $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   288
fun mk_imp  (S,T) = imp  $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   289
fun mk_lam  (x,T) = Abs(x,dummyT,T);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   290
fun mk_all  (x,P) = HOLogic.mk_all (x,dummyT,P);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   291
fun mk_ex   (x,P) = mk_exists (x,dummyT,P);
24680
0d355aa59e67 TypeInfer.constrain: canonical argument order;
wenzelm
parents: 23284
diff changeset
   292
val mk_constrain = uncurry TypeInfer.constrain;
0d355aa59e67 TypeInfer.constrain: canonical argument order;
wenzelm
parents: 23284
diff changeset
   293
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
   294
end
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   295
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   296
fun mk_All  (x,P) = %%:"all" $ mk_lam(x,P); (* meta universal quantification *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   297
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   298
infixr 0 ===>;  fun S ===> T = %%:"==>" $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   299
infixr 0 ==>;   fun S ==> T = mk_trp S ===> mk_trp T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   300
infix 0 ==;     fun S ==  T = %%:"==" $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   301
infix 1 ===;    fun S === T = %%:"op =" $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   302
infix 1 ~=;     fun S ~=  T = HOLogic.mk_not (S === T);
31076
99fe356cbbc2 rename constant sq_le to below; rename class sq_ord to below; less->below in many lemma names
huffman
parents: 31023
diff changeset
   303
infix 1 <<;     fun S <<  T = %%: @{const_name Porder.below} $ S $ T;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   304
infix 1 ~<<;    fun S ~<< T = HOLogic.mk_not (S << T);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   305
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   306
infix 9 `  ; fun f ` x = %%: @{const_name Rep_CFun} $ f $ x;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   307
infix 9 `% ; fun f`% s = f` %: s;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   308
infix 9 `%%; fun f`%%s = f` %%:s;
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   309
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   310
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
   311
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
   312
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
   313
fun mk_strictify t = %%: @{const_name strictify}`t;
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   314
(*val csplitN    = "Cprod.csplit";*)
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   315
(*val sfstN      = "Sprod.sfst";*)
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   316
(*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
   317
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
   318
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
   319
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
   320
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
   321
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
   322
fun mk_fup (t,u) = %%: @{const_name fup} ` t ` u;
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   323
val ONE = @{term ONE};
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   324
val TT = @{term TT};
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   325
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
   326
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
   327
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
   328
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
   329
val mk_fail = %%: @{const_name Fixrec.fail};
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   330
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   331
fun mk_branch t = %%: @{const_name Fixrec.branch} $ t;
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   332
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   333
val pcpoS = @{sort pcpo};
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   334
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   335
val list_ccomb = Library.foldl (op `); (* continuous version of list_comb *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   336
fun con_app2 con f args = list_ccomb(%%:con,map f args);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   337
fun con_app con = con_app2 con %#;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   338
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
   339
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
   340
fun prj _  _  x (   _::[]) _ = x
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   341
  | prj f1 _  x (_::y::ys) 0 = f1 x y
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   342
  | prj f1 f2 x (y::   ys) j = prj f1 f2 (f2 x y) ys (j-1);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   343
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
   344
fun lift tfn = Library.foldr (fn (x,t)=> (mk_trp(tfn x) ===> t));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   345
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   346
fun /\ v T = %%: @{const_name Abs_CFun} $ mk_lam(v,T);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   347
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
   348
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
   349
val UU = %%: @{const_name UU};
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   350
fun strict f = f`UU === UU;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   351
fun defined t = t ~= UU;
33396
45c5c3c51918 domain package no longer uses cfst/csnd/cpair
huffman
parents: 33317
diff changeset
   352
fun cpair (t,u) = %%: @{const_name Pair} $ t $ u;
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30190
diff changeset
   353
fun spair (t,u) = %%: @{const_name spair}`t`u;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   354
fun mk_ctuple [] = HOLogic.unit (* used in match_defs *)
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   355
  | mk_ctuple ts = foldr1 cpair ts;
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   356
fun mk_stuple [] = ONE
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   357
  | mk_stuple ts = foldr1 spair ts;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   358
fun mk_ctupleT [] = HOLogic.unitT   (* used in match_defs *)
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   359
  | mk_ctupleT Ts = foldr1 HOLogic.mk_prodT Ts;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   360
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
   361
fun cpair_pat (p1,p2) = %%: @{const_name cpair_pat} $ p1 $ p2;
26012
f6917792f8a4 new term-building combinators
huffman
parents: 25723
diff changeset
   362
val mk_ctuple_pat = foldr1 cpair_pat;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   363
fun lift_defined f = lift (fn x => defined (f x));
31986
a68f88d264f7 dropped find_index_eq
haftmann
parents: 31738
diff changeset
   364
fun bound_arg vns v = Bound (length vns - find_index (fn v' => v' = v) vns - 1);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   365
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   366
fun cont_eta_contract (Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body)) = 
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   367
    (case cont_eta_contract body  of
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   368
       body' as (Const("Cfun.Rep_CFun",Ta) $ f $ Bound 0) => 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   369
       if not (0 mem loose_bnos f) then incr_boundvars ~1 f 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   370
       else   Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body')
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   371
     | body' => Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body'))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   372
  | cont_eta_contract(f$t) = cont_eta_contract f $ cont_eta_contract t
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   373
  | cont_eta_contract t    = t;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   374
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   375
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
   376
fun when_funs cons = if length cons = 1 then ["f"] 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   377
                     else mapn (fn n => K("f"^(string_of_int n))) 1 cons;
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   378
fun when_body cons funarg =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   379
    let
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   380
      fun one_fun n (_,[]  ) = /\ "dummy" (funarg(1,n))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   381
        | one_fun n (_,args) = let
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   382
            val l2 = length args;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   383
            fun idxs m arg = (if is_lazy arg then (fn t => mk_fup (ID, t))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   384
                              else I) (Bound(l2-m));
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   385
          in cont_eta_contract
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   386
               (foldr'' 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   387
                  (fn (a,t) => mk_ssplit (/\# (a,t)))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   388
                  (args,
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   389
                fn a=> /\#(a,(list_ccomb(funarg(l2,n),mapn idxs 1 args))))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   390
               ) end;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   391
    in (if length cons = 1 andalso length(snd(hd cons)) <= 1
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   392
        then mk_strictify else I)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   393
         (foldr1 mk_sscase (mapn one_fun 1 cons)) end;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31231
diff changeset
   394
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   395
end; (* struct *)