src/HOLCF/domain/extender.ML
author wenzelm
Tue, 12 Feb 2002 20:28:27 +0100
changeset 12876 a70df1e5bf10
parent 12037 0282eacef4e7
child 14097 f4d2ff3cad09
permissions -rw-r--r--
got rid of explicit marginal comments (now stripped earlier from input);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2445
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
     1
(*  Title:      HOLCF/domain/extender.ML
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
     2
    ID:         $Id$
12037
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
     3
    Author:     David von Oheimb and Markus Wenzel
12030
wenzelm
parents: 11728
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
2445
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
     5
12037
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
     6
Theory extender for domain section, including new-style theory syntax.
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
     7
*)
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
     8
12037
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
     9
signature DOMAIN_EXTENDER =
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
    10
sig
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
    11
  val add_domain: string *
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
    12
      ((bstring * string list) * (string * mixfix * (bool * string * string) list) list) list
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
    13
    -> theory -> theory
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
    14
end;
2446
c2a9bf6c0948 The previous log message was wrong. The correct one is:
oheimb
parents: 2445
diff changeset
    15
12037
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
    16
structure Domain_Extender: DOMAIN_EXTENDER =
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    17
struct
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    18
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    19
open Domain_Library;
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    20
2445
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
    21
(* ----- general testing and preprocessing of constructor list -------------- *)
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    22
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    23
  fun check_and_sort_domain (dtnvs: (string * typ list) list, cons'' :
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4043
diff changeset
    24
     ((string * mixfix * (bool*string*typ) list) list) list) sg =
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    25
  let
