src/Pure/sign.ML
changeset 0 a5a9c433f639
child 19 929ad32d63fc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/sign.ML	Thu Sep 16 12:20:38 1993 +0200
     1.3 @@ -0,0 +1,332 @@
     1.4 +(*  Title: 	sign
     1.5 +    ID:         $Id$
     1.6 +    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
     1.7 +    Copyright   1992  University of Cambridge
     1.8 +
     1.9 +  the abstract types "sg" (signatures)
    1.10 +  and "cterm" (certified terms under a signature)
    1.11 +*)
    1.12 +
    1.13 +signature SIGN = 
    1.14 +sig
    1.15 +  structure Type: TYPE
    1.16 +  structure Symtab: SYMTAB
    1.17 +  structure Syntax: SYNTAX
    1.18 +  sharing Symtab=Type.Symtab
    1.19 +  type sg
    1.20 +  type cterm
    1.21 +  type ctyp
    1.22 +  val cfun: (term -> term) -> (cterm -> cterm)
    1.23 +  val cterm_of: sg -> term -> cterm
    1.24 +  val ctyp_of: sg -> typ -> ctyp
    1.25 +  val extend: sg -> string ->
    1.26 +	(class * class list) list * class list *
    1.27 +	(string list * int) list *
    1.28 +	(string list * (sort list * class)) list *
    1.29 +	(string list * string)list * Syntax.sext option -> sg
    1.30 +  val merge: sg * sg -> sg
    1.31 +  val pure: sg
    1.32 +  val read_cterm: sg -> string * typ -> cterm
    1.33 +  val read_ctyp: sg -> string -> ctyp
    1.34 +  val read_insts: sg -> (indexname -> typ option) * (indexname -> sort option)
    1.35 +	          -> (indexname -> typ option) * (indexname -> sort option)
    1.36 +		  -> (string*string)list
    1.37 +		  -> (indexname*ctyp)list * (cterm*cterm)list
    1.38 +  val read_typ: sg * (indexname -> sort option) -> string -> typ
    1.39 +  val rep_cterm: cterm -> {T: typ, t: term, sign: sg, maxidx: int}
    1.40 +  val rep_ctyp: ctyp -> {T: typ, sign: sg}
    1.41 +  val rep_sg: sg -> {tsig: Type.type_sig,
    1.42 +		     const_tab: typ Symtab.table,
    1.43 +		     syn: Syntax.syntax,
    1.44 +		     stamps: string ref list}
    1.45 +  val string_of_cterm: cterm -> string
    1.46 +  val string_of_ctyp: ctyp -> string
    1.47 +  val pprint_cterm: cterm -> pprint_args -> unit
    1.48 +  val pprint_ctyp: ctyp -> pprint_args -> unit
    1.49 +  val string_of_term: sg -> term -> string
    1.50 +  val string_of_typ: sg -> typ -> string
    1.51 +  val pprint_term: sg -> term -> pprint_args -> unit
    1.52 +  val pprint_typ: sg -> typ -> pprint_args -> unit
    1.53 +  val term_of: cterm -> term
    1.54 +  val typ_of: ctyp -> typ
    1.55 +  val pretty_term: sg -> term -> Syntax.Pretty.T
    1.56 +end;
    1.57 +
    1.58 +
    1.59 +functor SignFun (structure Type:TYPE and Syntax:SYNTAX) : SIGN = 
    1.60 +struct
    1.61 +structure Type = Type;
    1.62 +structure Symtab = Type.Symtab;
    1.63 +structure Syntax = Syntax;
    1.64 +structure Pretty = Syntax.Pretty
    1.65 +
    1.66 +(*Signatures of theories. *)
    1.67 +datatype sg = 
    1.68 +  Sg of {tsig: Type.type_sig,		(* order-sorted signature of types *)
    1.69 +	 const_tab: typ Symtab.table,	(*types of constants*)
    1.70 +	 syn: Syntax.syntax,		(*Parsing and printing operations*)
    1.71 +	 stamps: string ref list	(*unique theory indentifier*)  };
    1.72 +
    1.73 +
    1.74 +fun rep_sg (Sg args) = args;
    1.75 +
    1.76 +fun string_of_typ(Sg{tsig,syn,...}) = Syntax.string_of_typ syn;
    1.77 +
    1.78 +fun pprint_typ(Sg{syn,...}) = Pretty.pprint o Pretty.quote o (Syntax.pretty_typ syn);
    1.79 +
    1.80 +(*Is constant present in table with more generic type?*)
    1.81 +fun valid_const tsig ctab (a,T) = case Symtab.lookup(ctab, a) of
    1.82 +	Some U => Type.typ_instance(tsig,T,U) | _ => false;
    1.83 +
    1.84 +
    1.85 +(*Check a term for errors.  Are all constants and types valid in signature?
    1.86 +  Does not check that term is well-typed!*)
    1.87 +fun term_errors (sign as Sg{tsig,const_tab,...}) = 
    1.88 +let val showtyp = string_of_typ sign;
    1.89 +    fun terrs (Const (a,T), errs) =
    1.90 +	if valid_const tsig const_tab (a,T)
    1.91 +	then Type.type_errors (tsig,showtyp) (T,errs)
    1.92 +	else ("Illegal type for constant: " ^ a ^ ": " ^ showtyp T) :: errs
    1.93 +      | terrs (Free (_,T), errs) = Type.type_errors (tsig,showtyp) (T,errs)
    1.94 +      | terrs (Var  ((a,i),T), errs) =
    1.95 +	if  i>=0  then  Type.type_errors (tsig,showtyp) (T,errs)
    1.96 +	else  ("Negative index for Var: " ^ a) :: errs
    1.97 +      | terrs (Bound _, errs) = errs (*loose bvars detected by type_of*)
    1.98 +      | terrs (Abs (_,T,t), errs) = 
    1.99 +	    Type.type_errors(tsig,showtyp)(T,terrs(t,errs))
   1.100 +      | terrs (f$t, errs) = terrs(f, terrs (t,errs))
   1.101 +in  terrs  end;
   1.102 +
   1.103 +
   1.104 +(** The Extend operation **)
   1.105 +
   1.106 +
   1.107 +(*Reset TVar indices to zero, renaming to preserve distinctness*)
   1.108 +fun zero_tvar_indices tsig T = 
   1.109 +  let val inxSs = typ_tvars T;
   1.110 +      val nms' = variantlist(map (#1 o #1) inxSs,[]);
   1.111 +      val tye = map (fn ((v,S),a) => (v, TVar((a,0),S))) (inxSs ~~ nms')
   1.112 +  in typ_subst_TVars tye T end
   1.113 +
   1.114 +(*Check that all types mentioned in the list of declarations are valid.
   1.115 +  If errors found then raise exception.
   1.116 +  Zero type var indices because type inference requires it.
   1.117 +*)
   1.118 +fun read_consts(tsig,syn) =
   1.119 +let val showtyp = Syntax.string_of_typ syn;
   1.120 +    fun read [] = []
   1.121 +      | read((cs,s)::pairs) =
   1.122 +	let val t = Syntax.read syn Syntax.typeT s handle ERROR =>
   1.123 +	            error("The error above occurred in type " ^ s);
   1.124 +	    val S = Type.defaultS tsig;
   1.125 +	    val T = Type.varifyT(Syntax.typ_of_term (K S) t)
   1.126 +	    val T0 = zero_tvar_indices tsig T;
   1.127 +	in (case Type.type_errors (tsig,showtyp) (T0,[]) of
   1.128 +		[] => (cs,T0) :: read pairs
   1.129 +	    | errs => error (cat_lines
   1.130 + 	   (("Error in type of constants " ^ space_implode " " cs) :: errs)))
   1.131 +	end
   1.132 +in read end;
   1.133 +
   1.134 +
   1.135 +(*Extend a signature: may add classes, types and constants.
   1.136 +  Replaces syntax with "syn".
   1.137 +  The "ref" in stamps ensures that no two signatures are identical --
   1.138 +  it is impossible to forge a signature.  *)
   1.139 +fun extend (Sg{tsig, const_tab, syn, stamps, ...}) signame
   1.140 +	   (newclasses, newdefault, otypes, newtypes, const_decs, osext) : sg =
   1.141 +let val tsig' = Type.extend(tsig,newclasses,newdefault,otypes,newtypes);
   1.142 +    val S = Type.defaultS tsig';
   1.143 +    val roots = filter (Type.logical_type tsig')
   1.144 +		       (distinct(flat(map #1 newtypes)))
   1.145 +    val xconsts = map #1 newclasses @ flat (map #1 otypes) @ flat (map #1 const_decs);
   1.146 +    val syn' =
   1.147 +      case osext of
   1.148 +        Some sext => Syntax.extend (syn, K S) (roots, xconsts, sext)
   1.149 +      | None => if null roots andalso null xconsts then syn
   1.150 +                else Syntax.extend (syn, K S) (roots, xconsts, Syntax.empty_sext);
   1.151 +    val sconsts = case osext of
   1.152 +		    Some(sext) => Syntax.constants sext
   1.153 +		  | None => [];
   1.154 +    val const_decs' = read_consts(tsig',syn') (sconsts @ const_decs)
   1.155 +in Sg{tsig= tsig',
   1.156 +      const_tab= Symtab.st_of_declist (const_decs', const_tab)
   1.157 +		 handle Symtab.DUPLICATE(a) =>
   1.158 +		 error("Constant '" ^ a ^ "' declared twice"), 
   1.159 +      syn=syn', stamps= ref signame :: stamps}
   1.160 +end;
   1.161 +
   1.162 +
   1.163 +(* The empty signature *)
   1.164 +val sg0 = Sg{tsig= Type.tsig0, const_tab= Symtab.null,
   1.165 +	     syn=Syntax.type_syn,  stamps= []};
   1.166 +
   1.167 +(*The pure signature*)
   1.168 +val pure : sg = extend sg0 "Pure"
   1.169 +([("logic", [])],
   1.170 + ["logic"],
   1.171 + [(["fun"],2),
   1.172 +  (["prop"],0),
   1.173 +  (Syntax.syntax_types,0)],
   1.174 + [(["fun"],  ([["logic"], ["logic"]], "logic")),
   1.175 +  (["prop"], ([], "logic"))],
   1.176 + [(["*NORMALIZED*"], "'a::{} => 'a"),
   1.177 +  ([Syntax.constrainC], "'a::logic => 'a")],
   1.178 + Some(Syntax.pure_sext)
   1.179 +);
   1.180 +
   1.181 +
   1.182 +(** The Merge operation **)
   1.183 +
   1.184 +(*Update table with (a,x) providing any existing asgt to "a" equals x. *)
   1.185 +fun update_eq ((a,x),tab) =
   1.186 +    case Symtab.lookup(tab,a) of
   1.187 +	None => Symtab.update((a,x), tab)
   1.188 +      | Some y => if x=y then tab 
   1.189 +	    else  raise TERM ("Incompatible types for constant: "^a, []);
   1.190 +
   1.191 +(*Combine tables, updating tab2 by tab1 and checking.*)
   1.192 +fun merge_tabs (tab1,tab2) = 
   1.193 +    Symtab.balance (foldr update_eq (Symtab.alist_of tab1, tab2));
   1.194 +
   1.195 +(*Combine tables, overwriting tab2 with tab1.*)
   1.196 +fun smash_tabs (tab1,tab2) = 
   1.197 +    Symtab.balance (foldr Symtab.update (Symtab.alist_of tab1, tab2));
   1.198 +
   1.199 +(*Combine stamps, checking that theory names are disjoint. *)
   1.200 +fun merge_stamps (stamps1,stamps2) = 
   1.201 +  let val stamps = stamps1 union stamps2 in
   1.202 +  case findrep (map ! stamps) of
   1.203 +     a::_ => error ("Attempt to merge different versions of theory: " ^ a)
   1.204 +   | [] => stamps
   1.205 +  end;
   1.206 +
   1.207 +(*Merge two signatures.  Forms unions of tables.  Prefers sign1. *)
   1.208 +fun merge (sign1 as Sg{tsig=tsig1,const_tab=ctab1,stamps=stamps1,syn=syn1},
   1.209 +	   sign2 as Sg{tsig=tsig2,const_tab=ctab2,stamps=stamps2,syn=syn2}) =
   1.210 +    if stamps2 subset stamps1 then sign1
   1.211 +    else if stamps1 subset stamps2 then sign2
   1.212 +    else  (*neither is union already;  must form union*)
   1.213 +	   Sg{tsig= Type.merge(tsig1,tsig2),
   1.214 +	      const_tab= merge_tabs (ctab1, ctab2),
   1.215 +	      stamps= merge_stamps (stamps1,stamps2),
   1.216 +	      syn = Syntax.merge(syn1,syn2)};
   1.217 +
   1.218 +
   1.219 +(**** CERTIFIED TYPES ****)
   1.220 +
   1.221 +
   1.222 +(*Certified typs under a signature*)
   1.223 +datatype ctyp = Ctyp of {sign: sg,  T: typ};
   1.224 +
   1.225 +fun rep_ctyp(Ctyp ctyp) = ctyp;
   1.226 +
   1.227 +fun typ_of (Ctyp{sign,T}) = T;
   1.228 +
   1.229 +fun ctyp_of (sign as Sg{tsig,...}) T =
   1.230 +	case Type.type_errors (tsig,string_of_typ sign) (T,[]) of
   1.231 +	  [] => Ctyp{sign= sign,T= T}
   1.232 +	| errs =>  error (cat_lines ("Error in type:" :: errs));
   1.233 +
   1.234 +(*The only use is a horrible hack in the simplifier!*)
   1.235 +fun read_typ(Sg{tsig,syn,...}, defS) s =
   1.236 +    let val term = Syntax.read syn Syntax.typeT s;
   1.237 +	val S0 = Type.defaultS tsig;
   1.238 +	fun defS0 s = case defS s of Some S => S | None => S0;
   1.239 +    in Syntax.typ_of_term defS0 term end;
   1.240 +
   1.241 +fun read_ctyp sign = ctyp_of sign o read_typ(sign, K None);
   1.242 +
   1.243 +fun string_of_ctyp (Ctyp{sign,T}) = string_of_typ sign T;
   1.244 +
   1.245 +fun pprint_ctyp (Ctyp{sign,T}) = pprint_typ sign T;
   1.246 +
   1.247 +
   1.248 +(**** CERTIFIED TERMS ****)
   1.249 +
   1.250 +(*Certified terms under a signature, with checked typ and maxidx of Vars*)
   1.251 +datatype cterm = Cterm of {sign: sg,  t: term,  T: typ,  maxidx: int};
   1.252 +
   1.253 +fun rep_cterm (Cterm args) = args;
   1.254 +
   1.255 +(*Return the underlying term*)
   1.256 +fun term_of (Cterm{sign,t,T,maxidx}) = t;
   1.257 +
   1.258 +(** pretty printing of terms **)
   1.259 +
   1.260 +fun pretty_term (Sg{tsig,syn,...}) = Syntax.pretty_term syn;
   1.261 +
   1.262 +fun string_of_term sign t = Pretty.string_of (pretty_term sign t);
   1.263 +
   1.264 +fun pprint_term sign = Pretty.pprint o Pretty.quote o (pretty_term sign);
   1.265 +
   1.266 +fun string_of_cterm (Cterm{sign,t,...}) = string_of_term sign t;
   1.267 +
   1.268 +fun pprint_cterm (Cterm{sign,t,...}) = pprint_term sign t;
   1.269 +
   1.270 +(*Create a cterm by checking a "raw" term with respect to a signature*)
   1.271 +fun cterm_of sign t =
   1.272 +  case  term_errors sign (t,[])  of
   1.273 +      [] => Cterm{sign=sign, t=t, T= type_of t, maxidx= maxidx_of_term t}
   1.274 +    | errs => raise TERM(cat_lines("Term not in signature"::errs), [t]);
   1.275 +
   1.276 +fun cfun f = fn Cterm{sign,t,...} => cterm_of sign (f t);
   1.277 +
   1.278 +(*Lexing, parsing, polymorphic typechecking of a term.*)
   1.279 +fun read_def_cterm (sign as Sg{tsig, const_tab, syn,...}, types, sorts)
   1.280 +		   (a,T) =
   1.281 +  let val showtyp = string_of_typ sign
   1.282 +      and showterm = string_of_term sign
   1.283 +      fun termerr [] = ""
   1.284 +	| termerr [t] = "\nInvolving this term:\n" ^ showterm t ^ "\n"
   1.285 +	| termerr ts = "\nInvolving these terms:\n" ^
   1.286 +		       cat_lines (map showterm ts)
   1.287 +      val t = Syntax.read syn T a;
   1.288 +      val (t',tye) = Type.infer_types (tsig, const_tab, types,
   1.289 +				       sorts, showtyp, T, t)
   1.290 +		  handle TYPE (msg, Ts, ts) =>
   1.291 +	  error ("Type checking error: " ^ msg ^ "\n" ^
   1.292 +		  cat_lines (map showtyp Ts) ^ termerr ts)
   1.293 +  in (cterm_of sign t', tye)
   1.294 +  end
   1.295 +  handle TERM (msg, _) => error ("Error: " ^  msg);
   1.296 +
   1.297 +
   1.298 +fun read_cterm sign = #1 o (read_def_cterm (sign, K None, K None));
   1.299 +
   1.300 +(** reading of instantiations **)
   1.301 +
   1.302 +fun indexname cs = case Syntax.scan_varname cs of (v,[]) => v
   1.303 +	| _ => error("Lexical error in variable name: " ^ implode cs);
   1.304 +
   1.305 +fun absent ixn =
   1.306 +  error("No such variable in term: " ^ Syntax.string_of_vname ixn);
   1.307 +
   1.308 +fun inst_failure ixn =
   1.309 +  error("Instantiation of " ^ Syntax.string_of_vname ixn ^ " fails");
   1.310 +
   1.311 +fun read_insts (sign as Sg{tsig,...}) (rtypes,rsorts) (types,sorts) insts =
   1.312 +let fun split([],tvs,vs) = (tvs,vs)
   1.313 +      | split((sv,st)::l,tvs,vs) = (case explode sv of
   1.314 +		  "'"::cs => split(l,(indexname cs,st)::tvs,vs)
   1.315 +		| cs => split(l,tvs,(indexname cs,st)::vs));
   1.316 +    val (tvs,vs) = split(insts,[],[]);
   1.317 +    fun readT((a,i),st) =
   1.318 +	let val ixn = ("'" ^ a,i);
   1.319 +	    val S = case rsorts ixn of Some S => S | None => absent ixn;
   1.320 +	    val T = read_typ (sign,sorts) st;
   1.321 +	in if Type.typ_instance(tsig,T,TVar(ixn,S)) then (ixn,T)
   1.322 +	   else inst_failure ixn
   1.323 +	end
   1.324 +    val tye = map readT tvs;
   1.325 +    fun add_cterm ((cts,tye), (ixn,st)) =
   1.326 +	let val T = case rtypes ixn of
   1.327 +		      Some T => typ_subst_TVars tye T
   1.328 +		    | None => absent ixn;
   1.329 +	    val (ct,tye2) = read_def_cterm (sign,types,sorts) (st,T);
   1.330 +	    val cv = cterm_of sign (Var(ixn,typ_subst_TVars tye2 T))
   1.331 + 	in ((cv,ct)::cts,tye2 @ tye) end
   1.332 +    val (cterms,tye') = foldl add_cterm (([],tye), vs);
   1.333 +in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) tye', cterms) end;
   1.334 +
   1.335 +end;