src/HOLCF/Tools/domain/domain_extender.ML
author wenzelm
Tue Sep 25 17:06:14 2007 +0200 (2007-09-25)
changeset 24712 64ed05609568
parent 24707 dfeb98f84e93
child 24867 e5b55d7be9bb
permissions -rw-r--r--
proper Sign operations instead of Theory aliases;
wenzelm@23152
     1
(*  Title:      HOLCF/Tools/domain/domain_extender.ML
wenzelm@23152
     2
    ID:         $Id$
wenzelm@23152
     3
    Author:     David von Oheimb
wenzelm@23152
     4
wenzelm@23152
     5
Theory extender for domain command, including theory syntax.
wenzelm@23152
     6
wenzelm@23152
     7
###TODO: 
wenzelm@23152
     8
wenzelm@23152
     9
this definition
wenzelm@23152
    10
domain empty = silly empty
wenzelm@23152
    11
yields
wenzelm@23152
    12
Exception-
wenzelm@23152
    13
   TERM
wenzelm@23152
    14
      ("typ_of_term: bad encoding of type",
wenzelm@23152
    15
         [Abs ("uu", "_", Const ("NONE", "_"))]) raised
wenzelm@23152
    16
but this works fine:
wenzelm@23152
    17
domain Empty = silly Empty
wenzelm@23152
    18
wenzelm@23152
    19
strange syntax errors are produced for:
wenzelm@23152
    20
domain xx = xx ("x yy")
wenzelm@23152
    21
domain 'a foo = foo (sel::"'a") 
wenzelm@23152
    22
and bar = bar ("'a dummy")
wenzelm@23152
    23
wenzelm@23152
    24
*)
wenzelm@23152
    25
wenzelm@23152
    26
signature DOMAIN_EXTENDER =
wenzelm@23152
    27
sig
wenzelm@23152
    28
  val add_domain: string * ((bstring * string list) *
wenzelm@23152
    29
    (string * mixfix * (bool * string option * string) list) list) list
wenzelm@23152
    30
    -> theory -> theory
wenzelm@23152
    31
  val add_domain_i: string * ((bstring * string list) *
wenzelm@23152
    32
    (string * mixfix * (bool * string option * typ) list) list) list
wenzelm@23152
    33
    -> theory -> theory
wenzelm@23152
    34
end;
wenzelm@23152
    35
wenzelm@23152
    36
structure Domain_Extender: DOMAIN_EXTENDER =
wenzelm@23152
    37
struct
wenzelm@23152
    38
wenzelm@23152
    39
open Domain_Library;
wenzelm@23152
    40
wenzelm@23152
    41
(* ----- general testing and preprocessing of constructor list -------------- *)
wenzelm@23152
    42
fun check_and_sort_domain (dtnvs: (string * typ list) list, 
wenzelm@23152
    43
     cons'' : ((string * mixfix * (bool * string option * typ) list) list) list) sg =
wenzelm@23152
    44
  let
wenzelm@23152
    45
    val defaultS = Sign.defaultS sg;
wenzelm@23152
    46
    val test_dupl_typs = (case duplicates (op =) (map fst dtnvs) of 
wenzelm@23152
    47
	[] => false | dups => error ("Duplicate types: " ^ commas_quote dups));
wenzelm@23152
    48
    val test_dupl_cons = (case duplicates (op =) (map first (List.concat cons'')) of 
wenzelm@23152
    49
	[] => false | dups => error ("Duplicate constructors: " 
wenzelm@23152
    50
							 ^ commas_quote dups));
wenzelm@23152
    51
    val test_dupl_sels = (case duplicates (op =) (List.mapPartial second
wenzelm@23152
    52
			       (List.concat (map third (List.concat cons'')))) of
wenzelm@23152
    53
        [] => false | dups => error("Duplicate selectors: "^commas_quote dups));
wenzelm@23152
    54
    val test_dupl_tvars = exists(fn s=>case duplicates (op =) (map(fst o dest_TFree)s)of
wenzelm@23152
    55
	[] => false | dups => error("Duplicate type arguments: " 
wenzelm@23152
    56
		   ^commas_quote dups)) (map snd dtnvs);
wenzelm@23152
    57
    (* test for free type variables, illegal sort constraints on rhs,
wenzelm@23152
    58
	       non-pcpo-types and invalid use of recursive type;
wenzelm@23152
    59
       replace sorts in type variables on rhs *)