7653
0408848fa7d4 Sign.defaultS;
wenzelm
parents: 4753
diff changeset
    26
    val defaultS = Sign.defaultS sg;
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    27
    val test_dupl_typs = (case duplicates (map fst dtnvs) of 
1637
b8a8ae2e5de1 Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents: 1461
diff changeset
    28
	[] => false | dups => error ("Duplicate types: " ^ commas_quote dups));
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    29
    val test_dupl_cons = (case duplicates (map first (flat cons'')) of 
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    30
	[] => false | dups => error ("Duplicate constructors: " 
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    31
							 ^ commas_quote dups));
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    32
    val test_dupl_sels = (case duplicates 
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    33
			       (map second (flat (map third (flat cons'')))) of
2445
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
    34
        [] => false | dups => error("Duplicate selectors: "^commas_quote dups));
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
    35
    val test_dupl_tvars = exists(fn s=>case duplicates(map(fst o rep_TFree)s)of
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
    36
	[] => false | dups => error("Duplicate type arguments: " 
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    37
		   ^commas_quote dups)) (map snd dtnvs);
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    38
    (* test for free type variables, illegal sort constraints on rhs,
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    39
	       non-pcpo-types and invalid use of recursive type;
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    40
       replace sorts in type variables on rhs *)
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    41
    fun analyse_equation ((dname,typevars),cons') = 
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    42
      let
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    43
	val tvars = map rep_TFree typevars;
4030
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    44
	fun distinct_name s = "'"^Sign.base_name dname^"_"^s;
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    45
	val distinct_typevars = map (fn (n,sort) => 
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    46
				     TFree (distinct_name n,sort)) tvars;
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    47
	fun rm_sorts (TFree(s,_)) = TFree(s,[])
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    48
	|   rm_sorts (Type(s,ts)) = Type(s,remove_sorts ts)
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    49
	|   rm_sorts (TVar(s,_))  = TVar(s,[])
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    50
	and remove_sorts l = map rm_sorts l;
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    51
	fun analyse(TFree(v,s)) = (case assoc_string(tvars,v) of 
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    52
		    None      => error ("Free type variable " ^ v ^ " on rhs.")
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    53
	          | Some sort => if eq_set_string (s,defaultS) orelse
4030
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    54
				    eq_set_string (s,sort    )
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    55
				 then TFree(distinct_name v,sort)
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    56
				 else error ("Additional constraint on rhs "^
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    57
					     "for type variable "^quote v))
4753
b3aab5c73b52 improved checks
oheimb
parents: 4709
diff changeset
    58
	(** BUG OR FEATURE?: mutual recursion may use different arguments **)
b3aab5c73b52 improved checks
oheimb
parents: 4709
diff changeset
    59
        |   analyse(Type(s,typl)) = (case assoc_string((*dtnvs*)
b3aab5c73b52 improved checks
oheimb
parents: 4709
diff changeset
    60
						       [(dname,typevars)],s) of 
b3aab5c73b52 improved checks
oheimb
parents: 4709
diff changeset
    61
		None          => Type(s,map analyse typl)
b3aab5c73b52 improved checks
oheimb
parents: 4709
diff changeset
    62
	      | Some typevars => if remove_sorts typevars = remove_sorts typl 
b3aab5c73b52 improved checks
oheimb
parents: 4709
diff changeset
    63
				then Type(s,map analyse typl)
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    64
				else error ("Recursion of type " ^ s ^ 
4753
b3aab5c73b52 improved checks
oheimb
parents: 4709
diff changeset
    65
					    " with different arguments"))
b3aab5c73b52 improved checks
oheimb
parents: 4709
diff changeset
    66
        |   analyse(TVar _) = Imposs "extender:analyse";
b3aab5c73b52 improved checks
oheimb
parents: 4709
diff changeset
    67
	fun check_pcpo t = (pcpo_type sg t orelse error(
b3aab5c73b52 improved checks
oheimb
parents: 4709
diff changeset
    68
			   "Type not of sort pcpo: "^string_of_typ sg t); t);
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    69
	val analyse_con = upd_third (map (upd_third (check_pcpo o analyse)));
4030
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    70
      in ((dname,distinct_typevars), map analyse_con cons') end; 
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    71
  in ListPair.map analyse_equation (dtnvs,cons'')
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    72
  end; (* let *)
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    73
4030
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    74
(* ----- calls for building new thy and thms -------------------------------- *)
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    75
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    76
  fun add_domain (comp_dnam,eqs''') thy''' = let
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    77
    val sg''' = sign_of thy''';
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    78
    val dtnvs = map ((fn (dname,vs) => 
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    79
			 (Sign.full_name sg''' dname,map (str2typ sg''') vs))
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    80
                   o fst) eqs''';
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    81
    val cons''' = map snd eqs''';
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    82
    fun thy_type  (dname,tvars)  = (Sign.base_name dname, length tvars, NoSyn);
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    83
    fun thy_arity (dname,tvars)  = (dname, map (snd o rep_TFree) tvars, pcpoS);
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    84
    val thy'' = thy''' |> Theory.add_types     (map thy_type  dtnvs)
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    85
		       |> Theory.add_arities_i (map thy_arity dtnvs);
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    86
    val sg'' = sign_of thy'';
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    87
    val cons''=map (map (upd_third (map (upd_third (str2typ sg''))))) cons''';
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    88
    val eqs' = check_and_sort_domain (dtnvs,cons'') sg'';
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    89
    val thy' = thy'' |> Domain_Syntax.add_syntax (comp_dnam,eqs');
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    90
    val dts  = map (Type o fst) eqs';
4188
1025a27b08f9 changed libraray function find to find_index_eq, currying it
oheimb
parents: 4122
diff changeset
    91
    fun strip ss = drop (find_index_eq "'" ss +1, ss);
11728
b5f6963b193c fixed typid;
wenzelm
parents: 7653
diff changeset
    92
    fun typid (Type  (id,_)) =
b5f6963b193c fixed typid;
wenzelm
parents: 7653
diff changeset
    93
          let val c = hd (Symbol.explode (Sign.base_name id))
b5f6963b193c fixed typid;
wenzelm
parents: 7653
diff changeset
    94
          in if Symbol.is_letter c then c else "t" end
b5f6963b193c fixed typid;
wenzelm
parents: 7653
diff changeset
    95
      | typid (TFree (id,_)   ) = hd (strip (tl (Symbol.explode id)))
b5f6963b193c fixed typid;
wenzelm
parents: 7653
diff changeset
    96
      | typid (TVar ((id,_),_)) = hd (tl (Symbol.explode id));
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    97
    fun cons cons' = (map (fn (con,syn,args) =>
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4043
diff changeset
    98
	((Syntax.const_name con syn),
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    99
	 ListPair.map (fn ((lazy,sel,tp),vn) => ((lazy,
4188
1025a27b08f9 changed libraray function find to find_index_eq, currying it
oheimb
parents: 4122
diff changeset
   100
					find_index_eq tp dts),
1637
b8a8ae2e5de1 Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents: 1461
diff changeset
   101
					sel,vn))
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4043
diff changeset
   102
	     (args,(mk_var_names(map (typid o third) args)))
1637
b8a8ae2e5de1 Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents: 1461
diff changeset
   103
	 )) cons') : cons list;
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   104
    val eqs = map (fn (dtnvs,cons') => (dtnvs,cons cons')) eqs' : eq list;
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4043
diff changeset
   105
    val thy        = thy' |> Domain_Axioms.add_axioms (comp_dnam,eqs);
12037
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   106
    val (theorems_thy, (rewss, take_rews)) = (foldl_map (fn (thy0,eq) =>
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   107
      Domain_Theorems.theorems (eq,eqs) thy0) (thy,eqs))
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   108
      |>>> Domain_Theorems.comp_theorems (comp_dnam, eqs);
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   109
in
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   110
  theorems_thy
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   111
  |> Theory.add_path (Sign.base_name comp_dnam)
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   112
  |> (#1 o (PureThy.add_thmss [(("rews", flat rewss @ take_rews), [])]))
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   113
  |> Theory.parent_path
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   114
end;
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   115
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   116
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   117
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   118
(** outer syntax **)
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   119
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   120
local structure P = OuterParse and K = OuterSyntax.Keyword in
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   121
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   122
val dest_decl =
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   123
  P.$$$ "(" |-- Scan.optional (P.$$$ "lazy" >> K true) false --
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   124
    P.name -- (P.$$$ "::" |-- P.typ)  --| P.$$$ ")" >> P.triple1;
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   125
12037
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   126
val cons_decl =
12876
a70df1e5bf10 got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents: 12037
diff changeset
   127
  P.name -- Scan.repeat dest_decl -- P.opt_mixfix
12037
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   128
  >> (fn ((c, ds), mx) => (c, mx, ds));
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   129
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   130
val domain_decl = (P.type_args -- P.name >> Library.swap) -- (P.$$$ "=" |-- P.enum1 "|" cons_decl);
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   131
val domains_decl =
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   132
  Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- P.and_list1 domain_decl
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   133
  >> (fn (opt_name, doms) =>
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   134
      (case opt_name of None => space_implode "_" (map (#1 o #1) doms) | Some s => s, doms));
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   135
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   136
val domainP =
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   137
  OuterSyntax.command "domain" "define recursive domains (HOLCF)" K.thy_decl
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   138
    (domains_decl >> (Toplevel.theory o add_domain));
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   139
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   140
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   141
val _ = OuterSyntax.add_keywords ["lazy"];
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   142
val _ = OuterSyntax.add_parsers [domainP];
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   143
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   144
end;
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   145
0282eacef4e7 adapted to new-style theories;
wenzelm
parents: 12030
diff changeset
   146
end;