src/Pure/sign.ML
changeset 3791 c5db2c87a646
parent 3552 f348e8a2db4b
child 3805 a50d0b5219dd
     1.1 --- a/src/Pure/sign.ML	Mon Oct 06 19:07:14 1997 +0200
     1.2 +++ b/src/Pure/sign.ML	Mon Oct 06 19:11:56 1997 +0200
     1.3 @@ -6,12 +6,14 @@
     1.4  *)
     1.5  
     1.6  signature SIGN =
     1.7 -  sig
     1.8 +sig
     1.9    type sg
    1.10    val rep_sg: sg ->
    1.11     {tsig: Type.type_sig,
    1.12      const_tab: typ Symtab.table,
    1.13      syn: Syntax.syntax,
    1.14 +    path: string list,
    1.15 +    spaces: (string * NameSpace.T) list,
    1.16      stamps: string ref list}
    1.17    val subsig: sg * sg -> bool
    1.18    val eq_sg: sg * sg -> bool
    1.19 @@ -23,14 +25,27 @@
    1.20    val nodup_Vars: term -> unit
    1.21    val norm_sort: sg -> sort -> sort
    1.22    val nonempty_sort: sg -> sort list -> sort -> bool
    1.23 +  val long_names: bool ref
    1.24 +  val intern_class: sg -> class -> class
    1.25 +  val extern_class: sg -> class -> class
    1.26 +  val intern_sort: sg -> sort -> sort
    1.27 +  val extern_sort: sg -> sort -> sort
    1.28 +  val intern_typ: sg -> typ -> typ
    1.29 +  val extern_typ: sg -> typ -> typ
    1.30 +  val intern_term: sg -> term -> term
    1.31 +  val extern_term: sg -> term -> term
    1.32 +  val intern_tycons: sg -> typ -> typ
    1.33 +  val intern_tycon: sg -> string -> string
    1.34 +  val intern_const: sg -> string -> string
    1.35    val print_sg: sg -> unit
    1.36    val pretty_sg: sg -> Pretty.T
    1.37    val pprint_sg: sg -> pprint_args -> unit
    1.38    val pretty_term: sg -> term -> Pretty.T
    1.39    val pretty_typ: sg -> typ -> Pretty.T
    1.40 -  val pretty_sort: sort -> Pretty.T
    1.41 +  val pretty_sort: sg -> sort -> Pretty.T
    1.42    val string_of_term: sg -> term -> string
    1.43    val string_of_typ: sg -> typ -> string
    1.44 +  val string_of_sort: sg -> sort -> string
    1.45    val pprint_term: sg -> term -> pprint_args -> unit
    1.46    val pprint_typ: sg -> typ -> pprint_args -> unit
    1.47    val certify_typ: sg -> typ -> typ
    1.48 @@ -40,12 +55,16 @@
    1.49      (indexname -> sort option) -> string list -> bool
    1.50      -> term list * typ -> int * term * (indexname * typ) list
    1.51    val add_classes: (class * class list) list -> sg -> sg
    1.52 +  val add_classes_i: (class * class list) list -> sg -> sg
    1.53    val add_classrel: (class * class) list -> sg -> sg
    1.54 +  val add_classrel_i: (class * class) list -> sg -> sg
    1.55    val add_defsort: sort -> sg -> sg
    1.56 +  val add_defsort_i: sort -> sg -> sg
    1.57    val add_types: (string * int * mixfix) list -> sg -> sg
    1.58    val add_tyabbrs: (string * string list * string * mixfix) list -> sg -> sg
    1.59    val add_tyabbrs_i: (string * string list * typ * mixfix) list -> sg -> sg
    1.60    val add_arities: (string * sort list * sort) list -> sg -> sg
    1.61 +  val add_arities_i: (string * sort list * sort) list -> sg -> sg
    1.62    val add_consts: (string * string * mixfix) list -> sg -> sg
    1.63    val add_consts_i: (string * typ * mixfix) list -> sg -> sg
    1.64    val add_syntax: (string * string * mixfix) list -> sg -> sg
    1.65 @@ -63,6 +82,8 @@
    1.66      (string * string * (string -> string * int)) list -> sg -> sg
    1.67    val add_trrules: (string * string) Syntax.trrule list -> sg -> sg
    1.68    val add_trrules_i: ast Syntax.trrule list -> sg -> sg
    1.69 +  val add_path: string -> sg -> sg
    1.70 +  val add_space: string * string list -> sg -> sg
    1.71    val add_name: string -> sg -> sg
    1.72    val make_draft: sg -> sg
    1.73    val merge: sg * sg -> sg
    1.74 @@ -71,23 +92,24 @@
    1.75    val cpure: sg
    1.76    val const_of_class: class -> string
    1.77    val class_of_const: string -> class
    1.78 -  end;
    1.79 +end;
    1.80  
    1.81  structure Sign : SIGN =
    1.82  struct
    1.83  
    1.84 -
    1.85  (** datatype sg **)
    1.86  
    1.87 -(*the "ref" in stamps ensures that no two signatures are identical -- it is
    1.88 -  impossible to forge a signature*)
    1.89 -
    1.90  datatype sg =
    1.91    Sg of {
    1.92 -    tsig: Type.type_sig,            (*order-sorted signature of types*)
    1.93 -    const_tab: typ Symtab.table,    (*types of constants*)
    1.94 -    syn: Syntax.syntax,             (*syntax for parsing and printing*)
    1.95 -    stamps: string ref list};       (*unique theory indentifier*)
    1.96 +    tsig: Type.type_sig,                        (*order-sorted signature of types*)
    1.97 +    const_tab: typ Symtab.table,                (*types of constants*)
    1.98 +    syn: Syntax.syntax,                         (*syntax for parsing and printing*)
    1.99 +    path: string list,                     	(*current position in name space*)
   1.100 +    spaces: (string * NameSpace.T) list,   	(*name spaces for consts, types etc.*)
   1.101 +    stamps: string ref list};                   (*unique theory indentifier*)
   1.102 +
   1.103 +    (*the "ref" in stamps ensures that no two signatures are identical
   1.104 +      -- it is impossible to forge a signature*)
   1.105  
   1.106  fun rep_sg (Sg args) = args;
   1.107  val tsig_of = #tsig o rep_sg;
   1.108 @@ -146,9 +168,128 @@
   1.109  val norm_sort = Type.norm_sort o tsig_of;
   1.110  val nonempty_sort = Type.nonempty_sort o tsig_of;
   1.111  
   1.112 -(* FIXME move to Sorts? *)
   1.113 -fun pretty_sort [c] = Pretty.str c
   1.114 -  | pretty_sort cs = Pretty.str_list "{" "}" cs;
   1.115 +
   1.116 +
   1.117 +(** name spaces **)
   1.118 +
   1.119 +(*prune names by default*)
   1.120 +val long_names = ref false;
   1.121 +
   1.122 +
   1.123 +(* kinds *)
   1.124 +
   1.125 +val classK = "class";
   1.126 +val typeK = "type";
   1.127 +val constK = "const";
   1.128 +
   1.129 +
   1.130 +(* add and retrieve names *)
   1.131 +
   1.132 +fun space_of spaces kind =
   1.133 +  if_none (assoc (spaces, kind)) NameSpace.empty;
   1.134 +
   1.135 +(*input and output of qualified names*)
   1.136 +fun intrn spaces kind = NameSpace.lookup (space_of spaces kind);
   1.137 +fun extrn spaces kind = NameSpace.prune (space_of spaces kind);
   1.138 +
   1.139 +(*add names*)
   1.140 +fun add_names spaces kind names =
   1.141 +  let val space' = NameSpace.extend (names, space_of spaces kind) in
   1.142 +    overwrite (spaces, (kind, space'))
   1.143 +  end;
   1.144 +
   1.145 +fun full_name path name = NameSpace.pack (path @ (NameSpace.unpack name));
   1.146 +
   1.147 +
   1.148 +(* intern / extern names *)
   1.149 +
   1.150 +(*Note: These functions are not necessarily idempotent!*)	(* FIXME *)
   1.151 +
   1.152 +local
   1.153 +
   1.154 +  (* FIXME move to term.ML? *)
   1.155 +
   1.156 +  fun add_typ_classes (Type (_, Ts), cs) = foldr add_typ_classes (Ts, cs)
   1.157 +    | add_typ_classes (TFree (_, S), cs) = S union cs
   1.158 +    | add_typ_classes (TVar (_, S), cs) = S union cs;
   1.159 +
   1.160 +  fun add_typ_tycons (Type (c, Ts), cs) = foldr add_typ_tycons (Ts, c ins cs)
   1.161 +    | add_typ_tycons (_, cs) = cs;
   1.162 +
   1.163 +  val add_term_classes = it_term_types add_typ_classes;
   1.164 +  val add_term_tycons = it_term_types add_typ_tycons;
   1.165 +
   1.166 +  fun add_term_consts (Const (c, _), cs) = c ins cs
   1.167 +    | add_term_consts (t $ u, cs) = add_term_consts (t, add_term_consts (u, cs))
   1.168 +    | add_term_consts (Abs (_, _, t), cs) = add_term_consts (t, cs)
   1.169 +    | add_term_consts (_, cs) = cs;
   1.170 +
   1.171 +
   1.172 +  (*map classes, tycons*)
   1.173 +  fun map_typ f g (Type (c, Ts)) = Type (g c, map (map_typ f g) Ts)
   1.174 +    | map_typ f _ (TFree (x, S)) = TFree (x, map f S)
   1.175 +    | map_typ f _ (TVar (xi, S)) = TVar (xi, map f S);
   1.176 +
   1.177 +  (*map classes, tycons, consts*)
   1.178 +  fun map_term f g h (Const (c, T)) = Const (h c, map_typ f g T)
   1.179 +    | map_term f g _ (Free (x, T)) = Free (x, map_typ f g T)
   1.180 +    | map_term f g _ (Var (xi, T)) = Var (xi, map_typ f g T)
   1.181 +    | map_term _ _ _ (t as Bound _) = t
   1.182 +    | map_term f g h (Abs (x, T, t)) = Abs (x, map_typ f g T, map_term f g h t)
   1.183 +    | map_term f g h (t $ u) = map_term f g h t $ map_term f g h u;
   1.184 +
   1.185 +  (*prepare mapping of names*)
   1.186 +  fun mapping f add_xs t =
   1.187 +    let
   1.188 +      fun f' x = let val y = f x in if x = y then None else Some (x, y) end;
   1.189 +      val table = mapfilter f' (add_xs (t, []));
   1.190 +      fun lookup x = if_none (assoc (table, x)) x;
   1.191 +    in lookup end;
   1.192 +
   1.193 +  (*intern / extern typ*)
   1.194 +  fun trn_typ trn T =
   1.195 +    T |> map_typ
   1.196 +      (mapping (trn classK) add_typ_classes T)
   1.197 +      (mapping (trn typeK) add_typ_tycons T);
   1.198 +
   1.199 +  (*intern / extern term*)
   1.200 +  fun trn_term trn t =
   1.201 +    t |> map_term
   1.202 +      (mapping (trn classK) add_term_classes t)
   1.203 +      (mapping (trn typeK) add_term_tycons t)
   1.204 +      (mapping (trn constK) add_term_consts t);
   1.205 +
   1.206 +
   1.207 +  fun spaces_of (Sg {spaces, ...}) = spaces;
   1.208 +
   1.209 +in
   1.210 +
   1.211 +  fun intrn_class spaces = intrn spaces classK;
   1.212 +  fun extrn_class spaces = extrn spaces classK;
   1.213 +  val intrn_sort = map o intrn_class;
   1.214 +  val extrn_sort = map o extrn_class;
   1.215 +  val intrn_typ = trn_typ o intrn;
   1.216 +  val extrn_typ = trn_typ o extrn;
   1.217 +  val intrn_term = trn_term o intrn;
   1.218 +  val extrn_term = trn_term o extrn;
   1.219 +
   1.220 +  val intern_class = intrn_class o spaces_of;
   1.221 +  val extern_class = extrn_class o spaces_of;
   1.222 +  val intern_sort = intrn_sort o spaces_of;
   1.223 +  val extern_sort = extrn_sort o spaces_of;
   1.224 +  val intern_typ = intrn_typ o spaces_of;
   1.225 +  val extern_typ = extrn_typ o spaces_of;
   1.226 +  val intern_term = intrn_term o spaces_of;
   1.227 +  val extern_term = extrn_term o spaces_of;
   1.228 +
   1.229 +  fun intrn_tycons spaces T =
   1.230 +    map_typ I (mapping (intrn spaces typeK) add_typ_tycons T) T;
   1.231 +
   1.232 +  fun intern_tycon sg = intrn (spaces_of sg) typeK;
   1.233 +  fun intern_const sg = intrn (spaces_of sg) constK;
   1.234 +  val intern_tycons = intrn_tycons o spaces_of;
   1.235 +
   1.236 +end;
   1.237  
   1.238  
   1.239  
   1.240 @@ -158,48 +299,56 @@
   1.241  
   1.242  fun print_sg sg =
   1.243    let
   1.244 -    fun prt_typ syn ty = Pretty.quote (Syntax.pretty_typ syn ty);
   1.245 +    val Sg {syn, ...} = sg;
   1.246 +
   1.247 +    fun prt_typ ty = Pretty.quote (Syntax.pretty_typ syn ty);
   1.248 +    fun prt_cls c = Syntax.pretty_sort syn [c];
   1.249 +
   1.250 +    fun pretty_space (kind, space) = Pretty.block (Pretty.breaks
   1.251 +      (map Pretty.str (kind ^ ":" :: map quote (NameSpace.dest space))));
   1.252  
   1.253 -    fun pretty_classrel (cl, cls) = Pretty.block
   1.254 -      (Pretty.str (cl ^ " <") :: Pretty.brk 1 ::
   1.255 -        Pretty.commas (map Pretty.str cls));
   1.256 +    fun pretty_classes cs = Pretty.block
   1.257 +      (Pretty.breaks (Pretty.str "classes:" :: map prt_cls cs));
   1.258  
   1.259 -    fun pretty_default cls = Pretty.block
   1.260 -      [Pretty.str "default:", Pretty.brk 1, pretty_sort cls];
   1.261 +    fun pretty_classrel (c, cs) = Pretty.block
   1.262 +      (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 ::
   1.263 +        Pretty.commas (map prt_cls cs));
   1.264 +
   1.265 +    fun pretty_default S = Pretty.block
   1.266 +      [Pretty.str "default:", Pretty.brk 1, Syntax.pretty_sort syn S];
   1.267  
   1.268      fun pretty_ty (ty, n) = Pretty.str (ty ^ " " ^ string_of_int n);
   1.269  
   1.270 -    fun pretty_abbr syn (ty, (vs, rhs)) = Pretty.block
   1.271 -      [prt_typ syn (Type (ty, map (fn v => TVar ((v, 0), [])) vs)),
   1.272 -        Pretty.str " =", Pretty.brk 1, prt_typ syn rhs];
   1.273 +    fun pretty_abbr (ty, (vs, rhs)) = Pretty.block
   1.274 +      [prt_typ (Type (ty, map (fn v => TVar ((v, 0), [])) vs)),
   1.275 +        Pretty.str " =", Pretty.brk 1, prt_typ rhs];
   1.276  
   1.277 -    fun pretty_arity ty (cl, []) = Pretty.block
   1.278 -          [Pretty.str (ty ^ " ::"), Pretty.brk 1, Pretty.str cl]
   1.279 -      | pretty_arity ty (cl, srts) = Pretty.block
   1.280 +    fun pretty_arity ty (c, []) = Pretty.block
   1.281 +          [Pretty.str (ty ^ " ::"), Pretty.brk 1, prt_cls c]
   1.282 +      | pretty_arity ty (c, Ss) = Pretty.block
   1.283            [Pretty.str (ty ^ " ::"), Pretty.brk 1,
   1.284 -            Pretty.list "(" ")" (map pretty_sort srts),
   1.285 -            Pretty.brk 1, Pretty.str cl];
   1.286 +            Pretty.list "(" ")" (map (Syntax.pretty_sort syn) Ss),
   1.287 +            Pretty.brk 1, prt_cls c];
   1.288  
   1.289      fun pretty_arities (ty, ars) = map (pretty_arity ty) ars;
   1.290  
   1.291 -    fun pretty_const syn (c, ty) = Pretty.block
   1.292 -      [Pretty.str (quote c ^ " ::"), Pretty.brk 1, prt_typ syn ty];
   1.293 +    fun pretty_const (c, ty) = Pretty.block
   1.294 +      [Pretty.str (quote c ^ " ::"), Pretty.brk 1, prt_typ ty];
   1.295  
   1.296 -
   1.297 -    val Sg {tsig, const_tab, syn, stamps} = sg;
   1.298 +    val Sg {tsig, const_tab, syn, path, spaces, stamps} = sg;
   1.299      val {classes, classrel, default, tycons, abbrs, arities} =
   1.300        Type.rep_tsig tsig;
   1.301    in
   1.302      Pretty.writeln (Pretty.strs ("stamps:" :: stamp_names stamps));
   1.303 -    Pretty.writeln (Pretty.strs ("classes:" :: classes));
   1.304 -    Pretty.writeln (Pretty.big_list "classrel:" (map pretty_classrel classrel));
   1.305 +    Pretty.writeln (Pretty.strs ["name entry path:", NameSpace.pack path]);
   1.306 +    Pretty.writeln (Pretty.big_list "name spaces:" (map pretty_space spaces));
   1.307 +    Pretty.writeln (pretty_classes classes);
   1.308 +    Pretty.writeln (Pretty.big_list "class relation:" (map pretty_classrel classrel));
   1.309      Pretty.writeln (pretty_default default);
   1.310 -    Pretty.writeln (Pretty.big_list "types:" (map pretty_ty tycons));
   1.311 -    Pretty.writeln (Pretty.big_list "abbrs:" (map (pretty_abbr syn) abbrs));
   1.312 -    Pretty.writeln (Pretty.big_list "arities:"
   1.313 -      (List.concat (map pretty_arities arities)));
   1.314 -    Pretty.writeln (Pretty.big_list "consts:"
   1.315 -      (map (pretty_const syn) (Symtab.dest const_tab)))
   1.316 +    Pretty.writeln (Pretty.big_list "type constructors:" (map pretty_ty tycons));
   1.317 +    Pretty.writeln (Pretty.big_list "type abbreviations:" (map pretty_abbr abbrs));
   1.318 +    Pretty.writeln (Pretty.big_list "type arities:" (flat (map pretty_arities arities)));
   1.319 +    Pretty.writeln (Pretty.big_list "consts:" (map pretty_const (Symtab.dest const_tab)))
   1.320    end;
   1.321  
   1.322  
   1.323 @@ -212,17 +361,23 @@
   1.324  
   1.325  (** pretty printing of terms and types **)
   1.326  
   1.327 -fun pretty_term (Sg {syn, stamps, ...}) =
   1.328 -  let val curried = "CPure" mem_string (map ! stamps);
   1.329 -  in Syntax.pretty_term curried syn end;
   1.330 +fun pretty_term (Sg {syn, spaces, stamps, ...}) t =
   1.331 +  Syntax.pretty_term syn
   1.332 +    ("CPure" mem_string (map ! stamps))
   1.333 +    (if ! long_names then t else extrn_term spaces t);
   1.334  
   1.335 -fun pretty_typ (Sg {syn, ...}) = Syntax.pretty_typ syn;
   1.336 +fun pretty_typ (Sg {syn, spaces, ...}) T =
   1.337 +  Syntax.pretty_typ syn
   1.338 +    (if ! long_names then T else extrn_typ spaces T);
   1.339  
   1.340 -fun string_of_term (Sg {syn, stamps, ...}) =
   1.341 -  let val curried = "CPure" mem_string (map ! stamps);
   1.342 -  in Syntax.string_of_term curried syn end;
   1.343 +fun pretty_sort (Sg {syn, spaces, ...}) S =
   1.344 +  Syntax.pretty_sort syn
   1.345 +    (if ! long_names then S else extrn_sort spaces S);
   1.346  
   1.347 -fun string_of_typ (Sg {syn, ...}) = Syntax.string_of_typ syn;
   1.348 +
   1.349 +fun string_of_term sg t = Pretty.string_of (pretty_term sg t);
   1.350 +fun string_of_typ sg T = Pretty.string_of (pretty_typ sg T);
   1.351 +fun string_of_sort sg S = Pretty.string_of (pretty_sort sg S);
   1.352  
   1.353  fun pprint_term sg = Pretty.pprint o Pretty.quote o (pretty_term sg);
   1.354  fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg);
   1.355 @@ -234,13 +389,14 @@
   1.356  fun err_in_type s =
   1.357    error ("The error(s) above occurred in type " ^ quote s);
   1.358  
   1.359 -fun read_raw_typ syn tsig sort_of str =
   1.360 -  Syntax.read_typ syn (Type.eq_sort tsig) (Type.get_sort tsig sort_of) str
   1.361 -    handle ERROR => err_in_type str;
   1.362 -
   1.363 +fun read_raw_typ syn tsig spaces def_sort str =
   1.364 +  intrn_tycons spaces
   1.365 +    (Syntax.read_typ syn (Type.get_sort tsig def_sort (intrn_sort spaces)) str
   1.366 +      handle ERROR => err_in_type str);
   1.367 +  
   1.368  (*read and certify typ wrt a signature*)
   1.369 -fun read_typ (Sg {tsig, syn, ...}, sort_of) str =
   1.370 -  Type.cert_typ tsig (read_raw_typ syn tsig sort_of str)
   1.371 +fun read_typ (Sg {tsig, syn, spaces, ...}, def_sort) str =
   1.372 +  Type.cert_typ tsig (read_raw_typ syn tsig spaces def_sort str)
   1.373      handle TYPE (msg, _, _) => (error_msg msg; err_in_type str);
   1.374  
   1.375  
   1.376 @@ -258,8 +414,8 @@
   1.377        (case assoc_string_int (tvars, a) of
   1.378          Some S' =>
   1.379            if S = S' then tvars
   1.380 -          else raise_type ("Type variable " ^ Syntax.string_of_vname a ^
   1.381 -            " has two distinct sorts") [TVar (a, S'), T] []
   1.382 +          else raise TYPE ("Type variable " ^ Syntax.string_of_vname a ^
   1.383 +            " has two distinct sorts", [TVar (a, S'), T], [])
   1.384        | None => v :: tvars))
   1.385  (*equivalent to foldl nodup_TVars_list, but 3X faster under Poly/ML*)
   1.386  and nodup_TVars_list (tvars, []) = tvars
   1.387 @@ -277,11 +433,11 @@
   1.388            (case assoc_string_int (vars, ixn) of
   1.389              Some T' =>
   1.390                if T = T' then (vars, nodup_TVars (tvars, T))
   1.391 -              else raise_type ("Variable " ^ Syntax.string_of_vname ixn ^
   1.392 -                " has two distinct types") [T', T] []
   1.393 +              else raise TYPE ("Variable " ^ Syntax.string_of_vname ixn ^
   1.394 +                " has two distinct types", [T', T], [])
   1.395            | None => (v :: vars, tvars))
   1.396        | Bound _ => (vars, tvars)
   1.397 -      | Abs(_, T, t) => nodups vars (nodup_TVars (tvars, T)) t
   1.398 +      | Abs (_, T, t) => nodups vars (nodup_TVars (tvars, T)) t
   1.399        | s $ t =>
   1.400            let val (vars',tvars') = nodups vars tvars s in
   1.401              nodups vars' tvars' t
   1.402 @@ -312,12 +468,12 @@
   1.403      val norm_tm =
   1.404        (case it_term_types (Type.typ_errors tsig) (tm, []) of
   1.405          [] => map_term_types (Type.norm_typ tsig) tm
   1.406 -      | errs => raise_type (cat_lines errs) [] [tm]);
   1.407 +      | errs => raise TYPE (cat_lines errs, [], [tm]));
   1.408      val _ = nodup_Vars norm_tm;
   1.409    in
   1.410      (case mapfilt_atoms atom_err norm_tm of
   1.411        [] => (norm_tm, type_of norm_tm, maxidx_of_term norm_tm)
   1.412 -    | errs => raise_type (cat_lines errs) [] [norm_tm])
   1.413 +    | errs => raise TYPE (cat_lines errs, [], [norm_tm]))
   1.414    end;
   1.415  
   1.416  
   1.417 @@ -339,8 +495,8 @@
   1.418      val Sg {tsig, ...} = sg;
   1.419      val prt = setmp Syntax.show_brackets true (pretty_term sg);
   1.420      val prT = pretty_typ sg;
   1.421 -    val infer = Type.infer_types prt prT tsig (const_type sg)
   1.422 -      def_type def_sort used freeze;
   1.423 +    val infer = Type.infer_types prt prT tsig (const_type sg) def_type def_sort
   1.424 +      (intern_const sg) (intern_tycons sg) (intern_sort sg) used freeze;
   1.425  
   1.426      val T' = certify_typ sg T handle TYPE (msg, _, _) => error msg;
   1.427  
   1.428 @@ -376,7 +532,7 @@
   1.429          else (); res)
   1.430      | Errs errs => (warn (); error (cat_lines errs))
   1.431      | Ambigs us =>
   1.432 -        (warn (); error ("Error: More than one term is type correct:\n" ^
   1.433 +        (warn (); error ("More than one term is type correct:\n" ^
   1.434            (cat_lines (map (Pretty.string_of o prt) us)))))
   1.435    end;
   1.436  
   1.437 @@ -386,53 +542,64 @@
   1.438  
   1.439  (** signature extension functions **)  (*exception ERROR*)
   1.440  
   1.441 -fun decls_of name_of mfixs =
   1.442 -  map (fn (x, y, mx) => (name_of x mx, y)) mfixs;
   1.443 +fun decls_of path name_of mfixs =
   1.444 +  map (fn (x, y, mx) => (full_name path (name_of x mx), y)) mfixs;
   1.445 +
   1.446 +fun no_read _ _ _ decl = decl;
   1.447  
   1.448  
   1.449  (* add default sort *)
   1.450  
   1.451 -fun ext_defsort (syn, tsig, ctab) defsort =
   1.452 -  (syn, Type.ext_tsig_defsort tsig defsort, ctab);
   1.453 +fun ext_defsort int (syn, tsig, ctab, (path, spaces)) S =
   1.454 +  (syn, Type.ext_tsig_defsort tsig (if int then intrn_sort spaces S else S),
   1.455 +    ctab, (path, spaces));
   1.456  
   1.457  
   1.458  (* add type constructors *)
   1.459  
   1.460 -fun ext_types (syn, tsig, ctab) types =
   1.461 -  (Syntax.extend_type_gram syn types,
   1.462 -    Type.ext_tsig_types tsig (decls_of Syntax.type_name types),
   1.463 -    ctab);
   1.464 +fun ext_types (syn, tsig, ctab, (path, spaces)) types =
   1.465 +  let val decls = decls_of path Syntax.type_name types in
   1.466 +    (Syntax.extend_type_gram syn types,
   1.467 +      Type.ext_tsig_types tsig decls, ctab,
   1.468 +      (path, add_names spaces typeK (map fst decls)))
   1.469 +  end;
   1.470  
   1.471  
   1.472  (* add type abbreviations *)
   1.473  
   1.474 -fun read_abbr syn tsig (t, vs, rhs_src) =
   1.475 -  (t, vs, read_raw_typ syn tsig (K None) rhs_src)
   1.476 +fun read_abbr syn tsig spaces (t, vs, rhs_src) =
   1.477 +  (t, vs, read_raw_typ syn tsig spaces (K None) rhs_src)
   1.478      handle ERROR => error ("in type abbreviation " ^ t);
   1.479  
   1.480 -fun ext_abbrs rd_abbr (syn, tsig, ctab) abbrs =
   1.481 +fun ext_abbrs rd_abbr (syn, tsig, ctab, (path, spaces)) abbrs =
   1.482    let
   1.483      fun mfix_of (t, vs, _, mx) = (t, length vs, mx);
   1.484 -    val syn1 = Syntax.extend_type_gram syn (map mfix_of abbrs);
   1.485 +    val syn' = Syntax.extend_type_gram syn (map mfix_of abbrs);
   1.486  
   1.487 -    fun decl_of (t, vs, rhs, mx) =
   1.488 -      rd_abbr syn1 tsig (Syntax.type_name t mx, vs, rhs);
   1.489 +    val abbrs' =
   1.490 +      map (fn (t, vs, rhs, mx) =>
   1.491 +        (full_name path (Syntax.type_name t mx), vs, rhs)) abbrs;
   1.492 +    val space' = add_names spaces typeK (map #1 abbrs');
   1.493 +    val decls = map (rd_abbr syn' tsig space') abbrs';
   1.494    in
   1.495 -    (syn1, Type.ext_tsig_abbrs tsig (map decl_of abbrs), ctab)
   1.496 +    (syn', Type.ext_tsig_abbrs tsig decls, ctab, (path, spaces))
   1.497    end;
   1.498  
   1.499 -fun ext_tyabbrs_i arg = ext_abbrs (K (K I)) arg;
   1.500 -fun ext_tyabbrs   arg = ext_abbrs read_abbr arg;
   1.501 +fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs;
   1.502 +fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs;
   1.503  
   1.504  
   1.505  (* add type arities *)
   1.506  
   1.507 -fun ext_arities (syn, tsig, ctab) arities =
   1.508 +fun ext_arities int (syn, tsig, ctab, (path, spaces)) arities =
   1.509    let
   1.510 -    val tsig1 = Type.ext_tsig_arities tsig arities;
   1.511 -    val log_types = Type.logical_types tsig1;
   1.512 +    fun intrn_arity (c, Ss, S) =
   1.513 +      (intrn spaces typeK c, map (intrn_sort spaces) Ss, intrn_sort spaces S);
   1.514 +    val intrn = if int then map intrn_arity else I;
   1.515 +    val tsig' = Type.ext_tsig_arities tsig (intrn arities);
   1.516 +    val log_types = Type.logical_types tsig';
   1.517    in
   1.518 -    (Syntax.extend_log_types syn log_types, tsig1, ctab)
   1.519 +    (Syntax.extend_log_types syn log_types, tsig', ctab, (path, spaces))
   1.520    end;
   1.521  
   1.522  
   1.523 @@ -445,32 +612,33 @@
   1.524    error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
   1.525  
   1.526  
   1.527 -fun read_const syn tsig (c, ty_src, mx) =
   1.528 -  (c, read_raw_typ syn tsig (K None) ty_src, mx)
   1.529 +fun read_const syn tsig spaces (c, ty_src, mx) =
   1.530 +  (c, read_raw_typ syn tsig spaces (K None) ty_src, mx)
   1.531      handle ERROR => err_in_const (Syntax.const_name c mx);
   1.532  
   1.533 -fun ext_cnsts rd_const syn_only prmode (syn, tsig, ctab) raw_consts =
   1.534 +fun ext_cnsts rd_const syn_only prmode (syn, tsig, ctab, (path, spaces)) raw_consts =
   1.535    let
   1.536      fun prep_const (c, ty, mx) =
   1.537 -     (c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx)
   1.538 -       handle TYPE (msg, _, _)
   1.539 -         => (error_msg msg; err_in_const (Syntax.const_name c mx));
   1.540 +      (c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx)
   1.541 +        handle TYPE (msg, _, _) =>
   1.542 +          (error_msg msg; err_in_const (Syntax.const_name c mx));
   1.543  
   1.544 -    val consts = map (prep_const o rd_const syn tsig) raw_consts;
   1.545 +    val consts = map (prep_const o rd_const syn tsig spaces) raw_consts;
   1.546      val decls =
   1.547        if syn_only then []
   1.548 -      else filter_out (equal "" o fst) (decls_of Syntax.const_name consts);
   1.549 +      else decls_of path Syntax.const_name consts;
   1.550    in
   1.551      (Syntax.extend_const_gram syn prmode consts, tsig,
   1.552        Symtab.extend_new (ctab, decls)
   1.553 -        handle Symtab.DUPS cs => err_dup_consts cs)
   1.554 +        handle Symtab.DUPS cs => err_dup_consts cs,
   1.555 +      (path, add_names spaces constK (map fst decls)))
   1.556    end;
   1.557  
   1.558 -val ext_consts_i = ext_cnsts (K (K I)) false ("", true);
   1.559 +val ext_consts_i = ext_cnsts no_read false ("", true);
   1.560  val ext_consts = ext_cnsts read_const false ("", true);
   1.561 -val ext_syntax_i = ext_cnsts (K (K I)) true ("", true);
   1.562 +val ext_syntax_i = ext_cnsts no_read true ("", true);
   1.563  val ext_syntax = ext_cnsts read_const true ("", true);
   1.564 -fun ext_modesyntax_i sg (prmode, consts) = ext_cnsts (K (K I)) true prmode sg consts;
   1.565 +fun ext_modesyntax_i sg (prmode, consts) = ext_cnsts no_read true prmode sg consts;
   1.566  fun ext_modesyntax sg (prmode, consts) = ext_cnsts read_const true prmode sg consts;
   1.567  
   1.568  
   1.569 @@ -480,35 +648,63 @@
   1.570  
   1.571  fun class_of_const c_class =
   1.572    let
   1.573 -    val c = implode (take (size c_class - 6, explode c_class));
   1.574 +    val c = implode (take (size c_class - size "_class", explode c_class));
   1.575    in
   1.576      if const_of_class c = c_class then c
   1.577 -    else raise_term ("class_of_const: bad name " ^ quote c_class) []
   1.578 +    else raise TERM ("class_of_const: bad name " ^ quote c_class, [])
   1.579    end;
   1.580  
   1.581  
   1.582 -fun ext_classes (syn, tsig, ctab) classes =
   1.583 +fun ext_classes int (syn, tsig, ctab, (path, spaces)) classes =
   1.584    let
   1.585      val names = map fst classes;
   1.586      val consts =
   1.587        map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names;
   1.588 +
   1.589 +    val full_names = map (full_name path) names;
   1.590 +    val spaces' = add_names spaces classK full_names;
   1.591 +    val intrn = if int then map (intrn_class spaces') else I;
   1.592 +    val classes' =
   1.593 +      ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes);
   1.594    in
   1.595      ext_consts_i
   1.596 -      (Syntax.extend_consts syn names, Type.ext_tsig_classes tsig classes, ctab)
   1.597 -      consts
   1.598 +      (Syntax.extend_consts syn names,
   1.599 +        Type.ext_tsig_classes tsig classes', ctab, (path, spaces'))
   1.600 +    consts
   1.601    end;
   1.602  
   1.603  
   1.604  (* add to classrel *)
   1.605  
   1.606 -fun ext_classrel (syn, tsig, ctab) pairs =
   1.607 -  (syn, Type.ext_tsig_classrel tsig pairs, ctab);
   1.608 +fun ext_classrel int (syn, tsig, ctab, (path, spaces)) pairs =
   1.609 +  let val intrn = if int then map (pairself (intrn_class spaces)) else I in
   1.610 +    (syn, Type.ext_tsig_classrel tsig (intrn pairs), ctab, (path, spaces))
   1.611 +  end;
   1.612  
   1.613  
   1.614  (* add to syntax *)
   1.615  
   1.616 -fun ext_syn extfun (syn, tsig, ctab) args =
   1.617 -  (extfun syn args, tsig, ctab);
   1.618 +fun ext_syn extfun (syn, tsig, ctab, names) args =
   1.619 +  (extfun syn args, tsig, ctab, names);
   1.620 +
   1.621 +
   1.622 +(* add to path *)
   1.623 +
   1.624 +fun ext_path (syn, tsig, ctab, (path, spaces)) elem =
   1.625 +  let
   1.626 +    val path' =
   1.627 +      if elem = ".." andalso not (null path) then fst (split_last path)
   1.628 +      else if elem = "/" then []
   1.629 +      else path @ NameSpace.unpack elem;
   1.630 +  in
   1.631 +    (syn, tsig, ctab, (path', spaces))
   1.632 +  end;      
   1.633 +
   1.634 +
   1.635 +(* add to name space *)
   1.636 +
   1.637 +fun ext_space (syn, tsig, ctab, (path, spaces)) (kind, names) =
   1.638 +  (syn, tsig, ctab, (path, add_names spaces kind names));
   1.639  
   1.640  
   1.641  (* build signature *)
   1.642 @@ -522,23 +718,27 @@
   1.643      else ref name :: stmps
   1.644    end;
   1.645  
   1.646 -fun make_sign (syn, tsig, ctab) stamps name =
   1.647 -  Sg {tsig = tsig, const_tab = ctab, syn = syn,
   1.648 +fun make_sign (syn, tsig, ctab, (path, spaces)) stamps name =
   1.649 +  Sg {tsig = tsig, const_tab = ctab, syn = syn, path = path, spaces = spaces,
   1.650      stamps = ext_stamps stamps name};
   1.651  
   1.652 -fun extend_sign extfun name decls (Sg {tsig, const_tab, syn, stamps}) =
   1.653 -  make_sign (extfun (syn, tsig, const_tab) decls) stamps name;
   1.654 +fun extend_sign extfun name decls (Sg {tsig, const_tab, syn, path, spaces, stamps}) =
   1.655 +  make_sign (extfun (syn, tsig, const_tab, (path, spaces)) decls) stamps name;
   1.656  
   1.657  
   1.658  (* the external interfaces *)
   1.659  
   1.660 -val add_classes      = extend_sign ext_classes "#";
   1.661 -val add_classrel     = extend_sign ext_classrel "#";
   1.662 -val add_defsort      = extend_sign ext_defsort "#";
   1.663 +val add_classes      = extend_sign (ext_classes true) "#";
   1.664 +val add_classes_i    = extend_sign (ext_classes false) "#";
   1.665 +val add_classrel     = extend_sign (ext_classrel true) "#";
   1.666 +val add_classrel_i   = extend_sign (ext_classrel false) "#";
   1.667 +val add_defsort      = extend_sign (ext_defsort true) "#";
   1.668 +val add_defsort_i    = extend_sign (ext_defsort false) "#";
   1.669  val add_types        = extend_sign ext_types "#";
   1.670  val add_tyabbrs      = extend_sign ext_tyabbrs "#";
   1.671  val add_tyabbrs_i    = extend_sign ext_tyabbrs_i "#";
   1.672 -val add_arities      = extend_sign ext_arities "#";
   1.673 +val add_arities      = extend_sign (ext_arities true) "#";
   1.674 +val add_arities_i    = extend_sign (ext_arities false) "#";
   1.675  val add_consts       = extend_sign ext_consts "#";
   1.676  val add_consts_i     = extend_sign ext_consts_i "#";
   1.677  val add_syntax       = extend_sign ext_syntax "#";
   1.678 @@ -550,13 +750,15 @@
   1.679  val add_tokentrfuns  = extend_sign (ext_syn Syntax.extend_tokentrfuns) "#";
   1.680  val add_trrules      = extend_sign (ext_syn Syntax.extend_trrules) "#";
   1.681  val add_trrules_i    = extend_sign (ext_syn Syntax.extend_trrules_i) "#";
   1.682 +val add_path         = extend_sign ext_path "#";
   1.683 +val add_space        = extend_sign ext_space "#";
   1.684 +fun add_name name sg = extend_sign K name () sg;
   1.685  
   1.686 -fun add_name name sg = extend_sign K name () sg;
   1.687  val make_draft = add_name "#";
   1.688  
   1.689  
   1.690  
   1.691 -(** merge signatures **)    (*exception TERM*) (*exception ERROR (* FIXME *)*)
   1.692 +(** merge signatures **)    (*exception TERM*) (*exception ERROR (* FIXME *) handle_error? *)
   1.693  
   1.694  fun merge (sg1, sg2) =
   1.695    if fast_subsig (sg2, sg1) then sg1
   1.696 @@ -564,32 +766,39 @@
   1.697    else if subsig (sg2, sg1) then sg1
   1.698    else if subsig (sg1, sg2) then sg2
   1.699    else if is_draft sg1 orelse is_draft sg2 then
   1.700 -    raise_term "Illegal merge of draft signatures" []
   1.701 +    raise TERM ("Illegal merge of draft signatures", [])
   1.702    else
   1.703      (*neither is union already; must form union*)
   1.704      let
   1.705        val Sg {tsig = tsig1, const_tab = const_tab1, syn = syn1,
   1.706 -        stamps = stamps1} = sg1;
   1.707 +        path, spaces = spaces1, stamps = stamps1} = sg1;
   1.708        val Sg {tsig = tsig2, const_tab = const_tab2, syn = syn2,
   1.709 -        stamps = stamps2} = sg2;
   1.710 +        path = _, spaces = spaces2, stamps = stamps2} = sg2;
   1.711  
   1.712  
   1.713        val stamps = merge_rev_lists stamps1 stamps2;
   1.714        val _ =
   1.715          (case duplicates (stamp_names stamps) of
   1.716            [] => ()
   1.717 -        | dups => raise_term ("Attempt to merge different versions of theories "
   1.718 -            ^ commas_quote dups) []);
   1.719 +        | dups => raise TERM ("Attempt to merge different versions of theories "
   1.720 +            ^ commas_quote dups, []));
   1.721  
   1.722        val tsig = Type.merge_tsigs (tsig1, tsig2);
   1.723  
   1.724        val const_tab = Symtab.merge (op =) (const_tab1, const_tab2)
   1.725          handle Symtab.DUPS cs =>
   1.726 -          raise_term ("Incompatible types for constant(s) " ^ commas_quote cs) [];
   1.727 +          raise TERM ("Incompatible types for constant(s) " ^ commas_quote cs, []);
   1.728  
   1.729        val syn = Syntax.merge_syntaxes syn1 syn2;
   1.730 +
   1.731 +      val kinds = distinct (map fst (spaces1 @ spaces2));
   1.732 +      val spaces =
   1.733 +        kinds ~~
   1.734 +          ListPair.map NameSpace.merge
   1.735 +            (map (space_of spaces1) kinds, map (space_of spaces2) kinds);
   1.736      in
   1.737 -      Sg {tsig = tsig, const_tab = const_tab, syn = syn, stamps = stamps}
   1.738 +      Sg {tsig = tsig, const_tab = const_tab, syn = syn,
   1.739 +        path = path, spaces = spaces, stamps = stamps}
   1.740      end;
   1.741  
   1.742  
   1.743 @@ -597,15 +806,15 @@
   1.744  (** the Pure signature **)
   1.745  
   1.746  val proto_pure =
   1.747 -  make_sign (Syntax.pure_syn, Type.tsig0, Symtab.null) [] "#"
   1.748 +  make_sign (Syntax.pure_syn, Type.tsig0, Symtab.null, ([], [])) [] "#"
   1.749    |> add_types
   1.750     (("fun", 2, NoSyn) ::
   1.751      ("prop", 0, NoSyn) ::
   1.752      ("itself", 1, NoSyn) ::
   1.753      Syntax.pure_types)
   1.754 -  |> add_classes [(logicC, [])]
   1.755 -  |> add_defsort logicS
   1.756 -  |> add_arities
   1.757 +  |> add_classes_i [(logicC, [])]
   1.758 +  |> add_defsort_i logicS
   1.759 +  |> add_arities_i
   1.760     [("fun", [logicS, logicS], logicS),
   1.761      ("prop", [], logicS),
   1.762      ("itself", [logicS], logicS)]
   1.763 @@ -631,4 +840,8 @@
   1.764    |> add_syntax Syntax.pure_applC_syntax
   1.765    |> add_name "CPure";
   1.766  
   1.767 +
   1.768  end;
   1.769 +
   1.770 +
   1.771 +val long_names = Sign.long_names;