src/HOLCF/domain/extender.ML
changeset 4008 2444085532c6
parent 2446 c2a9bf6c0948
child 4030 ca44afcc259c
equal deleted inserted replaced
4007:1d6aed7ff375 4008:2444085532c6
    14 
    14 
    15 open Domain_Library;
    15 open Domain_Library;
    16 
    16 
    17 (* ----- general testing and preprocessing of constructor list -------------- *)
    17 (* ----- general testing and preprocessing of constructor list -------------- *)
    18 
    18 
    19   fun check_and_sort_domain (eqs'':((string * typ list) *
    19   fun check_and_sort_domain (dtnvs: (string * typ list) list, cons'' :
    20      (string * ThyOps.cmixfix * (bool*string*typ) list) list) list) thy'' = let
    20      ((string * ThyOps.cmixfix * (bool*string*typ) list) list) list) sg =
    21     val dtnvs = map fst eqs'';
    21   let
    22     val cons' = flat (map snd eqs'');
    22     val defaultS = Type.defaultS (tsig_of sg);
    23     val test_dupl_typs = (case duplicates (map fst dtnvs) of 
    23     val test_dupl_typs = (case duplicates (map fst dtnvs) of 
    24 	[] => false | dups => error ("Duplicate types: " ^ commas_quote dups));
    24 	[] => false | dups => error ("Duplicate types: " ^ commas_quote dups));
    25     val test_dupl_cons = (case duplicates (map first cons') of 
    25     val test_dupl_cons = (case duplicates (map first (flat cons'')) of 
    26 	[] => false | dups => error ("Duplicate constructors: " ^ commas_quote dups));
    26 	[] => false | dups => error ("Duplicate constructors: " 
    27     val test_dupl_sels = (case duplicates(map second (flat(map third cons'))) of
    27 							 ^ commas_quote dups));
       
    28     val test_dupl_sels = (case duplicates 
       
    29 			       (map second (flat (map third (flat cons'')))) of
    28         [] => false | dups => error("Duplicate selectors: "^commas_quote dups));
    30         [] => false | dups => error("Duplicate selectors: "^commas_quote dups));
    29     val test_dupl_tvars = exists(fn s=>case duplicates(map(fst o rep_TFree)s)of
    31     val test_dupl_tvars = exists(fn s=>case duplicates(map(fst o rep_TFree)s)of
    30 	[] => false | dups => error("Duplicate type arguments: " 
    32 	[] => false | dups => error("Duplicate type arguments: " 
    31 							   ^commas_quote dups))
    33 		   ^commas_quote dups)) (map snd dtnvs);
    32 	(map snd dtnvs);
    34     (* test for free type variables, illegal sort constraints on rhs,
    33     val default = ["_default"];
    35 	       non-pcpo-types and invalid use of recursive type;
    34     (*test for free type variables, Inconsistent sort constraints,
    36        replace sorts in type variables on rhs *)
    35 	       non-pcpo-types and invalid use of recursive type*)
    37     fun analyse_equation ((dname,typevars),cons') = 
    36     in map (fn ((dname,typevars),cons') => let
    38       let
    37       val tvars = ref (map rep_TFree typevars) : (string * sort) list ref;
    39 	val tvars = map rep_TFree typevars;
    38       fun newsort (TFree(v,s)) = TFree(v,case assoc_string (!tvars,v) of
    40 	fun rm_sorts (TFree(s,_)) = TFree(s,[])
    39 		None   => Imposs "extender:newsort"
    41 	|   rm_sorts (Type(s,ts)) = Type(s,remove_sorts ts)
    40 	      | Some s => if s=default then Type.defaultS(tsig_of thy'') else s)
    42 	|   rm_sorts (TVar(s,_))  = TVar(s,[])
    41       |   newsort (Type(s,typl)) = Type(s,map newsort typl)
    43 	and remove_sorts l = map rm_sorts l;
    42       |   newsort (TVar _) = Imposs "extender:newsort 2";
    44 	fun analyse(TFree(v,s)) = (case assoc_string(tvars,v) of 
    43       val analyse_cons = forall (fn con' => let
    45 		    None      => error ("Free type variable " ^ v ^ " on rhs.")
    44 	  val types = map third (third con');
    46 	          | Some sort => if eq_set_string (s,defaultS) orelse
    45 	  fun rm_sorts (TFree(s,_)) = TFree(s,[])
    47 				    eq_set_string (s,sort    ) then TFree(v,sort)
    46 	  |   rm_sorts (Type(s,ts)) = Type(s,remove_sorts ts)
    48 				 else error ("Additional constraint on rhs "^
    47 	  |   rm_sorts (TVar(s,_))  = TVar(s,[])
    49 					     "for type variable "^quote v))
    48 	  and remove_sorts l = map rm_sorts l;
    50         |    analyse(Type(s,typl)) = (case assoc_string (dtnvs,s) of 
    49           fun analyse(TFree(v,s)) = (case assoc_string(!tvars,v) of 
    51 		    None     =>      Type(s,map analyse typl)
    50 			None =>	error ("Free type variable " ^ v ^ " on rhs.")
    52 	          | Some tvs => if remove_sorts tvs = remove_sorts typl 
    51 		      | Some sort => s = default orelse
    53 				then Type(s,map analyse typl) 
    52 				     if sort = default 
    54 				else error ("Recursion of type " ^ s ^ 
    53 					then (tvars:= (v,s):: !tvars;true)
    55 					    " with different arguments"))
    54 					else eq_set_string (s,sort) orelse
    56         | analyse(TVar _) = Imposs "extender:analyse";
    55 					error ("Inconsistent sort constraints "^
    57 	fun check_pcpo t = (pcpo_type sg t orelse 
    56 					       "for type variable "^quote v))
    58 			   error("Not a pcpo type: "^string_of_typ sg t); t);
    57 	    | analyse(Type(s,typl)) = (case assoc_string (dtnvs,s) of 
    59 	val analyse_con = upd_third (map (upd_third (check_pcpo o analyse)));
    58 			None => forall analyse typl
    60       in ((dname,typevars), map analyse_con cons') end; 
    59 		      | Some tvs => remove_sorts tvs = remove_sorts typl orelse 
    61   in ListPair.map analyse_equation (dtnvs,cons'')
    60 		       		    error ("Recursion of type " ^ s ^ 
    62   end; (* let *)
    61 					   " with different arguments"))
    63 
    62 	    | analyse(TVar _) = Imposs "extender:analyse";
    64   fun check_gen_by sg' (typs': string list,cnstrss': string list list) = let
    63 	  in forall analyse types end) cons';
       
    64       fun check_pcpo t = (pcpo_type thy'' t orelse 
       
    65 			  error("Not a pcpo type: "^string_of_typ thy'' t); t);
       
    66       fun check_type (t as Type(s,typl)) = (case assoc_string (dtnvs,s) of 
       
    67 			None => check_pcpo t | Some _ => t)
       
    68       |   check_type t = check_pcpo t;
       
    69       in ((dname,map newsort typevars),
       
    70 	  map (upd_third (map (upd_third (check_type o newsort)))) cons')
       
    71       end) eqs''
       
    72     end; (* let *)
       
    73   fun check_gen_by thy' (typs': string list,cnstrss': string list list) = let
       
    74     val test_dupl_typs = (case duplicates typs' of [] => false
    65     val test_dupl_typs = (case duplicates typs' of [] => false
    75 	  | dups => error ("Duplicate types: " ^ commas_quote dups));
    66 	  | dups => error ("Duplicate types: " ^ commas_quote dups));
    76     val test_dupl_cnstrs = map (fn cs => (case duplicates cs of [] => false 
    67     val test_dupl_cnstrs = map (fn cs => (case duplicates cs of [] => false 
    77 	| ds => error ("Duplicate constructors: " ^ commas_quote ds))) cnstrss';
    68 	| ds => error ("Duplicate constructors: " ^ commas_quote ds))) cnstrss';
    78     val tycons = map fst (#tycons(Type.rep_tsig (tsig_of thy')));
    69     val tycons = map fst (#tycons(Type.rep_tsig (tsig_of sg')));
    79     val test_types = forall (fn t => t mem tycons orelse 
    70     val test_types = forall (fn t => t mem tycons orelse 
    80 				     error("Unknown type: "^t)) typs';
    71 				     error("Unknown type: "^t)) typs';
    81     val cnstrss = let
    72     val cnstrss = let
    82 	fun type_of c = case (Sign.const_type(sign_of thy') c) of Some t => t
    73 	fun type_of c = case (Sign.const_type sg' c) of Some t => t
    83 				| None => error ("Unknown constructor: "^c);
    74 				| None => error ("Unknown constructor: "^c);
    84 	fun args_result_type (t as (Type(tn,[arg,rest]))) = 
    75 	fun args_result_type (t as (Type(tn,[arg,rest]))) = 
    85 		if tn = "->" orelse tn = "=>"
    76 		if tn = "->" orelse tn = "=>"
    86 		then let val (ts,r) = args_result_type rest in (arg::ts,r) end
    77 		then let val (ts,r) = args_result_type rest in (arg::ts,r) end
    87 		else ([],t)
    78 		else ([],t)
    93 	   (typ list *			(* argument types *)
    84 	   (typ list *			(* argument types *)
    94 	    typ))			(* result type *)
    85 	    typ))			(* result type *)
    95 	  list list end;
    86 	  list list end;
    96     fun test_equal_type tn (cn,_,(_,rt)) = fst (rep_Type rt) = tn orelse
    87     fun test_equal_type tn (cn,_,(_,rt)) = fst (rep_Type rt) = tn orelse
    97 		      error("Inappropriate result type for constructor "^cn);
    88 		      error("Inappropriate result type for constructor "^cn);
    98     val typs = map (fn (tn, cnstrs) => (map (test_equal_type tn) cnstrs; 
    89     val typs = ListPair.map (fn (tn, cnstrs) => (map (test_equal_type tn) cnstrs;
    99 				snd(third(hd(cnstrs)))))  (typs'~~cnstrss);
    90 				snd(third(hd(cnstrs)))))  (typs',cnstrss);
   100     val test_typs = map (fn (typ,cnstrs) => 
    91     val test_typs = ListPair.map (fn (typ,cnstrs) => 
   101 			if not (pcpo_type thy' typ)
    92 			if not (pcpo_type sg' typ)
   102 			then error("Not a pcpo type: "^string_of_typ thy' typ)
    93 			then error("Not a pcpo type: "^string_of_typ sg' typ)
   103 			else map (fn (cn,_,(_,rt)) => rt=typ orelse error(
    94 			else map (fn (cn,_,(_,rt)) => rt=typ orelse error(
   104 				"Non-identical result types for constructors "^
    95 				"Non-identical result types for constructors "^
   105 			        first(hd cnstrs)^" and "^ cn ))  cnstrs)
    96 			        first(hd cnstrs)^" and "^ cn ))  cnstrs)
   106 		    (typs~~cnstrss);
    97 		    (typs,cnstrss);
   107     val proper_args = let
    98     val proper_args = let
   108 	fun occurs tn (Type(tn',ts)) = (tn'=tn) orelse exists (occurs tn) ts
    99 	fun occurs tn (Type(tn',ts)) = (tn'=tn) orelse exists (occurs tn) ts
   109 	|   occurs _  _              = false;
   100 	|   occurs _  _              = false;
   110 	fun proper_arg cn atyp = forall (fn typ => let 
   101 	fun proper_arg cn atyp = forall (fn typ => let 
   111 				   val tn = fst (rep_Type typ) 
   102 				   val tn = fst (rep_Type typ) 
   118 
   109 
   119 (* ----- calls for building new thy and thms -------------------------------------- *)
   110 (* ----- calls for building new thy and thms -------------------------------------- *)
   120 
   111 
   121 in
   112 in
   122 
   113 
   123   fun add_domain (comp_dname,eqs'') thy'' = let
   114   fun add_domain (comp_dnam,eqs''') thy''' = let
   124     val eqs' = check_and_sort_domain eqs'' thy'';
   115     val sg''' = sign_of thy''';
   125     val thy' = thy'' |> Domain_Syntax.add_syntax (comp_dname,eqs');
   116     val dtnvs = map ((fn (dname,vs) => 
       
   117 			 (Sign.full_name sg''' dname,map (str2typ sg''') vs))
       
   118                    o fst) eqs''';
       
   119     val cons''' = map snd eqs''';
       
   120     fun thy_type  (dname,tvars)  = (Sign.base_name dname, length tvars, NoSyn);
       
   121     fun thy_arity (dname,tvars)  = (dname, map (snd o rep_TFree) tvars, pcpoS);
       
   122     val thy'' = thy''' |> Theory.add_types     (map thy_type  dtnvs)
       
   123 		       |> Theory.add_arities_i (map thy_arity dtnvs);
       
   124     val sg'' = sign_of thy'';
       
   125     val cons''=map (map (upd_third (map (upd_third (str2typ sg''))))) cons''';
       
   126     val eqs' = check_and_sort_domain (dtnvs,cons'') sg'';
       
   127     val thy' = thy'' |> Domain_Syntax.add_syntax (comp_dnam,eqs');
   126     val dts  = map (Type o fst) eqs';
   128     val dts  = map (Type o fst) eqs';
   127     fun cons cons' = (map (fn (con,syn,args) =>
   129     fun cons cons' = (map (fn (con,syn,args) =>
   128 	(ThyOps.const_name con syn,
   130 	((ThyOps.const_name con syn),
   129 	 map (fn ((lazy,sel,tp),vn) => ((lazy,
   131 	 ListPair.map (fn ((lazy,sel,tp),vn) => ((lazy,
   130 					 find (tp,dts) handle LIST "find" => ~1),
   132 					 find (tp,dts) handle LIST "find" => ~1),
   131 					sel,vn))
   133 					sel,vn))
   132 	     (args~~(mk_var_names(map third args)))
   134 	     (args,(mk_var_names(map third args)))
   133 	 )) cons') : cons list;
   135 	 )) cons') : cons list;
   134     val eqs = map (fn (dtnvs,cons') => (dtnvs,cons cons')) eqs' : eq list;
   136     val eqs = map (fn (dtnvs,cons') => (dtnvs,cons cons')) eqs' : eq list;
   135     val thy         = thy' |> Domain_Axioms.add_axioms (comp_dname,eqs);
   137     val thy         = thy' |> Domain_Axioms.add_axioms (comp_dnam,eqs);
   136   in (thy,eqs) end;
   138   in (thy,eqs) end;
   137 
   139 
   138   fun add_gen_by ((tname,finite),(typs',cnstrss')) thy' = let
   140   fun add_gen_by ((tname,finite),(typs',cnstrss')) thy' = let
   139    val (typs,cnstrs) = check_gen_by thy' (typs',cnstrss');
   141    val (typs,cnstrs) = check_gen_by (sign_of thy') (typs',cnstrss');
   140   in
   142   in
   141    Domain_Axioms.add_induct ((tname,finite),(typs,cnstrs)) thy' end;
   143    Domain_Axioms.add_induct ((tname,finite),(typs,cnstrs)) thy' end;
   142 
   144 
   143 end (* local *)
   145 end (* local *)
   144 end (* struct *)
   146 end (* struct *)