src/Pure/sign.ML
author wenzelm
Sat, 11 Jun 2005 22:15:53 +0200
changeset 16368 a06868ebeb0f
parent 16354 6f0ca9628840
child 16442 1171ecf7fb7e
permissions -rw-r--r--
discontinued named name spaces (classK, typeK, constK); name space of classes and types maintained in tsig; read_tyname/read_const now raise ERROR instead of TYPE; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19
929ad32d63fc Pure/ROOT.ML
wenzelm
parents: 0
diff changeset
     1
(*  Title:      Pure/sign.ML
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
251
c9b984c0a674 major cleanup;
wenzelm
parents: 229
diff changeset
     3
    Author:     Lawrence C Paulson and Markus Wenzel
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
251
c9b984c0a674 major cleanup;
wenzelm
parents: 229
diff changeset
     5
The abstract type "sg" of signatures.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
19
929ad32d63fc Pure/ROOT.ML
wenzelm
parents: 0
diff changeset
     8
signature SIGN =
3791
c5db2c87a646 now supports qualified names (intern vs. extern) !!!
wenzelm
parents: 3552
diff changeset
     9
sig
1501
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
    10
  type sg
3967
edd5ff9371f8 sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents: 3956
diff changeset
    11
  type sg_ref
4256
e768c42069bb removed data.ML (made part of sign.ML);
wenzelm
parents: 4249
diff changeset
    12
  type data
2197
e895937fcd56 added add_modesyntax(_i);
wenzelm
parents: 2187
diff changeset
    13
  val rep_sg: sg ->
3975
ddeb5a0fd08d hide id, stamps;
wenzelm
parents: 3969
diff changeset
    14
   {self: sg_ref,
14784
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
    15
    tsig: Type.tsig,
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
    16
    consts: (typ * stamp) NameSpace.table,
16133
cd0f1ea21abf renamed cond_extern to extern;
wenzelm
parents: 15889
diff changeset
    17
    naming: NameSpace.naming,
4256
e768c42069bb removed data.ML (made part of sign.ML);
wenzelm
parents: 4249
diff changeset
    18
    data: data}
3975
ddeb5a0fd08d hide id, stamps;
wenzelm
parents: 3969
diff changeset
    19
  val stamp_names_of: sg -> string list
10932
ad13abb0a264 added exists_stamp;
wenzelm
parents: 10443
diff changeset
    20
  val exists_stamp: string -> sg -> bool
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    21
  val pretty_sg: sg -> Pretty.T
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    22
  val pprint_sg: sg -> pprint_args -> unit
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    23
  val pretty_abbrev_sg: sg -> Pretty.T
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    24
  val str_of_sg: sg -> string
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    25
  val is_stale: sg -> bool
3967
edd5ff9371f8 sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents: 3956
diff changeset
    26
  val self_ref: sg -> sg_ref
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    27
  val deref: sg_ref -> sg
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    28
  val name_of: sg -> string
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    29
  val is_draft: sg -> bool
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    30
  val eq_sg: sg * sg -> bool
1501
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
    31
  val subsig: sg * sg -> bool
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
    32
  val same_sg: sg * sg -> bool
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    33
  val joinable: sg * sg -> bool
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    34
  val prep_ext: sg -> sg
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    35
  val copy: sg -> sg
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    36
  val add_name: string -> sg -> sg
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    37
  val data_kinds: data -> string list
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    38
  val print_all_data: sg -> unit
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    39
  val pre_pure: sg
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    40
  val PureN: string
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    41
  val CPureN: string
16288
df2b550a17f6 added the_const_type;
wenzelm
parents: 16189
diff changeset
    42
  val naming_of: sg -> NameSpace.naming
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    43
  val base_name: string -> bstring
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    44
  val full_name: sg -> bstring -> string
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    45
  val full_name_path: sg -> string -> bstring -> string
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    46
  val declare_name: sg -> string -> NameSpace.T -> NameSpace.T
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    47
  val syn_of: sg -> Syntax.syntax
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    48
  val tsig_of: sg -> Type.tsig
1501
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
    49
  val classes: sg -> class list
4844
4fb63c77f2df added defaultS: sg -> sort;
wenzelm
parents: 4627
diff changeset
    50
  val defaultS: sg -> sort
1501
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
    51
  val subsort: sg -> sort * sort -> bool
4568
7be03035c553 added of_sort;
wenzelm
parents: 4489
diff changeset
    52
  val of_sort: sg -> typ * sort -> bool
7640
6b7daae5d316 added witness_sorts, univ_witness;
wenzelm
parents: 7068
diff changeset
    53
  val witness_sorts: sg -> sort list -> sort list -> (typ * sort) list
14784
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
    54
  val universal_witness: sg -> (typ * sort) option
10443
0a68dc9edba5 added certify_tycon, certify_tyabbr, certify_const;
wenzelm
parents: 10404
diff changeset
    55
  val typ_instance: sg -> typ * typ -> bool
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    56
  val is_logtype: sg -> string -> bool
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    57
  val const_type: sg -> string -> typ option
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    58
  val the_const_type: sg -> string -> typ
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    59
  val declared_tyname: sg -> string -> bool
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    60
  val declared_const: sg -> string -> bool
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
    61
  val class_space: sg -> NameSpace.T
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
    62
  val type_space: sg -> NameSpace.T
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
    63
  val const_space: sg -> NameSpace.T
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    64
  val intern_class: sg -> xstring -> string
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    65
  val extern_class: sg -> string -> xstring
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
    66
  val intern_type: sg -> xstring -> string
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
    67
  val extern_type: sg -> string -> xstring
3805
a50d0b5219dd improved types of add_XXX funs (xtyp etc.);
wenzelm
parents: 3791
diff changeset
    68
  val intern_const: sg -> xstring -> string
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    69
  val extern_const: sg -> string -> xstring
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    70
  val intern_sort: sg -> sort -> sort
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    71
  val extern_sort: sg -> sort -> sort
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    72
  val intern_typ: sg -> typ -> typ
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    73
  val extern_typ: sg -> typ -> typ
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    74
  val intern_term: sg -> term -> term
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    75
  val extern_term: sg -> term -> term
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    76
  val intern_tycons: sg -> typ -> typ
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    77
  val pretty_term': Syntax.syntax -> sg -> term -> Pretty.T
1501
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
    78
  val pretty_term: sg -> term -> Pretty.T
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
    79
  val pretty_typ: sg -> typ -> Pretty.T
3791
c5db2c87a646 now supports qualified names (intern vs. extern) !!!
wenzelm
parents: 3552
diff changeset
    80
  val pretty_sort: sg -> sort -> Pretty.T
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
    81
  val pretty_classrel: sg -> class list -> Pretty.T
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
    82
  val pretty_arity: sg -> arity -> Pretty.T
1501
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
    83
  val string_of_term: sg -> term -> string
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
    84
  val string_of_typ: sg -> typ -> string
3791
c5db2c87a646 now supports qualified names (intern vs. extern) !!!
wenzelm
parents: 3552
diff changeset
    85
  val string_of_sort: sg -> sort -> string
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
    86
  val string_of_classrel: sg -> class list -> string
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
    87
  val string_of_arity: sg -> arity -> string
1501
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
    88
  val pprint_term: sg -> term -> pprint_args -> unit
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
    89
  val pprint_typ: sg -> typ -> pprint_args -> unit
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
    90
  val pp: sg -> Pretty.pp
8898
a1ee54500516 removed is_type_abbr;
wenzelm
parents: 8802
diff changeset
    91
  val certify_class: sg -> class -> class
a1ee54500516 removed is_type_abbr;
wenzelm
parents: 8802
diff changeset
    92
  val certify_sort: sg -> sort -> sort
1501
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
    93
  val certify_typ: sg -> typ -> typ
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    94
  val certify_typ_syntax: sg -> typ -> typ
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    95
  val certify_typ_abbrev: sg -> typ -> typ
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
    96
  val certify_term: Pretty.pp -> sg -> term -> term * typ * int
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    97
  val read_sort': Syntax.syntax -> sg -> string -> sort
8898
a1ee54500516 removed is_type_abbr;
wenzelm
parents: 8802
diff changeset
    98
  val read_sort: sg -> string -> sort
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
    99
  val read_typ': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   100
  val read_typ_syntax': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   101
  val read_typ_abbrev': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
1501
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
   102
  val read_typ: sg * (indexname -> sort option) -> string -> typ
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   103
  val read_typ_syntax: sg * (indexname -> sort option) -> string -> typ
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   104
  val read_typ_abbrev: sg * (indexname -> sort option) -> string -> typ
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   105
  val read_tyname: sg -> string -> typ
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   106
  val read_const: sg -> string -> term
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   107
  val infer_types_simult: Pretty.pp -> sg -> (indexname -> typ option) ->
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   108
    (indexname -> sort option) -> string list -> bool
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   109
    -> (term list * typ) list -> term list * (indexname * typ) list
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   110
  val infer_types: Pretty.pp -> sg -> (indexname -> typ option) ->
1501
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
   111
    (indexname -> sort option) -> string list -> bool
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   112
    -> term list * typ -> term * (indexname * typ) list
14905
5f3fc2f62071 added is_logtype (replaces logtypes field of syntax); tuned merge;
wenzelm
parents: 14856
diff changeset
   113
  val read_def_terms': Pretty.pp -> (string -> bool) -> Syntax.syntax ->
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   114
    sg * (indexname -> typ option) * (indexname -> sort option) ->
12068
469f372d63db added pretty_term', read_typ', read_typ_no_norm', read_def_terms'
wenzelm
parents: 11720
diff changeset
   115
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
8607
bf129c6505de read_def_terms (no certify yet);
wenzelm
parents: 8290
diff changeset
   116
  val read_def_terms:
bf129c6505de read_def_terms (no certify yet);
wenzelm
parents: 8290
diff changeset
   117
    sg * (indexname -> typ option) * (indexname -> sort option) ->
bf129c6505de read_def_terms (no certify yet);
wenzelm
parents: 8290
diff changeset
   118
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
8802
2c37263eb903 added simple_read_term;
wenzelm
parents: 8730
diff changeset
   119
  val simple_read_term: sg -> typ -> string -> term
8898
a1ee54500516 removed is_type_abbr;
wenzelm
parents: 8802
diff changeset
   120
  val add_defsort: string -> sg -> sg
3791
c5db2c87a646 now supports qualified names (intern vs. extern) !!!
wenzelm
parents: 3552
diff changeset
   121
  val add_defsort_i: sort -> sg -> sg
3956
d59fe4579004 tuned types;
wenzelm
parents: 3937
diff changeset
   122
  val add_types: (bstring * int * mixfix) list -> sg -> sg
4844
4fb63c77f2df added defaultS: sg -> sort;
wenzelm
parents: 4627
diff changeset
   123
  val add_nonterminals: bstring list -> sg -> sg
3956
d59fe4579004 tuned types;
wenzelm
parents: 3937
diff changeset
   124
  val add_tyabbrs: (bstring * string list * string * mixfix) list -> sg -> sg
d59fe4579004 tuned types;
wenzelm
parents: 3937
diff changeset
   125
  val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> sg -> sg
8898
a1ee54500516 removed is_type_abbr;
wenzelm
parents: 8802
diff changeset
   126
  val add_arities: (xstring * string list * string) list -> sg -> sg
3791
c5db2c87a646 now supports qualified names (intern vs. extern) !!!
wenzelm
parents: 3552
diff changeset
   127
  val add_arities_i: (string * sort list * sort) list -> sg -> sg
3956
d59fe4579004 tuned types;
wenzelm
parents: 3937
diff changeset
   128
  val add_syntax: (bstring * string * mixfix) list -> sg -> sg
d59fe4579004 tuned types;
wenzelm
parents: 3937
diff changeset
   129
  val add_syntax_i: (bstring * typ * mixfix) list -> sg -> sg
d59fe4579004 tuned types;
wenzelm
parents: 3937
diff changeset
   130
  val add_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg
d59fe4579004 tuned types;
wenzelm
parents: 3937
diff changeset
   131
  val add_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg
15746
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   132
  val del_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   133
  val del_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   134
  val add_consts: (bstring * string * mixfix) list -> sg -> sg
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   135
  val add_consts_i: (bstring * typ * mixfix) list -> sg -> sg
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   136
  val const_of_class: class -> string
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   137
  val class_of_const: string -> class
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   138
  val add_classes: (bstring * xstring list) list -> sg -> sg
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   139
  val add_classes_i: (bstring * class list) list -> sg -> sg
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   140
  val add_classrel: (xstring * xstring) list -> sg -> sg
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   141
  val add_classrel_i: (class * class) list -> sg -> sg
1501
bb7f99a0a6f0 Elimination of fully-functorial style.
paulson
parents: 1494
diff changeset
   142
  val add_trfuns:
4344
e000b5db4087 tuned trfuns types;
wenzelm
parents: 4261
diff changeset
   143
    (string * (ast list -> ast)) list *
e000b5db4087 tuned trfuns types;
wenzelm
parents: 4261
diff changeset
   144
    (string * (term list -> term)) list *
e000b5db4087 tuned trfuns types;
wenzelm
parents: 4261
diff changeset
   145
    (string * (term list -> term)) list *
e000b5db4087 tuned trfuns types;
wenzelm
parents: 4261
diff changeset
   146
    (string * (ast list -> ast)) list -> sg -> sg
2385
73d1435aa729 added typed print translations;
wenzelm
parents: 2359
diff changeset
   147
  val add_trfunsT:
4344
e000b5db4087 tuned trfuns types;
wenzelm
parents: 4261
diff changeset
   148
    (string * (bool -> typ -> term list -> term)) list -> sg -> sg
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   149
  val add_advanced_trfuns:
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   150
    (string * (sg -> ast list -> ast)) list *
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   151
    (string * (sg -> term list -> term)) list *
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   152
    (string * (sg -> term list -> term)) list *
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   153
    (string * (sg -> ast list -> ast)) list -> sg -> sg
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   154
  val add_advanced_trfunsT:
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   155
    (string * (sg -> bool -> typ -> term list -> term)) list -> sg -> sg
2693
8300bba275e3 added add_tokentrfuns;
wenzelm
parents: 2672
diff changeset
   156
  val add_tokentrfuns:
6311
15652e058e28 token translation: real;
wenzelm
parents: 6191
diff changeset
   157
    (string * string * (string -> string * real)) list -> sg -> sg
4619
72edc2a9200f fixed add_trrules: intern root;
wenzelm
parents: 4568
diff changeset
   158
  val add_trrules: (xstring * string) Syntax.trrule list -> sg -> sg
1810
0eef167ebe1b Translation infixes <->, etc., no longer available at top-level
paulson
parents: 1580
diff changeset
   159
  val add_trrules_i: ast Syntax.trrule list -> sg -> sg
3791
c5db2c87a646 now supports qualified names (intern vs. extern) !!!
wenzelm
parents: 3552
diff changeset
   160
  val add_path: string -> sg -> sg
16133
cd0f1ea21abf renamed cond_extern to extern;
wenzelm
parents: 15889
diff changeset
   161
  val qualified_names: sg -> sg
cd0f1ea21abf renamed cond_extern to extern;
wenzelm
parents: 15889
diff changeset
   162
  val no_base_names: sg -> sg
cd0f1ea21abf renamed cond_extern to extern;
wenzelm
parents: 15889
diff changeset
   163
  val custom_accesses: (string list -> string list list) -> sg -> sg
cd0f1ea21abf renamed cond_extern to extern;
wenzelm
parents: 15889
diff changeset
   164
  val set_policy: (string -> bstring -> string) * (string list -> string list list) -> sg -> sg
16189
74dc76a28038 replaced set_naming by restore_naming;
wenzelm
parents: 16133
diff changeset
   165
  val restore_naming: sg -> sg -> sg
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   166
  val hide_classes: bool -> xstring list -> sg -> sg
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   167
  val hide_classes_i: bool -> string list -> sg -> sg
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   168
  val hide_types: bool -> xstring list -> sg -> sg
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   169
  val hide_types_i: bool -> string list -> sg -> sg
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   170
  val hide_consts: bool -> xstring list -> sg -> sg
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   171
  val hide_consts_i: bool -> string list -> sg -> sg
3967
edd5ff9371f8 sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents: 3956
diff changeset
   172
  val merge_refs: sg_ref * sg_ref -> sg_ref
4627
ae95666c71cc Sign.merge vs. Sign.nontriv_merge;
wenzelm
parents: 4619
diff changeset
   173
  val merge: sg * sg -> sg
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
   174
  val prep_ext_merge: sg list -> sg
3791
c5db2c87a646 now supports qualified names (intern vs. extern) !!!
wenzelm
parents: 3552
diff changeset
   175
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   176
6191
381b27ca0543 renamed sig to PRIVATE_SIGN;
wenzelm
parents: 6040
diff changeset
   177
signature PRIVATE_SIGN =
5642
1b3e48bdbb93 PRIVATE sig parts;
wenzelm
parents: 5635
diff changeset
   178
sig
1b3e48bdbb93 PRIVATE sig parts;
wenzelm
parents: 5635
diff changeset
   179
  include SIGN
6546
995a66249a9b theory data: copy;
wenzelm
parents: 6311
diff changeset
   180
  val init_data: Object.kind * (Object.T * (Object.T -> Object.T) * (Object.T -> Object.T) *
12310
26407b087c8e data: removed obsolete finish method;
wenzelm
parents: 12296
diff changeset
   181
    (Object.T * Object.T -> Object.T) * (sg -> Object.T -> unit)) -> sg -> sg
5642
1b3e48bdbb93 PRIVATE sig parts;
wenzelm
parents: 5635
diff changeset
   182
  val get_data: Object.kind -> (Object.T -> 'a) -> sg -> 'a
1b3e48bdbb93 PRIVATE sig parts;
wenzelm
parents: 5635
diff changeset
   183
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> sg -> sg
1b3e48bdbb93 PRIVATE sig parts;
wenzelm
parents: 5635
diff changeset
   184
  val print_data: Object.kind -> sg -> unit
1b3e48bdbb93 PRIVATE sig parts;
wenzelm
parents: 5635
diff changeset
   185
end;
1b3e48bdbb93 PRIVATE sig parts;
wenzelm
parents: 5635
diff changeset
   186
6191
381b27ca0543 renamed sig to PRIVATE_SIGN;
wenzelm
parents: 6040
diff changeset
   187
structure Sign: PRIVATE_SIGN =
143
f8152ca36cd5 Sign.extend: Syntax.extend now called with read_ty;
wenzelm
parents: 19
diff changeset
   188
struct
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   189
251
c9b984c0a674 major cleanup;
wenzelm
parents: 229
diff changeset
   190
(** datatype sg **)
c9b984c0a674 major cleanup;
wenzelm
parents: 229
diff changeset
   191
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   192
(* types sg, data, syn, sg_ref *)
4256
e768c42069bb removed data.ML (made part of sign.ML);
wenzelm
parents: 4249
diff changeset
   193
19
929ad32d63fc Pure/ROOT.ML
wenzelm
parents: 0
diff changeset
   194
datatype sg =
3975
ddeb5a0fd08d hide id, stamps;
wenzelm
parents: 3969
diff changeset
   195
  Sg of
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   196
   {id: string ref,
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   197
    stamps: string ref list,                  (*unique theory indentifier*)
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   198
    syn: syn} *                               (*syntax for parsing and printing*)
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   199
   {self: sg_ref,                             (*mutable self reference*)
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   200
    tsig: Type.tsig,                          (*order-sorted signature of types*)
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   201
    consts: (typ * stamp) NameSpace.table,    (*type schemes of constants*)
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   202
    naming: NameSpace.naming,
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   203
    data: data}
4256
e768c42069bb removed data.ML (made part of sign.ML);
wenzelm
parents: 4249
diff changeset
   204
and data =
e768c42069bb removed data.ML (made part of sign.ML);
wenzelm
parents: 4249
diff changeset
   205
  Data of
16133
cd0f1ea21abf renamed cond_extern to extern;
wenzelm
parents: 15889
diff changeset
   206
    (Object.kind *                                (*kind (for authorization)*)
cd0f1ea21abf renamed cond_extern to extern;
wenzelm
parents: 15889
diff changeset
   207
      (Object.T *                                 (*value*)
cd0f1ea21abf renamed cond_extern to extern;
wenzelm
parents: 15889
diff changeset
   208
        ((Object.T -> Object.T) *                 (*copy method*)
cd0f1ea21abf renamed cond_extern to extern;
wenzelm
parents: 15889
diff changeset
   209
          (Object.T -> Object.T) *                (*prepare extend method*)
cd0f1ea21abf renamed cond_extern to extern;
wenzelm
parents: 15889
diff changeset
   210
          (Object.T * Object.T -> Object.T) *     (*merge and prepare extend method*)
cd0f1ea21abf renamed cond_extern to extern;
wenzelm
parents: 15889
diff changeset
   211
          (sg -> Object.T -> unit))))             (*print method*)
4256
e768c42069bb removed data.ML (made part of sign.ML);
wenzelm
parents: 4249
diff changeset
   212
    Symtab.table
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   213
and syn =
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   214
  Syn of
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   215
    Syntax.syntax *
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   216
     (((sg -> ast list -> ast) * stamp) Symtab.table *
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   217
      ((sg -> term list -> term) * stamp) Symtab.table *
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   218
      ((sg -> bool -> typ -> term list -> term) * stamp) list Symtab.table *
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   219
      ((sg -> ast list -> ast) * stamp) list Symtab.table)
3967
edd5ff9371f8 sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents: 3956
diff changeset
   220
and sg_ref =
4998
28fe46a570d7 improved data: secure version using Object.T and Object.kind;
wenzelm
parents: 4961
diff changeset
   221
  SgRef of sg ref option;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   222
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   223
(* FIXME assign!??? *)
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   224
fun make_sg (id, self, stamps) naming data (syn, tsig, consts) =
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   225
  Sg ({id = id, stamps = stamps, syn = syn}, {self = self, tsig = tsig,
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   226
    consts = consts, naming = naming, data = data});
3975
ddeb5a0fd08d hide id, stamps;
wenzelm
parents: 3969
diff changeset
   227
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   228
fun rep_sg (Sg (_, args)) = args;
3975
ddeb5a0fd08d hide id, stamps;
wenzelm
parents: 3969
diff changeset
   229
ddeb5a0fd08d hide id, stamps;
wenzelm
parents: 3969
diff changeset
   230
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   231
(* stamps *)
3967
edd5ff9371f8 sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents: 3956
diff changeset
   232
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15797
diff changeset
   233
fun stamps_of (Sg ({stamps, ...}, _)) = stamps;
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15797
diff changeset
   234
val stamp_names_of = rev o map ! o stamps_of;
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15797
diff changeset
   235
fun exists_stamp name = exists (equal name o !) o stamps_of;
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15797
diff changeset
   236
16354
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   237
fun is_stale (Sg ({id, ...}, {self = SgRef (SOME (ref (Sg ({id = id', ...}, _)))), ...})) =
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   238
      id <> id'
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   239
  | is_stale _ = false;
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   240
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   241
fun pretty_sg sg =
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   242
  Pretty.str_list "{" "}" (stamp_names_of sg @ (if is_stale sg then ["!"] else []));
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   243
3975
ddeb5a0fd08d hide id, stamps;
wenzelm
parents: 3969
diff changeset
   244
val pprint_sg = Pretty.pprint o pretty_sg;
ddeb5a0fd08d hide id, stamps;
wenzelm
parents: 3969
diff changeset
   245
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   246
fun pretty_abbrev_sg sg =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   247
  let
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   248
    val stamps = map ! (stamps_of sg);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   249
    val abbrev = if length stamps > 5 then "..." :: rev (List.take (stamps, 5)) else rev stamps;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   250
  in Pretty.str_list "{" "}" abbrev end;
16288
df2b550a17f6 added the_const_type;
wenzelm
parents: 16189
diff changeset
   251
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   252
val str_of_sg = Pretty.str_of o pretty_abbrev_sg;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
   253
3967
edd5ff9371f8 sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents: 3956
diff changeset
   254
3995
88fc1015d241 self_ref: check_stale;
wenzelm
parents: 3975
diff changeset
   255
(* id and self *)
3967
edd5ff9371f8 sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents: 3956
diff changeset
   256
16354
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   257
fun check_stale pos sg =
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   258
  if is_stale sg then raise TERM ("Stale signature (in " ^ pos ^ "): " ^ str_of_sg sg, [])
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   259
  else sg;
4951
8637b29e6c38 added is_stale;
wenzelm
parents: 4908
diff changeset
   260
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   261
fun self_ref (sg as Sg (_, {self, ...})) = (check_stale "Sign.self_ref" sg; self);
3995
88fc1015d241 self_ref: check_stale;
wenzelm
parents: 3975
diff changeset
   262
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14993
diff changeset
   263
fun deref (SgRef (SOME (ref sg))) = sg
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14993
diff changeset
   264
  | deref (SgRef NONE) = sys_error "Sign.deref";
3995
88fc1015d241 self_ref: check_stale;
wenzelm
parents: 3975
diff changeset
   265
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   266
fun assign (SgRef (SOME r)) sg = r := sg
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   267
  | assign (SgRef NONE) _ = sys_error "Sign.assign";
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   268
3975
ddeb5a0fd08d hide id, stamps;
wenzelm
parents: 3969
diff changeset
   269
fun name_of (sg as Sg ({id = ref name, ...}, _)) =
4844
4fb63c77f2df added defaultS: sg -> sort;
wenzelm
parents: 4627
diff changeset
   270
  if name = "" orelse ord name = ord "#" then
3975
ddeb5a0fd08d hide id, stamps;
wenzelm
parents: 3969
diff changeset
   271
    raise TERM ("Nameless signature " ^ str_of_sg sg, [])
ddeb5a0fd08d hide id, stamps;
wenzelm
parents: 3969
diff changeset
   272
  else name;
ddeb5a0fd08d hide id, stamps;
wenzelm
parents: 3969
diff changeset
   273
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   274
fun is_draft (Sg ({stamps = ref name :: _, ...}, _)) =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   275
      name = "" orelse ord name = ord "#"
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   276
  | is_draft _ = sys_error "Sign.is_draft";
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   277
206
0d624d1ba9cc added subsig: sg * sg -> bool to test if one signature is contained in another.
nipkow
parents: 200
diff changeset
   278
2979
db6941221197 improved type check error messages;
wenzelm
parents: 2963
diff changeset
   279
(* inclusion and equality *)
2180
934572a94139 Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents: 2144
diff changeset
   280
2185
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   281
local
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   282
  (*avoiding polymorphic equality: factor 10 speedup*)
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   283
  fun mem_stamp (_: string ref, []) = false
2185
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   284
    | mem_stamp (x, y :: ys) = x = y orelse mem_stamp (x, ys);
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   285
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   286
  fun subset_stamp ([], ys) = true
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   287
    | subset_stamp (x :: xs, ys) =
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   288
        mem_stamp (x, ys) andalso subset_stamp (xs, ys);
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   289
in
3975
ddeb5a0fd08d hide id, stamps;
wenzelm
parents: 3969
diff changeset
   290
  fun eq_sg (sg1 as Sg ({id = id1, ...}, _), sg2 as Sg ({id = id2, ...}, _)) =
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   291
    (check_stale "Sign.eq_sg" sg1; check_stale "Sign.eq_sg" sg2; id1 = id2);
2185
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   292
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15797
diff changeset
   293
  fun subsig (sg1, sg2) =
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15797
diff changeset
   294
    eq_sg (sg1, sg2) orelse mem_stamp (hd (stamps_of sg1), stamps_of sg2);
2180
934572a94139 Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents: 2144
diff changeset
   295
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15797
diff changeset
   296
  fun subsig_internal (sg1, sg2) =
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15797
diff changeset
   297
    eq_sg (sg1, sg2) orelse subset_stamp (stamps_of sg1, stamps_of sg2);
2185
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   298
end;
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   299
402
16a8fe4f2250 added subsort, norm_sort, classes;
wenzelm
parents: 386
diff changeset
   300
9031
8f75b9ce2f06 fixed Thm.eq_thm: use Sign.joinable;
wenzelm
parents: 8927
diff changeset
   301
fun joinable (sg1, sg2) = subsig (sg1, sg2) orelse subsig (sg2, sg1);
8f75b9ce2f06 fixed Thm.eq_thm: use Sign.joinable;
wenzelm
parents: 8927
diff changeset
   302
2185
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   303
(*test if same theory names are contained in signatures' stamps,
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   304
  i.e. if signatures belong to same theory but not necessarily to the
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   305
  same version of it*)
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15797
diff changeset
   306
fun same_sg (sg1, sg2) =
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15797
diff changeset
   307
  eq_sg (sg1, sg2) orelse eq_set_string (pairself (map ! o stamps_of) (sg1, sg2));
2185
f9686e7e6d4d subsig tuning;
wenzelm
parents: 2184
diff changeset
   308
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   309
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   310
(* data operations *)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   311
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   312
fun err_method name kind e =    (* FIXME wrap exn msg!? *)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   313
  (warning ("Error while invoking " ^ quote kind ^ " " ^ name ^ " method"); raise e);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   314
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   315
fun err_inconsistent kinds =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   316
  error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " data");
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   317
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   318
val empty_data = Data Symtab.empty;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   319
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   320
fun merge_data (Data tab1, Data tab2) =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   321
  let
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   322
    val data1 = map snd (Symtab.dest tab1);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   323
    val data2 = map snd (Symtab.dest tab2);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   324
    val all_data = data1 @ data2;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   325
    val kinds = gen_distinct Object.eq_kind (map fst all_data);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   326
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   327
   fun entry data kind =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   328
     (case gen_assoc Object.eq_kind (data, kind) of
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   329
       NONE => []
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   330
     | SOME x => [(kind, x)]);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   331
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   332
    fun merge_entries [(kind, (e, mths as (_, ext, _, _)))] =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   333
          (kind, (ext e handle exn => err_method "prep_ext" (Object.name_of_kind kind) exn, mths))
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   334
      | merge_entries [(kind, (e1, mths as (_, _, mrg, _))), (_, (e2, _))] =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   335
          (kind, (mrg (e1, e2)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   336
            handle exn => err_method "merge" (Object.name_of_kind kind) exn, mths))
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   337
      | merge_entries _ = sys_error "merge_entries";
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   338
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   339
    val data = map (fn k => merge_entries (entry data1 k @ entry data2 k)) kinds;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   340
    val data_idx = map (fn (k, x) => (Object.name_of_kind k, (k, x))) data;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   341
  in
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   342
    Data (Symtab.make data_idx)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   343
      handle Symtab.DUPS dups => err_inconsistent dups
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   344
  end;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   345
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   346
fun prep_ext_data data = merge_data (data, empty_data);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   347
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   348
fun copy_data (Data tab) =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   349
  let
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   350
    fun cp_data (k, (e, mths as (cp, _, _, _))) =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   351
      (k, (cp e handle exn => err_method "copy" (Object.name_of_kind k) exn, mths));
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   352
  in Data (Symtab.map cp_data tab) end;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   353
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   354
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   355
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   356
(** build signatures **)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   357
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   358
fun create_sg name self stamps naming data sign =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   359
  let
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   360
    val id = ref name;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   361
    val stamps' = (case stamps of ref "#" :: ss => ss | ss => ss);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   362
    val _ = conditional (exists (equal name o !) stamps')
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   363
      (fn () => error ("Theory already contains a " ^ quote name ^ " component"));
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   364
    val sg = make_sg (id, self, id :: stamps') naming data sign;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   365
  in assign self sg; sg end;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   366
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   367
fun new_sg f (sg as Sg ({stamps, syn, ...}, {self = _, tsig, consts, naming, data})) =
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   368
  let
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   369
    val _ = check_stale "Sign.new_sg" sg;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   370
    val self' = SgRef (SOME (ref sg));
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   371
    val data' = f data;
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   372
  in create_sg "#" self' stamps naming data' (syn, tsig, consts) end;
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   373
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   374
val prep_ext = new_sg prep_ext_data;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   375
val copy = new_sg copy_data;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   376
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   377
fun add_name name (sg as Sg ({stamps, syn, ...}, {self, tsig, consts, naming, data})) =
16354
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   378
  let
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   379
    val _ = check_stale "Sign.add_name" sg;
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   380
    val (self', data') =
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   381
      if is_draft sg then (self, data)
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   382
      else (SgRef (SOME (ref sg)), prep_ext_data data);
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   383
  in create_sg name self' stamps naming data' (syn, tsig, consts) end;
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   384
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   385
fun map_sg keep f (sg as Sg ({stamps, syn, ...}, {self, tsig, consts, naming, data})) =
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   386
  let
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   387
    val _ = check_stale "Sign.map_sg" sg;
16354
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   388
    val (self', data') =
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   389
      if is_draft sg andalso keep then (self, data)
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   390
      else (SgRef (SOME (ref sg)), prep_ext_data data);
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   391
    val (naming', data', sign') = f (naming, data', (syn, tsig, consts));
16354
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   392
  in create_sg "#" self' stamps naming' data' sign' end;
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   393
16354
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   394
fun map_naming f = map_sg true (fn (naming, data, sign) => (f naming, data, sign));
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   395
fun map_data f = map_sg true (fn (naming, data, sign) => (naming, f data, sign));
6f0ca9628840 simplified is_stale, check_stale;
wenzelm
parents: 16337
diff changeset
   396
fun map_sign f = map_sg true (fn (naming, data, sign) => (naming, data, f sign));
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   397
fun map_syn f = map_sign (fn (syn, tsig, consts) => (f syn, tsig, consts));
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   398
fun map_tsig f = map_sign (fn (syn, tsig, consts) => (syn, f tsig, consts));
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   399
fun map_consts f = map_sign (fn (syn, tsig, consts) => (syn, tsig, f consts));
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   400
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   401
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   402
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   403
(** signature data **)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   404
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   405
(* errors *)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   406
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   407
fun of_theory sg = "\nof theory " ^ str_of_sg sg;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   408
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   409
fun err_dup_init sg kind =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   410
  error ("Duplicate initialization of " ^ quote kind ^ " data" ^ of_theory sg);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   411
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   412
fun err_uninit sg kind =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   413
  error ("Tried to access uninitialized " ^ quote kind ^ " data" ^ of_theory sg);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   414
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   415
fun err_access sg kind =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   416
  error ("Unauthorized access to " ^ quote kind ^ " data" ^ of_theory sg);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   417
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   418
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   419
(* access data *)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   420
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   421
fun data_kinds (Data tab) = Symtab.keys tab;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   422
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   423
fun lookup_data sg tab kind =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   424
  let val name = Object.name_of_kind kind in
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   425
    (case Symtab.lookup (tab, name) of
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   426
      SOME (k, x) =>
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   427
        if Object.eq_kind (kind, k) then x
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   428
        else err_access sg name
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   429
    | NONE => err_uninit sg name)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   430
  end;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   431
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   432
fun init_data (kind, (e, cp, ext, mrg, prt)) sg = sg |> map_data (fn Data tab =>
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   433
  Data (Symtab.update_new ((Object.name_of_kind kind, (kind, (e, (cp, ext, mrg, prt)))), tab))
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   434
    handle Symtab.DUP name => err_dup_init sg name);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   435
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   436
fun get_data kind dest (sg as Sg (_, {data = Data tab, ...})) =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   437
  let val x = fst (lookup_data sg tab kind)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   438
  in dest x handle Match => Object.kind_error kind end;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   439
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   440
fun put_data kind mk x sg = sg |> map_data (fn Data tab =>
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   441
  Data (Symtab.update ((Object.name_of_kind kind,
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   442
    (kind, (mk x, snd (lookup_data sg tab kind)))), tab)));
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   443
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   444
fun print_data kind (sg as Sg (_, {data = Data tab, ...})) =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   445
  let val (e, (_, _, _, prt)) = lookup_data sg tab kind in
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   446
    prt sg e handle exn =>
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   447
      err_method ("print" ^ of_theory sg) (Object.name_of_kind kind) exn
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   448
  end;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   449
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   450
fun print_all_data (sg as Sg (_, {data = Data tab, ...})) =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   451
  List.app (fn kind => print_data kind sg) (map (#1 o #2) (Symtab.dest tab));
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   452
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   453
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   454
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   455
(** primitive signatures **)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   456
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   457
val pure_syn =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   458
  Syn (Syntax.pure_syn, (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty));
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   459
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   460
val dummy_sg = make_sg (ref "", SgRef NONE, []) NameSpace.default_naming empty_data
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   461
  (pure_syn, Type.empty_tsig, NameSpace.empty_table);
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   462
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   463
val pre_pure =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   464
  create_sg "#" (SgRef (SOME (ref dummy_sg))) [] NameSpace.default_naming empty_data
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   465
    (pure_syn, Type.empty_tsig, NameSpace.empty_table);
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   466
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   467
val PureN = "Pure";
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   468
val CPureN = "CPure";
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   469
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   470
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   471
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   472
(** signature content **)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   473
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   474
(* naming *)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   475
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   476
fun naming_of (Sg (_, {naming, ...})) = naming;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   477
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   478
val base_name = NameSpace.base;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   479
val full_name = NameSpace.full o naming_of;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   480
fun full_name_path sg elems = NameSpace.full (NameSpace.add_path elems (naming_of sg));
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   481
val declare_name = NameSpace.declare o naming_of;
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   482
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   483
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   484
(* syntax *)
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   485
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   486
fun map_syntax f (Syn (syntax, trfuns)) = Syn (f syntax, trfuns);
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   487
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   488
fun make_syntax sg (Syn (syntax, (atrs, trs, tr's, atr's))) =
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   489
  let
15746
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   490
    fun apply (c, (f, s)) = (c, (f sg, s));
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   491
    fun prep tab = map apply (Symtab.dest tab);
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   492
    fun prep' tab = map apply (Symtab.dest_multi tab);
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   493
  in syntax |> Syntax.extend_trfuns (prep atrs, prep trs, prep' tr's, prep' atr's) end;
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   494
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   495
fun syn_of (sg as Sg ({syn, ...}, _)) = make_syntax sg syn;
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   496
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   497
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   498
(* advanced translation functions *)
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   499
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   500
fun extend_trfuns (atrs, trs, tr's, atr's)
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   501
    (Syn (syn, (parse_ast_trtab, parse_trtab, print_trtab, print_ast_trtab))) =
15746
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   502
  Syn (syn, (Syntax.extend_trtab "parse ast translation" atrs parse_ast_trtab,
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   503
    Syntax.extend_trtab "parse translation" trs parse_trtab,
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   504
    Syntax.extend_tr'tab tr's print_trtab,
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   505
    Syntax.extend_tr'tab atr's print_ast_trtab));
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   506
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   507
fun merge_trfuns
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   508
    (parse_ast_trtab1, parse_trtab1, print_trtab1, print_ast_trtab1)
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   509
    (parse_ast_trtab2, parse_trtab2, print_trtab2, print_ast_trtab2) =
15746
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   510
  (Syntax.merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   511
    Syntax.merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   512
    Syntax.merge_tr'tabs print_trtab1 print_trtab2,
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   513
    Syntax.merge_tr'tabs print_ast_trtab1 print_ast_trtab2);
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   514
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   515
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   516
(* type signature *)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   517
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   518
val tsig_of = #tsig o rep_sg;
4568
7be03035c553 added of_sort;
wenzelm
parents: 4489
diff changeset
   519
7640
6b7daae5d316 added witness_sorts, univ_witness;
wenzelm
parents: 7068
diff changeset
   520
val classes = Type.classes o tsig_of;
4844
4fb63c77f2df added defaultS: sg -> sort;
wenzelm
parents: 4627
diff changeset
   521
val defaultS = Type.defaultS o tsig_of;
4568
7be03035c553 added of_sort;
wenzelm
parents: 4489
diff changeset
   522
val subsort = Type.subsort o tsig_of;
7640
6b7daae5d316 added witness_sorts, univ_witness;
wenzelm
parents: 7068
diff changeset
   523
val of_sort = Type.of_sort o tsig_of;
6b7daae5d316 added witness_sorts, univ_witness;
wenzelm
parents: 7068
diff changeset
   524
val witness_sorts = Type.witness_sorts o tsig_of;
14784
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   525
val universal_witness = Type.universal_witness o tsig_of;
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   526
val typ_instance = Type.typ_instance o tsig_of;
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   527
fun is_logtype sg c = c mem_string Type.logical_types (tsig_of sg);
4256
e768c42069bb removed data.ML (made part of sign.ML);
wenzelm
parents: 4249
diff changeset
   528
e768c42069bb removed data.ML (made part of sign.ML);
wenzelm
parents: 4249
diff changeset
   529
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   530
(* consts signature *)
4256
e768c42069bb removed data.ML (made part of sign.ML);
wenzelm
parents: 4249
diff changeset
   531
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   532
fun const_type (Sg (_, {consts, ...})) c = Option.map #1 (Symtab.lookup (#2 consts, c));
4256
e768c42069bb removed data.ML (made part of sign.ML);
wenzelm
parents: 4249
diff changeset
   533
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   534
fun the_const_type sg c =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   535
  (case const_type sg c of SOME T => T
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   536
  | NONE => raise TYPE ("Undeclared constant " ^ quote c, [], []));
4256
e768c42069bb removed data.ML (made part of sign.ML);
wenzelm
parents: 4249
diff changeset
   537
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   538
fun declared_tyname sg c =
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   539
  is_some (Symtab.lookup (#2 (#types (Type.rep_tsig (tsig_of sg))), c));
3791
c5db2c87a646 now supports qualified names (intern vs. extern) !!!
wenzelm
parents: 3552
diff changeset
   540
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   541
fun declared_const sg c = is_some (const_type sg c);
620
f787eb061618 exported pretty_sort;
wenzelm
parents: 583
diff changeset
   542
402
16a8fe4f2250 added subsort, norm_sort, classes;
wenzelm
parents: 386
diff changeset
   543
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   544
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   545
(** intern / extern names **)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   546
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   547
val class_space = #1 o #classes o Type.rep_tsig o tsig_of;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   548
val type_space = #1 o #types o Type.rep_tsig o tsig_of;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   549
val const_space = #1 o #consts o rep_sg
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   550
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   551
val intern_class = NameSpace.intern o class_space;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   552
val extern_class = NameSpace.extern o class_space;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   553
val intern_type = NameSpace.intern o type_space;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   554
val extern_type = NameSpace.extern o type_space;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   555
val intern_const = NameSpace.intern o const_space;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   556
val extern_const = NameSpace.extern o const_space;
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   557
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   558
val intern_sort = map o intern_class;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   559
val extern_sort = map o extern_class;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   560
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   561
local
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   562
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   563
fun mapping add_names f t =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   564
  let
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   565
    fun f' x = let val y = f x in if x = y then NONE else SOME (x, y) end;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   566
    val tab = List.mapPartial f' (add_names (t, []));
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   567
    fun get x = if_none (assoc_string (tab, x)) x;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   568
  in get end;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   569
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   570
fun typ_mapping f g sg T =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   571
  T |> Term.map_typ
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   572
    (mapping add_typ_classes (f sg) T)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   573
    (mapping add_typ_tycons (g sg) T);
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   574
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   575
fun term_mapping f g h sg t =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   576
  t |> Term.map_term
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   577
    (mapping add_term_classes (f sg) t)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   578
    (mapping add_term_tycons (g sg) t)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   579
    (mapping add_term_consts (h sg) t);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   580
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   581
in
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   582
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   583
val intern_typ = typ_mapping intern_class intern_type;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   584
val extern_typ = typ_mapping extern_class extern_type;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   585
val intern_term = term_mapping intern_class intern_type intern_const;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   586
val extern_term = term_mapping extern_class extern_type extern_const;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   587
val intern_tycons = typ_mapping (K I) intern_type;
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   588
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   589
end;
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   590
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   591
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   592
4249
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   593
(** pretty printing of terms, types etc. **)
3937
988ce6fbf85b fixed types of add_XXX;
wenzelm
parents: 3899
diff changeset
   594
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   595
fun pretty_term' syn sg t = Syntax.pretty_term syn (exists_stamp CPureN sg) (extern_term sg t);
14974
b1ecb7859c99 avoid premature evaluation of syn_of (wastes time in conjunction with pp);
wenzelm
parents: 14905
diff changeset
   596
fun pretty_term sg t = pretty_term' (syn_of sg) sg t;
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   597
fun pretty_typ sg T = Syntax.pretty_typ (syn_of sg) (extern_typ sg T);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   598
fun pretty_sort sg S = Syntax.pretty_sort (syn_of sg) (extern_sort sg S);
3937
988ce6fbf85b fixed types of add_XXX;
wenzelm
parents: 3899
diff changeset
   599
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   600
fun pretty_classrel sg cs = Pretty.block (List.concat
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   601
  (separate [Pretty.str " <", Pretty.brk 1] (map (single o pretty_sort sg o single) cs)));
3937
988ce6fbf85b fixed types of add_XXX;
wenzelm
parents: 3899
diff changeset
   602
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   603
fun pretty_arity sg (a, Ss, S) =
3937
988ce6fbf85b fixed types of add_XXX;
wenzelm
parents: 3899
diff changeset
   604
  let
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   605
    val a' = extern_type sg a;
3937
988ce6fbf85b fixed types of add_XXX;
wenzelm
parents: 3899
diff changeset
   606
    val dom =
988ce6fbf85b fixed types of add_XXX;
wenzelm
parents: 3899
diff changeset
   607
      if null Ss then []
988ce6fbf85b fixed types of add_XXX;
wenzelm
parents: 3899
diff changeset
   608
      else [Pretty.list "(" ")" (map (pretty_sort sg) Ss), Pretty.brk 1];
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   609
  in Pretty.block ([Pretty.str (a' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort sg S]) end;
3937
988ce6fbf85b fixed types of add_XXX;
wenzelm
parents: 3899
diff changeset
   610
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   611
val string_of_term = Pretty.string_of oo pretty_term;
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   612
val string_of_typ = Pretty.string_of oo pretty_typ;
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   613
val string_of_sort = Pretty.string_of oo pretty_sort;
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   614
val string_of_classrel = Pretty.string_of oo pretty_classrel;
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   615
val string_of_arity = Pretty.string_of oo pretty_arity;
3937
988ce6fbf85b fixed types of add_XXX;
wenzelm
parents: 3899
diff changeset
   616
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   617
val pprint_term = (Pretty.pprint o Pretty.quote) oo pretty_term;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   618
val pprint_typ = (Pretty.pprint o Pretty.quote) oo pretty_typ;
3937
988ce6fbf85b fixed types of add_XXX;
wenzelm
parents: 3899
diff changeset
   619
14974
b1ecb7859c99 avoid premature evaluation of syn_of (wastes time in conjunction with pp);
wenzelm
parents: 14905
diff changeset
   620
fun pp sg = Pretty.pp (pretty_term sg, pretty_typ sg, pretty_sort sg,
b1ecb7859c99 avoid premature evaluation of syn_of (wastes time in conjunction with pp);
wenzelm
parents: 14905
diff changeset
   621
  pretty_classrel sg, pretty_arity sg);
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   622
3937
988ce6fbf85b fixed types of add_XXX;
wenzelm
parents: 3899
diff changeset
   623
988ce6fbf85b fixed types of add_XXX;
wenzelm
parents: 3899
diff changeset
   624
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   625
(** certify entities **)    (*exception TYPE*)
8898
a1ee54500516 removed is_type_abbr;
wenzelm
parents: 8802
diff changeset
   626
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   627
(* certify wrt. type signature *)
8898
a1ee54500516 removed is_type_abbr;
wenzelm
parents: 8802
diff changeset
   628
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   629
fun certify cert = cert o tsig_of o check_stale "Sign.certify";
562
e9572d03b724 added pretty_sg;
wenzelm
parents: 421
diff changeset
   630
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   631
val certify_class      = certify Type.cert_class;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   632
val certify_sort       = certify Type.cert_sort;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   633
val certify_typ        = certify Type.cert_typ;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   634
val certify_typ_syntax = certify Type.cert_typ_syntax;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   635
val certify_typ_abbrev = certify Type.cert_typ_abbrev;
10443
0a68dc9edba5 added certify_tycon, certify_tyabbr, certify_const;
wenzelm
parents: 10404
diff changeset
   636
4961
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   637
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   638
(* certify_term *)
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   639
14987
699239c7632c tuned certify_term;
wenzelm
parents: 14981
diff changeset
   640
local
1494
22f67e796445 added nodup_Vars check in cterm_of. Prevents same var with distinct types.
nipkow
parents: 1460
diff changeset
   641
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   642
(*determine and check the type of a term*)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   643
fun type_check pp tm =
4961
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   644
  let
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   645
    fun err_appl why bs t T u U =
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   646
      let
10404
93efbb62667c added typ_instance;
wenzelm
parents: 9504
diff changeset
   647
        val xs = map Free bs;           (*we do not rename here*)
4961
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   648
        val t' = subst_bounds (xs, t);
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   649
        val u' = subst_bounds (xs, u);
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   650
        val msg = cat_lines
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   651
          (TypeInfer.appl_error (Syntax.pp_show_brackets pp) why t' T u' U);
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   652
      in raise TYPE (msg, [T, U], [t', u']) end;
4961
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   653
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   654
    fun typ_of (_, Const (_, T)) = T
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   655
      | typ_of (_, Free  (_, T)) = T
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   656
      | typ_of (_, Var (_, T)) = T
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   657
      | typ_of (bs, Bound i) = snd (List.nth (bs, i) handle Subscript =>
4961
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   658
          raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   659
      | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body)
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   660
      | typ_of (bs, t $ u) =
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   661
          let val T = typ_of (bs, t) and U = typ_of (bs, u) in
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   662
            (case T of
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   663
              Type ("fun", [T1, T2]) =>
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   664
                if T1 = U then T2 else err_appl "Incompatible operand type" bs t T u U
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   665
            | _ => err_appl "Operator not of function type" bs t T u U)
4961
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   666
          end;
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   667
27f559b54c57 certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents: 4951
diff changeset
   668
  in typ_of ([], tm) end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   669
14987
699239c7632c tuned certify_term;
wenzelm
parents: 14981
diff changeset
   670
in
699239c7632c tuned certify_term;
wenzelm
parents: 14981
diff changeset
   671
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   672
fun certify_term pp sg tm =
251
c9b984c0a674 major cleanup;
wenzelm
parents: 229
diff changeset
   673
  let
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   674
    val _ = check_stale "Sign.certify_term" sg;
14784
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   675
14993
802f3732a54e tuned certify_typ/term;
wenzelm
parents: 14987
diff changeset
   676
    val tm' = map_term_types (Type.cert_typ (tsig_of sg)) tm;
14784
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   677
    val tm' = if tm = tm' then tm else tm';  (*avoid copying of already normal term*)
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   678
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   679
    fun err msg = raise TYPE (msg, [], [tm']);
3969
9c742951a923 certify: check_stale;
wenzelm
parents: 3967
diff changeset
   680
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   681
    fun show_const a T = quote a ^ " :: " ^ Pretty.string_of_typ pp T;
169
1b2765146aab extend: cleaned up, adapted for new Syntax.extend;
wenzelm
parents: 155
diff changeset
   682
14784
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   683
    fun check_atoms (t $ u) = (check_atoms t; check_atoms u)
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   684
      | check_atoms (Abs (_, _, t)) = check_atoms t
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   685
      | check_atoms (Const (a, T)) =
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   686
          (case const_type sg a of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14993
diff changeset
   687
            NONE => err ("Undeclared constant " ^ show_const a T)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14993
diff changeset
   688
          | SOME U =>
14784
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   689
              if typ_instance sg (T, U) then ()
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   690
              else err ("Illegal type for constant " ^ show_const a T))
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   691
      | check_atoms (Var ((x, i), _)) =
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   692
          if i < 0 then err ("Malformed variable: " ^ quote x) else ()
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   693
      | check_atoms _ = ();
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   694
  in
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   695
    check_atoms tm';
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   696
    (tm', type_check pp tm', maxidx_of_term tm')
14784
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   697
  end;
251
c9b984c0a674 major cleanup;
wenzelm
parents: 229
diff changeset
   698
14987
699239c7632c tuned certify_term;
wenzelm
parents: 14981
diff changeset
   699
end;
699239c7632c tuned certify_term;
wenzelm
parents: 14981
diff changeset
   700
251
c9b984c0a674 major cleanup;
wenzelm
parents: 229
diff changeset
   701
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   702
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   703
(** read and certify entities **)    (*exception ERROR/TYPE*)
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   704
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   705
(* sorts *)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   706
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   707
fun read_sort' syn sg str =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   708
  let
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   709
    val _ = check_stale "Sign.read_sort'" sg;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   710
    val S = intern_sort sg (Syntax.read_sort syn str);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   711
  in Type.cert_sort (tsig_of sg) S handle TYPE (msg, _, _) => error msg end;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   712
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   713
fun read_sort sg str = read_sort' (syn_of sg) sg str;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   714
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   715
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   716
(* types *)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   717
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   718
local
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   719
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   720
fun gen_read_typ' cert syn (sg, def_sort) str =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   721
  let
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   722
    val _ = check_stale "Sign.gen_read_typ'" sg;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   723
    val get_sort = TypeInfer.get_sort (tsig_of sg) def_sort (intern_sort sg);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   724
    val T = intern_tycons sg (Syntax.read_typ syn get_sort (intern_sort sg) str);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   725
  in cert (tsig_of sg) T handle TYPE (msg, _, _) => error msg end
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   726
  handle ERROR => error ("The error(s) above occurred in type " ^ quote str);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   727
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   728
fun gen_read_typ cert (sg, def_sort) str = gen_read_typ' cert (syn_of sg) (sg, def_sort) str;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   729
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   730
in
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   731
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   732
fun no_def_sort sg = (sg, K NONE);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   733
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   734
val read_typ'        = gen_read_typ' Type.cert_typ;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   735
val read_typ_syntax' = gen_read_typ' Type.cert_typ_syntax;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   736
val read_typ_abbrev' = gen_read_typ' Type.cert_typ_abbrev;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   737
val read_typ         = gen_read_typ Type.cert_typ;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   738
val read_typ_syntax  = gen_read_typ Type.cert_typ_syntax;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   739
val read_typ_abbrev  = gen_read_typ Type.cert_typ_abbrev;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   740
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   741
end;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   742
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   743
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   744
(* type and constant names *)
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
   745
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
   746
fun read_tyname sg raw_c =
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   747
  let val c = intern_type sg raw_c in
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   748
    (case Symtab.lookup (#2 (#types (Type.rep_tsig (tsig_of sg))), c) of
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
   749
      SOME (Type.LogicalType n, _) => Type (c, replicate n dummyT)
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   750
    | _ => error ("Undeclared type constructor: " ^ quote c))
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
   751
  end;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
   752
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
   753
fun read_const sg raw_c =
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   754
  let
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   755
    val c = intern_const sg raw_c;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   756
    val _ = the_const_type sg c handle TYPE (msg, _, _) => error msg;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   757
  in Const (c, dummyT) end;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
   758
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
   759
251
c9b984c0a674 major cleanup;
wenzelm
parents: 229
diff changeset
   760
583
550292083e66 minor internal changes;
wenzelm
parents: 573
diff changeset
   761
(** infer_types **)         (*exception ERROR*)
251
c9b984c0a674 major cleanup;
wenzelm
parents: 229
diff changeset
   762
2979
db6941221197 improved type check error messages;
wenzelm
parents: 2963
diff changeset
   763
(*
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   764
  def_type: partial map from indexnames to types (constrains Frees and Vars)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   765
  def_sort: partial map from indexnames to sorts (constrains TFrees and TVars)
2979
db6941221197 improved type check error messages;
wenzelm
parents: 2963
diff changeset
   766
  used: list of already used type variables
db6941221197 improved type check error messages;
wenzelm
parents: 2963
diff changeset
   767
  freeze: if true then generated parameters are turned into TFrees, else TVars
4249
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   768
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   769
  termss: lists of alternative parses (only one combination should be type-correct)
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   770
  typs: expected types
2979
db6941221197 improved type check error messages;
wenzelm
parents: 2963
diff changeset
   771
*)
db6941221197 improved type check error messages;
wenzelm
parents: 2963
diff changeset
   772
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   773
fun infer_types_simult pp sg def_type def_sort used freeze args =
251
c9b984c0a674 major cleanup;
wenzelm
parents: 229
diff changeset
   774
  let
3975
ddeb5a0fd08d hide id, stamps;
wenzelm
parents: 3969
diff changeset
   775
    val tsig = tsig_of sg;
4249
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   776
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   777
    val termss = foldr multiply [[]] (map fst args);
4249
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   778
    val typs =
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   779
      map (fn (_, T) => certify_typ sg T handle TYPE (msg, _, _) => error msg) args;
169
1b2765146aab extend: cleaned up, adapted for new Syntax.extend;
wenzelm
parents: 155
diff changeset
   780
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   781
    fun infer ts = OK (TypeInfer.infer_types (Syntax.pp_show_brackets pp) tsig
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   782
        (const_type sg) def_type def_sort (intern_const sg) (intern_tycons sg)
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   783
        (intern_sort sg) used freeze typs ts)
4249
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   784
      handle TYPE (msg, _, _) => Error msg;
623
ca9f5dbab880 added print_msg;
clasohm
parents: 620
diff changeset
   785
4249
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   786
    val err_results = map infer termss;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   787
    val errs = List.mapPartial get_error err_results;
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   788
    val results = List.mapPartial get_ok err_results;
4249
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   789
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   790
    val ambiguity = length termss;
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   791
4249
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   792
    fun ambig_msg () =
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   793
      if ambiguity > 1 andalso ambiguity <= ! Syntax.ambiguity_level
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   794
      then
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   795
        error_msg "Got more than one parse tree.\n\
3805
a50d0b5219dd improved types of add_XXX funs (xtyp etc.);
wenzelm
parents: 3791
diff changeset
   796
          \Retry with smaller Syntax.ambiguity_level for more information."
952
9e10962866b0 Removed an old bug which made some simultaneous instantiations fail if they
nipkow
parents: 949
diff changeset
   797
      else ();
4249
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   798
  in
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   799
    if null results then (ambig_msg (); error (cat_lines errs))
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   800
    else if length results = 1 then
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   801
      (if ambiguity > ! Syntax.ambiguity_level then
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   802
        warning "Fortunately, only one parse tree is type correct.\n\
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   803
          \You may still want to disambiguate your grammar or your input."
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   804
      else (); hd results)
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   805
    else (ambig_msg (); error ("More than one term is type correct:\n" ^
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   806
      cat_lines (map (Pretty.string_of_term pp) (List.concat (map fst results)))))
4249
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   807
  end;
623
ca9f5dbab880 added print_msg;
clasohm
parents: 620
diff changeset
   808
4249
34b7aafdc1bc exported pretty_classrel, pretty_arity;
wenzelm
parents: 4228
diff changeset
   809
14828
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   810
fun infer_types pp sg def_type def_sort used freeze tsT =
15d12761ba54 improved output; refer to Pretty.pp;
wenzelm
parents: 14784
diff changeset
   811
  apfst hd (infer_types_simult pp sg def_type def_sort used freeze [tsT]);
251
c9b984c0a674 major cleanup;
wenzelm
parents: 229
diff changeset
   812
c9b984c0a674 major cleanup;
wenzelm
parents: 229
diff changeset
   813
8607
bf129c6505de read_def_terms (no certify yet);
wenzelm
parents: 8290
diff changeset
   814
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   815
(** read_def_terms -- read terms and infer types **)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   816
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   817
fun read_def_terms' pp is_logtype syn (sg, types, sorts) used freeze sTs =
8607
bf129c6505de read_def_terms (no certify yet);
wenzelm
parents: 8290
diff changeset
   818
  let
bf129c6505de read_def_terms (no certify yet);
wenzelm
parents: 8290
diff changeset
   819
    fun read (s, T) =
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   820
      let val T' = certify_typ sg T handle TYPE (msg, _, _) => error msg
14905
5f3fc2f62071 added is_logtype (replaces logtypes field of syntax); tuned merge;
wenzelm
parents: 14856
diff changeset
   821
      in (Syntax.read is_logtype syn T' s, T') end;
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   822
  in infer_types_simult pp sg types sorts used freeze (map read sTs) end;
8607
bf129c6505de read_def_terms (no certify yet);
wenzelm
parents: 8290
diff changeset
   823
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   824
fun read_def_terms (sg, types, sorts) =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   825
  read_def_terms' (pp sg) (is_logtype sg) (syn_of sg) (sg, types, sorts);
12068
469f372d63db added pretty_term', read_typ', read_typ_no_norm', read_def_terms'
wenzelm
parents: 11720
diff changeset
   826
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   827
fun simple_read_term sg T s =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   828
  let val ([t], _) = read_def_terms (sg, K NONE, K NONE) [] true [(s, T)]
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   829
  in t end
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   830
  handle ERROR => error ("The error(s) above occurred for term " ^ s);
8802
2c37263eb903 added simple_read_term;
wenzelm
parents: 8730
diff changeset
   831
8607
bf129c6505de read_def_terms (no certify yet);
wenzelm
parents: 8290
diff changeset
   832
2979
db6941221197 improved type check error messages;
wenzelm
parents: 2963
diff changeset
   833
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   834
(** signature extension functions **)  (*exception ERROR/TYPE*)
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   835
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   836
(* add default sort *)
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   837
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   838
fun gen_add_defsort prep_sort s sg =
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   839
  sg |> map_tsig (Type.set_defsort (prep_sort sg s));
8898
a1ee54500516 removed is_type_abbr;
wenzelm
parents: 8802
diff changeset
   840
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   841
val add_defsort = gen_add_defsort read_sort;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   842
val add_defsort_i = gen_add_defsort certify_sort;
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   843
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   844
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   845
(* add type constructors *)
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   846
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   847
fun add_types types sg = sg |> map_sign (fn (syn, tsig, consts) =>
14856
669a9a0e7279 proper treatment of logical types within syntax;
wenzelm
parents: 14828
diff changeset
   848
  let
14905
5f3fc2f62071 added is_logtype (replaces logtypes field of syntax); tuned merge;
wenzelm
parents: 14856
diff changeset
   849
    val syn' = map_syntax (Syntax.extend_type_gram types) syn;
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   850
    val decls = map (fn (a, n, mx) => (Syntax.type_name a mx, n)) types;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   851
    val tsig' = Type.add_types (naming_of sg) decls tsig;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   852
  in (syn', tsig', consts) end);
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   853
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   854
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   855
(* add nonterminals *)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   856
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   857
fun add_nonterminals bnames sg = sg |> map_sign (fn (syn, tsig, consts) =>
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   858
  let
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   859
    val syn' = map_syntax (Syntax.extend_consts bnames) syn;
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   860
    val tsig' = Type.add_nonterminals (naming_of sg) bnames tsig;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   861
  in (syn', tsig', consts) end);
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   862
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   863
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   864
(* add type abbreviations *)
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   865
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   866
fun gen_add_tyabbr prep_typ (a, vs, rhs, mx) sg =
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   867
  sg |> map_sign (fn (syn, tsig, consts) =>
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   868
    let
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   869
      val syn' = map_syntax (Syntax.extend_type_gram [(a, length vs, mx)]) syn;
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   870
      val a' = Syntax.type_name a mx;
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   871
      val abbr = (a', vs, prep_typ sg rhs) handle ERROR =>
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   872
        error ("in type abbreviation " ^ quote a');
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   873
      val tsig' = Type.add_abbrevs (naming_of sg) [abbr] tsig;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   874
    in (syn', tsig', consts) end);
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   875
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   876
val add_tyabbrs = fold (gen_add_tyabbr (read_typ_syntax o no_def_sort));
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   877
val add_tyabbrs_i = fold (gen_add_tyabbr certify_typ_syntax);
14784
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   878
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
   879
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   880
(* add type arities *)
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   881
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   882
fun gen_add_arities int_type prep_sort arities sg = sg |> map_tsig (fn tsig =>
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   883
  let
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   884
    fun prep_arity (a, Ss, S) = (int_type sg a, map (prep_sort sg) Ss, prep_sort sg S)
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   885
      handle ERROR => error ("in arity for type " ^ quote a);
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   886
    val tsig' = Type.add_arities (pp sg) (map prep_arity arities) tsig;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   887
  in tsig' end);
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   888
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   889
val add_arities = gen_add_arities intern_type read_sort;
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   890
val add_arities_i = gen_add_arities (K I) certify_sort;
8898
a1ee54500516 removed is_type_abbr;
wenzelm
parents: 8802
diff changeset
   891
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   892
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   893
(* modify syntax *)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   894
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   895
fun gen_syntax change_gram prep_typ (prmode, args) sg =
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   896
  let
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   897
    fun prep (c, T, mx) = (c, prep_typ sg T, mx) handle ERROR =>
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   898
      error ("in syntax declaration " ^ quote (Syntax.const_name c mx));
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   899
  in sg |> map_syn (map_syntax (change_gram (is_logtype sg) prmode (map prep args))) end;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   900
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   901
fun gen_add_syntax x = gen_syntax Syntax.extend_const_gram x;
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   902
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   903
val add_modesyntax = gen_add_syntax (read_typ_syntax o no_def_sort);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   904
val add_modesyntax_i = gen_add_syntax certify_typ_syntax;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   905
val add_syntax = curry add_modesyntax Syntax.default_mode;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   906
val add_syntax_i = curry add_modesyntax_i Syntax.default_mode;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   907
val del_modesyntax = gen_syntax Syntax.remove_const_gram (read_typ_syntax o no_def_sort);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   908
val del_modesyntax_i = gen_syntax Syntax.remove_const_gram certify_typ_syntax;
3805
a50d0b5219dd improved types of add_XXX funs (xtyp etc.);
wenzelm
parents: 3791
diff changeset
   909
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   910
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   911
(* add constants *)
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   912
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   913
fun err_dup_consts cs =
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   914
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   915
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   916
fun gen_add_consts prep_typ raw_args sg =
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   917
  let
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   918
    val prepT = compress_type o Type.varifyT o Type.no_tvars o Term.no_dummyT o prep_typ sg;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   919
    fun prep (c, T, mx) = ((c, prepT T, mx) handle TYPE (msg, _, _) => error msg)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   920
      handle ERROR => error ("in declaration of constant " ^ quote (Syntax.const_name c mx));
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   921
    val args = map prep raw_args;
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   922
    val decls = args |> map (fn (c, T, mx) => (Syntax.const_name c mx, (T, stamp ())));
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   923
    fun extend_consts consts = NameSpace.extend_table (naming_of sg) (consts, decls)
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   924
      handle Symtab.DUPS cs => err_dup_consts cs;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   925
  in sg |> map_consts extend_consts |> add_syntax_i args end;
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   926
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   927
val add_consts = gen_add_consts (read_typ o no_def_sort);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   928
val add_consts_i = gen_add_consts certify_typ;
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   929
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   930
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   931
(* add type classes *)
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   932
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   933
val classN = "_class";
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   934
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   935
val const_of_class = suffix classN;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   936
fun class_of_const c = unsuffix classN c
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   937
  handle Fail _ => raise TERM ("class_of_const: bad name " ^ quote c, []);
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   938
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   939
fun gen_add_class int_class (bclass, raw_classes) sg =
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   940
  sg |> map_sign (fn (syn, tsig, consts) =>
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   941
    let
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   942
      val classes = map (int_class sg) raw_classes;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   943
      val syn' = map_syntax (Syntax.extend_consts [bclass]) syn;
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   944
      val tsig' = Type.add_classes (pp sg) (naming_of sg) [(bclass, classes)] tsig;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   945
    in (syn', tsig', consts) end)
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   946
  |> add_consts_i [(const_of_class bclass, a_itselfT --> propT, Syntax.NoSyn)];
3791
c5db2c87a646 now supports qualified names (intern vs. extern) !!!
wenzelm
parents: 3552
diff changeset
   947
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   948
val add_classes = fold (gen_add_class intern_class);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   949
val add_classes_i = fold (gen_add_class (K I));
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   950
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   951
2963
f3b5af1c5a67 renamed subclass to classrel;
wenzelm
parents: 2693
diff changeset
   952
(* add to classrel *)
421
95e1d4faa863 added add_classrel;
wenzelm
parents: 418
diff changeset
   953
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   954
fun gen_add_classrel int_class raw_pairs sg = sg |> map_tsig (fn tsig =>
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   955
  let
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   956
    val pairs = map (pairself (int_class sg)) raw_pairs;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   957
    val tsig' = Type.add_classrel (pp sg) pairs tsig;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   958
  in tsig' end);
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   959
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   960
val add_classrel = gen_add_classrel intern_class;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   961
val add_classrel_i = gen_add_classrel (K I);
421
95e1d4faa863 added add_classrel;
wenzelm
parents: 418
diff changeset
   962
95e1d4faa863 added add_classrel;
wenzelm
parents: 418
diff changeset
   963
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   964
(* add translation functions *)
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   965
15746
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   966
local
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   967
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   968
fun mk trs = map Syntax.mk_trfun trs;
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   969
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   970
fun gen_add_trfuns ext non_typed (atrs, trs, tr's, atr's) sg = sg |> map_syn (fn syn =>
15746
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   971
  let val syn' = syn |> ext (mk atrs, mk trs, mk (map (apsnd non_typed) tr's), mk atr's)
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   972
  in make_syntax sg syn'; syn' end);
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   973
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   974
fun gen_add_trfunsT ext tr's sg = sg |> map_syn (fn syn =>
15746
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   975
  let val syn' = syn |> ext ([], [], mk tr's, [])
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   976
  in make_syntax sg syn'; syn' end);
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   977
15746
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   978
in
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   979
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   980
val add_trfuns = gen_add_trfuns (map_syntax o Syntax.extend_trfuns) Syntax.non_typed_tr';
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   981
val add_trfunsT = gen_add_trfunsT (map_syntax o Syntax.extend_trfuns);
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   982
val add_advanced_trfuns = gen_add_trfuns extend_trfuns Syntax.non_typed_tr'';
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   983
val add_advanced_trfunsT = gen_add_trfunsT extend_trfuns;
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   984
15746
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   985
end;
44260d72de35 added del_modesyntax(_i);
wenzelm
parents: 15723
diff changeset
   986
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   987
val add_tokentrfuns = map_syn o map_syntax o Syntax.extend_tokentrfuns;
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   988
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
   989
4619
72edc2a9200f fixed add_trrules: intern root;
wenzelm
parents: 4568
diff changeset
   990
(* add translation rules *)
72edc2a9200f fixed add_trrules: intern root;
wenzelm
parents: 4568
diff changeset
   991
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   992
fun add_trrules args sg = sg |> map_syn (fn syn =>
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
   993
  let val rules = map (Syntax.map_trrule (apfst (intern_type sg))) args
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   994
  in map_syntax (Syntax.extend_trrules (is_logtype sg) (make_syntax sg syn) rules) syn end);
8725
0e48ee5b52db name space hide operations;
wenzelm
parents: 8715
diff changeset
   995
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   996
val add_trrules_i = map_syn o map_syntax o Syntax.extend_trrules_i;
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   997
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
   998
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
   999
(* modify naming *)
6546
995a66249a9b theory data: copy;
wenzelm
parents: 6311
diff changeset
  1000
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1001
val add_path        = map_naming o NameSpace.add_path;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1002
val qualified_names = map_naming NameSpace.qualified_names;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1003
val no_base_names   = map_naming NameSpace.no_base_names;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1004
val custom_accesses = map_naming o NameSpace.custom_accesses;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1005
val set_policy      = map_naming o NameSpace.set_policy;
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1006
val restore_naming  = map_naming o K o naming_of;
6546
995a66249a9b theory data: copy;
wenzelm
parents: 6311
diff changeset
  1007
995a66249a9b theory data: copy;
wenzelm
parents: 6311
diff changeset
  1008
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1009
(* hide names *)
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
  1010
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
  1011
fun hide_classes b xs sg = sg |> map_tsig (Type.hide_classes b (map (intern_class sg) xs));
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
  1012
val hide_classes_i = map_tsig oo Type.hide_classes;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
  1013
fun hide_types b xs sg = sg |> map_tsig (Type.hide_types b (map (intern_type sg) xs));
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
  1014
val hide_types_i = map_tsig oo Type.hide_types;
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
  1015
fun hide_consts b xs sg =
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
  1016
  sg |> map_consts (apfst (fold (NameSpace.hide b o intern_const sg) xs));
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
  1017
val hide_consts_i = (map_consts o apfst) oo (fold o NameSpace.hide);
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
  1018
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
  1019
3867
3b2587c809f4 added additional generic data;
wenzelm
parents: 3855
diff changeset
  1020
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1021
(** merge signatures **)
4228
22e3f0368c85 merge_refs: check for different versions of theories;
wenzelm
parents: 4227
diff changeset
  1022
22e3f0368c85 merge_refs: check for different versions of theories;
wenzelm
parents: 4227
diff changeset
  1023
fun merge_stamps stamps1 stamps2 =
12296
45269a593e1b clarified order of merge_lists';
wenzelm
parents: 12286
diff changeset
  1024
  let val stamps = merge_lists' stamps1 stamps2 in
4228
22e3f0368c85 merge_refs: check for different versions of theories;
wenzelm
parents: 4227
diff changeset
  1025
    (case duplicates (map ! stamps) of
22e3f0368c85 merge_refs: check for different versions of theories;
wenzelm
parents: 4227
diff changeset
  1026
      [] => stamps
15826
e9b4c9feb296 improved read_tyname;
wenzelm
parents: 15801
diff changeset
  1027
    | dups => error ("Attempt to merge different versions of theories " ^ commas_quote dups))
4228
22e3f0368c85 merge_refs: check for different versions of theories;
wenzelm
parents: 4227
diff changeset
  1028
  end;
22e3f0368c85 merge_refs: check for different versions of theories;
wenzelm
parents: 4227
diff changeset
  1029
22e3f0368c85 merge_refs: check for different versions of theories;
wenzelm
parents: 4227
diff changeset
  1030
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1031
(* trivial merge *)
3877
83c5310aaaab make_draft replaced by prep_ext;
wenzelm
parents: 3867
diff changeset
  1032
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14993
diff changeset
  1033
fun merge_refs (sgr1 as SgRef (SOME (ref (sg1 as Sg ({stamps = s1, ...}, _)))),
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14993
diff changeset
  1034
        sgr2 as SgRef (SOME (ref (sg2 as Sg ({stamps = s2, ...}, _))))) =
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1035
      if subsig (sg2, sg1) then sgr1
3967
edd5ff9371f8 sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents: 3956
diff changeset
  1036
      else if subsig (sg1, sg2) then sgr2
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1037
      else (merge_stamps s1 s2;  (*check for different versions*)
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1038
        error ("Attempt to do non-trivial merge of signature\n" ^
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1039
          str_of_sg sg1 ^ " and " ^ str_of_sg sg2))
3967
edd5ff9371f8 sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents: 3956
diff changeset
  1040
  | merge_refs _ = sys_error "Sign.merge_refs";
3867
3b2587c809f4 added additional generic data;
wenzelm
parents: 3855
diff changeset
  1041
4627
ae95666c71cc Sign.merge vs. Sign.nontriv_merge;
wenzelm
parents: 4619
diff changeset
  1042
val merge = deref o merge_refs o pairself self_ref;
ae95666c71cc Sign.merge vs. Sign.nontriv_merge;
wenzelm
parents: 4619
diff changeset
  1043
3867
3b2587c809f4 added additional generic data;
wenzelm
parents: 3855
diff changeset
  1044
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1045
(* non-trivial merge *)              (*exception TERM/ERROR*)
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1046
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1047
local
10932
ad13abb0a264 added exists_stamp;
wenzelm
parents: 10443
diff changeset
  1048
6961
4d404c52ca80 err_method: pass exn;
wenzelm
parents: 6845
diff changeset
  1049
fun nontriv_merge (sg1, sg2) =
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1050
  if subsig_internal (sg2, sg1) then sg1
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1051
  else if subsig_internal (sg1, sg2) then sg2
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1052
  else
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15797
diff changeset
  1053
    if exists_stamp CPureN sg1 <> exists_stamp CPureN sg2
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15797
diff changeset
  1054
    then error "Cannot merge Pure and CPure developments"
251
c9b984c0a674 major cleanup;
wenzelm
parents: 229
diff changeset
  1055
  else
c9b984c0a674 major cleanup;
wenzelm
parents: 229
diff changeset
  1056
    let
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
  1057
      val Sg ({id = _, stamps = stamps1, syn = Syn (syntax1, trfuns1)},
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
  1058
          {self = _, tsig = tsig1, consts = consts1, naming = _, data = data1}) = sg1;
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
  1059
      val Sg ({id = _, stamps = stamps2, syn = Syn (syntax2, trfuns2)},
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
  1060
          {self = _, tsig = tsig2, consts = consts2, naming = _, data = data2}) = sg2;
386
e9ba9f7e2542 added const_type: sg -> typ option;
wenzelm
parents: 277
diff changeset
  1061
4228
22e3f0368c85 merge_refs: check for different versions of theories;
wenzelm
parents: 4227
diff changeset
  1062
      val stamps = merge_stamps stamps1 stamps2;
14645
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
  1063
      val syntax = Syntax.merge_syntaxes syntax1 syntax2;
83776a9f0a9c support for advanced translation functions;
wenzelm
parents: 14383
diff changeset
  1064
      val trfuns = merge_trfuns trfuns1 trfuns2;
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1065
      val syn = Syn (syntax, trfuns);
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
  1066
      val consts = NameSpace.merge_tables eq_snd (consts1, consts2)
14784
e65d77313a94 xxx_typ_raw replace xxx_typ_no_norm forms; prevent duplicate consts declarations in merge; misc cleanup;
wenzelm
parents: 14700
diff changeset
  1067
        handle Symtab.DUPS cs => err_dup_consts cs;
16133
cd0f1ea21abf renamed cond_extern to extern;
wenzelm
parents: 15889
diff changeset
  1068
      val naming = NameSpace.default_naming;
4256
e768c42069bb removed data.ML (made part of sign.ML);
wenzelm
parents: 4249
diff changeset
  1069
      val data = merge_data (data1, data2);
3967
edd5ff9371f8 sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents: 3956
diff changeset
  1070
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1071
      val pre_sg = make_sg (ref "", SgRef (SOME (ref dummy_sg)), ref "#" :: stamps1)
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
  1072
        naming data (syn, tsig1, consts);
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1073
      val tsig = Type.merge_tsigs (pp pre_sg) (tsig1, tsig2);
14905
5f3fc2f62071 added is_logtype (replaces logtypes field of syntax); tuned merge;
wenzelm
parents: 14856
diff changeset
  1074
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1075
      val self = SgRef (SOME (ref dummy_sg));
16368
a06868ebeb0f discontinued named name spaces (classK, typeK, constK);
wenzelm
parents: 16354
diff changeset
  1076
      val sg = make_sg (ref "", self, stamps) naming data (syn, tsig, consts);
16337
5734de2f7ace Major cleanup:
wenzelm
parents: 16288
diff changeset
  1077
    in assign self sg; syn_of sg; sg end;
3791
c5db2c87a646 now supports qualified names (intern vs. extern) !!!
wenzelm
parents: 3552
diff changeset
  1078
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1079
in
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1080
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1081
fun prep_ext_merge sgs =
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1082
  if null sgs then
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1083
    error "Merge: no parent theories"
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1084
  else if exists is_draft sgs then
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1085
    error "Attempt to merge draft theories"
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1086
  else
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1087
    Library.foldl nontriv_merge (hd sgs, tl sgs)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1088
    |> prep_ext
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1089
    |> add_path "/";
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1090
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1091
end;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1092
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15624
diff changeset
  1093
end;