src/HOLCF/domain/extender.ML
author oheimb
Tue, 04 Nov 1997 14:40:29 +0100
changeset 4122 f63c283cefaf
parent 4043 35766855f344
child 4188 1025a27b08f9
permissions -rw-r--r--
* removed "axioms" and "generated by" section * replaced "ops" section by extended "consts" section, which is capable of handling the continuous function space "->" directly * domain package: now uses normal mixfix annotations (instead of cinfix...)
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$
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
     3
    Author : David von Oheimb
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
     4
    Copyright 1995, 1996 TU Muenchen
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
     5
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
     6
theory extender for domain section
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
     7
*)
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
     8
2446
c2a9bf6c0948 The previous log message was wrong. The correct one is:
oheimb
parents: 2445
diff changeset
     9
1637
b8a8ae2e5de1 Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents: 1461
diff changeset
    10
structure Domain_Extender =
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    11
struct
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    12
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    13
local
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    14
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    15
open Domain_Library;
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    16
2445
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
    17
(* ----- general testing and preprocessing of constructor list -------------- *)
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    18
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    19
  fun check_and_sort_domain (dtnvs: (string * typ list) list, cons'' :
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4043
diff changeset
    20
     ((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
    21
  let
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    22
    val defaultS = Type.defaultS (tsig_of sg);
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    23
    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
    24
	[] => false | dups => error ("Duplicate types: " ^ commas_quote dups));
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    25
    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
    26
	[] => false | dups => error ("Duplicate constructors: " 
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    27
							 ^ commas_quote dups));
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    28
    val test_dupl_sels = (case duplicates 
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    29
			       (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
    30
        [] => 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
    31
    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
    32
	[] => false | dups => error("Duplicate type arguments: " 
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    33
		   ^commas_quote dups)) (map snd dtnvs);
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    34
    (* 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
    35
	       non-pcpo-types and invalid use of recursive type;
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    36
       replace sorts in type variables on rhs *)
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    37
    fun analyse_equation ((dname,typevars),cons') = 
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    38
      let
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    39
	val tvars = map rep_TFree typevars;
4030
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    40
	fun distinct_name s = "'"^Sign.base_name dname^"_"^s;
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    41
	val distinct_typevars = map (fn (n,sort) => 
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    42
				     TFree (distinct_name n,sort)) tvars;
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    43
	fun rm_sorts (TFree(s,_)) = TFree(s,[])
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    44
	|   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
    45
	|   rm_sorts (TVar(s,_))  = TVar(s,[])
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    46
	and remove_sorts l = map rm_sorts l;
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    47
	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
    48
		    None      => error ("Free type variable " ^ v ^ " on rhs.")
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    49
	          | Some sort => if eq_set_string (s,defaultS) orelse
4030
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    50
				    eq_set_string (s,sort    )
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    51
				 then TFree(distinct_name v,sort)
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    52
				 else error ("Additional constraint on rhs "^
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    53
					     "for type variable "^quote v))
4030
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    54
        |    analyse(Type(s,typl)) = if s <> dname 
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    55
			then Type(s,map analyse typl)
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    56
			else if remove_sorts typevars = remove_sorts typl 
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    57
				then Type(s,map analyse typl) 
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    58
				else error ("Recursion of type " ^ s ^ 
4030
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    59
					    " with different arguments")
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    60
        | analyse(TVar _) = Imposs "extender:analyse";
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    61
	fun check_pcpo t = (pcpo_type sg t orelse 
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    62
			   error("Not a pcpo type: "^string_of_typ sg t); t);
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    63
	val analyse_con = upd_third (map (upd_third (check_pcpo o analyse)));
4030
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    64
      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
    65
  in ListPair.map analyse_equation (dtnvs,cons'')
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    66
  end; (* let *)
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    67
4030
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    68
(* ----- calls for building new thy and thms -------------------------------- *)
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    69
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    70
in
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    71
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    72
  fun add_domain (comp_dnam,eqs''') thy''' = let
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    73
    val sg''' = sign_of thy''';
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    74
    val dtnvs = map ((fn (dname,vs) => 
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    75
			 (Sign.full_name sg''' dname,map (str2typ sg''') vs))
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    76
                   o fst) eqs''';
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    77
    val cons''' = map snd eqs''';
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    78
    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
    79
    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
    80
    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
    81
		       |> Theory.add_arities_i (map thy_arity dtnvs);
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    82
    val sg'' = sign_of thy'';
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    83
    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
    84
    val eqs' = check_and_sort_domain (dtnvs,cons'') sg'';
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    85
    val thy' = thy'' |> Domain_Syntax.add_syntax (comp_dnam,eqs');
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    86
    val dts  = map (Type o fst) eqs';
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4043
diff changeset
    87
    fun strip ss = drop (find ("'", ss)+1, ss);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4043
diff changeset
    88
    fun typid (Type  (id,_)   ) = hd     (explode (Sign.base_name id))
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4043
diff changeset
    89
      | typid (TFree (id,_)   ) = hd (strip (tl (explode (Sign.base_name id))))
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4043
diff changeset
    90
      | typid (TVar ((id,_),_)) = hd (tl (explode (Sign.base_name id)));
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    91
    fun cons cons' = (map (fn (con,syn,args) =>
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4043
diff changeset
    92
	((Syntax.const_name con syn),
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 2446
diff changeset
    93
	 ListPair.map (fn ((lazy,sel,tp),vn) => ((lazy,
4030
ca44afcc259c debugging concerning sort variables
oheimb
parents: 4008
diff changeset
    94
					find (tp,dts) handle LIST "find" => ~1),
1637
b8a8ae2e5de1 Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents: 1461
diff changeset
    95
					sel,vn))
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4043
diff changeset
    96
	     (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
    97
	 )) cons') : cons list;
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    98
    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
    99
    val thy        = thy' |> Domain_Axioms.add_axioms (comp_dnam,eqs);
4043
35766855f344 domain package:
oheimb
parents: 4030
diff changeset
   100
  in (foldl (fn (thy0,eq) => Domain_Theorems.theorems (eq,eqs) thy0) (thy,eqs)) 
35766855f344 domain package:
oheimb
parents: 4030
diff changeset
   101
                          |> Domain_Theorems.comp_theorems (comp_dnam, eqs) end;
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   102
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   103
end (* local *)
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   104
end (* struct *)