src/Pure/sign.ML
author haftmann
Mon Feb 06 11:01:28 2006 +0100 (2006-02-06)
changeset 18928 042608ffa2ec
parent 18892 51360da418b2
child 18941 18cb1e2ab77d
permissions -rw-r--r--
subsituted gen_duplicates / has_duplicates for duplicates whenever appropriate
     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_typedecls: (bstring * string list * mixfix) list -> theory -> theory
    15   val add_nonterminals: bstring list -> theory -> theory
    16   val add_tyabbrs: (bstring * string list * string * mixfix) list -> theory -> theory
    17   val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> theory -> theory
    18   val add_arities: (xstring * string list * string) list -> theory -> theory
    19   val add_arities_i: (string * sort list * sort) list -> theory -> theory
    20   val add_syntax: (bstring * string * mixfix) list -> theory -> theory
    21   val add_syntax_i: (bstring * typ * mixfix) list -> theory -> theory
    22   val add_modesyntax: (string * bool) -> (bstring * string * mixfix) list -> theory -> theory
    23   val add_modesyntax_i: (string * bool) -> (bstring * typ * mixfix) list -> theory -> theory
    24   val del_modesyntax: (string * bool) -> (bstring * string * mixfix) list -> theory -> theory
    25   val del_modesyntax_i: (string * bool) -> (bstring * typ * mixfix) list -> theory -> theory
    26   val add_consts: (bstring * string * mixfix) list -> theory -> theory
    27   val add_consts_i: (bstring * typ * mixfix) list -> theory -> theory
    28   val add_const_constraint: xstring * string -> theory -> theory
    29   val add_const_constraint_i: string * typ -> theory -> theory
    30   val add_classes: (bstring * xstring list) list -> theory -> theory
    31   val add_classes_i: (bstring * class list) list -> theory -> theory
    32   val add_classrel: (xstring * xstring) list -> theory -> theory
    33   val add_classrel_i: (class * class) list -> theory -> theory
    34   val add_trfuns:
    35     (string * (ast list -> ast)) list *
    36     (string * (term list -> term)) list *
    37     (string * (term list -> term)) list *
    38     (string * (ast list -> ast)) list -> theory -> theory
    39   val add_trfunsT:
    40     (string * (bool -> typ -> term list -> term)) list -> theory -> theory
    41   val add_advanced_trfuns:
    42     (string * (Context.generic -> ast list -> ast)) list *
    43     (string * (Context.generic -> term list -> term)) list *
    44     (string * (Context.generic -> term list -> term)) list *
    45     (string * (Context.generic -> ast list -> ast)) list -> theory -> theory
    46   val add_advanced_trfunsT:
    47     (string * (Context.generic -> bool -> typ -> term list -> term)) list -> theory -> theory
    48   val add_tokentrfuns:
    49     (string * string * (string -> string * real)) list -> theory -> theory
    50   val add_mode_tokentrfuns: string -> (string * (string -> string * real)) list
    51     -> theory -> theory
    52   val parse_ast_translation: bool * string -> theory -> theory
    53   val parse_translation: bool * string -> theory -> theory
    54   val print_translation: bool * string -> theory -> theory
    55   val typed_print_translation: bool * string -> theory -> theory
    56   val print_ast_translation: bool * string -> theory -> theory
    57   val token_translation: string -> theory -> theory
    58   val add_trrules: (xstring * string) Syntax.trrule list -> theory -> theory
    59   val add_trrules_i: ast Syntax.trrule list -> theory -> theory
    60   val add_path: string -> theory -> theory
    61   val parent_path: theory -> theory
    62   val root_path: theory -> theory
    63   val absolute_path: theory -> theory
    64   val local_path: theory -> theory
    65   val qualified_names: theory -> theory
    66   val no_base_names: theory -> theory
    67   val custom_accesses: (string list -> string list list) -> theory -> theory
    68   val set_policy: (string -> bstring -> string) * (string list -> string list list) ->
    69     theory -> theory
    70   val restore_naming: theory -> theory -> theory
    71   val hide_classes: bool -> xstring list -> theory -> theory
    72   val hide_classes_i: bool -> string list -> theory -> theory
    73   val hide_types: bool -> xstring list -> theory -> theory
    74   val hide_types_i: bool -> string list -> theory -> theory
    75   val hide_consts: bool -> xstring list -> theory -> theory
    76   val hide_consts_i: bool -> string list -> theory -> theory
    77   val hide_names: bool -> string * xstring list -> theory -> theory
    78   val hide_names_i: bool -> string * string list -> theory -> theory
    79 end
    80 
    81 signature SIGN =
    82 sig
    83   type sg    (*obsolete*)
    84   val init_data: theory -> theory
    85   val rep_sg: theory ->
    86    {naming: NameSpace.naming,
    87     syn: Syntax.syntax,
    88     tsig: Type.tsig,
    89     consts: Consts.T}
    90   val naming_of: theory -> NameSpace.naming
    91   val base_name: string -> bstring
    92   val full_name: theory -> bstring -> string
    93   val full_name_path: theory -> string -> bstring -> string
    94   val declare_name: theory -> string -> NameSpace.T -> NameSpace.T
    95   val syn_of: theory -> Syntax.syntax
    96   val tsig_of: theory -> Type.tsig
    97   val classes_of: theory -> Sorts.classes
    98   val classes_arities_of: theory -> Sorts.classes * Sorts.arities
    99   val classes: theory -> class list
   100   val defaultS: theory -> sort
   101   val subsort: theory -> sort * sort -> bool
   102   val of_sort: theory -> typ * sort -> bool
   103   val witness_sorts: theory -> sort list -> sort list -> (typ * sort) list
   104   val universal_witness: theory -> (typ * sort) option
   105   val all_sorts_nonempty: theory -> bool
   106   val typ_instance: theory -> typ * typ -> bool
   107   val typ_match: theory -> typ * typ -> Type.tyenv -> Type.tyenv
   108   val typ_unify: theory -> typ * typ -> Type.tyenv * int -> Type.tyenv * int
   109   val is_logtype: theory -> string -> bool
   110   val const_constraint: theory -> string -> typ option
   111   val the_const_constraint: theory -> string -> typ
   112   val const_type: theory -> string -> typ option
   113   val the_const_type: theory -> string -> typ
   114   val declared_tyname: theory -> string -> bool
   115   val declared_const: theory -> string -> bool
   116   val const_monomorphic: theory -> string -> bool
   117   val const_typargs: theory -> string * typ -> typ list
   118   val const_instance: theory -> string * typ list -> typ
   119   val class_space: theory -> NameSpace.T
   120   val type_space: theory -> NameSpace.T
   121   val const_space: theory -> NameSpace.T
   122   val intern_class: theory -> xstring -> string
   123   val extern_class: theory -> string -> xstring
   124   val intern_type: theory -> xstring -> string
   125   val extern_type: theory -> string -> xstring
   126   val intern_const: theory -> xstring -> string
   127   val extern_const: theory -> string -> xstring
   128   val intern_sort: theory -> sort -> sort
   129   val extern_sort: theory -> sort -> sort
   130   val intern_typ: theory -> typ -> typ
   131   val extern_typ: theory -> typ -> typ
   132   val intern_term: theory -> term -> term
   133   val extern_term: theory -> term -> term
   134   val intern_tycons: theory -> typ -> typ
   135   val pretty_term': Syntax.syntax -> Context.generic -> term -> Pretty.T
   136   val pretty_term: theory -> term -> Pretty.T
   137   val pretty_typ: theory -> typ -> Pretty.T
   138   val pretty_sort: theory -> sort -> Pretty.T
   139   val pretty_classrel: theory -> class list -> Pretty.T
   140   val pretty_arity: theory -> arity -> Pretty.T
   141   val string_of_term: theory -> term -> string
   142   val string_of_typ: theory -> typ -> string
   143   val string_of_sort: theory -> sort -> string
   144   val string_of_classrel: theory -> class list -> string
   145   val string_of_arity: theory -> arity -> string
   146   val pprint_term: theory -> term -> pprint_args -> unit
   147   val pprint_typ: theory -> typ -> pprint_args -> unit
   148   val pp: theory -> Pretty.pp
   149   val certify_class: theory -> class -> class
   150   val certify_sort: theory -> sort -> sort
   151   val certify_typ: theory -> typ -> typ
   152   val certify_typ_syntax: theory -> typ -> typ
   153   val certify_typ_abbrev: theory -> typ -> typ
   154   val certify_term: Pretty.pp -> theory -> term -> term * typ * int
   155   val certify_prop: Pretty.pp -> theory -> term -> term * typ * int
   156   val cert_term: theory -> term -> term
   157   val cert_prop: theory -> term -> term
   158   val read_sort': Syntax.syntax -> Context.generic -> string -> sort
   159   val read_sort: theory -> string -> sort
   160   val read_typ': Syntax.syntax -> Context.generic ->
   161     (indexname -> sort option) -> string -> typ
   162   val read_typ_syntax': Syntax.syntax -> Context.generic ->
   163     (indexname -> sort option) -> string -> typ
   164   val read_typ_abbrev': Syntax.syntax -> Context.generic ->
   165     (indexname -> sort option) -> string -> typ
   166   val read_typ: theory * (indexname -> sort option) -> string -> typ
   167   val read_typ_syntax: theory * (indexname -> sort option) -> string -> typ
   168   val read_typ_abbrev: theory * (indexname -> sort option) -> string -> typ
   169   val read_tyname: theory -> string -> typ
   170   val read_const: theory -> string -> term
   171   val infer_types_simult: Pretty.pp -> theory -> (indexname -> typ option) ->
   172     (indexname -> sort option) -> string list -> bool
   173     -> (term list * typ) list -> term list * (indexname * typ) list
   174   val infer_types: Pretty.pp -> theory -> (indexname -> typ option) ->
   175     (indexname -> sort option) -> string list -> bool
   176     -> term list * typ -> term * (indexname * typ) list
   177   val read_def_terms': Pretty.pp -> (string -> bool) -> Syntax.syntax -> Context.generic ->
   178     (indexname -> typ option) * (indexname -> sort option) ->
   179     string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
   180   val read_def_terms:
   181     theory * (indexname -> typ option) * (indexname -> sort option) ->
   182     string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
   183   val simple_read_term: theory -> typ -> string -> term
   184   val read_term: theory -> string -> term
   185   val read_prop: theory -> string -> term
   186   val const_of_class: class -> string
   187   val class_of_const: string -> class
   188   include SIGN_THEORY
   189 end
   190 
   191 structure Sign: SIGN =
   192 struct
   193 
   194 type sg = theory;
   195 
   196 
   197 (** datatype sign **)
   198 
   199 datatype sign = Sign of
   200  {naming: NameSpace.naming,     (*common naming conventions*)
   201   syn: Syntax.syntax,           (*concrete syntax for terms, types, sorts*)
   202   tsig: Type.tsig,              (*order-sorted signature of types*)
   203   consts: Consts.T};            (*polymorphic constants*)
   204 
   205 fun make_sign (naming, syn, tsig, consts) =
   206   Sign {naming = naming, syn = syn, tsig = tsig, consts = consts};
   207 
   208 structure SignData = TheoryDataFun
   209 (struct
   210   val name = "Pure/sign";
   211   type T = sign;
   212   val copy = I;
   213   fun extend (Sign {syn, tsig, consts, ...}) =
   214     make_sign (NameSpace.default_naming, syn, tsig, consts);
   215 
   216   val empty =
   217     make_sign (NameSpace.default_naming, Syntax.basic_syn, Type.empty_tsig, Consts.empty);
   218 
   219   fun merge pp (sign1, sign2) =
   220     let
   221       val Sign {naming = _, syn = syn1, tsig = tsig1, consts = consts1} = sign1;
   222       val Sign {naming = _, syn = syn2, tsig = tsig2, consts = consts2} = sign2;
   223 
   224       val naming = NameSpace.default_naming;
   225       val syn = Syntax.merge_syntaxes syn1 syn2;
   226       val tsig = Type.merge_tsigs pp (tsig1, tsig2);
   227       val consts = Consts.merge (consts1, consts2);
   228     in make_sign (naming, syn, tsig, consts) end;
   229 
   230   fun print _ _ = ();
   231 end);
   232 
   233 val init_data = SignData.init;
   234 
   235 fun rep_sg thy = SignData.get thy |> (fn Sign args => args);
   236 
   237 fun map_sign f = SignData.map (fn Sign {naming, syn, tsig, consts} =>
   238   make_sign (f (naming, syn, tsig, consts)));
   239 
   240 fun map_naming f = map_sign (fn (naming, syn, tsig, consts) => (f naming, syn, tsig, consts));
   241 fun map_syn f = map_sign (fn (naming, syn, tsig, consts) => (naming, f syn, tsig, consts));
   242 fun map_tsig f = map_sign (fn (naming, syn, tsig, consts) => (naming, syn, f tsig, consts));
   243 fun map_consts f = map_sign (fn (naming, syn, tsig, consts) => (naming, syn, tsig, f consts));
   244 
   245 
   246 (* naming *)
   247 
   248 val naming_of = #naming o rep_sg;
   249 val base_name = NameSpace.base;
   250 val full_name = NameSpace.full o naming_of;
   251 fun full_name_path thy elems = NameSpace.full (NameSpace.add_path elems (naming_of thy));
   252 val declare_name = NameSpace.declare o naming_of;
   253 
   254 
   255 (* syntax *)
   256 
   257 val syn_of = #syn o rep_sg;
   258 
   259 
   260 (* type signature *)
   261 
   262 val tsig_of = #tsig o rep_sg;
   263 val classes_of = snd o #classes o Type.rep_tsig o tsig_of;
   264 fun classes_arities_of thy = (classes_of thy, #arities (Type.rep_tsig (tsig_of thy)));
   265 val classes = Type.classes o tsig_of;
   266 val defaultS = Type.defaultS o tsig_of;
   267 val subsort = Type.subsort o tsig_of;
   268 val of_sort = Type.of_sort o tsig_of;
   269 val witness_sorts = Type.witness_sorts o tsig_of;
   270 val universal_witness = Type.universal_witness o tsig_of;
   271 val all_sorts_nonempty = is_some o universal_witness;
   272 val typ_instance = Type.typ_instance o tsig_of;
   273 val typ_match = Type.typ_match o tsig_of;
   274 val typ_unify = Type.unify o tsig_of;
   275 fun is_logtype thy c = c mem_string Type.logical_types (tsig_of thy);
   276 
   277 
   278 (* polymorphic constants *)
   279 
   280 val consts_of = #consts o rep_sg;
   281 val the_const_constraint = Consts.constraint o consts_of;
   282 val const_constraint = try o the_const_constraint;
   283 val the_const_type = Consts.declaration o consts_of;
   284 val const_type = try o the_const_type;
   285 val const_monomorphic = Consts.monomorphic o consts_of;
   286 val const_typargs = Consts.typargs o consts_of;
   287 val const_instance = Consts.instance o consts_of;
   288 
   289 val declared_tyname = Symtab.defined o #2 o #types o Type.rep_tsig o tsig_of;
   290 val declared_const = is_some oo const_type;
   291 
   292 
   293 
   294 (** intern / extern names **)
   295 
   296 val class_space = #1 o #classes o Type.rep_tsig o tsig_of;
   297 val type_space = #1 o #types o Type.rep_tsig o tsig_of;
   298 val const_space = Consts.space o consts_of;
   299 
   300 val intern_class = NameSpace.intern o class_space;
   301 val extern_class = NameSpace.extern o class_space;
   302 val intern_type = NameSpace.intern o type_space;
   303 val extern_type = NameSpace.extern o type_space;
   304 val intern_const = NameSpace.intern o const_space;
   305 val extern_const = NameSpace.extern o const_space;
   306 
   307 val intern_sort = map o intern_class;
   308 val extern_sort = map o extern_class;
   309 
   310 local
   311 
   312 fun map_typ f g (Type (c, Ts)) = Type (g c, map (map_typ f g) Ts)
   313   | map_typ f _ (TFree (x, S)) = TFree (x, map f S)
   314   | map_typ f _ (TVar (xi, S)) = TVar (xi, map f S);
   315 
   316 fun map_term f g h (Const (c, T)) = Const (h c, map_typ f g T)
   317   | map_term f g _ (Free (x, T)) = Free (x, map_typ f g T)
   318   | map_term f g _ (Var (xi, T)) = Var (xi, map_typ f g T)
   319   | map_term _ _ _ (t as Bound _) = t
   320   | map_term f g h (Abs (x, T, t)) = Abs (x, map_typ f g T, map_term f g h t)
   321   | map_term f g h (t $ u) = map_term f g h t $ map_term f g h u;
   322 
   323 fun mapping add_names f t =
   324   let
   325     fun f' x = let val y = f x in if x = y then NONE else SOME (x, y) end;
   326     val tab = List.mapPartial f' (add_names (t, []));
   327     fun get x = if_none (AList.lookup (op =) tab x) x;
   328   in get end;
   329 
   330 fun typ_mapping f g thy T =
   331   T |> map_typ
   332     (mapping add_typ_classes (f thy) T)
   333     (mapping add_typ_tycons (g thy) T);
   334 
   335 fun term_mapping f g h thy t =
   336   t |> map_term
   337     (mapping add_term_classes (f thy) t)
   338     (mapping add_term_tycons (g thy) t)
   339     (mapping add_term_consts (h thy) t);
   340 
   341 in
   342 
   343 val intern_typ = typ_mapping intern_class intern_type;
   344 val extern_typ = typ_mapping extern_class extern_type;
   345 val intern_term = term_mapping intern_class intern_type intern_const;
   346 val extern_term = term_mapping extern_class extern_type extern_const;
   347 val intern_tycons = typ_mapping (K I) intern_type;
   348 
   349 end;
   350 
   351 
   352 
   353 (** pretty printing of terms, types etc. **)
   354 
   355 fun pretty_term' syn context t =
   356   let
   357     val thy = Context.theory_of context;
   358     val curried = Context.exists_name Context.CPureN thy;
   359   in Syntax.pretty_term context syn curried (extern_term thy t) end;
   360 
   361 fun pretty_term thy t = pretty_term' (syn_of thy) (Context.Theory thy) t;
   362 
   363 fun pretty_typ thy T = Syntax.pretty_typ (Context.Theory thy) (syn_of thy) (extern_typ thy T);
   364 fun pretty_sort thy S = Syntax.pretty_sort (Context.Theory thy) (syn_of thy) (extern_sort thy S);
   365 
   366 fun pretty_classrel thy cs = Pretty.block (List.concat
   367   (separate [Pretty.str " <", Pretty.brk 1] (map (single o pretty_sort thy o single) cs)));
   368 
   369 fun pretty_arity thy (a, Ss, S) =
   370   let
   371     val a' = extern_type thy a;
   372     val dom =
   373       if null Ss then []
   374       else [Pretty.list "(" ")" (map (pretty_sort thy) Ss), Pretty.brk 1];
   375   in Pretty.block ([Pretty.str (a' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort thy S]) end;
   376 
   377 val string_of_term = Pretty.string_of oo pretty_term;
   378 val string_of_typ = Pretty.string_of oo pretty_typ;
   379 val string_of_sort = Pretty.string_of oo pretty_sort;
   380 val string_of_classrel = Pretty.string_of oo pretty_classrel;
   381 val string_of_arity = Pretty.string_of oo pretty_arity;
   382 
   383 val pprint_term = (Pretty.pprint o Pretty.quote) oo pretty_term;
   384 val pprint_typ = (Pretty.pprint o Pretty.quote) oo pretty_typ;
   385 
   386 fun pp thy = Pretty.pp (pretty_term thy, pretty_typ thy, pretty_sort thy,
   387   pretty_classrel thy, pretty_arity thy);
   388 
   389 
   390 
   391 (** certify entities **)    (*exception TYPE*)
   392 
   393 (* certify wrt. type signature *)
   394 
   395 fun certify cert = cert o tsig_of o Context.check_thy;
   396 
   397 val certify_class      = certify Type.cert_class;
   398 val certify_sort       = certify Type.cert_sort;
   399 val certify_typ        = certify Type.cert_typ;
   400 val certify_typ_syntax = certify Type.cert_typ_syntax;
   401 val certify_typ_abbrev = certify Type.cert_typ_abbrev;
   402 
   403 
   404 (* certify_term *)
   405 
   406 local
   407 
   408 (*determine and check the type of a term*)
   409 fun type_check pp tm =
   410   let
   411     fun err_appl why bs t T u U =
   412       let
   413         val xs = map Free bs;           (*we do not rename here*)
   414         val t' = subst_bounds (xs, t);
   415         val u' = subst_bounds (xs, u);
   416         val msg = cat_lines
   417           (TypeInfer.appl_error (Syntax.pp_show_brackets pp) why t' T u' U);
   418       in raise TYPE (msg, [T, U], [t', u']) end;
   419 
   420     fun typ_of (_, Const (_, T)) = T
   421       | typ_of (_, Free  (_, T)) = T
   422       | typ_of (_, Var (_, T)) = T
   423       | typ_of (bs, Bound i) = snd (List.nth (bs, i) handle Subscript =>
   424           raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
   425       | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body)
   426       | typ_of (bs, t $ u) =
   427           let val T = typ_of (bs, t) and U = typ_of (bs, u) in
   428             (case T of
   429               Type ("fun", [T1, T2]) =>
   430                 if T1 = U then T2 else err_appl "Incompatible operand type" bs t T u U
   431             | _ => err_appl "Operator not of function type" bs t T u U)
   432           end;
   433 
   434   in typ_of ([], tm) end;
   435 
   436 in
   437 
   438 fun certify_term pp thy tm =
   439   let
   440     val _ = Context.check_thy thy;
   441 
   442     val tm' = map_term_types (certify_typ thy) tm;
   443     val tm' = if tm = tm' then tm else tm';  (*avoid copying of already normal term*)
   444 
   445     fun err msg = raise TYPE (msg, [], [tm']);
   446 
   447     fun show_const a T = quote a ^ " :: " ^ Pretty.string_of_typ pp T;
   448 
   449     fun check_atoms (t $ u) = (check_atoms t; check_atoms u)
   450       | check_atoms (Abs (_, _, t)) = check_atoms t
   451       | check_atoms (Const (a, T)) =
   452           (case const_type thy a of
   453             NONE => err ("Undeclared constant " ^ show_const a T)
   454           | SOME U =>
   455               if typ_instance thy (T, U) then ()
   456               else err ("Illegal type for constant " ^ show_const a T))
   457       | check_atoms (Var ((x, i), _)) =
   458           if i < 0 then err ("Malformed variable: " ^ quote x) else ()
   459       | check_atoms _ = ();
   460   in
   461     check_atoms tm';
   462     (tm', type_check pp tm', maxidx_of_term tm')
   463   end;
   464 
   465 end;
   466 
   467 fun certify_prop pp thy tm =
   468   let val res as (tm', T, _) = certify_term pp thy tm
   469   in if T <> propT then raise TYPE ("Term not of type prop", [T], [tm']) else res end;
   470 
   471 fun cert_term thy tm = #1 (certify_term (pp thy) thy tm);
   472 fun cert_prop thy tm = #1 (certify_prop (pp thy) thy tm);
   473 
   474 
   475 
   476 (** read and certify entities **)    (*exception ERROR*)
   477 
   478 (* sorts *)
   479 
   480 fun read_sort' syn context str =
   481   let
   482     val thy = Context.theory_of context;
   483     val _ = Context.check_thy thy;
   484     val S = intern_sort thy (Syntax.read_sort context syn str);
   485   in certify_sort thy S handle TYPE (msg, _, _) => error msg end;
   486 
   487 fun read_sort thy str = read_sort' (syn_of thy) (Context.Theory thy) str;
   488 
   489 
   490 (* types *)
   491 
   492 local
   493 
   494 fun gen_read_typ' cert syn context def_sort str =
   495   let
   496     val thy = Context.theory_of context;
   497     val _ = Context.check_thy thy;
   498     val get_sort = TypeInfer.get_sort (tsig_of thy) def_sort (intern_sort thy);
   499     val T = intern_tycons thy (Syntax.read_typ context syn get_sort (intern_sort thy) str);
   500   in cert thy T handle TYPE (msg, _, _) => error msg end
   501   handle ERROR msg => cat_error msg ("The error(s) above occurred in type " ^ quote str);
   502 
   503 fun gen_read_typ cert (thy, def_sort) str =
   504   gen_read_typ' cert (syn_of thy) (Context.Theory thy) def_sort str;
   505 
   506 in
   507 
   508 fun no_def_sort thy = (thy: theory, K NONE);
   509 
   510 val read_typ'        = gen_read_typ' certify_typ;
   511 val read_typ_syntax' = gen_read_typ' certify_typ_syntax;
   512 val read_typ_abbrev' = gen_read_typ' certify_typ_abbrev;
   513 val read_typ         = gen_read_typ certify_typ;
   514 val read_typ_syntax  = gen_read_typ certify_typ_syntax;
   515 val read_typ_abbrev  = gen_read_typ certify_typ_abbrev;
   516 
   517 end;
   518 
   519 
   520 (* type and constant names *)
   521 
   522 fun read_tyname thy raw_c =
   523   let val c = intern_type thy raw_c in
   524     (case Symtab.lookup (#2 (#types (Type.rep_tsig (tsig_of thy)))) c of
   525       SOME (Type.LogicalType n, _) => Type (c, replicate n dummyT)
   526     | _ => error ("Undeclared type constructor: " ^ quote c))
   527   end;
   528 
   529 fun read_const thy raw_c =
   530   let
   531     val c = intern_const thy raw_c;
   532     val _ = the_const_type thy c handle TYPE (msg, _, _) => error msg;
   533   in Const (c, dummyT) end;
   534 
   535 
   536 
   537 (** infer_types **)         (*exception ERROR*)
   538 
   539 (*
   540   def_type: partial map from indexnames to types (constrains Frees and Vars)
   541   def_sort: partial map from indexnames to sorts (constrains TFrees and TVars)
   542   used: list of already used type variables
   543   freeze: if true then generated parameters are turned into TFrees, else TVars
   544 
   545   termss: lists of alternative parses (only one combination should be type-correct)
   546   typs: expected types
   547 *)
   548 
   549 fun infer_types_simult pp thy def_type def_sort used freeze args =
   550   let
   551     val termss = fold_rev (multiply o fst) args [[]];
   552     val typs =
   553       map (fn (_, T) => certify_typ thy T handle TYPE (msg, _, _) => error msg) args;
   554 
   555     fun infer ts = Result (TypeInfer.infer_types (Syntax.pp_show_brackets pp) (tsig_of thy)
   556         (const_constraint thy) def_type def_sort (intern_const thy) (intern_tycons thy)
   557         (intern_sort thy) used freeze typs ts)
   558       handle TYPE (msg, _, _) => Exn (ERROR msg);
   559 
   560     val err_results = map infer termss;
   561     val errs = List.mapPartial (fn Exn (ERROR msg) => SOME msg | _ => NONE) err_results;
   562     val results = List.mapPartial get_result err_results;
   563 
   564     val ambiguity = length termss;
   565     fun ambig_msg () =
   566       if ambiguity > 1 andalso ambiguity <= ! Syntax.ambiguity_level then
   567         "Got more than one parse tree.\n\
   568         \Retry with smaller Syntax.ambiguity_level for more information."
   569       else "";
   570   in
   571     if null results then (cat_error (ambig_msg ()) (cat_lines errs))
   572     else if length results = 1 then
   573       (if ambiguity > ! Syntax.ambiguity_level then
   574         warning "Fortunately, only one parse tree is type correct.\n\
   575           \You may still want to disambiguate your grammar or your input."
   576       else (); hd results)
   577     else (cat_error (ambig_msg ()) ("More than one term is type correct:\n" ^
   578       cat_lines (map (Pretty.string_of_term pp) (List.concat (map fst results)))))
   579   end;
   580 
   581 fun infer_types pp thy def_type def_sort used freeze tsT =
   582   apfst hd (infer_types_simult pp thy def_type def_sort used freeze [tsT]);
   583 
   584 
   585 (* read_def_terms -- read terms and infer types *)    (*exception ERROR*)
   586 
   587 fun read_def_terms' pp is_logtype syn context (types, sorts) used freeze sTs =
   588   let
   589     val thy = Context.theory_of context;
   590     fun read (s, T) =
   591       let val T' = certify_typ thy T handle TYPE (msg, _, _) => error msg
   592       in (Syntax.read context is_logtype syn T' s, T') end;
   593   in infer_types_simult pp thy types sorts used freeze (map read sTs) end;
   594 
   595 fun read_def_terms (thy, types, sorts) =
   596   read_def_terms' (pp thy) (is_logtype thy) (syn_of thy) (Context.Theory thy) (types, sorts);
   597 
   598 fun simple_read_term thy T s =
   599   let val ([t], _) = read_def_terms (thy, K NONE, K NONE) [] true [(s, T)]
   600   in t end
   601   handle ERROR msg => cat_error msg ("The error(s) above occurred for term " ^ s);
   602 
   603 fun read_term thy = simple_read_term thy TypeInfer.logicT;
   604 fun read_prop thy = simple_read_term thy propT;
   605 
   606 
   607 
   608 (** signature extension functions **)  (*exception ERROR/TYPE*)
   609 
   610 (* add default sort *)
   611 
   612 fun gen_add_defsort prep_sort s thy =
   613   thy |> map_tsig (Type.set_defsort (prep_sort thy s));
   614 
   615 val add_defsort = gen_add_defsort read_sort;
   616 val add_defsort_i = gen_add_defsort certify_sort;
   617 
   618 
   619 (* add type constructors *)
   620 
   621 fun add_types types thy = thy |> map_sign (fn (naming, syn, tsig, consts) =>
   622   let
   623     val syn' = Syntax.extend_type_gram types syn;
   624     val decls = map (fn (a, n, mx) => (Syntax.type_name a mx, n)) types;
   625     val tsig' = Type.add_types naming decls tsig;
   626   in (naming, syn', tsig', consts) end);
   627 
   628 fun add_typedecls decls thy =
   629   let
   630     fun type_of (a, vs, mx) =
   631       if not (has_duplicates (op =) vs) then (a, length vs, mx)
   632       else error ("Duplicate parameters in type declaration: " ^ quote a);
   633   in add_types (map type_of decls) thy end;
   634 
   635 
   636 (* add nonterminals *)
   637 
   638 fun add_nonterminals ns thy = thy |> map_sign (fn (naming, syn, tsig, consts) =>
   639   let
   640     val syn' = Syntax.extend_consts ns syn;
   641     val tsig' = Type.add_nonterminals naming ns tsig;
   642   in (naming, syn', tsig', consts) end);
   643 
   644 
   645 (* add type abbreviations *)
   646 
   647 fun gen_add_tyabbr prep_typ (a, vs, rhs, mx) thy =
   648   thy |> map_sign (fn (naming, syn, tsig, consts) =>
   649     let
   650       val syn' = Syntax.extend_type_gram [(a, length vs, mx)] syn;
   651       val a' = Syntax.type_name a mx;
   652       val abbr = (a', vs, prep_typ thy rhs)
   653         handle ERROR msg => cat_error msg ("in type abbreviation " ^ quote a');
   654       val tsig' = Type.add_abbrevs naming [abbr] tsig;
   655     in (naming, syn', tsig', consts) end);
   656 
   657 val add_tyabbrs = fold (gen_add_tyabbr (read_typ_syntax o no_def_sort));
   658 val add_tyabbrs_i = fold (gen_add_tyabbr certify_typ_syntax);
   659 
   660 
   661 (* add type arities *)
   662 
   663 fun gen_add_arities int_type prep_sort arities thy = thy |> map_tsig (fn tsig =>
   664   let
   665     fun prep_arity (a, Ss, S) = (int_type thy a, map (prep_sort thy) Ss, prep_sort thy S)
   666       handle ERROR msg => cat_error msg ("in arity for type " ^ quote a);
   667     val tsig' = Type.add_arities (pp thy) (map prep_arity arities) tsig;
   668   in tsig' end);
   669 
   670 val add_arities = gen_add_arities intern_type read_sort;
   671 val add_arities_i = gen_add_arities (K I) certify_sort;
   672 
   673 
   674 (* modify syntax *)
   675 
   676 fun gen_syntax change_gram prep_typ prmode args thy =
   677   let
   678     fun prep (c, T, mx) = (c, prep_typ thy T, mx) handle ERROR msg =>
   679       cat_error msg ("in syntax declaration " ^ quote (Syntax.const_name c mx));
   680   in thy |> map_syn (change_gram (is_logtype thy) prmode (map prep args)) end;
   681 
   682 fun gen_add_syntax x = gen_syntax Syntax.extend_const_gram x;
   683 
   684 val add_modesyntax = gen_add_syntax (read_typ_syntax o no_def_sort);
   685 val add_modesyntax_i = gen_add_syntax certify_typ_syntax;
   686 val add_syntax = add_modesyntax Syntax.default_mode;
   687 val add_syntax_i = add_modesyntax_i Syntax.default_mode;
   688 val del_modesyntax = gen_syntax Syntax.remove_const_gram (read_typ_syntax o no_def_sort);
   689 val del_modesyntax_i = gen_syntax Syntax.remove_const_gram certify_typ_syntax;
   690 
   691 
   692 (* add constants *)
   693 
   694 local
   695 
   696 fun gen_add_consts prep_typ raw_args thy =
   697   let
   698     val prepT = Compress.typ thy o Type.varifyT o Type.no_tvars o Term.no_dummyT o prep_typ thy;
   699     fun prep (c, T, mx) = ((c, prepT T, mx) handle TYPE (msg, _, _) => error msg)
   700       handle ERROR msg =>
   701         cat_error msg ("in declaration of constant " ^ quote (Syntax.const_name c mx));
   702     val args = map prep raw_args;
   703     val decls = args |> map (fn (c, T, mx) => (Syntax.const_name c mx, T));
   704   in
   705     thy
   706     |> map_consts (fold (Consts.declare (naming_of thy)) decls)
   707     |> add_syntax_i args
   708   end;
   709 
   710 in
   711 
   712 val add_consts = gen_add_consts (read_typ o no_def_sort);
   713 val add_consts_i = gen_add_consts certify_typ;
   714 
   715 end;
   716 
   717 
   718 (* add constraints *)
   719 
   720 fun gen_add_constraint int_const prep_typ (raw_c, raw_T) thy =
   721   let
   722     val c = int_const thy raw_c;
   723     val T = Term.zero_var_indexesT (Term.no_dummyT (prep_typ thy raw_T))
   724       handle TYPE (msg, _, _) => error msg;
   725     val _ = cert_term thy (Const (c, T))
   726       handle TYPE (msg, _, _) => error msg;
   727   in thy |> map_consts (Consts.constrain (c, T)) end;
   728 
   729 val add_const_constraint = gen_add_constraint intern_const (read_typ o no_def_sort);
   730 val add_const_constraint_i = gen_add_constraint (K I) certify_typ;
   731 
   732 
   733 (* add type classes *)
   734 
   735 val classN = "_class";
   736 
   737 val const_of_class = suffix classN;
   738 fun class_of_const c = unsuffix classN c
   739   handle Fail _ => raise TERM ("class_of_const: bad name " ^ quote c, []);
   740 
   741 fun gen_add_class int_class (bclass, raw_classes) thy =
   742   thy |> map_sign (fn (naming, syn, tsig, consts) =>
   743     let
   744       val classes = map (int_class thy) raw_classes;
   745       val syn' = Syntax.extend_consts [bclass] syn;
   746       val tsig' = Type.add_classes (pp thy) naming [(bclass, classes)] tsig;
   747     in (naming, syn', tsig', consts) end)
   748   |> add_consts_i [(const_of_class bclass, a_itselfT --> propT, NoSyn)];
   749 
   750 val add_classes = fold (gen_add_class intern_class);
   751 val add_classes_i = fold (gen_add_class (K I));
   752 
   753 
   754 (* add to classrel *)
   755 
   756 fun gen_add_classrel int_class raw_pairs thy = thy |> map_tsig (fn tsig =>
   757   let
   758     val pairs = map (pairself (int_class thy)) raw_pairs;
   759     val tsig' = Type.add_classrel (pp thy) pairs tsig;
   760   in tsig' end);
   761 
   762 val add_classrel = gen_add_classrel intern_class;
   763 val add_classrel_i = gen_add_classrel (K I);
   764 
   765 
   766 (* add translation functions *)
   767 
   768 local
   769 
   770 fun mk trs = map Syntax.mk_trfun trs;
   771 
   772 fun gen_add_trfuns ext non_typed (atrs, trs, tr's, atr's) =
   773   map_syn (ext (mk atrs, mk trs, mk (map (apsnd non_typed) tr's), mk atr's));
   774 
   775 fun gen_add_trfunsT ext tr's = map_syn (ext ([], [], mk tr's, []));
   776 
   777 in
   778 
   779 val add_trfuns = gen_add_trfuns Syntax.extend_trfuns Syntax.non_typed_tr';
   780 val add_trfunsT = gen_add_trfunsT Syntax.extend_trfuns;
   781 val add_advanced_trfuns = gen_add_trfuns Syntax.extend_advanced_trfuns Syntax.non_typed_tr'';
   782 val add_advanced_trfunsT = gen_add_trfunsT Syntax.extend_advanced_trfuns;
   783 
   784 end;
   785 
   786 val add_tokentrfuns = map_syn o Syntax.extend_tokentrfuns;
   787 fun add_mode_tokentrfuns m = add_tokentrfuns o map (fn (s, f) => (m, s, f));
   788 
   789 
   790 (* compile translation functions *)
   791 
   792 local
   793 
   794 fun advancedT false = ""
   795   | advancedT true = "Context.generic -> ";
   796 
   797 fun advancedN false = ""
   798   | advancedN true = "advanced_";
   799 
   800 in
   801 
   802 fun parse_ast_translation (a, txt) =
   803   txt |> Context.use_let ("val parse_ast_translation: (string * (" ^ advancedT a ^
   804       "Syntax.ast list -> Syntax.ast)) list")
   805     ("Sign.add_" ^ advancedN a ^ "trfuns (parse_ast_translation, [], [], [])");
   806 
   807 fun parse_translation (a, txt) =
   808   txt |> Context.use_let ("val parse_translation: (string * (" ^ advancedT a ^
   809       "term list -> term)) list")
   810     ("Sign.add_" ^ advancedN a ^ "trfuns ([], parse_translation, [], [])");
   811 
   812 fun print_translation (a, txt) =
   813   txt |> Context.use_let ("val print_translation: (string * (" ^ advancedT a ^
   814       "term list -> term)) list")
   815     ("Sign.add_" ^ advancedN a ^ "trfuns ([], [], print_translation, [])");
   816 
   817 fun print_ast_translation (a, txt) =
   818   txt |> Context.use_let ("val print_ast_translation: (string * (" ^ advancedT a ^
   819       "Syntax.ast list -> Syntax.ast)) list")
   820     ("Sign.add_" ^ advancedN a ^ "trfuns ([], [], [], print_ast_translation)");
   821 
   822 fun typed_print_translation (a, txt) =
   823   txt |> Context.use_let ("val typed_print_translation: (string * (" ^ advancedT a ^
   824       "bool -> typ -> term list -> term)) list")
   825     ("Sign.add_" ^ advancedN a ^ "trfunsT typed_print_translation");
   826 
   827 val token_translation =
   828   Context.use_let "val token_translation: (string * string * (string -> string * real)) list"
   829     "Sign.add_tokentrfuns token_translation";
   830 
   831 end;
   832 
   833 
   834 (* add translation rules *)
   835 
   836 fun add_trrules args thy = thy |> map_syn (fn syn =>
   837   let val rules = map (Syntax.map_trrule (apfst (intern_type thy))) args
   838   in Syntax.extend_trrules (Context.Theory thy) (is_logtype thy) syn rules syn end);
   839 
   840 val add_trrules_i = map_syn o Syntax.extend_trrules_i;
   841 
   842 
   843 (* modify naming *)
   844 
   845 val add_path        = map_naming o NameSpace.add_path;
   846 val qualified_names = map_naming NameSpace.qualified_names;
   847 val no_base_names   = map_naming NameSpace.no_base_names;
   848 val custom_accesses = map_naming o NameSpace.custom_accesses;
   849 val set_policy      = map_naming o NameSpace.set_policy;
   850 val restore_naming  = map_naming o K o naming_of;
   851 
   852 val parent_path     = add_path "..";
   853 val root_path       = add_path "/";
   854 val absolute_path   = add_path "//";
   855 
   856 fun local_path thy = thy |> root_path |> add_path (Context.theory_name thy);
   857 
   858 
   859 (* hide names *)
   860 
   861 fun hide_classes b xs thy = thy |> map_tsig (Type.hide_classes b (map (intern_class thy) xs));
   862 val hide_classes_i = map_tsig oo Type.hide_classes;
   863 fun hide_types b xs thy = thy |> map_tsig (Type.hide_types b (map (intern_type thy) xs));
   864 val hide_types_i = map_tsig oo Type.hide_types;
   865 fun hide_consts b xs thy = thy |> map_consts (fold (Consts.hide b o intern_const thy) xs);
   866 val hide_consts_i = map_consts oo (fold o Consts.hide);
   867 
   868 local
   869 
   870 val kinds =
   871  [("class", (intern_class, can o certify_class, hide_classes_i)),
   872   ("type", (intern_type, declared_tyname, hide_types_i)),
   873   ("const", (intern_const, declared_const, hide_consts_i))];
   874 
   875 fun gen_hide int b (kind, xnames) thy =
   876   (case AList.lookup (op =) kinds kind of
   877     SOME (intern, check, hide) =>
   878       let
   879         val names = if int then map (intern thy) xnames else xnames;
   880         val bads = filter_out (check thy) names;
   881       in
   882         if null bads then hide b names thy
   883         else error ("Attempt to hide undeclared item(s): " ^ commas_quote bads)
   884       end
   885   | NONE => error ("Bad name space specification: " ^ quote kind));
   886 
   887 in
   888 
   889 val hide_names = gen_hide true;
   890 val hide_names_i = gen_hide false;
   891 
   892 end;
   893 
   894 end;