wenzelm@23152
    60
    fun analyse_equation ((dname,typevars),cons') = 
wenzelm@23152
    61
      let
wenzelm@23152
    62
	val tvars = map dest_TFree typevars;
wenzelm@23152
    63
	val distinct_typevars = map TFree tvars;
wenzelm@23152
    64
	fun rm_sorts (TFree(s,_)) = TFree(s,[])
wenzelm@23152
    65
	|   rm_sorts (Type(s,ts)) = Type(s,remove_sorts ts)
wenzelm@23152
    66
	|   rm_sorts (TVar(s,_))  = TVar(s,[])
wenzelm@23152
    67
	and remove_sorts l = map rm_sorts l;
wenzelm@23152
    68
	val indirect_ok = ["*","Cfun.->","Ssum.++","Sprod.**","Up.u"]
wenzelm@23152
    69
	fun analyse indirect (TFree(v,s))  = (case AList.lookup (op =) tvars v of 
wenzelm@23152
    70
		    NONE => error ("Free type variable " ^ quote v ^ " on rhs.")
wenzelm@23152
    71
	          | SOME sort => if eq_set_string (s,defaultS) orelse
wenzelm@23152
    72
				    eq_set_string (s,sort    )
wenzelm@23152
    73
				 then TFree(v,sort)
wenzelm@23152
    74
				 else error ("Inconsistent sort constraint" ^
wenzelm@23152
    75
				             " for type variable " ^ quote v))
wenzelm@23152
    76
        |   analyse indirect (t as Type(s,typl)) = (case AList.lookup (op =) dtnvs s of
wenzelm@23152
    77
		NONE          => if s mem indirect_ok
wenzelm@23152
    78
				 then Type(s,map (analyse false) typl)
wenzelm@23152
    79
				 else Type(s,map (analyse true) typl)
wenzelm@23152
    80
	      | SOME typevars => if indirect 
wenzelm@23152
    81
                           then error ("Indirect recursion of type " ^ 
wenzelm@23152
    82
				        quote (string_of_typ sg t))
wenzelm@23152
    83
                           else if dname <> s orelse (** BUG OR FEATURE?: 
wenzelm@23152
    84
                                mutual recursion may use different arguments **)
wenzelm@23152
    85
				   remove_sorts typevars = remove_sorts typl 
wenzelm@23152
    86
				then Type(s,map (analyse true) typl)
wenzelm@23152
    87
				else error ("Direct recursion of type " ^ 
wenzelm@23152
    88
					     quote (string_of_typ sg t) ^ 
wenzelm@23152
    89
					    " with different arguments"))
wenzelm@23152
    90
        |   analyse indirect (TVar _) = Imposs "extender:analyse";
wenzelm@23152
    91
	fun check_pcpo T = if pcpo_type sg T then T
wenzelm@23152
    92
          else error("Constructor argument type is not of sort pcpo: "^string_of_typ sg T);
wenzelm@23152
    93
	val analyse_con = upd_third (map (upd_third (check_pcpo o analyse false)));
wenzelm@23152
    94
      in ((dname,distinct_typevars), map analyse_con cons') end; 
wenzelm@23152
    95
  in ListPair.map analyse_equation (dtnvs,cons'')
wenzelm@23152
    96
  end; (* let *)
wenzelm@23152
    97
wenzelm@23152
    98
(* ----- calls for building new thy and thms -------------------------------- *)
wenzelm@23152
    99
wenzelm@23152
   100
fun gen_add_domain prep_typ (comp_dnam, eqs''') thy''' =
wenzelm@23152
   101
  let
wenzelm@23152
   102
    val dtnvs = map ((fn (dname,vs) => 
wenzelm@24707
   103
			 (Sign.full_name thy''' dname, map (Syntax.read_typ_global thy''') vs))
wenzelm@23152
   104
                   o fst) eqs''';
wenzelm@23152
   105
    val cons''' = map snd eqs''';
wenzelm@23152
   106
    fun thy_type  (dname,tvars)  = (Sign.base_name dname, length tvars, NoSyn);
wenzelm@23152
   107
    fun thy_arity (dname,tvars)  = (dname, map (snd o dest_TFree) tvars, pcpoS);
wenzelm@24712
   108
    val thy'' = thy''' |> Sign.add_types     (map thy_type  dtnvs)
wenzelm@23152
   109
		       |> fold (AxClass.axiomatize_arity_i o thy_arity) dtnvs;
wenzelm@23152
   110
    val cons'' = map (map (upd_third (map (upd_third (prep_typ thy''))))) cons''';
wenzelm@23152
   111
    val eqs' = check_and_sort_domain (dtnvs,cons'') thy'';
wenzelm@23152
   112
    val thy' = thy'' |> Domain_Syntax.add_syntax (comp_dnam,eqs');
wenzelm@23152
   113
    val dts  = map (Type o fst) eqs';
wenzelm@23152
   114
    val new_dts = map (fn ((s,Ts),_) => (s, map (fst o dest_TFree) Ts)) eqs';
wenzelm@23152
   115
    fun strip ss = Library.drop (find_index_eq "'" ss +1, ss);
wenzelm@23152
   116
    fun typid (Type  (id,_)) =
wenzelm@23152
   117
          let val c = hd (Symbol.explode (Sign.base_name id))
wenzelm@23152
   118
          in if Symbol.is_letter c then c else "t" end
wenzelm@23152
   119
      | typid (TFree (id,_)   ) = hd (strip (tl (Symbol.explode id)))
wenzelm@23152
   120
      | typid (TVar ((id,_),_)) = hd (tl (Symbol.explode id));
wenzelm@23152
   121
    fun one_con (con,mx,args) =
wenzelm@23152
   122
	((Syntax.const_name con mx),
wenzelm@23152
   123
	 ListPair.map (fn ((lazy,sel,tp),vn) => ((lazy,
wenzelm@23152
   124
					find_index_eq tp dts,
wenzelm@23152
   125
					DatatypeAux.dtyp_of_typ new_dts tp),
wenzelm@23152
   126
					sel,vn))
wenzelm@23152
   127
	     (args,(mk_var_names(map (typid o third) args)))
wenzelm@23152
   128
	 ) : cons;
wenzelm@23152
   129
    val eqs = map (fn (dtnvs,cons') => (dtnvs, map one_con cons')) eqs' : eq list;
wenzelm@23152
   130
    val thy        = thy' |> Domain_Axioms.add_axioms (comp_dnam,eqs);
wenzelm@23152
   131
    val (theorems_thy, (rewss, take_rews)) = (foldl_map (fn (thy0,eq) =>
wenzelm@23152
   132
      Domain_Theorems.theorems (eq,eqs) thy0) (thy,eqs))
wenzelm@23152
   133
      |>>> Domain_Theorems.comp_theorems (comp_dnam, eqs);
wenzelm@23152
   134
  in
wenzelm@23152
   135
    theorems_thy
wenzelm@24712
   136
    |> Sign.add_path (Sign.base_name comp_dnam)
wenzelm@23152
   137
    |> (snd o (PureThy.add_thmss [(("rews", List.concat rewss @ take_rews), [])]))
wenzelm@24712
   138
    |> Sign.parent_path
wenzelm@23152
   139
  end;
wenzelm@23152
   140
wenzelm@23152
   141
val add_domain_i = gen_add_domain Sign.certify_typ;
wenzelm@24707
   142
val add_domain = gen_add_domain Syntax.read_typ_global;
wenzelm@23152
   143
wenzelm@23152
   144
wenzelm@23152
   145
(** outer syntax **)
wenzelm@23152
   146
wenzelm@23152
   147
local structure P = OuterParse and K = OuterKeyword in
wenzelm@23152
   148
wenzelm@23152
   149
val dest_decl =
wenzelm@23152
   150
  P.$$$ "(" |-- Scan.optional (P.$$$ "lazy" >> K true) false --
wenzelm@23152
   151
    (P.name >> SOME) -- (P.$$$ "::" |-- P.typ)  --| P.$$$ ")" >> P.triple1
wenzelm@23152
   152
  || P.$$$ "(" |-- P.$$$ "lazy" |-- P.typ --| P.$$$ ")"
wenzelm@23152
   153
       >> (fn t => (true,NONE,t))
wenzelm@23152
   154
  || P.typ >> (fn t => (false,NONE,t));
wenzelm@23152
   155
wenzelm@23152
   156
val cons_decl =
wenzelm@23152
   157
  P.name -- Scan.repeat dest_decl -- P.opt_mixfix
wenzelm@23152
   158
  >> (fn ((c, ds), mx) => (c, mx, ds));
wenzelm@23152
   159
wenzelm@23152
   160
val type_var' = (P.type_ident ^^ 
wenzelm@23152
   161
                 Scan.optional (P.$$$ "::" ^^ P.!!! P.sort) "");
wenzelm@23152
   162
val type_args' = type_var' >> single ||
wenzelm@23152
   163
                 P.$$$ "(" |-- P.!!! (P.list1 type_var' --| P.$$$ ")") ||
wenzelm@23152
   164
 		 Scan.succeed [];
wenzelm@23152
   165
wenzelm@23152
   166
val domain_decl = (type_args' -- P.name >> Library.swap) -- 
wenzelm@23152
   167
                  (P.$$$ "=" |-- P.enum1 "|" cons_decl);
wenzelm@23152
   168
val domains_decl =
wenzelm@23152
   169
  Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- P.and_list1 domain_decl
wenzelm@23152
   170
  >> (fn (opt_name, doms) =>
wenzelm@23152
   171
      (case opt_name of NONE => space_implode "_" (map (#1 o #1) doms) | SOME s => s, doms));
wenzelm@23152
   172
wenzelm@23152
   173
val domainP =
wenzelm@23152
   174
  OuterSyntax.command "domain" "define recursive domains (HOLCF)" K.thy_decl
wenzelm@23152
   175
    (domains_decl >> (Toplevel.theory o add_domain));
wenzelm@23152
   176
wenzelm@23152
   177
wenzelm@23152
   178
val _ = OuterSyntax.add_keywords ["lazy"];
wenzelm@23152
   179
val _ = OuterSyntax.add_parsers [domainP];
wenzelm@23152
   180
wenzelm@23152
   181
end; (* local structure *)
wenzelm@23152
   182
wenzelm@23152
   183
end;