src/HOLCF/Tools/domain/domain_extender.ML
author huffman
Mon, 20 Apr 2009 17:38:25 -0700
changeset 30916 a3d2128cac92
parent 30915 f8877f60e1ee
child 30919 dcf8a7a66bd1
permissions -rw-r--r--
allow infix declarations for type constructors defined with domain package
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_extender.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
Theory extender for domain command, including theory syntax.
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
signature DOMAIN_EXTENDER =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     8
sig
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
     9
  val add_domain: string * ((bstring * string list * mixfix) *
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    10
    (string * mixfix * (bool * string option * string) list) list) list
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    11
    -> theory -> theory
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
    12
  val add_domain_i: string * ((bstring * string list * mixfix) *
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    13
    (string * mixfix * (bool * string option * typ) list) list) list
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    14
    -> theory -> theory
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    15
end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    16
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    17
structure Domain_Extender: DOMAIN_EXTENDER =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    18
struct
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    19
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    20
open Domain_Library;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    21
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    22
(* ----- general testing and preprocessing of constructor list -------------- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    23
fun check_and_sort_domain (dtnvs: (string * typ list) list, 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    24
     cons'' : ((string * mixfix * (bool * string option * typ) list) list) list) sg =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    25
  let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    26
    val defaultS = Sign.defaultS sg;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    27
    val test_dupl_typs = (case duplicates (op =) (map fst dtnvs) of 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    28
	[] => false | dups => error ("Duplicate types: " ^ commas_quote dups));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    29
    val test_dupl_cons = (case duplicates (op =) (map first (List.concat cons'')) of 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    30
	[] => false | dups => error ("Duplicate constructors: " 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    31
							 ^ commas_quote dups));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    32
    val test_dupl_sels = (case duplicates (op =) (List.mapPartial second
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    33
			       (List.concat (map third (List.concat cons'')))) of
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    34
        [] => false | dups => error("Duplicate selectors: "^commas_quote dups));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    35
    val test_dupl_tvars = exists(fn s=>case duplicates (op =) (map(fst o dest_TFree)s)of
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    36
	[] => false | dups => error("Duplicate type arguments: " 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    37
		   ^commas_quote dups)) (map snd dtnvs);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    38
    (* test for free type variables, illegal sort constraints on rhs,
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    39
	       non-pcpo-types and invalid use of recursive type;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    40
       replace sorts in type variables on rhs *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    41
    fun analyse_equation ((dname,typevars),cons') = 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    42
      let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    43
	val tvars = map dest_TFree typevars;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    44
	val distinct_typevars = map TFree tvars;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    45
	fun rm_sorts (TFree(s,_)) = TFree(s,[])
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    46
	|   rm_sorts (Type(s,ts)) = Type(s,remove_sorts ts)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    47
	|   rm_sorts (TVar(s,_))  = TVar(s,[])
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    48
	and remove_sorts l = map rm_sorts l;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    49
	val indirect_ok = ["*","Cfun.->","Ssum.++","Sprod.**","Up.u"]
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    50
	fun analyse indirect (TFree(v,s))  = (case AList.lookup (op =) tvars v of 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    51
		    NONE => error ("Free type variable " ^ quote v ^ " on rhs.")
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    52
	          | SOME sort => if eq_set_string (s,defaultS) orelse
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    53
				    eq_set_string (s,sort    )
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    54
				 then TFree(v,sort)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    55
				 else error ("Inconsistent sort constraint" ^
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    56
				             " for type variable " ^ quote v))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    57
        |   analyse indirect (t as Type(s,typl)) = (case AList.lookup (op =) dtnvs s of
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    58
		NONE          => if s mem indirect_ok
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    59
				 then Type(s,map (analyse false) typl)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    60
				 else Type(s,map (analyse true) typl)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    61
	      | SOME typevars => if indirect 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    62
                           then error ("Indirect recursion of type " ^ 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    63
				        quote (string_of_typ sg t))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    64
                           else if dname <> s orelse (** BUG OR FEATURE?: 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    65
                                mutual recursion may use different arguments **)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    66
				   remove_sorts typevars = remove_sorts typl 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    67
				then Type(s,map (analyse true) typl)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    68
				else error ("Direct recursion of type " ^ 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    69
					     quote (string_of_typ sg t) ^ 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    70
					    " with different arguments"))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    71
        |   analyse indirect (TVar _) = Imposs "extender:analyse";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    72
	fun check_pcpo T = if pcpo_type sg T then T
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    73
          else error("Constructor argument type is not of sort pcpo: "^string_of_typ sg T);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    74
	val analyse_con = upd_third (map (upd_third (check_pcpo o analyse false)));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    75
      in ((dname,distinct_typevars), map analyse_con cons') end; 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    76
  in ListPair.map analyse_equation (dtnvs,cons'')
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    77
  end; (* let *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    78
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    79
(* ----- calls for building new thy and thms -------------------------------- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    80
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    81
fun gen_add_domain prep_typ (comp_dnam, eqs''') thy''' =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    82
  let
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
    83
    val dtnvs = map ((fn (dname,vs,mx) => 
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
    84
			 (Sign.full_bname thy''' dname, map (Syntax.read_typ_global thy''') vs, mx))
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    85
                   o fst) eqs''';
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    86
    val cons''' = map snd eqs''';
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
    87
    fun thy_type  (dname,tvars,mx) = (Binding.name (Long_Name.base_name dname), length tvars, mx);
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
    88
    fun thy_arity (dname,tvars,mx) = (dname, map (snd o dest_TFree) tvars, pcpoS);
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
    89
    val thy'' = thy''' |> Sign.add_types (map thy_type dtnvs)
24926
bcb6b098df11 AxClass.axiomatize: renamed XXX_i to XXX, and XXX to XXX_cmd;
wenzelm
parents: 24867
diff changeset
    90
		       |> fold (AxClass.axiomatize_arity o thy_arity) dtnvs;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    91
    val cons'' = map (map (upd_third (map (upd_third (prep_typ thy''))))) cons''';
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
    92
    val dtnvs' = map (fn (dname,vs,mx) => (dname,vs)) dtnvs;
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
    93
    val eqs' = check_and_sort_domain (dtnvs',cons'') thy'';
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    94
    val thy' = thy'' |> Domain_Syntax.add_syntax (comp_dnam,eqs');
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    95
    val dts  = map (Type o fst) eqs';
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    96
    val new_dts = map (fn ((s,Ts),_) => (s, map (fst o dest_TFree) Ts)) eqs';
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    97
    fun strip ss = Library.drop (find_index_eq "'" ss +1, ss);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    98
    fun typid (Type  (id,_)) =
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30345
diff changeset
    99
          let val c = hd (Symbol.explode (Long_Name.base_name id))
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   100
          in if Symbol.is_letter c then c else "t" end
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   101
      | typid (TFree (id,_)   ) = hd (strip (tl (Symbol.explode id)))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   102
      | typid (TVar ((id,_),_)) = hd (tl (Symbol.explode id));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   103
    fun one_con (con,mx,args) =
30345
76fd85bbf139 more uniform handling of binding in packages;
wenzelm
parents: 30280
diff changeset
   104
	((Syntax.const_name mx con),
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   105
	 ListPair.map (fn ((lazy,sel,tp),vn) => ((lazy,
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   106
					find_index_eq tp dts,
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   107
					DatatypeAux.dtyp_of_typ new_dts tp),
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   108
					sel,vn))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   109
	     (args,(mk_var_names(map (typid o third) args)))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   110
	 ) : cons;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   111
    val eqs = map (fn (dtnvs,cons') => (dtnvs, map one_con cons')) eqs' : eq list;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   112
    val thy        = thy' |> Domain_Axioms.add_axioms (comp_dnam,eqs);
28536
8dccb6035d0f established canonical argument order
haftmann
parents: 27353
diff changeset
   113
    val ((rewss, take_rews), theorems_thy) = thy |> fold_map (fn eq =>
8dccb6035d0f established canonical argument order
haftmann
parents: 27353
diff changeset
   114
      Domain_Theorems.theorems (eq, eqs)) eqs
8dccb6035d0f established canonical argument order
haftmann
parents: 27353
diff changeset
   115
      ||>> Domain_Theorems.comp_theorems (comp_dnam, eqs);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   116
  in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   117
    theorems_thy
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30345
diff changeset
   118
    |> Sign.add_path (Long_Name.base_name comp_dnam)
29585
c23295521af5 binding replaces bstring
haftmann
parents: 28965
diff changeset
   119
    |> (snd o (PureThy.add_thmss [((Binding.name "rews", List.concat rewss @ take_rews), [])]))
24712
64ed05609568 proper Sign operations instead of Theory aliases;
wenzelm
parents: 24707
diff changeset
   120
    |> Sign.parent_path
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   121
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   122
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   123
val add_domain_i = gen_add_domain Sign.certify_typ;
24707
dfeb98f84e93 Syntax.parse/check/read;
wenzelm
parents: 23152
diff changeset
   124
val add_domain = gen_add_domain Syntax.read_typ_global;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   125
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   126
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   127
(** outer syntax **)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   128
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   129
local structure P = OuterParse and K = OuterKeyword in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   130
27353
71c4dd53d4cb moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents: 24926
diff changeset
   131
val _ = OuterKeyword.keyword "lazy";
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   132
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   133
val dest_decl : (bool * string option * string) parser =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   134
  P.$$$ "(" |-- Scan.optional (P.$$$ "lazy" >> K true) false --
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   135
    (P.name >> SOME) -- (P.$$$ "::" |-- P.typ)  --| P.$$$ ")" >> P.triple1
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   136
  || P.$$$ "(" |-- P.$$$ "lazy" |-- P.typ --| P.$$$ ")"
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   137
       >> (fn t => (true,NONE,t))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   138
  || P.typ >> (fn t => (false,NONE,t));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   139
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   140
val cons_decl =
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   141
  P.name -- Scan.repeat dest_decl -- P.opt_mixfix;
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   142
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   143
val type_var' =
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   144
  (P.type_ident ^^ Scan.optional (P.$$$ "::" ^^ P.!!! P.sort) "");
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   145
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   146
val type_args' =
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   147
  type_var' >> single ||
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   148
  P.$$$ "(" |-- P.!!! (P.list1 type_var' --| P.$$$ ")") ||
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   149
  Scan.succeed [];
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   150
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   151
val domain_decl =
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   152
  (type_args' -- P.name -- P.opt_infix) --
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   153
  (P.$$$ "=" |-- P.enum1 "|" cons_decl);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   154
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   155
val domains_decl =
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   156
  Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") --
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   157
  P.and_list1 domain_decl;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   158
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   159
fun mk_domain (opt_name : string option, doms : (((string list * bstring) * mixfix) *
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   160
    ((string * (bool * string option * string) list) * mixfix) list) list ) =
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   161
  let
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   162
    val names = map (fn (((_, t), _), _) => t) doms;
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   163
    val specs = map (fn (((vs, t), mx), cons) =>
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   164
      ((t, vs, mx), map (fn ((c, ds), mx) => (c, mx, ds)) cons)) doms;
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   165
    val big_name =
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   166
      case opt_name of NONE => space_implode "_" names | SOME s => s;
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   167
  in add_domain (big_name, specs) end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   168
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   169
val _ =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   170
  OuterSyntax.command "domain" "define recursive domains (HOLCF)" K.thy_decl
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   171
    (domains_decl >> (Toplevel.theory o mk_domain));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   172
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   173
end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   174
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   175
end;