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