src/Pure/sign.ML
author wenzelm
Sat Apr 12 17:00:45 2008 +0200 (2008-04-12)
changeset 26631 d6b6c74a8bcf
parent 26268 80aaf4d034be
child 26637 0bfccafc52eb
permissions -rw-r--r--
rep_cterm/rep_thm: no longer dereference theory_ref;
removed obsolete compression;
     1 (*  Title:      Pure/sign.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson and Markus Wenzel
     4 
     5 Logical signature content: naming conventions, concrete syntax, type
     6 signature, polymorphic constants.
     7 *)
     8 
     9 signature SIGN_THEORY =
    10 sig
    11   val add_defsort: string -> theory -> theory
    12   val add_defsort_i: sort -> theory -> theory
    13   val add_types: (bstring * int * mixfix) list -> theory -> theory
    14   val add_nonterminals: bstring list -> theory -> theory
    15   val add_tyabbrs: (bstring * string list * string * mixfix) list -> theory -> theory
    16   val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> theory -> theory
    17   val add_syntax: (bstring * string * mixfix) list -> theory -> theory
    18   val add_syntax_i: (bstring * typ * mixfix) list -> theory -> theory
    19   val add_modesyntax: Syntax.mode -> (bstring * string * mixfix) list -> theory -> theory
    20   val add_modesyntax_i: Syntax.mode -> (bstring * typ * mixfix) list -> theory -> theory
    21   val del_modesyntax: Syntax.mode -> (bstring * string * mixfix) list -> theory -> theory
    22   val del_modesyntax_i: Syntax.mode -> (bstring * typ * mixfix) list -> theory -> theory
    23   val add_consts: (bstring * string * mixfix) list -> theory -> theory
    24   val add_consts_i: (bstring * typ * mixfix) list -> theory -> theory
    25   val add_trfuns:
    26     (string * (ast list -> ast)) list *
    27     (string * (term list -> term)) list *
    28     (string * (term list -> term)) list *
    29     (string * (ast list -> ast)) list -> theory -> theory
    30   val add_trfunsT:
    31     (string * (bool -> typ -> term list -> term)) list -> theory -> theory
    32   val add_advanced_trfuns:
    33     (string * (Proof.context -> ast list -> ast)) list *
    34     (string * (Proof.context -> term list -> term)) list *
    35     (string * (Proof.context -> term list -> term)) list *
    36     (string * (Proof.context -> ast list -> ast)) list -> theory -> theory
    37   val add_advanced_trfunsT:
    38     (string * (Proof.context -> bool -> typ -> term list -> term)) list -> theory -> theory
    39   val add_tokentrfuns:
    40     (string * string * (string -> output * int)) list -> theory -> theory
    41   val add_mode_tokentrfuns: string -> (string * (string -> output * int)) list
    42     -> theory -> theory
    43   val add_trrules: (xstring * string) Syntax.trrule list -> theory -> theory
    44   val del_trrules: (xstring * string) Syntax.trrule list -> theory -> theory
    45   val add_trrules_i: ast Syntax.trrule list -> theory -> theory
    46   val del_trrules_i: ast Syntax.trrule list -> theory -> theory
    47   val add_path: string -> theory -> theory
    48   val parent_path: theory -> theory
    49   val root_path: theory -> theory
    50   val absolute_path: theory -> theory
    51   val local_path: theory -> theory
    52   val no_base_names: theory -> theory
    53   val qualified_names: theory -> theory
    54   val sticky_prefix: string -> theory -> theory
    55   val restore_naming: theory -> theory -> theory
    56 end
    57 
    58 signature SIGN =
    59 sig
    60   val rep_sg: theory ->
    61    {naming: NameSpace.naming,
    62     syn: Syntax.syntax,
    63     tsig: Type.tsig,
    64     consts: Consts.T}
    65   val naming_of: theory -> NameSpace.naming
    66   val base_name: string -> bstring
    67   val full_name: theory -> bstring -> string
    68   val full_name_path: theory -> string -> bstring -> string
    69   val declare_name: theory -> string -> NameSpace.T -> NameSpace.T
    70   val syn_of: theory -> Syntax.syntax
    71   val tsig_of: theory -> Type.tsig
    72   val classes_of: theory -> Sorts.algebra
    73   val all_classes: theory -> class list
    74   val super_classes: theory -> class -> class list
    75   val minimize_sort: theory -> sort -> sort
    76   val complete_sort: theory -> sort -> sort
    77   val defaultS: theory -> sort
    78   val subsort: theory -> sort * sort -> bool
    79   val of_sort: theory -> typ * sort -> bool
    80   val witness_sorts: theory -> sort list -> sort list -> (typ * sort) list
    81   val universal_witness: theory -> (typ * sort) option
    82   val all_sorts_nonempty: theory -> bool
    83   val is_logtype: theory -> string -> bool
    84   val typ_instance: theory -> typ * typ -> bool
    85   val typ_equiv: theory -> typ * typ -> bool
    86   val typ_match: theory -> typ * typ -> Type.tyenv -> Type.tyenv
    87   val typ_unify: theory -> typ * typ -> Type.tyenv * int -> Type.tyenv * int
    88   val consts_of: theory -> Consts.T
    89   val the_const_constraint: theory -> string -> typ
    90   val const_type: theory -> string -> typ option
    91   val the_const_type: theory -> string -> typ
    92   val declared_tyname: theory -> string -> bool
    93   val declared_const: theory -> string -> bool
    94   val const_monomorphic: theory -> string -> bool
    95   val const_syntax_name: theory -> string -> string
    96   val const_typargs: theory -> string * typ -> typ list
    97   val const_instance: theory -> string * typ list -> typ
    98   val mk_const: theory -> string * typ list -> term
    99   val class_space: theory -> NameSpace.T
   100   val type_space: theory -> NameSpace.T
   101   val const_space: theory -> NameSpace.T
   102   val intern_class: theory -> xstring -> string
   103   val extern_class: theory -> string -> xstring
   104   val intern_type: theory -> xstring -> string
   105   val extern_type: theory -> string -> xstring
   106   val intern_const: theory -> xstring -> string
   107   val extern_const: theory -> string -> xstring
   108   val intern_sort: theory -> sort -> sort
   109   val extern_sort: theory -> sort -> sort
   110   val intern_typ: theory -> typ -> typ
   111   val extern_typ: theory -> typ -> typ
   112   val intern_term: theory -> term -> term
   113   val extern_term: (string -> xstring) -> theory -> term -> term
   114   val intern_tycons: theory -> typ -> typ
   115   val pretty_term: theory -> term -> Pretty.T
   116   val pretty_typ: theory -> typ -> Pretty.T
   117   val pretty_sort: theory -> sort -> Pretty.T
   118   val string_of_term: theory -> term -> string
   119   val string_of_typ: theory -> typ -> string
   120   val string_of_sort: theory -> sort -> string
   121   val pp: theory -> Pretty.pp
   122   val arity_number: theory -> string -> int
   123   val arity_sorts: theory -> string -> sort -> sort list
   124   val certify_class: theory -> class -> class
   125   val certify_sort: theory -> sort -> sort
   126   val certify_typ: theory -> typ -> typ
   127   val certify_typ_mode: Type.mode -> theory -> typ -> typ
   128   val certify': bool -> Pretty.pp -> bool -> Consts.T -> theory -> term -> term * typ * int
   129   val certify_term: theory -> term -> term * typ * int
   130   val certify_prop: theory -> term -> term * typ * int
   131   val cert_term: theory -> term -> term
   132   val cert_prop: theory -> term -> term
   133   val no_frees: Pretty.pp -> term -> term
   134   val no_vars: Pretty.pp -> term -> term
   135   val cert_def: Proof.context -> term -> (string * typ) * term
   136   val read_class: theory -> xstring -> class
   137   val read_arity: theory -> xstring * string list * string -> arity
   138   val cert_arity: theory -> arity -> arity
   139   val get_sort: theory ->
   140     (indexname -> sort option) -> (indexname * sort) list -> indexname -> sort
   141   val read_def_typ: theory * (indexname -> sort option) -> string -> typ
   142   val read_typ: theory -> string -> typ
   143   val read_typ_syntax: theory -> string -> typ
   144   val read_typ_abbrev: theory -> string -> typ
   145   val read_def_terms:
   146     theory * (indexname -> typ option) * (indexname -> sort option) ->
   147     string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
   148   val simple_read_term: theory -> typ -> string -> term
   149   val read_term: theory -> string -> term
   150   val read_prop: theory -> string -> term
   151   val declare_const: Markup.property list -> bstring * typ * mixfix -> theory -> term * theory
   152   val notation: bool -> Syntax.mode -> (term * mixfix) list -> theory -> theory
   153   val add_abbrev: string -> Markup.property list ->
   154     bstring * term -> theory -> (term * term) * theory
   155   val revert_abbrev: string -> string -> theory -> theory
   156   include SIGN_THEORY
   157   val add_const_constraint: string * typ option -> theory -> theory
   158   val primitive_class: string * class list -> theory -> theory
   159   val primitive_classrel: class * class -> theory -> theory
   160   val primitive_arity: arity -> theory -> theory
   161   val hide_classes: bool -> xstring list -> theory -> theory
   162   val hide_classes_i: bool -> string list -> theory -> theory
   163   val hide_types: bool -> xstring list -> theory -> theory
   164   val hide_types_i: bool -> string list -> theory -> theory
   165   val hide_consts: bool -> xstring list -> theory -> theory
   166   val hide_consts_i: bool -> string list -> theory -> theory
   167   val hide_names: bool -> string * xstring list -> theory -> theory
   168   val hide_names_i: bool -> string * string list -> theory -> theory
   169 end
   170 
   171 structure Sign: SIGN =
   172 struct
   173 
   174 (** datatype sign **)
   175 
   176 datatype sign = Sign of
   177  {naming: NameSpace.naming,     (*common naming conventions*)
   178   syn: Syntax.syntax,           (*concrete syntax for terms, types, sorts*)
   179   tsig: Type.tsig,              (*order-sorted signature of types*)
   180   consts: Consts.T};            (*polymorphic constants*)
   181 
   182 fun make_sign (naming, syn, tsig, consts) =
   183   Sign {naming = naming, syn = syn, tsig = tsig, consts = consts};
   184 
   185 structure SignData = TheoryDataFun
   186 (
   187   type T = sign;
   188   val copy = I;
   189   fun extend (Sign {syn, tsig, consts, ...}) =
   190     make_sign (NameSpace.default_naming, syn, tsig, consts);
   191 
   192   val empty =
   193     make_sign (NameSpace.default_naming, Syntax.basic_syn, Type.empty_tsig, Consts.empty);
   194 
   195   fun merge pp (sign1, sign2) =
   196     let
   197       val Sign {naming = _, syn = syn1, tsig = tsig1, consts = consts1} = sign1;
   198       val Sign {naming = _, syn = syn2, tsig = tsig2, consts = consts2} = sign2;
   199 
   200       val naming = NameSpace.default_naming;
   201       val syn = Syntax.merge_syntaxes syn1 syn2;
   202       val tsig = Type.merge_tsigs pp (tsig1, tsig2);
   203       val consts = Consts.merge (consts1, consts2);
   204     in make_sign (naming, syn, tsig, consts) end;
   205 );
   206 
   207 fun rep_sg thy = SignData.get thy |> (fn Sign args => args);
   208 
   209 fun map_sign f = SignData.map (fn Sign {naming, syn, tsig, consts} =>
   210   make_sign (f (naming, syn, tsig, consts)));
   211 
   212 fun map_naming f = map_sign (fn (naming, syn, tsig, consts) => (f naming, syn, tsig, consts));
   213 fun map_syn f = map_sign (fn (naming, syn, tsig, consts) => (naming, f syn, tsig, consts));
   214 fun map_tsig f = map_sign (fn (naming, syn, tsig, consts) => (naming, syn, f tsig, consts));
   215 fun map_consts f = map_sign (fn (naming, syn, tsig, consts) => (naming, syn, tsig, f consts));
   216 
   217 
   218 (* naming *)
   219 
   220 val naming_of = #naming o rep_sg;
   221 val base_name = NameSpace.base;
   222 val full_name = NameSpace.full o naming_of;
   223 fun full_name_path thy elems = NameSpace.full (NameSpace.add_path elems (naming_of thy));
   224 val declare_name = NameSpace.declare o naming_of;
   225 
   226 
   227 (* syntax *)
   228 
   229 val syn_of = #syn o rep_sg;
   230 
   231 
   232 (* type signature *)
   233 
   234 val tsig_of = #tsig o rep_sg;
   235 
   236 val classes_of = #2 o #classes o Type.rep_tsig o tsig_of;
   237 val all_classes = Sorts.all_classes o classes_of;
   238 val minimal_classes = Sorts.minimal_classes o classes_of;
   239 val super_classes = Sorts.super_classes o classes_of;
   240 val minimize_sort = Sorts.minimize_sort o classes_of;
   241 val complete_sort = Sorts.complete_sort o classes_of;
   242 
   243 val defaultS = Type.defaultS o tsig_of;
   244 val subsort = Type.subsort o tsig_of;
   245 val of_sort = Type.of_sort o tsig_of;
   246 val witness_sorts = Type.witness_sorts o tsig_of;
   247 val universal_witness = Type.universal_witness o tsig_of;
   248 val all_sorts_nonempty = is_some o universal_witness;
   249 val is_logtype = member (op =) o Type.logical_types o tsig_of;
   250 
   251 val typ_instance = Type.typ_instance o tsig_of;
   252 fun typ_equiv thy (T, U) = typ_instance thy (T, U) andalso typ_instance thy (U, T);
   253 val typ_match = Type.typ_match o tsig_of;
   254 val typ_unify = Type.unify o tsig_of;
   255 
   256 
   257 (* polymorphic constants *)
   258 
   259 val consts_of = #consts o rep_sg;
   260 val the_const_constraint = Consts.the_constraint o consts_of;
   261 val the_const_type = Consts.the_type o consts_of;
   262 val const_type = try o the_const_type;
   263 val const_monomorphic = Consts.is_monomorphic o consts_of;
   264 val const_syntax_name = Consts.syntax_name o consts_of;
   265 val const_typargs = Consts.typargs o consts_of;
   266 val const_instance = Consts.instance o consts_of;
   267 
   268 fun mk_const thy (c, Ts) = Const (c, const_instance thy (c, Ts));
   269 
   270 val declared_tyname = Symtab.defined o #2 o #types o Type.rep_tsig o tsig_of;
   271 val declared_const = can o the_const_constraint;
   272 
   273 
   274 
   275 (** intern / extern names **)
   276 
   277 val class_space = #1 o #classes o Type.rep_tsig o tsig_of;
   278 val type_space = #1 o #types o Type.rep_tsig o tsig_of;
   279 val const_space = Consts.space_of o consts_of;
   280 
   281 val intern_class = NameSpace.intern o class_space;
   282 val extern_class = NameSpace.extern o class_space;
   283 val intern_type = NameSpace.intern o type_space;
   284 val extern_type = NameSpace.extern o type_space;
   285 val intern_const = NameSpace.intern o const_space;
   286 val extern_const = NameSpace.extern o const_space;
   287 
   288 val intern_sort = map o intern_class;
   289 val extern_sort = map o extern_class;
   290 
   291 local
   292 
   293 fun map_typ f g (Type (c, Ts)) = Type (g c, map (map_typ f g) Ts)
   294   | map_typ f _ (TFree (x, S)) = TFree (x, map f S)
   295   | map_typ f _ (TVar (xi, S)) = TVar (xi, map f S);
   296 
   297 fun map_term f g h (Const (c, T)) = Const (h c, map_typ f g T)
   298   | map_term f g _ (Free (x, T)) = Free (x, map_typ f g T)
   299   | map_term f g _ (Var (xi, T)) = Var (xi, map_typ f g T)
   300   | map_term _ _ _ (t as Bound _) = t
   301   | map_term f g h (Abs (x, T, t)) = Abs (x, map_typ f g T, map_term f g h t)
   302   | map_term f g h (t $ u) = map_term f g h t $ map_term f g h u;
   303 
   304 val add_classesT = Term.fold_atyps
   305   (fn TFree (_, S) => fold (insert (op =)) S
   306     | TVar (_, S) => fold (insert (op =)) S
   307     | _ => I);
   308 
   309 fun add_tyconsT (Type (c, Ts)) = insert (op =) c #> fold add_tyconsT Ts
   310   | add_tyconsT _ = I;
   311 
   312 val add_consts = Term.fold_aterms (fn Const (c, _) => insert (op =) c | _ => I);
   313 
   314 fun mapping add_names f t =
   315   let
   316     fun f' (x: string) = let val y = f x in if x = y then NONE else SOME (x, y) end;
   317     val tab = map_filter f' (add_names t []);
   318     fun get x = the_default x (AList.lookup (op =) tab x);
   319   in get end;
   320 
   321 fun typ_mapping f g thy T =
   322   T |> map_typ
   323     (mapping add_classesT (f thy) T)
   324     (mapping add_tyconsT (g thy) T);
   325 
   326 fun term_mapping f g h thy t =
   327   t |> map_term
   328     (mapping (Term.fold_types add_classesT) (f thy) t)
   329     (mapping (Term.fold_types add_tyconsT) (g thy) t)
   330     (mapping add_consts (h thy) t);
   331 
   332 in
   333 
   334 val intern_typ = typ_mapping intern_class intern_type;
   335 val extern_typ = typ_mapping extern_class extern_type;
   336 val intern_term = term_mapping intern_class intern_type intern_const;
   337 fun extern_term h = term_mapping extern_class extern_type (K h);
   338 val intern_tycons = typ_mapping (K I) intern_type;
   339 
   340 end;
   341 
   342 
   343 
   344 (** pretty printing of terms, types etc. **)
   345 
   346 val pretty_term = Syntax.pretty_term o ProofContext.init;
   347 val pretty_typ = Syntax.pretty_typ o ProofContext.init;
   348 val pretty_sort = Syntax.pretty_sort o ProofContext.init;
   349 
   350 val string_of_term = Syntax.string_of_term o ProofContext.init;
   351 val string_of_typ = Syntax.string_of_typ o ProofContext.init;
   352 val string_of_sort = Syntax.string_of_sort o ProofContext.init;
   353 
   354 (*pp operations -- deferred evaluation*)
   355 fun pp thy = Pretty.pp
   356  (fn x => pretty_term thy x,
   357   fn x => pretty_typ thy x,
   358   fn x => pretty_sort thy x,
   359   fn x => Syntax.pretty_classrel (ProofContext.init thy) x,
   360   fn x => Syntax.pretty_arity (ProofContext.init thy) x);
   361 
   362 
   363 
   364 (** certify entities **)    (*exception TYPE*)
   365 
   366 (* certify wrt. type signature *)
   367 
   368 val arity_number = Type.arity_number o tsig_of;
   369 fun arity_sorts thy = Type.arity_sorts (pp thy) (tsig_of thy);
   370 
   371 val certify_class         = Type.cert_class o tsig_of;
   372 val certify_sort          = Type.cert_sort o tsig_of;
   373 val certify_typ           = Type.cert_typ o tsig_of;
   374 fun certify_typ_mode mode = Type.cert_typ_mode mode o tsig_of;
   375 
   376 
   377 (* certify term/prop *)
   378 
   379 local
   380 
   381 fun type_check pp tm =
   382   let
   383     fun err_appl why bs t T u U =
   384       let
   385         val xs = map Free bs;           (*we do not rename here*)
   386         val t' = subst_bounds (xs, t);
   387         val u' = subst_bounds (xs, u);
   388         val msg = cat_lines
   389           (TypeInfer.appl_error (Syntax.pp_show_brackets pp) why t' T u' U);
   390       in raise TYPE (msg, [T, U], [t', u']) end;
   391 
   392     fun typ_of (_, Const (_, T)) = T
   393       | typ_of (_, Free  (_, T)) = T
   394       | typ_of (_, Var (_, T)) = T
   395       | typ_of (bs, Bound i) = snd (List.nth (bs, i) handle Subscript =>
   396           raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
   397       | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body)
   398       | typ_of (bs, t $ u) =
   399           let val T = typ_of (bs, t) and U = typ_of (bs, u) in
   400             (case T of
   401               Type ("fun", [T1, T2]) =>
   402                 if T1 = U then T2 else err_appl "Incompatible operand type" bs t T u U
   403             | _ => err_appl "Operator not of function type" bs t T u U)
   404           end;
   405   in typ_of ([], tm) end;
   406 
   407 fun err msg = raise TYPE (msg, [], []);
   408 
   409 fun check_vars (t $ u) = (check_vars t; check_vars u)
   410   | check_vars (Abs (_, _, t)) = check_vars t
   411   | check_vars (Var (xi as (_, i), _)) =
   412       if i < 0 then err ("Malformed variable: " ^ quote (Term.string_of_vname xi)) else ()
   413   | check_vars _ = ();
   414 
   415 in
   416 
   417 fun certify' prop pp do_expand consts thy tm =
   418   let
   419     val _ = check_vars tm;
   420     val tm' = Term.map_types (certify_typ thy) tm;
   421     val T = type_check pp tm';
   422     val _ = if prop andalso T <> propT then err "Term not of type prop" else ();
   423     val tm'' = Consts.certify pp (tsig_of thy) do_expand consts tm';
   424   in (if tm = tm'' then tm else tm'', T, Term.maxidx_of_term tm'') end;
   425 
   426 fun certify_term thy = certify' false (pp thy) true (consts_of thy) thy;
   427 fun certify_prop thy = certify' true (pp thy) true (consts_of thy) thy;
   428 
   429 fun cert_term_abbrev thy = #1 o certify' false (pp thy) false (consts_of thy) thy;
   430 val cert_term = #1 oo certify_term;
   431 val cert_prop = #1 oo certify_prop;
   432 
   433 end;
   434 
   435 
   436 (* specifications *)
   437 
   438 fun no_variables kind add addT mk mkT pp tm =
   439   (case (add tm [], addT tm []) of
   440     ([], []) => tm
   441   | (frees, tfrees) => error (Pretty.string_of (Pretty.block
   442       (Pretty.str ("Illegal " ^ kind ^ " variable(s) in term:") :: Pretty.brk 1 ::
   443        Pretty.commas (map (Pretty.term pp o mk) frees @ map (Pretty.typ pp o mkT) tfrees)))));
   444 
   445 val no_frees = no_variables "free" Term.add_frees Term.add_tfrees Free TFree;
   446 val no_vars = no_variables "schematic" Term.add_vars Term.add_tvars Var TVar;
   447 
   448 fun cert_def ctxt tm =
   449   let val ((lhs, rhs), _) = tm
   450     |> no_vars (Syntax.pp ctxt)
   451     |> Logic.strip_imp_concl
   452     |> PrimitiveDefs.dest_def ctxt Term.is_Const (K false) (K false)
   453   in (Term.dest_Const (Term.head_of lhs), rhs) end
   454   handle TERM (msg, _) => error msg;
   455 
   456 
   457 
   458 (** read and certify entities **)    (*exception ERROR*)
   459 
   460 (* classes *)
   461 
   462 fun read_class thy c = certify_class thy (intern_class thy c)
   463   handle TYPE (msg, _, _) => error msg;
   464 
   465 
   466 (* type arities *)
   467 
   468 fun prep_arity prep_tycon prep_sort thy (t, Ss, S) =
   469   let val arity = (prep_tycon thy t, map (prep_sort thy) Ss, prep_sort thy S)
   470   in Type.add_arity (pp thy) arity (tsig_of thy); arity end;
   471 
   472 val read_arity = prep_arity intern_type Syntax.read_sort_global;
   473 val cert_arity = prep_arity (K I) certify_sort;
   474 
   475 
   476 (* types *)
   477 
   478 fun get_sort thy def_sort raw_env =
   479   let
   480     val tsig = tsig_of thy;
   481 
   482     fun eq ((xi, S), (xi', S')) =
   483       Term.eq_ix (xi, xi') andalso Type.eq_sort tsig (S, S');
   484     val env = distinct eq raw_env;
   485     val _ = (case duplicates (eq_fst (op =)) env of [] => ()
   486       | dups => error ("Inconsistent sort constraints for type variable(s) "
   487           ^ commas_quote (map (Term.string_of_vname' o fst) dups)));
   488 
   489     fun get xi =
   490       (case (AList.lookup (op =) env xi, def_sort xi) of
   491         (NONE, NONE) => Type.defaultS tsig
   492       | (NONE, SOME S) => S
   493       | (SOME S, NONE) => S
   494       | (SOME S, SOME S') =>
   495           if Type.eq_sort tsig (S, S') then S'
   496           else error ("Sort constraint inconsistent with default for type variable " ^
   497             quote (Term.string_of_vname' xi)));
   498   in get end;
   499 
   500 local
   501 
   502 fun gen_read_typ mode (thy, def_sort) str =
   503   let
   504     val ctxt = ProofContext.init thy;
   505     val syn = syn_of thy;
   506     val T = intern_tycons thy
   507       (Syntax.standard_parse_typ ctxt syn (get_sort thy def_sort) (intern_sort thy) str);
   508   in certify_typ_mode mode thy T handle TYPE (msg, _, _) => error msg end
   509   handle ERROR msg => cat_error msg ("The error(s) above occurred in type " ^ quote str);
   510 
   511 in
   512 
   513 fun no_def_sort thy = (thy: theory, K NONE);
   514 
   515 val read_def_typ     = gen_read_typ Type.mode_default;
   516 val read_typ         = gen_read_typ Type.mode_default o no_def_sort;
   517 val read_typ_syntax  = gen_read_typ Type.mode_syntax o no_def_sort;
   518 val read_typ_abbrev  = gen_read_typ Type.mode_abbrev o no_def_sort;
   519 
   520 end;
   521 
   522 
   523 (* read_def_terms -- read terms and infer types *)    (*exception ERROR*)
   524 
   525 (*
   526   def_type: partial map from indexnames to types (constrains Frees and Vars)
   527   def_sort: partial map from indexnames to sorts (constrains TFrees and TVars)
   528   used: context of already used type variables
   529   freeze: if true then generated parameters are turned into TFrees, else TVars
   530 *)
   531 
   532 fun read_def_terms'
   533     pp is_logtype syn consts map_free ctxt (def_type, def_sort) used freeze raw_args =
   534   let
   535     val thy = ProofContext.theory_of ctxt;
   536     fun check_typs Ts = map (certify_typ thy) Ts
   537       handle TYPE (msg, _, _) => error msg;
   538 
   539     fun infer args = TypeInfer.infer_types pp (tsig_of thy) check_typs
   540         (try (Consts.the_constraint consts)) def_type used ~1 (SOME freeze) args |>> map fst
   541       handle TYPE (msg, _, _) => error msg;
   542 
   543     fun check T t = (singleton (fst o infer) (t, T); NONE) handle ERROR msg => SOME msg;
   544     val map_const = try (#1 o Term.dest_Const o Consts.read_const consts);
   545     fun read T = Syntax.standard_parse_term pp (check T) (get_sort thy def_sort) map_const map_free
   546         (intern_tycons thy) (intern_sort thy) ctxt is_logtype syn T;
   547   in
   548     raw_args
   549     |> map (fn (s, raw_T) =>
   550       let val T = certify_typ thy raw_T handle TYPE (msg, _, _) => error msg
   551       in (read (#1 (TypeInfer.paramify_dummies T 0)) s, T) end)
   552     |> infer
   553   end;
   554 
   555 fun read_def_terms (thy, types, sorts) used freeze sTs =
   556   let
   557     val pp = pp thy;
   558     val consts = consts_of thy;
   559     val cert_consts = Consts.certify pp (tsig_of thy) true consts;
   560     fun map_free x = if is_some (types (x, ~1)) then SOME x else NONE;
   561     val (ts, inst) =
   562       read_def_terms' pp (is_logtype thy) (syn_of thy) consts map_free
   563         (ProofContext.init thy) (types, sorts) (Name.make_context used) freeze sTs;
   564   in (map cert_consts ts, inst) end;
   565 
   566 fun simple_read_term thy T s =
   567   let val ([t], _) = read_def_terms (thy, K NONE, K NONE) [] true [(s, T)]
   568   in t end handle ERROR msg => cat_error msg ("The error(s) above occurred for term " ^ s);
   569 
   570 fun read_term thy = simple_read_term thy dummyT;
   571 fun read_prop thy = simple_read_term thy propT;
   572 
   573 
   574 
   575 (** signature extension functions **)  (*exception ERROR/TYPE*)
   576 
   577 (* add default sort *)
   578 
   579 fun gen_add_defsort prep_sort s thy =
   580   thy |> map_tsig (Type.set_defsort (prep_sort thy s));
   581 
   582 val add_defsort = gen_add_defsort Syntax.read_sort_global;
   583 val add_defsort_i = gen_add_defsort certify_sort;
   584 
   585 
   586 (* add type constructors *)
   587 
   588 fun add_types types thy = thy |> map_sign (fn (naming, syn, tsig, consts) =>
   589   let
   590     val syn' = Syntax.update_type_gram types syn;
   591     val decls = map (fn (a, n, mx) => (Syntax.type_name a mx, n)) types;
   592     val tsig' = Type.add_types naming decls tsig;
   593   in (naming, syn', tsig', consts) end);
   594 
   595 
   596 (* add nonterminals *)
   597 
   598 fun add_nonterminals ns thy = thy |> map_sign (fn (naming, syn, tsig, consts) =>
   599   let
   600     val syn' = Syntax.update_consts ns syn;
   601     val tsig' = Type.add_nonterminals naming ns tsig;
   602   in (naming, syn', tsig', consts) end);
   603 
   604 
   605 (* add type abbreviations *)
   606 
   607 fun gen_add_tyabbr parse_typ (a, vs, rhs, mx) thy =
   608   thy |> map_sign (fn (naming, syn, tsig, consts) =>
   609     let
   610       val ctxt = ProofContext.init thy;
   611       val syn' = Syntax.update_type_gram [(a, length vs, mx)] syn;
   612       val a' = Syntax.type_name a mx;
   613       val abbr = (a', vs, certify_typ_mode Type.mode_syntax thy (parse_typ ctxt rhs))
   614         handle ERROR msg => cat_error msg ("in type abbreviation " ^ quote a');
   615       val tsig' = Type.add_abbrevs naming [abbr] tsig;
   616     in (naming, syn', tsig', consts) end);
   617 
   618 val add_tyabbrs = fold (gen_add_tyabbr Syntax.parse_typ);
   619 val add_tyabbrs_i = fold (gen_add_tyabbr (K I));
   620 
   621 
   622 (* modify syntax *)
   623 
   624 fun gen_syntax change_gram parse_typ mode args thy =
   625   let
   626     val ctxt = ProofContext.init thy;
   627     fun prep (c, T, mx) = (c, certify_typ_mode Type.mode_syntax thy (parse_typ ctxt T), mx)
   628       handle ERROR msg =>
   629         cat_error msg ("in syntax declaration " ^ quote (Syntax.const_name c mx));
   630   in thy |> map_syn (change_gram (is_logtype thy) mode (map prep args)) end;
   631 
   632 fun gen_add_syntax x = gen_syntax Syntax.update_const_gram x;
   633 
   634 val add_modesyntax = gen_add_syntax Syntax.parse_typ;
   635 val add_modesyntax_i = gen_add_syntax (K I);
   636 val add_syntax = add_modesyntax Syntax.mode_default;
   637 val add_syntax_i = add_modesyntax_i Syntax.mode_default;
   638 val del_modesyntax = gen_syntax Syntax.remove_const_gram Syntax.parse_typ;
   639 val del_modesyntax_i = gen_syntax Syntax.remove_const_gram (K I);
   640 
   641 fun notation add mode args thy =
   642   let
   643     val change_gram = if add then Syntax.update_const_gram else Syntax.remove_const_gram;
   644     fun const_syntax (Const (c, _), mx) = try (Consts.syntax (consts_of thy)) (c, mx)
   645       | const_syntax _ = NONE;
   646   in gen_syntax change_gram (K I) mode (map_filter const_syntax args) thy end;
   647 
   648 
   649 (* add constants *)
   650 
   651 local
   652 
   653 fun gen_add_consts parse_typ authentic tags raw_args thy =
   654   let
   655     val ctxt = ProofContext.init thy;
   656     val prepT = Type.no_tvars o Term.no_dummyT o certify_typ thy o parse_typ ctxt;
   657     fun prep (raw_c, raw_T, raw_mx) =
   658       let
   659         val (c, mx) = Syntax.const_mixfix raw_c raw_mx;
   660         val full_c = full_name thy c;
   661         val c' = if authentic then Syntax.constN ^ full_c else c;
   662         val T = (prepT raw_T handle TYPE (msg, _, _) => error msg) handle ERROR msg =>
   663           cat_error msg ("in declaration of constant " ^ quote c);
   664         val T' = Logic.varifyT T;
   665       in ((c, T'), (c', T', mx), Const (full_c, T)) end;
   666     val args = map prep raw_args;
   667   in
   668     thy
   669     |> map_consts (fold (Consts.declare authentic (naming_of thy) tags o #1) args)
   670     |> add_syntax_i (map #2 args)
   671     |> pair (map #3 args)
   672   end;
   673 
   674 in
   675 
   676 val add_consts = snd oo gen_add_consts Syntax.parse_typ false [];
   677 val add_consts_i = snd oo gen_add_consts (K I) false [];
   678 
   679 fun declare_const tags arg = gen_add_consts (K I) true tags [arg] #>> the_single;
   680 
   681 end;
   682 
   683 
   684 (* abbreviations *)
   685 
   686 fun add_abbrev mode tags (c, raw_t) thy =
   687   let
   688     val pp = pp thy;
   689     val prep_tm = no_frees pp o Term.no_dummy_patterns o cert_term_abbrev thy;
   690     val t = (prep_tm raw_t handle TYPE (msg, _, _) => error msg | TERM (msg, _) => error msg)
   691       handle ERROR msg => cat_error msg ("in constant abbreviation " ^ quote c);
   692     val (res, consts') = consts_of thy
   693       |> Consts.abbreviate pp (tsig_of thy) (naming_of thy) mode tags (c, t);
   694   in (res, thy |> map_consts (K consts')) end;
   695 
   696 fun revert_abbrev mode c = map_consts (Consts.revert_abbrev mode c);
   697 
   698 
   699 (* add constraints *)
   700 
   701 fun add_const_constraint (c, opt_T) thy =
   702   let
   703     fun prepT raw_T =
   704       let val T = Logic.varifyT (Type.no_tvars (Term.no_dummyT (certify_typ thy raw_T)))
   705       in cert_term thy (Const (c, T)); T end
   706       handle TYPE (msg, _, _) => error msg;
   707   in thy |> map_consts (Consts.constrain (c, Option.map prepT opt_T)) end;
   708 
   709 
   710 (* primitive classes and arities *)
   711 
   712 fun primitive_class (bclass, classes) thy =
   713   thy |> map_sign (fn (naming, syn, tsig, consts) =>
   714     let
   715       val syn' = Syntax.update_consts [bclass] syn;
   716       val tsig' = Type.add_class (pp thy) naming (bclass, classes) tsig;
   717     in (naming, syn', tsig', consts) end)
   718   |> add_consts_i [(Logic.const_of_class bclass, Term.a_itselfT --> propT, NoSyn)];
   719 
   720 fun primitive_classrel arg thy = thy |> map_tsig (Type.add_classrel (pp thy) arg);
   721 fun primitive_arity arg thy = thy |> map_tsig (Type.add_arity (pp thy) arg);
   722 
   723 
   724 (* add translation functions *)
   725 
   726 local
   727 
   728 fun mk trs = map Syntax.mk_trfun trs;
   729 
   730 fun gen_add_trfuns ext non_typed (atrs, trs, tr's, atr's) =
   731   map_syn (ext (mk atrs, mk trs, mk (map (apsnd non_typed) tr's), mk atr's));
   732 
   733 fun gen_add_trfunsT ext tr's = map_syn (ext ([], [], mk tr's, []));
   734 
   735 in
   736 
   737 val add_trfuns = gen_add_trfuns Syntax.update_trfuns Syntax.non_typed_tr';
   738 val add_trfunsT = gen_add_trfunsT Syntax.update_trfuns;
   739 val add_advanced_trfuns = gen_add_trfuns Syntax.update_advanced_trfuns Syntax.non_typed_tr'';
   740 val add_advanced_trfunsT = gen_add_trfunsT Syntax.update_advanced_trfuns;
   741 
   742 end;
   743 
   744 val add_tokentrfuns = map_syn o Syntax.extend_tokentrfuns;
   745 fun add_mode_tokentrfuns m = add_tokentrfuns o map (fn (s, f) => (m, s, f));
   746 
   747 
   748 (* translation rules *)
   749 
   750 fun gen_trrules f args thy = thy |> map_syn (fn syn =>
   751   let val rules = map (Syntax.map_trrule (apfst (intern_type thy))) args
   752   in f (ProofContext.init thy) (is_logtype thy) syn rules syn end);
   753 
   754 val add_trrules = gen_trrules Syntax.update_trrules;
   755 val del_trrules = gen_trrules Syntax.remove_trrules;
   756 val add_trrules_i = map_syn o Syntax.update_trrules_i;
   757 val del_trrules_i = map_syn o Syntax.remove_trrules_i;
   758 
   759 
   760 (* modify naming *)
   761 
   762 val add_path        = map_naming o NameSpace.add_path;
   763 val no_base_names   = map_naming NameSpace.no_base_names;
   764 val qualified_names = map_naming NameSpace.qualified_names;
   765 val sticky_prefix   = map_naming o NameSpace.sticky_prefix;
   766 val restore_naming  = map_naming o K o naming_of;
   767 
   768 val parent_path   = add_path "..";
   769 val root_path     = add_path "/";
   770 val absolute_path = add_path "//";
   771 
   772 fun local_path thy = thy |> root_path |> add_path (Context.theory_name thy);
   773 
   774 
   775 (* hide names *)
   776 
   777 fun hide_classes b xs thy = thy |> map_tsig (Type.hide_classes b (map (intern_class thy) xs));
   778 val hide_classes_i = map_tsig oo Type.hide_classes;
   779 fun hide_types b xs thy = thy |> map_tsig (Type.hide_types b (map (intern_type thy) xs));
   780 val hide_types_i = map_tsig oo Type.hide_types;
   781 fun hide_consts b xs thy = thy |> map_consts (fold (Consts.hide b o intern_const thy) xs);
   782 val hide_consts_i = map_consts oo (fold o Consts.hide);
   783 
   784 local
   785 
   786 val kinds =
   787  [("class", (intern_class, can o certify_class, hide_classes_i)),
   788   ("type", (intern_type, declared_tyname, hide_types_i)),
   789   ("const", (intern_const, declared_const, hide_consts_i))];
   790 
   791 fun gen_hide int b (kind, xnames) thy =
   792   (case AList.lookup (op =) kinds kind of
   793     SOME (intern, check, hide) =>
   794       let
   795         val names = if int then map (intern thy) xnames else xnames;
   796         val bads = filter_out (check thy) names;
   797       in
   798         if null bads then hide b names thy
   799         else error ("Attempt to hide undeclared item(s): " ^ commas_quote bads)
   800       end
   801   | NONE => error ("Bad name space specification: " ^ quote kind));
   802 
   803 in
   804 
   805 val hide_names = gen_hide true;
   806 val hide_names_i = gen_hide false;
   807 
   808 end;
   809 
   810 end;