src/Pure/sign.ML
changeset 18062 7a666583e869
parent 18032 3295e9982a5b
child 18146 47463b1825c6
equal deleted inserted replaced
18061:972e3d554eb8 18062:7a666583e869
     1 (*  Title:      Pure/sign.ML
     1 (*  Title:      Pure/sign.ML
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson and Markus Wenzel
     3     Author:     Lawrence C Paulson and Markus Wenzel
     4 
     4 
     5 Logical signature content: naming conventions, concrete syntax, type
     5 Logical signature content: naming conventions, concrete syntax, type
     6 signature, constant declarations.
     6 signature, polymorphic constants.
     7 *)
     7 *)
     8 
     8 
     9 signature SIGN_THEORY =
     9 signature SIGN_THEORY =
    10 sig
    10 sig
    11   val add_defsort: string -> theory -> theory
    11   val add_defsort: string -> theory -> theory
    84   val init_data: theory -> theory
    84   val init_data: theory -> theory
    85   val rep_sg: theory ->
    85   val rep_sg: theory ->
    86    {naming: NameSpace.naming,
    86    {naming: NameSpace.naming,
    87     syn: Syntax.syntax,
    87     syn: Syntax.syntax,
    88     tsig: Type.tsig,
    88     tsig: Type.tsig,
    89     consts: ((typ * bool) * serial) NameSpace.table * typ Symtab.table}
    89     consts: Consts.T}
    90   val naming_of: theory -> NameSpace.naming
    90   val naming_of: theory -> NameSpace.naming
    91   val base_name: string -> bstring
    91   val base_name: string -> bstring
    92   val full_name: theory -> bstring -> string
    92   val full_name: theory -> bstring -> string
    93   val full_name_path: theory -> string -> bstring -> string
    93   val full_name_path: theory -> string -> bstring -> string
    94   val declare_name: theory -> string -> NameSpace.T -> NameSpace.T
    94   val declare_name: theory -> string -> NameSpace.T -> NameSpace.T
   111   val the_const_constraint: theory -> string -> typ
   111   val the_const_constraint: theory -> string -> typ
   112   val const_type: theory -> string -> typ option
   112   val const_type: theory -> string -> typ option
   113   val the_const_type: theory -> string -> typ
   113   val the_const_type: theory -> string -> typ
   114   val declared_tyname: theory -> string -> bool
   114   val declared_tyname: theory -> string -> bool
   115   val declared_const: theory -> string -> bool
   115   val declared_const: theory -> string -> bool
   116   val monomorphic: theory -> string -> bool
   116   val const_monomorphic: theory -> string -> bool
       
   117   val const_typargs: theory -> string -> typ -> typ list
   117   val class_space: theory -> NameSpace.T
   118   val class_space: theory -> NameSpace.T
   118   val type_space: theory -> NameSpace.T
   119   val type_space: theory -> NameSpace.T
   119   val const_space: theory -> NameSpace.T
   120   val const_space: theory -> NameSpace.T
   120   val intern_class: theory -> xstring -> string
   121   val intern_class: theory -> xstring -> string
   121   val extern_class: theory -> string -> xstring
   122   val extern_class: theory -> string -> xstring
   190 
   191 
   191 
   192 
   192 (** datatype sign **)
   193 (** datatype sign **)
   193 
   194 
   194 datatype sign = Sign of
   195 datatype sign = Sign of
   195  {naming: NameSpace.naming,                   (*common naming conventions*)
   196  {naming: NameSpace.naming,     (*common naming conventions*)
   196   syn: Syntax.syntax,                         (*concrete syntax for terms, types, sorts*)
   197   syn: Syntax.syntax,           (*concrete syntax for terms, types, sorts*)
   197   tsig: Type.tsig,                            (*order-sorted signature of types*)
   198   tsig: Type.tsig,              (*order-sorted signature of types*)
   198   consts:
   199   consts: Consts.T};            (*polymorphic constants*)
   199     ((typ * bool) * serial) NameSpace.table * (*type schemes of declared term constants*)
       
   200     typ Symtab.table};                        (*type constraints for constants*)
       
   201 
       
   202 
   200 
   203 fun make_sign (naming, syn, tsig, consts) =
   201 fun make_sign (naming, syn, tsig, consts) =
   204   Sign {naming = naming, syn = syn, tsig = tsig, consts = consts};
   202   Sign {naming = naming, syn = syn, tsig = tsig, consts = consts};
   205 
       
   206 fun err_dup_consts cs =
       
   207   error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
       
   208 
       
   209 fun err_inconsistent_constraints cs =
       
   210   error ("Inconsistent type constraints for constant(s) " ^ commas_quote cs);
       
   211 
   203 
   212 structure SignData = TheoryDataFun
   204 structure SignData = TheoryDataFun
   213 (struct
   205 (struct
   214   val name = "Pure/sign";
   206   val name = "Pure/sign";
   215   type T = sign;
   207   type T = sign;
   216   val copy = I;
   208   val copy = I;
   217   fun extend (Sign {syn, tsig, consts, ...}) =
   209   fun extend (Sign {syn, tsig, consts, ...}) =
   218     make_sign (NameSpace.default_naming, syn, tsig, consts);
   210     make_sign (NameSpace.default_naming, syn, tsig, consts);
   219 
   211 
   220   val empty = make_sign (NameSpace.default_naming, Syntax.pure_syn, Type.empty_tsig,
   212   val empty =
   221     (NameSpace.empty_table, Symtab.empty));
   213     make_sign (NameSpace.default_naming, Syntax.pure_syn, Type.empty_tsig, Consts.empty);
   222 
   214 
   223   fun merge pp (sign1, sign2) =
   215   fun merge pp (sign1, sign2) =
   224     let
   216     let
   225       val Sign {naming = _, syn = syn1, tsig = tsig1, consts = (consts1, constraints1)} = sign1;
   217       val Sign {naming = _, syn = syn1, tsig = tsig1, consts = consts1} = sign1;
   226       val Sign {naming = _, syn = syn2, tsig = tsig2, consts = (consts2, constraints2)} = sign2;
   218       val Sign {naming = _, syn = syn2, tsig = tsig2, consts = consts2} = sign2;
   227 
   219 
   228       val naming = NameSpace.default_naming;
   220       val naming = NameSpace.default_naming;
   229       val syn = Syntax.merge_syntaxes syn1 syn2;
   221       val syn = Syntax.merge_syntaxes syn1 syn2;
   230       val tsig = Type.merge_tsigs pp (tsig1, tsig2);
   222       val tsig = Type.merge_tsigs pp (tsig1, tsig2);
   231       val consts = NameSpace.merge_tables (eq_snd (op =)) (consts1, consts2)
   223       val consts = Consts.merge (consts1, consts2);
   232         handle Symtab.DUPS cs => err_dup_consts cs;
   224     in make_sign (naming, syn, tsig, consts) end;
   233       val constraints = Symtab.merge (op =) (constraints1, constraints2)
       
   234         handle Symtab.DUPS cs => err_inconsistent_constraints cs;
       
   235     in make_sign (naming, syn, tsig, (consts, constraints)) end;
       
   236 
   225 
   237   fun print _ _ = ();
   226   fun print _ _ = ();
   238 end);
   227 end);
   239 
   228 
   240 val init_data = SignData.init;
   229 val init_data = SignData.init;
   280 val typ_match = Type.typ_match o tsig_of;
   269 val typ_match = Type.typ_match o tsig_of;
   281 val typ_unify = Type.unify o tsig_of;
   270 val typ_unify = Type.unify o tsig_of;
   282 fun is_logtype thy c = c mem_string Type.logical_types (tsig_of thy);
   271 fun is_logtype thy c = c mem_string Type.logical_types (tsig_of thy);
   283 
   272 
   284 
   273 
   285 (* consts signature *)
   274 (* polymorphic constants *)
   286 
   275 
   287 fun const_constraint thy c =
   276 val consts_of = #consts o rep_sg;
   288   let val ((_, consts), constraints) = #consts (rep_sg thy) in
   277 val the_const_constraint = Consts.constraint o consts_of;
   289     (case Symtab.lookup constraints c of
   278 val const_constraint = try o the_const_constraint;
   290       NONE => Option.map (#1 o #1) (Symtab.lookup consts c)
   279 val the_const_type = Consts.declaration o consts_of;
   291     | some => some)
   280 val const_type = try o the_const_type;
   292   end;
   281 val const_monomorphic = Consts.monomorphic o consts_of;
   293 
   282 val const_typargs = Consts.typargs o consts_of;
   294 fun the_const_constraint thy c =
       
   295   (case const_constraint thy c of SOME T => T
       
   296   | NONE => raise TYPE ("Undeclared constant " ^ quote c, [], []));
       
   297 
       
   298 val lookup_const = Symtab.lookup o #2 o #1 o #consts o rep_sg;
       
   299 val const_type = Option.map (#1 o #1) oo lookup_const;
       
   300 
       
   301 fun the_const_type thy c =
       
   302   (case const_type thy c of SOME T => T
       
   303   | NONE => raise TYPE ("Undeclared constant " ^ quote c, [], []));
       
   304 
   283 
   305 val declared_tyname = Symtab.defined o #2 o #types o Type.rep_tsig o tsig_of;
   284 val declared_tyname = Symtab.defined o #2 o #types o Type.rep_tsig o tsig_of;
   306 val declared_const = is_some oo const_type;
   285 val declared_const = is_some oo const_type;
   307 
   286 
   308 fun monomorphic thy =
       
   309   let val mono = Symtab.lookup (#2 (#1 (#consts (rep_sg thy))))
       
   310   in fn c => (case mono c of SOME ((_, m), _) => m | _ => false) end;
       
   311 
       
   312 
   287 
   313 
   288 
   314 (** intern / extern names **)
   289 (** intern / extern names **)
   315 
   290 
   316 val class_space = #1 o #classes o Type.rep_tsig o tsig_of;
   291 val class_space = #1 o #classes o Type.rep_tsig o tsig_of;
   317 val type_space = #1 o #types o Type.rep_tsig o tsig_of;
   292 val type_space = #1 o #types o Type.rep_tsig o tsig_of;
   318 val const_space = #1 o #1 o #consts o rep_sg
   293 val const_space = Consts.space o consts_of;
   319 
   294 
   320 val intern_class = NameSpace.intern o class_space;
   295 val intern_class = NameSpace.intern o class_space;
   321 val extern_class = NameSpace.extern o class_space;
   296 val extern_class = NameSpace.extern o class_space;
   322 val intern_type = NameSpace.intern o type_space;
   297 val intern_type = NameSpace.intern o type_space;
   323 val extern_type = NameSpace.extern o type_space;
   298 val extern_type = NameSpace.extern o type_space;
   451     fun show_const a T = quote a ^ " :: " ^ Pretty.string_of_typ pp T;
   426     fun show_const a T = quote a ^ " :: " ^ Pretty.string_of_typ pp T;
   452 
   427 
   453     fun check_atoms (t $ u) = (check_atoms t; check_atoms u)
   428     fun check_atoms (t $ u) = (check_atoms t; check_atoms u)
   454       | check_atoms (Abs (_, _, t)) = check_atoms t
   429       | check_atoms (Abs (_, _, t)) = check_atoms t
   455       | check_atoms (Const (a, T)) =
   430       | check_atoms (Const (a, T)) =
   456           (case lookup_const thy a of
   431           (case const_type thy a of
   457             NONE => err ("Undeclared constant " ^ show_const a T)
   432             NONE => err ("Undeclared constant " ^ show_const a T)
   458           | SOME ((U, mono), _) =>
   433           | SOME U =>
   459               if mono andalso T = U orelse typ_instance thy (T, U) then ()
   434               if typ_instance thy (T, U) then ()
   460               else err ("Illegal type for constant " ^ show_const a T))
   435               else err ("Illegal type for constant " ^ show_const a T))
   461       | check_atoms (Var ((x, i), _)) =
   436       | check_atoms (Var ((x, i), _)) =
   462           if i < 0 then err ("Malformed variable: " ^ quote x) else ()
   437           if i < 0 then err ("Malformed variable: " ^ quote x) else ()
   463       | check_atoms _ = ();
   438       | check_atoms _ = ();
   464   in
   439   in
   694 
   669 
   695 (* add constants *)
   670 (* add constants *)
   696 
   671 
   697 local
   672 local
   698 
   673 
   699 fun is_mono (Type (_, Ts)) = forall is_mono Ts
       
   700   | is_mono _ = false;
       
   701 
       
   702 fun gen_add_consts prep_typ raw_args thy =
   674 fun gen_add_consts prep_typ raw_args thy =
   703   let
   675   let
   704     val prepT = Compress.typ thy o Type.varifyT o Type.no_tvars o Term.no_dummyT o prep_typ thy;
   676     val prepT = Compress.typ thy o Type.varifyT o Type.no_tvars o Term.no_dummyT o prep_typ thy;
   705     fun prep (c, T, mx) = ((c, prepT T, mx) handle TYPE (msg, _, _) => error msg)
   677     fun prep (c, T, mx) = ((c, prepT T, mx) handle TYPE (msg, _, _) => error msg)
   706       handle ERROR => error ("in declaration of constant " ^ quote (Syntax.const_name c mx));
   678       handle ERROR => error ("in declaration of constant " ^ quote (Syntax.const_name c mx));
   707     val args = map prep raw_args;
   679     val args = map prep raw_args;
   708 
   680     val decls = args |> map (fn (c, T, mx) => (Syntax.const_name c mx, T));
   709     val decls = args |> map (fn (c, T, mx) =>
       
   710       (Syntax.const_name c mx, ((T, is_mono T), serial ())));
       
   711 
       
   712     fun extend_consts consts = NameSpace.extend_table (naming_of thy) (consts, decls)
       
   713       handle Symtab.DUPS cs => err_dup_consts cs;
       
   714   in
   681   in
   715     thy
   682     thy
   716     |> map_consts (apfst extend_consts)
   683     |> map_consts (fold (Consts.declare (naming_of thy)) decls)
   717     |> add_syntax_i args
   684     |> add_syntax_i args
   718   end;
   685   end;
   719 
   686 
   720 in
   687 in
   721 
   688 
   732     val c = int_const thy raw_c;
   699     val c = int_const thy raw_c;
   733     val T = Term.zero_var_indexesT (Term.no_dummyT (prep_typ thy raw_T))
   700     val T = Term.zero_var_indexesT (Term.no_dummyT (prep_typ thy raw_T))
   734       handle TYPE (msg, _, _) => error msg;
   701       handle TYPE (msg, _, _) => error msg;
   735     val _ = cert_term thy (Const (c, T))
   702     val _ = cert_term thy (Const (c, T))
   736       handle TYPE (msg, _, _) => error msg;
   703       handle TYPE (msg, _, _) => error msg;
   737   in thy |> map_consts (apsnd (Symtab.update (c, T))) end;
   704   in thy |> map_consts (Consts.constrain (c, T)) end;
   738 
   705 
   739 val add_const_constraint = gen_add_constraint intern_const (read_typ o no_def_sort);
   706 val add_const_constraint = gen_add_constraint intern_const (read_typ o no_def_sort);
   740 val add_const_constraint_i = gen_add_constraint (K I) certify_typ;
   707 val add_const_constraint_i = gen_add_constraint (K I) certify_typ;
   741 
   708 
   742 
   709 
   870 
   837 
   871 fun hide_classes b xs thy = thy |> map_tsig (Type.hide_classes b (map (intern_class thy) xs));
   838 fun hide_classes b xs thy = thy |> map_tsig (Type.hide_classes b (map (intern_class thy) xs));
   872 val hide_classes_i = map_tsig oo Type.hide_classes;
   839 val hide_classes_i = map_tsig oo Type.hide_classes;
   873 fun hide_types b xs thy = thy |> map_tsig (Type.hide_types b (map (intern_type thy) xs));
   840 fun hide_types b xs thy = thy |> map_tsig (Type.hide_types b (map (intern_type thy) xs));
   874 val hide_types_i = map_tsig oo Type.hide_types;
   841 val hide_types_i = map_tsig oo Type.hide_types;
   875 fun hide_consts b xs thy =
   842 fun hide_consts b xs thy = thy |> map_consts (fold (Consts.hide b o intern_const thy) xs);
   876   thy |> map_consts (apfst (apfst (fold (NameSpace.hide b o intern_const thy) xs)));
   843 val hide_consts_i = map_consts oo (fold o Consts.hide);
   877 val hide_consts_i = (map_consts o apfst o apfst) oo (fold o NameSpace.hide);
       
   878 
   844 
   879 local
   845 local
   880 
   846 
   881 val kinds =
   847 val kinds =
   882  [("class", (intern_class, can o certify_class, hide_classes_i)),
   848  [("class", (intern_class, can o certify_class, hide_classes_i)),