src/Pure/sign.ML
author wenzelm
Sun Jun 05 23:07:26 2005 +0200 (2005-06-05)
changeset 16288 df2b550a17f6
parent 16189 74dc76a28038
child 16337 5734de2f7ace
permissions -rw-r--r--
added the_const_type;
wenzelm@19
     1
(*  Title:      Pure/sign.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@251
     3
    Author:     Lawrence C Paulson and Markus Wenzel
clasohm@0
     4
wenzelm@251
     5
The abstract type "sg" of signatures.
clasohm@0
     6
*)
clasohm@0
     7
wenzelm@3956
     8
(*base names*)
wenzelm@3956
     9
type bclass = class;
wenzelm@16133
    10
(*external forms*)
wenzelm@3805
    11
type xclass = class;
wenzelm@3805
    12
type xsort = sort;
wenzelm@3805
    13
type xtyp = typ;
wenzelm@3805
    14
type xterm = term;
wenzelm@3805
    15
wenzelm@19
    16
signature SIGN =
wenzelm@3791
    17
sig
paulson@1501
    18
  type sg
wenzelm@3967
    19
  type sg_ref
wenzelm@4256
    20
  type data
wenzelm@2197
    21
  val rep_sg: sg ->
wenzelm@3975
    22
   {self: sg_ref,
wenzelm@14784
    23
    tsig: Type.tsig,
wenzelm@14784
    24
    consts: (typ * stamp) Symtab.table,
wenzelm@16133
    25
    naming: NameSpace.naming,
wenzelm@3791
    26
    spaces: (string * NameSpace.T) list,
wenzelm@4256
    27
    data: data}
wenzelm@3975
    28
  val name_of: sg -> string
wenzelm@3975
    29
  val stamp_names_of: sg -> string list
wenzelm@10932
    30
  val exists_stamp: string -> sg -> bool
wenzelm@14784
    31
  val tsig_of: sg -> Type.tsig
wenzelm@14905
    32
  val is_logtype: sg -> string -> bool
wenzelm@3967
    33
  val deref: sg_ref -> sg
wenzelm@3967
    34
  val self_ref: sg -> sg_ref
paulson@1501
    35
  val subsig: sg * sg -> bool
wenzelm@9031
    36
  val joinable: sg * sg -> bool
paulson@1501
    37
  val eq_sg: sg * sg -> bool
paulson@1501
    38
  val same_sg: sg * sg -> bool
paulson@1501
    39
  val is_draft: sg -> bool
wenzelm@4951
    40
  val is_stale: sg -> bool
wenzelm@14645
    41
  val syn_of: sg -> Syntax.syntax
wenzelm@16288
    42
  val naming_of: sg -> NameSpace.naming
paulson@1501
    43
  val const_type: sg -> string -> typ option
wenzelm@16288
    44
  val the_const_type: sg -> string -> typ          (*exception TYPE*)
wenzelm@15703
    45
  val declared_tyname: sg -> string -> bool
wenzelm@15703
    46
  val declared_const: sg -> string -> bool
paulson@1501
    47
  val classes: sg -> class list
wenzelm@4844
    48
  val defaultS: sg -> sort
paulson@1501
    49
  val subsort: sg -> sort * sort -> bool
wenzelm@4568
    50
  val of_sort: sg -> typ * sort -> bool
wenzelm@7640
    51
  val witness_sorts: sg -> sort list -> sort list -> (typ * sort) list
wenzelm@14784
    52
  val universal_witness: sg -> (typ * sort) option
wenzelm@10443
    53
  val typ_instance: sg -> typ * typ -> bool
wenzelm@3810
    54
  val classK: string
wenzelm@3810
    55
  val typeK: string
wenzelm@3810
    56
  val constK: string
wenzelm@16133
    57
  val base_name: string -> bstring
wenzelm@3956
    58
  val full_name: sg -> bstring -> string
wenzelm@4844
    59
  val full_name_path: sg -> string -> bstring -> string
wenzelm@16133
    60
  val declare_name: sg -> string -> NameSpace.T -> NameSpace.T
wenzelm@3810
    61
  val intern: sg -> string -> xstring -> string
wenzelm@3810
    62
  val extern: sg -> string -> string -> xstring
wenzelm@16133
    63
  val extern_table: sg -> string -> 'a Symtab.table -> (xstring * 'a) list
schirmer@14700
    64
  val extern_typ: sg -> typ -> typ
wenzelm@3805
    65
  val intern_class: sg -> xclass -> class
wenzelm@3805
    66
  val intern_tycon: sg -> xstring -> string
wenzelm@3805
    67
  val intern_const: sg -> xstring -> string
wenzelm@3937
    68
  val intern_sort: sg -> xsort -> sort
wenzelm@3937
    69
  val intern_typ: sg -> xtyp -> typ
wenzelm@3937
    70
  val intern_term: sg -> xterm -> term
wenzelm@3937
    71
  val intern_tycons: sg -> xtyp -> typ
paulson@1501
    72
  val pretty_sg: sg -> Pretty.T
wenzelm@4051
    73
  val str_of_sg: sg -> string
paulson@1501
    74
  val pprint_sg: sg -> pprint_args -> unit
wenzelm@14645
    75
  val PureN: string
wenzelm@14645
    76
  val CPureN: string
wenzelm@14645
    77
  val pre_pure: sg
paulson@1501
    78
  val pretty_term: sg -> term -> Pretty.T
wenzelm@12068
    79
  val pretty_term': Syntax.syntax -> sg -> term -> Pretty.T
paulson@1501
    80
  val pretty_typ: sg -> typ -> Pretty.T
wenzelm@3791
    81
  val pretty_sort: sg -> sort -> Pretty.T
wenzelm@14828
    82
  val pretty_classrel: sg -> class list -> Pretty.T
wenzelm@14828
    83
  val pretty_arity: sg -> arity -> Pretty.T
paulson@1501
    84
  val string_of_term: sg -> term -> string
paulson@1501
    85
  val string_of_typ: sg -> typ -> string
wenzelm@3791
    86
  val string_of_sort: sg -> sort -> string
wenzelm@14828
    87
  val string_of_classrel: sg -> class list -> string
wenzelm@14828
    88
  val string_of_arity: sg -> arity -> string
paulson@1501
    89
  val pprint_term: sg -> term -> pprint_args -> unit
paulson@1501
    90
  val pprint_typ: sg -> typ -> pprint_args -> unit
wenzelm@14828
    91
  val pp: sg -> Pretty.pp
wenzelm@8898
    92
  val certify_class: sg -> class -> class
wenzelm@8898
    93
  val certify_sort: sg -> sort -> sort
paulson@1501
    94
  val certify_typ: sg -> typ -> typ
wenzelm@14784
    95
  val certify_typ_raw: sg -> typ -> typ
wenzelm@14828
    96
  val certify_term: Pretty.pp -> sg -> term -> term * typ * int
wenzelm@8898
    97
  val read_sort: sg -> string -> sort
wenzelm@4227
    98
  val read_raw_typ: sg * (indexname -> sort option) -> string -> typ
paulson@1501
    99
  val read_typ: sg * (indexname -> sort option) -> string -> typ
wenzelm@12068
   100
  val read_typ': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
wenzelm@14784
   101
  val read_typ_raw': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
wenzelm@15703
   102
  val read_tyname: sg -> string -> typ
wenzelm@16288
   103
  val read_const: sg -> string -> term        (*exception TYPE*)
wenzelm@14828
   104
  val infer_types: Pretty.pp -> sg -> (indexname -> typ option) ->
paulson@1501
   105
    (indexname -> sort option) -> string list -> bool
wenzelm@4249
   106
    -> xterm list * typ -> term * (indexname * typ) list
wenzelm@14828
   107
  val infer_types_simult: Pretty.pp -> sg -> (indexname -> typ option) ->
wenzelm@4249
   108
    (indexname -> sort option) -> string list -> bool
wenzelm@4249
   109
    -> (xterm list * typ) list -> term list * (indexname * typ) list
wenzelm@14905
   110
  val read_def_terms': Pretty.pp -> (string -> bool) -> Syntax.syntax ->
wenzelm@14828
   111
    sg * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@12068
   112
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@8607
   113
  val read_def_terms:
wenzelm@8607
   114
    sg * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@8607
   115
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@8802
   116
  val simple_read_term: sg -> typ -> string -> term
wenzelm@14645
   117
  val const_of_class: class -> string
wenzelm@14645
   118
  val class_of_const: string -> class
wenzelm@3956
   119
  val add_classes: (bclass * xclass list) list -> sg -> sg
wenzelm@3956
   120
  val add_classes_i: (bclass * class list) list -> sg -> sg
wenzelm@3805
   121
  val add_classrel: (xclass * xclass) list -> sg -> sg
wenzelm@3791
   122
  val add_classrel_i: (class * class) list -> sg -> sg
wenzelm@8898
   123
  val add_defsort: string -> sg -> sg
wenzelm@3791
   124
  val add_defsort_i: sort -> sg -> sg
wenzelm@3956
   125
  val add_types: (bstring * int * mixfix) list -> sg -> sg
wenzelm@4844
   126
  val add_nonterminals: bstring list -> sg -> sg
wenzelm@3956
   127
  val add_tyabbrs: (bstring * string list * string * mixfix) list -> sg -> sg
wenzelm@3956
   128
  val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> sg -> sg
wenzelm@8898
   129
  val add_arities: (xstring * string list * string) list -> sg -> sg
wenzelm@3791
   130
  val add_arities_i: (string * sort list * sort) list -> sg -> sg
wenzelm@3956
   131
  val add_consts: (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   132
  val add_consts_i: (bstring * typ * mixfix) list -> sg -> sg
wenzelm@3956
   133
  val add_syntax: (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   134
  val add_syntax_i: (bstring * typ * mixfix) list -> sg -> sg
wenzelm@3956
   135
  val add_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   136
  val add_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg
wenzelm@15746
   137
  val del_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg
wenzelm@15746
   138
  val del_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg
paulson@1501
   139
  val add_trfuns:
wenzelm@4344
   140
    (string * (ast list -> ast)) list *
wenzelm@4344
   141
    (string * (term list -> term)) list *
wenzelm@4344
   142
    (string * (term list -> term)) list *
wenzelm@4344
   143
    (string * (ast list -> ast)) list -> sg -> sg
wenzelm@2385
   144
  val add_trfunsT:
wenzelm@4344
   145
    (string * (bool -> typ -> term list -> term)) list -> sg -> sg
wenzelm@14645
   146
  val add_advanced_trfuns:
wenzelm@14645
   147
    (string * (sg -> ast list -> ast)) list *
wenzelm@14645
   148
    (string * (sg -> term list -> term)) list *
wenzelm@14645
   149
    (string * (sg -> term list -> term)) list *
wenzelm@14645
   150
    (string * (sg -> ast list -> ast)) list -> sg -> sg
wenzelm@14645
   151
  val add_advanced_trfunsT:
wenzelm@14645
   152
    (string * (sg -> bool -> typ -> term list -> term)) list -> sg -> sg
wenzelm@2693
   153
  val add_tokentrfuns:
wenzelm@6311
   154
    (string * string * (string -> string * real)) list -> sg -> sg
wenzelm@4619
   155
  val add_trrules: (xstring * string) Syntax.trrule list -> sg -> sg
paulson@1810
   156
  val add_trrules_i: ast Syntax.trrule list -> sg -> sg
wenzelm@3791
   157
  val add_path: string -> sg -> sg
wenzelm@16133
   158
  val qualified_names: sg -> sg
wenzelm@16133
   159
  val no_base_names: sg -> sg
wenzelm@16133
   160
  val custom_accesses: (string list -> string list list) -> sg -> sg
wenzelm@16133
   161
  val set_policy: (string -> bstring -> string) * (string list -> string list list) -> sg -> sg
wenzelm@16189
   162
  val restore_naming: sg -> sg -> sg
wenzelm@3810
   163
  val add_space: string * string list -> sg -> sg
wenzelm@12588
   164
  val hide_space: bool -> string * string list -> sg -> sg
wenzelm@12588
   165
  val hide_space_i: bool -> string * string list -> sg -> sg
paulson@1501
   166
  val add_name: string -> sg -> sg
wenzelm@4256
   167
  val data_kinds: data -> string list
wenzelm@15746
   168
  val print_all_data: sg -> unit
wenzelm@3967
   169
  val merge_refs: sg_ref * sg_ref -> sg_ref
wenzelm@4627
   170
  val merge: sg * sg -> sg
wenzelm@12123
   171
  val copy: sg -> sg
wenzelm@3975
   172
  val prep_ext: sg -> sg
wenzelm@15703
   173
  val prep_ext_merge: sg list -> sg
wenzelm@3791
   174
end;
clasohm@0
   175
wenzelm@6191
   176
signature PRIVATE_SIGN =
wenzelm@5642
   177
sig
wenzelm@5642
   178
  include SIGN
wenzelm@6546
   179
  val init_data: Object.kind * (Object.T * (Object.T -> Object.T) * (Object.T -> Object.T) *
wenzelm@12310
   180
    (Object.T * Object.T -> Object.T) * (sg -> Object.T -> unit)) -> sg -> sg
wenzelm@5642
   181
  val get_data: Object.kind -> (Object.T -> 'a) -> sg -> 'a
wenzelm@5642
   182
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> sg -> sg
wenzelm@5642
   183
  val print_data: Object.kind -> sg -> unit
wenzelm@5642
   184
end;
wenzelm@5642
   185
wenzelm@6191
   186
structure Sign: PRIVATE_SIGN =
wenzelm@143
   187
struct
clasohm@0
   188
wenzelm@251
   189
(** datatype sg **)
wenzelm@251
   190
wenzelm@14645
   191
(* types sg, data, syn, sg_ref *)
wenzelm@4256
   192
wenzelm@19
   193
datatype sg =
wenzelm@3975
   194
  Sg of
wenzelm@16133
   195
   {id: string ref,                               (*id*)
wenzelm@16133
   196
    stamps: string ref list,                      (*unique theory indentifier*)
wenzelm@16133
   197
    syn: syn} *                                   (*syntax for parsing and printing*)
wenzelm@16133
   198
   {self: sg_ref,                                 (*mutable self reference*)
wenzelm@16133
   199
    tsig: Type.tsig,                              (*order-sorted signature of types*)
wenzelm@16133
   200
    consts: (typ * stamp) Symtab.table,           (*type schemes of constants*)
wenzelm@16133
   201
    naming: NameSpace.naming,                     (*name space declaration context*)
wenzelm@16133
   202
    spaces: (string * NameSpace.T) list,          (*name spaces for consts, types etc.*)
wenzelm@16133
   203
    data: data}                                   (*anytype data*)
wenzelm@4256
   204
and data =
wenzelm@4256
   205
  Data of
wenzelm@16133
   206
    (Object.kind *                                (*kind (for authorization)*)
wenzelm@16133
   207
      (Object.T *                                 (*value*)
wenzelm@16133
   208
        ((Object.T -> Object.T) *                 (*copy method*)
wenzelm@16133
   209
          (Object.T -> Object.T) *                (*prepare extend method*)
wenzelm@16133
   210
          (Object.T * Object.T -> Object.T) *     (*merge and prepare extend method*)
wenzelm@16133
   211
          (sg -> Object.T -> unit))))             (*print method*)
wenzelm@4256
   212
    Symtab.table
wenzelm@14645
   213
and syn =
wenzelm@14645
   214
  Syn of
wenzelm@14645
   215
    Syntax.syntax *
wenzelm@14645
   216
     (((sg -> ast list -> ast) * stamp) Symtab.table *
wenzelm@14645
   217
      ((sg -> term list -> term) * stamp) Symtab.table *
wenzelm@14645
   218
      ((sg -> bool -> typ -> term list -> term) * stamp) list Symtab.table *
wenzelm@14645
   219
      ((sg -> ast list -> ast) * stamp) list Symtab.table)
wenzelm@3967
   220
and sg_ref =
wenzelm@4998
   221
  SgRef of sg ref option;
clasohm@0
   222
wenzelm@3967
   223
(*make signature*)
wenzelm@16133
   224
fun make_sign (id, self, tsig, consts, syn, naming, spaces, data, stamps) =
wenzelm@14645
   225
  Sg ({id = id, stamps = stamps, syn = syn}, {self = self, tsig = tsig,
wenzelm@16133
   226
    consts = consts, naming = naming, spaces = spaces, data = data});
wenzelm@3975
   227
wenzelm@3975
   228
wenzelm@4256
   229
(* basic operations *)
wenzelm@3975
   230
wenzelm@3975
   231
fun rep_sg (Sg (_, args)) = args;
wenzelm@3967
   232
wenzelm@15801
   233
fun stamps_of (Sg ({stamps, ...}, _)) = stamps;
wenzelm@15801
   234
val stamp_names_of = rev o map ! o stamps_of;
wenzelm@15801
   235
fun exists_stamp name = exists (equal name o !) o stamps_of;
wenzelm@15801
   236
wenzelm@3975
   237
fun pretty_sg sg = Pretty.str_list "{" "}" (stamp_names_of sg);
wenzelm@3975
   238
val str_of_sg = Pretty.str_of o pretty_sg;
wenzelm@3975
   239
val pprint_sg = Pretty.pprint o pretty_sg;
wenzelm@3975
   240
wenzelm@16288
   241
fun naming_of (Sg (_, {naming, ...})) = naming;
wenzelm@402
   242
val tsig_of = #tsig o rep_sg;
wenzelm@14905
   243
fun is_logtype sg c = c mem_string Type.logical_types (tsig_of sg);
wenzelm@16288
   244
skalberg@15570
   245
fun const_type (Sg (_, {consts, ...})) c = Option.map #1 (Symtab.lookup (consts, c));
wenzelm@16288
   246
wenzelm@16288
   247
fun the_const_type sg c =
wenzelm@16288
   248
  (case const_type sg c of SOME T => T
wenzelm@16288
   249
  | NONE => raise TYPE ("Undeclared constant " ^ quote c, [], []));
wenzelm@3967
   250
wenzelm@16133
   251
fun declared_tyname sg c = is_some (Symtab.lookup (#types (Type.rep_tsig (tsig_of sg)), c));
wenzelm@16133
   252
fun declared_const sg c = is_some (const_type sg c);
wenzelm@15703
   253
wenzelm@3967
   254
wenzelm@3995
   255
(* id and self *)
wenzelm@3967
   256
wenzelm@3975
   257
fun check_stale (sg as Sg ({id, ...},
skalberg@15531
   258
        {self = SgRef (SOME (ref (Sg ({id = id', ...}, _)))), ...})) =
wenzelm@3967
   259
      if id = id' then sg
wenzelm@3975
   260
      else raise TERM ("Stale signature: " ^ str_of_sg sg, [])
wenzelm@3967
   261
  | check_stale _ = sys_error "Sign.check_stale";
clasohm@0
   262
wenzelm@4951
   263
fun is_stale sg = (check_stale sg; false) handle TERM _ => true;
wenzelm@4951
   264
wenzelm@3995
   265
fun self_ref (sg as Sg (_, {self, ...})) = (check_stale sg; self);
wenzelm@3995
   266
skalberg@15531
   267
fun deref (SgRef (SOME (ref sg))) = sg
skalberg@15531
   268
  | deref (SgRef NONE) = sys_error "Sign.deref";
wenzelm@3995
   269
wenzelm@3975
   270
fun name_of (sg as Sg ({id = ref name, ...}, _)) =
wenzelm@4844
   271
  if name = "" orelse ord name = ord "#" then
wenzelm@3975
   272
    raise TERM ("Nameless signature " ^ str_of_sg sg, [])
wenzelm@3975
   273
  else name;
wenzelm@3975
   274
nipkow@206
   275
wenzelm@2979
   276
(* inclusion and equality *)
paulson@2180
   277
wenzelm@2185
   278
local
wenzelm@2185
   279
  (*avoiding polymorphic equality: factor 10 speedup*)
wenzelm@2185
   280
  fun mem_stamp (_:string ref, []) = false
wenzelm@2185
   281
    | mem_stamp (x, y :: ys) = x = y orelse mem_stamp (x, ys);
wenzelm@2185
   282
wenzelm@2185
   283
  fun subset_stamp ([], ys) = true
wenzelm@2185
   284
    | subset_stamp (x :: xs, ys) =
wenzelm@2185
   285
        mem_stamp (x, ys) andalso subset_stamp (xs, ys);
wenzelm@2185
   286
in
wenzelm@3975
   287
  fun eq_sg (sg1 as Sg ({id = id1, ...}, _), sg2 as Sg ({id = id2, ...}, _)) =
wenzelm@3967
   288
    (check_stale sg1; check_stale sg2; id1 = id2);
wenzelm@2185
   289
wenzelm@15801
   290
  fun subsig (sg1, sg2) =
wenzelm@15801
   291
    eq_sg (sg1, sg2) orelse mem_stamp (hd (stamps_of sg1), stamps_of sg2);
paulson@2180
   292
wenzelm@15801
   293
  fun subsig_internal (sg1, sg2) =
wenzelm@15801
   294
    eq_sg (sg1, sg2) orelse subset_stamp (stamps_of sg1, stamps_of sg2);
wenzelm@2185
   295
end;
wenzelm@2185
   296
wenzelm@402
   297
wenzelm@9031
   298
fun joinable (sg1, sg2) = subsig (sg1, sg2) orelse subsig (sg2, sg1);
wenzelm@9031
   299
wenzelm@2185
   300
(*test if same theory names are contained in signatures' stamps,
wenzelm@2185
   301
  i.e. if signatures belong to same theory but not necessarily to the
wenzelm@2185
   302
  same version of it*)
wenzelm@15801
   303
fun same_sg (sg1, sg2) =
wenzelm@15801
   304
  eq_sg (sg1, sg2) orelse eq_set_string (pairself (map ! o stamps_of) (sg1, sg2));
wenzelm@2185
   305
wenzelm@2185
   306
(*test for drafts*)
wenzelm@14645
   307
fun is_draft (Sg ({stamps = ref name :: _, ...}, _)) =
wenzelm@14645
   308
  name = "" orelse ord name = ord "#";
wenzelm@14645
   309
wenzelm@14645
   310
wenzelm@14645
   311
(* syntax *)
wenzelm@14645
   312
wenzelm@14645
   313
fun map_syntax f (Syn (syntax, trfuns)) = Syn (f syntax, trfuns);
wenzelm@14645
   314
wenzelm@14645
   315
fun make_syntax sg (Syn (syntax, (atrs, trs, tr's, atr's))) =
wenzelm@14645
   316
  let
wenzelm@15746
   317
    fun apply (c, (f, s)) = (c, (f sg, s));
wenzelm@14645
   318
    fun prep tab = map apply (Symtab.dest tab);
wenzelm@14645
   319
    fun prep' tab = map apply (Symtab.dest_multi tab);
wenzelm@14645
   320
  in syntax |> Syntax.extend_trfuns (prep atrs, prep trs, prep' tr's, prep' atr's) end;
wenzelm@14645
   321
wenzelm@14645
   322
fun syn_of (sg as Sg ({syn, ...}, _)) = make_syntax sg syn;
wenzelm@14645
   323
wenzelm@14645
   324
wenzelm@14645
   325
(* advanced translation functions *)
wenzelm@14645
   326
wenzelm@14645
   327
fun extend_trfuns (atrs, trs, tr's, atr's)
wenzelm@14645
   328
    (Syn (syn, (parse_ast_trtab, parse_trtab, print_trtab, print_ast_trtab))) =
wenzelm@15746
   329
  Syn (syn, (Syntax.extend_trtab "parse ast translation" atrs parse_ast_trtab,
wenzelm@15746
   330
    Syntax.extend_trtab "parse translation" trs parse_trtab,
wenzelm@15746
   331
    Syntax.extend_tr'tab tr's print_trtab,
wenzelm@15746
   332
    Syntax.extend_tr'tab atr's print_ast_trtab));
wenzelm@14645
   333
wenzelm@14645
   334
fun merge_trfuns
wenzelm@14645
   335
    (parse_ast_trtab1, parse_trtab1, print_trtab1, print_ast_trtab1)
wenzelm@14645
   336
    (parse_ast_trtab2, parse_trtab2, print_trtab2, print_ast_trtab2) =
wenzelm@15746
   337
  (Syntax.merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
wenzelm@15746
   338
    Syntax.merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
wenzelm@14645
   339
    Syntax.merge_tr'tabs print_trtab1 print_trtab2,
wenzelm@14645
   340
    Syntax.merge_tr'tabs print_ast_trtab1 print_ast_trtab2);
wenzelm@386
   341
clasohm@0
   342
wenzelm@4568
   343
(* classes and sorts *)
wenzelm@4568
   344
wenzelm@7640
   345
val classes = Type.classes o tsig_of;
wenzelm@4844
   346
val defaultS = Type.defaultS o tsig_of;
wenzelm@4568
   347
val subsort = Type.subsort o tsig_of;
wenzelm@7640
   348
val of_sort = Type.of_sort o tsig_of;
wenzelm@7640
   349
val witness_sorts = Type.witness_sorts o tsig_of;
wenzelm@14784
   350
val universal_witness = Type.universal_witness o tsig_of;
wenzelm@14784
   351
val typ_instance = Type.typ_instance o tsig_of;
wenzelm@4568
   352
wenzelm@4256
   353
wenzelm@14828
   354
wenzelm@4256
   355
(** signature data **)
wenzelm@4256
   356
wenzelm@4256
   357
(* errors *)
wenzelm@4256
   358
wenzelm@4261
   359
fun of_theory sg = "\nof theory " ^ str_of_sg sg;
wenzelm@4256
   360
wenzelm@4998
   361
fun err_inconsistent kinds =
wenzelm@4998
   362
  error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " data");
wenzelm@4998
   363
wenzelm@6961
   364
fun err_method name kind e =
wenzelm@6961
   365
  (writeln ("Error while invoking " ^ quote kind ^ " " ^ name ^ " method"); raise e);
wenzelm@4256
   366
wenzelm@4256
   367
fun err_dup_init sg kind =
wenzelm@4256
   368
  error ("Duplicate initialization of " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@4256
   369
wenzelm@4256
   370
fun err_uninit sg kind =
wenzelm@14784
   371
  error ("Tried to access uninitialized " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@4256
   372
paulson@6040
   373
(*Trying to access theory data using get / put operations from a different
paulson@6040
   374
  instance of the TheoryDataFun result.  Typical cure: re-load all files*)
wenzelm@4998
   375
fun err_access sg kind =
wenzelm@4998
   376
  error ("Unauthorized access to " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@4998
   377
wenzelm@4256
   378
wenzelm@4256
   379
(* prepare data *)
wenzelm@4256
   380
wenzelm@4489
   381
val empty_data = Data Symtab.empty;
wenzelm@4256
   382
wenzelm@4256
   383
fun merge_data (Data tab1, Data tab2) =
wenzelm@4256
   384
  let
wenzelm@4998
   385
    val data1 = map snd (Symtab.dest tab1);
wenzelm@4998
   386
    val data2 = map snd (Symtab.dest tab2);
wenzelm@4256
   387
    val all_data = data1 @ data2;
wenzelm@4998
   388
    val kinds = gen_distinct Object.eq_kind (map fst all_data);
wenzelm@4256
   389
wenzelm@4256
   390
   fun entry data kind =
wenzelm@4998
   391
     (case gen_assoc Object.eq_kind (data, kind) of
skalberg@15531
   392
       NONE => []
skalberg@15531
   393
     | SOME x => [(kind, x)]);
wenzelm@4256
   394
wenzelm@12310
   395
    fun merge_entries [(kind, (e, mths as (_, ext, _, _)))] =
wenzelm@6961
   396
          (kind, (ext e handle exn => err_method "prep_ext" (Object.name_of_kind kind) exn, mths))
wenzelm@12310
   397
      | merge_entries [(kind, (e1, mths as (_, _, mrg, _))), (_, (e2, _))] =
wenzelm@6961
   398
          (kind, (mrg (e1, e2)
wenzelm@6961
   399
            handle exn => err_method "merge" (Object.name_of_kind kind) exn, mths))
wenzelm@4256
   400
      | merge_entries _ = sys_error "merge_entries";
wenzelm@4256
   401
wenzelm@4256
   402
    val data = map (fn k => merge_entries (entry data1 k @ entry data2 k)) kinds;
wenzelm@4998
   403
    val data_idx = map (fn (k, x) => (Object.name_of_kind k, (k, x))) data;
wenzelm@4998
   404
  in
wenzelm@4998
   405
    Data (Symtab.make data_idx)
wenzelm@4998
   406
      handle Symtab.DUPS dups => err_inconsistent dups
wenzelm@4998
   407
  end;
wenzelm@4256
   408
wenzelm@4256
   409
fun prep_ext_data data = merge_data (data, empty_data);
wenzelm@4256
   410
wenzelm@12310
   411
fun init_data_sg sg (Data tab) kind e cp ext mrg prt =
wenzelm@4998
   412
  let val name = Object.name_of_kind kind in
wenzelm@12310
   413
    Data (Symtab.update_new ((name, (kind, (e, (cp, ext, mrg, prt)))), tab))
wenzelm@4998
   414
      handle Symtab.DUP _ => err_dup_init sg name
wenzelm@4998
   415
  end;
wenzelm@4256
   416
wenzelm@4256
   417
wenzelm@4256
   418
(* access data *)
wenzelm@4256
   419
wenzelm@4256
   420
fun data_kinds (Data tab) = map fst (Symtab.dest tab);
wenzelm@4256
   421
wenzelm@4256
   422
fun lookup_data sg tab kind =
wenzelm@4998
   423
  let val name = Object.name_of_kind kind in
wenzelm@4998
   424
    (case Symtab.lookup (tab, name) of
skalberg@15531
   425
      SOME (k, x) =>
wenzelm@4998
   426
        if Object.eq_kind (kind, k) then x
wenzelm@4998
   427
        else err_access sg name
skalberg@15531
   428
    | NONE => err_uninit sg name)
wenzelm@4998
   429
  end;
wenzelm@4256
   430
wenzelm@4998
   431
fun get_data kind f (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@4998
   432
  let val x = fst (lookup_data sg tab kind)
wenzelm@4998
   433
  in f x handle Match => Object.kind_error kind end;
wenzelm@4998
   434
wenzelm@4998
   435
fun print_data kind (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@12310
   436
  let val (e, (_, _, _, prt)) = lookup_data sg tab kind
wenzelm@6961
   437
  in prt sg e handle exn => err_method ("print" ^ of_theory sg) (Object.name_of_kind kind) exn end;
wenzelm@4256
   438
wenzelm@15746
   439
fun print_all_data (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@15746
   440
  app (fn kind => print_data kind sg) (map (#1 o #2) (Symtab.dest tab));
wenzelm@15746
   441
wenzelm@4998
   442
fun put_data_sg sg (Data tab) kind f x =
wenzelm@4998
   443
  Data (Symtab.update ((Object.name_of_kind kind,
wenzelm@4998
   444
    (kind, (f x, snd (lookup_data sg tab kind)))), tab));
wenzelm@4256
   445
wenzelm@4256
   446
wenzelm@4256
   447
wenzelm@4256
   448
(** build signatures **)
wenzelm@3967
   449
wenzelm@3967
   450
fun ext_stamps stamps (id as ref name) =
wenzelm@3967
   451
  let val stmps = (case stamps of ref "#" :: ss => ss | ss => ss) in
wenzelm@3967
   452
    if exists (equal name o !) stmps then
wenzelm@3967
   453
      error ("Theory already contains a " ^ quote name ^ " component")
wenzelm@3967
   454
    else id :: stmps
wenzelm@3967
   455
  end;
wenzelm@3967
   456
wenzelm@16133
   457
fun create_sign self stamps name (syn, tsig, ctab, (naming, spaces), data) =
wenzelm@3967
   458
  let
wenzelm@3967
   459
    val id = ref name;
wenzelm@3967
   460
    val sign =
wenzelm@16133
   461
      make_sign (id, self, tsig, ctab, syn, naming, spaces, data, ext_stamps stamps id);
wenzelm@3967
   462
  in
wenzelm@3967
   463
    (case self of
skalberg@15531
   464
      SgRef (SOME r) => r := sign
wenzelm@3967
   465
    | _ => sys_error "Sign.create_sign");
wenzelm@3967
   466
    sign
wenzelm@3967
   467
  end;
wenzelm@3967
   468
wenzelm@3975
   469
fun extend_sign keep extfun name decls
wenzelm@16133
   470
    (sg as Sg ({id = _, stamps, syn}, {self, tsig, consts, naming, spaces, data})) =
wenzelm@3967
   471
  let
wenzelm@3967
   472
    val _ = check_stale sg;
wenzelm@3967
   473
    val (self', data') =
wenzelm@3975
   474
      if is_draft sg andalso keep then (self, data)
skalberg@15531
   475
      else (SgRef (SOME (ref sg)), prep_ext_data data);
wenzelm@3967
   476
  in
wenzelm@3967
   477
    create_sign self' stamps name
wenzelm@16133
   478
      (extfun sg (syn, tsig, consts, (naming, spaces), data') decls)
wenzelm@3967
   479
  end;
wenzelm@3967
   480
wenzelm@3967
   481
wenzelm@3791
   482
wenzelm@3791
   483
(** name spaces **)
wenzelm@3791
   484
wenzelm@16133
   485
(* naming *)
wenzelm@16133
   486
wenzelm@16133
   487
val base_name = NameSpace.base;
wenzelm@16133
   488
val full_name = NameSpace.full o naming_of;
wenzelm@16133
   489
fun full_name_path sg elems = NameSpace.full (NameSpace.add_path elems (naming_of sg));
wenzelm@16133
   490
val declare_name = NameSpace.declare o naming_of;
wenzelm@16133
   491
wenzelm@16133
   492
wenzelm@3791
   493
(* kinds *)
wenzelm@3791
   494
wenzelm@3791
   495
val classK = "class";
wenzelm@3791
   496
val typeK = "type";
wenzelm@3791
   497
val constK = "const";
wenzelm@3791
   498
wenzelm@3791
   499
wenzelm@8725
   500
(* declare and retrieve names *)
wenzelm@3791
   501
wenzelm@16133
   502
fun get_space spaces kind =
wenzelm@16133
   503
  if_none (assoc_string (spaces, kind)) NameSpace.empty;
wenzelm@3791
   504
wenzelm@3791
   505
(*input and output of qualified names*)
wenzelm@16133
   506
val intrn = NameSpace.intern oo get_space;
wenzelm@16133
   507
val extrn = NameSpace.extern oo get_space;
wenzelm@16133
   508
fun extrn_table spaces = NameSpace.extern_table o (get_space spaces);
wenzelm@3791
   509
wenzelm@8725
   510
(*add / hide names*)
wenzelm@16133
   511
fun change_space f kind names spaces =
wenzelm@16133
   512
  overwrite (spaces, (kind, f names (get_space spaces kind)));
wenzelm@3791
   513
wenzelm@16133
   514
val add_names = change_space o fold o declare_name;
wenzelm@16133
   515
val hide_names = change_space o fold o NameSpace.hide;
wenzelm@3791
   516
wenzelm@3791
   517
wenzelm@3791
   518
(* intern / extern names *)
wenzelm@3791
   519
wenzelm@3791
   520
local
wenzelm@3791
   521
  (*prepare mapping of names*)
wenzelm@3791
   522
  fun mapping f add_xs t =
wenzelm@3791
   523
    let
skalberg@15531
   524
      fun f' x = let val y = f x in if x = y then NONE else SOME (x, y) end;
skalberg@15570
   525
      val table = List.mapPartial f' (add_xs (t, []));
wenzelm@16133
   526
      fun lookup x = if_none (assoc (table, x)) x;
wenzelm@3791
   527
    in lookup end;
wenzelm@3791
   528
wenzelm@3791
   529
  (*intern / extern typ*)
wenzelm@3791
   530
  fun trn_typ trn T =
wenzelm@3791
   531
    T |> map_typ
wenzelm@3791
   532
      (mapping (trn classK) add_typ_classes T)
wenzelm@3791
   533
      (mapping (trn typeK) add_typ_tycons T);
wenzelm@3791
   534
wenzelm@3791
   535
  (*intern / extern term*)
wenzelm@3791
   536
  fun trn_term trn t =
wenzelm@3791
   537
    t |> map_term
wenzelm@3791
   538
      (mapping (trn classK) add_term_classes t)
wenzelm@3791
   539
      (mapping (trn typeK) add_term_tycons t)
wenzelm@3791
   540
      (mapping (trn constK) add_term_consts t);
wenzelm@3791
   541
wenzelm@3975
   542
  val spaces_of = #spaces o rep_sg;
wenzelm@3791
   543
in
wenzelm@3791
   544
  fun intrn_class spaces = intrn spaces classK;
wenzelm@16133
   545
  fun extrn_class spaces = extrn spaces classK;
wenzelm@3937
   546
wenzelm@3791
   547
  val intrn_sort = map o intrn_class;
wenzelm@3791
   548
  val intrn_typ = trn_typ o intrn;
wenzelm@3937
   549
  val intrn_term = trn_term o intrn;
wenzelm@3937
   550
wenzelm@3937
   551
  val extrn_sort = map o extrn_class;
wenzelm@16133
   552
  val extrn_typ = trn_typ o extrn;
wenzelm@16133
   553
  val extrn_term = trn_term o extrn;
wenzelm@3791
   554
wenzelm@3805
   555
  fun intrn_tycons spaces T =
wenzelm@3805
   556
    map_typ I (mapping (intrn spaces typeK) add_typ_tycons T) T;
wenzelm@3805
   557
wenzelm@3810
   558
  val intern = intrn o spaces_of;
wenzelm@3855
   559
  val extern = extrn o spaces_of;
wenzelm@16133
   560
  fun extern_table sg = extrn_table (spaces_of sg);
wenzelm@3937
   561
schirmer@14700
   562
  fun extern_typ (sg as Sg (_, {spaces, ...})) T =
schirmer@14700
   563
       if ! NameSpace.long_names then T else extrn_typ spaces T;
schirmer@14700
   564
wenzelm@3791
   565
  val intern_class = intrn_class o spaces_of;
wenzelm@3791
   566
  val intern_sort = intrn_sort o spaces_of;
wenzelm@3791
   567
  val intern_typ = intrn_typ o spaces_of;
wenzelm@3791
   568
  val intern_term = intrn_term o spaces_of;
wenzelm@3791
   569
wenzelm@3791
   570
  fun intern_tycon sg = intrn (spaces_of sg) typeK;
wenzelm@3791
   571
  fun intern_const sg = intrn (spaces_of sg) constK;
wenzelm@3937
   572
wenzelm@3791
   573
  val intern_tycons = intrn_tycons o spaces_of;
wenzelm@3791
   574
end;
wenzelm@620
   575
wenzelm@402
   576
clasohm@0
   577
wenzelm@14645
   578
(** partial Pure signature **)
wenzelm@14645
   579
wenzelm@14645
   580
val pure_syn =
wenzelm@14645
   581
  Syn (Syntax.pure_syn, (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty));
wenzelm@14645
   582
skalberg@15531
   583
val dummy_sg = make_sign (ref "", SgRef NONE, Type.empty_tsig,
wenzelm@16133
   584
  Symtab.empty, pure_syn, NameSpace.default_naming, [], empty_data, []);
wenzelm@14645
   585
wenzelm@14645
   586
val pre_pure =
skalberg@15531
   587
  create_sign (SgRef (SOME (ref dummy_sg))) [] "#"
wenzelm@16133
   588
    (pure_syn, Type.empty_tsig, Symtab.empty, (NameSpace.default_naming, []), empty_data);
wenzelm@14645
   589
wenzelm@14645
   590
val PureN = "Pure";
wenzelm@14645
   591
val CPureN = "CPure";
wenzelm@14645
   592
wenzelm@14645
   593
wenzelm@14645
   594
wenzelm@4249
   595
(** pretty printing of terms, types etc. **)
wenzelm@3937
   596
wenzelm@12068
   597
fun pretty_term' syn (sg as Sg ({stamps, ...}, {spaces, ...})) t =
wenzelm@3937
   598
  Syntax.pretty_term syn
wenzelm@14645
   599
    (exists (equal CPureN o !) stamps)
wenzelm@5175
   600
    (if ! NameSpace.long_names then t else extrn_term spaces t);
wenzelm@3937
   601
wenzelm@14974
   602
fun pretty_term sg t = pretty_term' (syn_of sg) sg t;
wenzelm@12068
   603
schirmer@14700
   604
fun pretty_typ sg T =
schirmer@14700
   605
  Syntax.pretty_typ (syn_of sg) (extern_typ sg T);
wenzelm@3937
   606
wenzelm@14645
   607
fun pretty_sort (sg as Sg (_, {spaces, ...})) S =
wenzelm@14645
   608
  Syntax.pretty_sort (syn_of sg)
wenzelm@5175
   609
    (if ! NameSpace.long_names then S else extrn_sort spaces S);
wenzelm@3937
   610
skalberg@15570
   611
fun pretty_classrel sg cs = Pretty.block (List.concat
wenzelm@14828
   612
  (separate [Pretty.str " <", Pretty.brk 1] (map (single o pretty_sort sg o single) cs)));
wenzelm@3937
   613
wenzelm@3937
   614
fun pretty_arity sg (t, Ss, S) =
wenzelm@3937
   615
  let
wenzelm@16133
   616
    val t' = extern sg typeK t;
wenzelm@3937
   617
    val dom =
wenzelm@3937
   618
      if null Ss then []
wenzelm@3937
   619
      else [Pretty.list "(" ")" (map (pretty_sort sg) Ss), Pretty.brk 1];
wenzelm@3937
   620
  in
wenzelm@3937
   621
    Pretty.block
wenzelm@3937
   622
      ([Pretty.str (t' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort sg S])
wenzelm@3937
   623
  end;
wenzelm@3937
   624
wenzelm@14828
   625
val string_of_term = Pretty.string_of oo pretty_term;
wenzelm@14828
   626
val string_of_typ = Pretty.string_of oo pretty_typ;
wenzelm@14828
   627
val string_of_sort = Pretty.string_of oo pretty_sort;
wenzelm@14828
   628
val string_of_classrel = Pretty.string_of oo pretty_classrel;
wenzelm@14828
   629
val string_of_arity = Pretty.string_of oo pretty_arity;
wenzelm@3937
   630
wenzelm@3937
   631
fun pprint_term sg = Pretty.pprint o Pretty.quote o (pretty_term sg);
wenzelm@3937
   632
fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg);
wenzelm@3937
   633
wenzelm@14974
   634
fun pp sg = Pretty.pp (pretty_term sg, pretty_typ sg, pretty_sort sg,
wenzelm@14974
   635
  pretty_classrel sg, pretty_arity sg);
wenzelm@14828
   636
wenzelm@3937
   637
wenzelm@3937
   638
wenzelm@8898
   639
(** read sorts **)  (*exception ERROR*)
wenzelm@8898
   640
wenzelm@8898
   641
fun err_in_sort s =
wenzelm@8898
   642
  error ("The error(s) above occurred in sort " ^ quote s);
wenzelm@8898
   643
wenzelm@14645
   644
fun rd_sort sg syn tsig spaces str =
wenzelm@14645
   645
  let val S = intrn_sort spaces (Syntax.read_sort (make_syntax sg syn) str
wenzelm@14645
   646
    handle ERROR => err_in_sort str)
wenzelm@8898
   647
  in Type.cert_sort tsig S handle TYPE (msg, _, _) => (error_msg msg; err_in_sort str) end;
wenzelm@8898
   648
wenzelm@8898
   649
(*read and certify sort wrt a signature*)
wenzelm@14645
   650
fun read_sort (sg as Sg ({syn, ...}, {tsig, spaces, ...})) str =
wenzelm@14645
   651
  (check_stale sg; rd_sort sg syn tsig spaces str);
wenzelm@8898
   652
wenzelm@14645
   653
fun cert_sort _ _ tsig _ = Type.cert_sort tsig;
wenzelm@8898
   654
wenzelm@8898
   655
wenzelm@8898
   656
wenzelm@562
   657
(** read types **)  (*exception ERROR*)
wenzelm@562
   658
wenzelm@562
   659
fun err_in_type s =
wenzelm@562
   660
  error ("The error(s) above occurred in type " ^ quote s);
wenzelm@562
   661
wenzelm@4227
   662
fun rd_raw_typ syn tsig spaces def_sort str =
wenzelm@14784
   663
  intrn_tycons spaces (Syntax.read_typ syn
wenzelm@14784
   664
    (TypeInfer.get_sort tsig def_sort (intrn_sort spaces)) (intrn_sort spaces) str
wenzelm@3791
   665
      handle ERROR => err_in_type str);
wenzelm@4227
   666
wenzelm@12068
   667
fun read_raw_typ' syn (sg as Sg (_, {tsig, spaces, ...}), def_sort) str =
wenzelm@4227
   668
  (check_stale sg; rd_raw_typ syn tsig spaces def_sort str);
wenzelm@4227
   669
wenzelm@12068
   670
fun read_raw_typ (sg, def_sort) = read_raw_typ' (syn_of sg) (sg, def_sort);
wenzelm@12068
   671
wenzelm@562
   672
(*read and certify typ wrt a signature*)
wenzelm@9504
   673
local
wenzelm@12068
   674
  fun read_typ_aux rd cert (sg, def_sort) str =
wenzelm@14993
   675
    cert (tsig_of sg) (rd (sg, def_sort) str)
wenzelm@14993
   676
      handle TYPE (msg, _, _) => (error_msg msg; err_in_type str);
wenzelm@9504
   677
in
wenzelm@14784
   678
  val read_typ          = read_typ_aux read_raw_typ Type.cert_typ;
wenzelm@14784
   679
  val read_typ_raw      = read_typ_aux read_raw_typ Type.cert_typ_raw;
wenzelm@14784
   680
  fun read_typ' syn     = read_typ_aux (read_raw_typ' syn) Type.cert_typ;
wenzelm@14784
   681
  fun read_typ_raw' syn = read_typ_aux (read_raw_typ' syn) Type.cert_typ_raw;
wenzelm@9504
   682
end;
wenzelm@562
   683
wenzelm@562
   684
wenzelm@562
   685
wenzelm@8898
   686
(** certify classes, sorts, types and terms **)   (*exception TYPE*)
wenzelm@251
   687
wenzelm@8898
   688
val certify_class = Type.cert_class o tsig_of;
wenzelm@8898
   689
val certify_sort = Type.cert_sort o tsig_of;
wenzelm@14993
   690
val certify_typ = Type.cert_typ o tsig_of;
wenzelm@14993
   691
val certify_typ_raw = Type.cert_typ_raw o tsig_of;
wenzelm@10443
   692
wenzelm@4961
   693
wenzelm@4961
   694
(* certify_term *)
wenzelm@4961
   695
wenzelm@14987
   696
local
nipkow@1494
   697
wenzelm@4961
   698
(*compute and check type of the term*)
wenzelm@14828
   699
fun type_check pp sg tm =
wenzelm@4961
   700
  let
wenzelm@4961
   701
    fun err_appl why bs t T u U =
wenzelm@4961
   702
      let
wenzelm@10404
   703
        val xs = map Free bs;           (*we do not rename here*)
wenzelm@4961
   704
        val t' = subst_bounds (xs, t);
wenzelm@4961
   705
        val u' = subst_bounds (xs, u);
wenzelm@14828
   706
        val text = cat_lines
wenzelm@14828
   707
          (TypeInfer.appl_error (Syntax.pp_show_brackets pp) why t' T u' U);
wenzelm@4961
   708
      in raise TYPE (text, [T, U], [t', u']) end;
wenzelm@4961
   709
wenzelm@4961
   710
    fun typ_of (_, Const (_, T)) = T
wenzelm@4961
   711
      | typ_of (_, Free  (_, T)) = T
wenzelm@4961
   712
      | typ_of (_, Var (_, T)) = T
skalberg@15570
   713
      | typ_of (bs, Bound i) = snd (List.nth (bs, i) handle Subscript =>
wenzelm@4961
   714
          raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
wenzelm@4961
   715
      | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body)
wenzelm@4961
   716
      | typ_of (bs, t $ u) =
wenzelm@4961
   717
          let val T = typ_of (bs, t) and U = typ_of (bs, u) in
wenzelm@4961
   718
            (case T of
wenzelm@4961
   719
              Type ("fun", [T1, T2]) =>
wenzelm@14828
   720
                if T1 = U then T2 else err_appl "Incompatible operand type" bs t T u U
wenzelm@14828
   721
            | _ => err_appl "Operator not of function type" bs t T u U)
wenzelm@4961
   722
          end;
wenzelm@4961
   723
wenzelm@4961
   724
  in typ_of ([], tm) end;
clasohm@0
   725
wenzelm@14987
   726
in
wenzelm@14987
   727
wenzelm@14828
   728
fun certify_term pp sg tm =
wenzelm@251
   729
  let
wenzelm@3969
   730
    val _ = check_stale sg;
wenzelm@14784
   731
wenzelm@14993
   732
    val tm' = map_term_types (Type.cert_typ (tsig_of sg)) tm;
wenzelm@14784
   733
    val tm' = if tm = tm' then tm else tm';  (*avoid copying of already normal term*)
wenzelm@14784
   734
wenzelm@14784
   735
    fun err msg = raise TYPE (msg, [], [tm']);
wenzelm@3969
   736
wenzelm@14828
   737
    fun show_const a T = quote a ^ " :: " ^ Pretty.string_of_typ pp T;
wenzelm@169
   738
wenzelm@14784
   739
    fun check_atoms (t $ u) = (check_atoms t; check_atoms u)
wenzelm@14784
   740
      | check_atoms (Abs (_, _, t)) = check_atoms t
wenzelm@14784
   741
      | check_atoms (Const (a, T)) =
wenzelm@14784
   742
          (case const_type sg a of
skalberg@15531
   743
            NONE => err ("Undeclared constant " ^ show_const a T)
skalberg@15531
   744
          | SOME U =>
wenzelm@14784
   745
              if typ_instance sg (T, U) then ()
wenzelm@14784
   746
              else err ("Illegal type for constant " ^ show_const a T))
wenzelm@14784
   747
      | check_atoms (Var ((x, i), _)) =
wenzelm@14784
   748
          if i < 0 then err ("Malformed variable: " ^ quote x) else ()
wenzelm@14784
   749
      | check_atoms _ = ();
wenzelm@14784
   750
  in
wenzelm@14784
   751
    check_atoms tm';
wenzelm@14993
   752
    (tm', type_check pp sg tm', maxidx_of_term tm')
wenzelm@14784
   753
  end;
wenzelm@251
   754
wenzelm@14987
   755
end;
wenzelm@14987
   756
wenzelm@251
   757
wenzelm@15703
   758
(* type and constant names *)
wenzelm@15703
   759
wenzelm@15703
   760
fun read_tyname sg raw_c =
wenzelm@15703
   761
  let val c = intern_tycon sg raw_c in
wenzelm@15703
   762
    (case Symtab.lookup (#types (Type.rep_tsig (tsig_of sg)), c) of
wenzelm@15703
   763
      SOME (Type.LogicalType n, _) => Type (c, replicate n dummyT)
wenzelm@15826
   764
    | _ => raise TYPE ("Undeclared type constructor: " ^ quote c, [], []))
wenzelm@15703
   765
  end;
wenzelm@15703
   766
wenzelm@15703
   767
fun read_const sg raw_c =
wenzelm@16288
   768
  let val c = intern_const sg raw_c
wenzelm@16288
   769
  in the_const_type sg c; Const (c, dummyT) end;
wenzelm@15703
   770
wenzelm@15703
   771
wenzelm@251
   772
wenzelm@583
   773
(** infer_types **)         (*exception ERROR*)
wenzelm@251
   774
wenzelm@2979
   775
(*
wenzelm@2979
   776
  def_type: partial map from indexnames to types (constrains Frees, Vars)
wenzelm@2979
   777
  def_sort: partial map from indexnames to sorts (constrains TFrees, TVars)
wenzelm@2979
   778
  used: list of already used type variables
wenzelm@2979
   779
  freeze: if true then generated parameters are turned into TFrees, else TVars
wenzelm@4249
   780
wenzelm@4249
   781
  termss: lists of alternative parses (only one combination should be type-correct)
wenzelm@4249
   782
  typs: expected types
wenzelm@2979
   783
*)
wenzelm@2979
   784
wenzelm@14828
   785
fun infer_types_simult pp sg def_type def_sort used freeze args =
wenzelm@251
   786
  let
wenzelm@3975
   787
    val tsig = tsig_of sg;
wenzelm@4249
   788
skalberg@15574
   789
    val termss = foldr multiply [[]] (map fst args);
wenzelm@4249
   790
    val typs =
wenzelm@4249
   791
      map (fn (_, T) => certify_typ sg T handle TYPE (msg, _, _) => error msg) args;
wenzelm@169
   792
wenzelm@14828
   793
    fun infer ts = OK (TypeInfer.infer_types (Syntax.pp_show_brackets pp) tsig
wenzelm@14828
   794
        (const_type sg) def_type def_sort (intern_const sg) (intern_tycons sg)
wenzelm@14828
   795
        (intern_sort sg) used freeze typs ts)
wenzelm@4249
   796
      handle TYPE (msg, _, _) => Error msg;
clasohm@623
   797
wenzelm@4249
   798
    val err_results = map infer termss;
skalberg@15570
   799
    val errs = List.mapPartial get_error err_results;
skalberg@15570
   800
    val results = List.mapPartial get_ok err_results;
wenzelm@4249
   801
wenzelm@14645
   802
    val ambiguity = length termss;
wenzelm@14645
   803
wenzelm@4249
   804
    fun ambig_msg () =
wenzelm@4249
   805
      if ambiguity > 1 andalso ambiguity <= ! Syntax.ambiguity_level
wenzelm@4249
   806
      then
wenzelm@4249
   807
        error_msg "Got more than one parse tree.\n\
wenzelm@3805
   808
          \Retry with smaller Syntax.ambiguity_level for more information."
nipkow@952
   809
      else ();
wenzelm@4249
   810
  in
wenzelm@4249
   811
    if null results then (ambig_msg (); error (cat_lines errs))
wenzelm@4249
   812
    else if length results = 1 then
wenzelm@4249
   813
      (if ambiguity > ! Syntax.ambiguity_level then
wenzelm@4249
   814
        warning "Fortunately, only one parse tree is type correct.\n\
wenzelm@4249
   815
          \You may still want to disambiguate your grammar or your input."
wenzelm@4249
   816
      else (); hd results)
wenzelm@4249
   817
    else (ambig_msg (); error ("More than one term is type correct:\n" ^
skalberg@15570
   818
      cat_lines (map (Pretty.string_of_term pp) (List.concat (map fst results)))))
wenzelm@4249
   819
  end;
clasohm@623
   820
wenzelm@4249
   821
wenzelm@14828
   822
fun infer_types pp sg def_type def_sort used freeze tsT =
wenzelm@14828
   823
  apfst hd (infer_types_simult pp sg def_type def_sort used freeze [tsT]);
wenzelm@251
   824
wenzelm@251
   825
wenzelm@8607
   826
(** read_def_terms **)
wenzelm@8607
   827
wenzelm@8607
   828
(*read terms, infer types*)
wenzelm@14905
   829
fun read_def_terms' pp is_logtype syn (sign, types, sorts) used freeze sTs =
wenzelm@8607
   830
  let
wenzelm@8607
   831
    fun read (s, T) =
wenzelm@8607
   832
      let val T' = certify_typ sign T handle TYPE (msg, _, _) => error msg
wenzelm@14905
   833
      in (Syntax.read is_logtype syn T' s, T') end;
wenzelm@8607
   834
    val tsTs = map read sTs;
wenzelm@14828
   835
  in infer_types_simult pp sign types sorts used freeze tsTs end;
wenzelm@8607
   836
wenzelm@14645
   837
fun read_def_terms (sign, types, sorts) =
wenzelm@14905
   838
  read_def_terms' (pp sign) (is_logtype sign) (syn_of sign) (sign, types, sorts);
wenzelm@12068
   839
wenzelm@8802
   840
fun simple_read_term sign T s =
skalberg@15531
   841
  (read_def_terms (sign, K NONE, K NONE) [] true [(s, T)]
wenzelm@8802
   842
    handle ERROR => error ("The error(s) above occurred for " ^ s)) |> #1 |> hd;
wenzelm@8802
   843
wenzelm@8607
   844
wenzelm@2979
   845
clasohm@623
   846
(** extend signature **)    (*exception ERROR*)
clasohm@623
   847
wenzelm@620
   848
(** signature extension functions **)  (*exception ERROR*)
wenzelm@386
   849
wenzelm@16133
   850
fun decls_of sg name_of mfixs =
wenzelm@16133
   851
  map (fn (x, y, mx) => (full_name sg (name_of x mx), y)) mfixs;
wenzelm@3791
   852
wenzelm@14645
   853
fun no_read _ _ _ _ decl = decl;
wenzelm@386
   854
wenzelm@386
   855
wenzelm@386
   856
(* add default sort *)
wenzelm@386
   857
wenzelm@16133
   858
fun ext_defS prep_sort sg (syn, tsig, ctab, (naming, spaces), data) S =
wenzelm@14784
   859
  (syn, Type.set_defsort (prep_sort sg syn tsig spaces S) tsig,
wenzelm@16133
   860
    ctab, (naming, spaces), data);
wenzelm@8898
   861
wenzelm@14645
   862
fun ext_defsort arg = ext_defS rd_sort arg;
paulson@8927
   863
fun ext_defsort_i arg = ext_defS cert_sort arg;
wenzelm@386
   864
wenzelm@386
   865
wenzelm@386
   866
(* add type constructors *)
wenzelm@386
   867
wenzelm@16133
   868
fun ext_types sg (syn, tsig, ctab, (naming, spaces), data) types =
wenzelm@14856
   869
  let
wenzelm@16133
   870
    val decls = decls_of sg Syntax.type_name types;
wenzelm@14905
   871
    val syn' = map_syntax (Syntax.extend_type_gram types) syn;
wenzelm@14856
   872
    val tsig' = Type.add_types decls tsig;
wenzelm@16133
   873
  in (syn', tsig', ctab, (naming, add_names sg typeK (map fst decls) spaces), data) end;
wenzelm@386
   874
wenzelm@386
   875
wenzelm@386
   876
(* add type abbreviations *)
wenzelm@386
   877
wenzelm@14645
   878
fun read_abbr sg syn tsig spaces (t, vs, rhs_src) =
skalberg@15531
   879
  (t, vs, rd_raw_typ (make_syntax sg syn) tsig spaces (K NONE) rhs_src)
wenzelm@386
   880
    handle ERROR => error ("in type abbreviation " ^ t);
wenzelm@386
   881
wenzelm@16133
   882
fun ext_abbrs rd_abbr sg (syn, tsig, ctab, (naming, spaces), data) abbrs =
wenzelm@386
   883
  let
wenzelm@386
   884
    fun mfix_of (t, vs, _, mx) = (t, length vs, mx);
wenzelm@14905
   885
    val syn' = syn |> map_syntax (Syntax.extend_type_gram (map mfix_of abbrs));
wenzelm@386
   886
wenzelm@3791
   887
    val abbrs' =
wenzelm@3791
   888
      map (fn (t, vs, rhs, mx) =>
wenzelm@16133
   889
        (full_name sg (Syntax.type_name t mx), vs, rhs)) abbrs;
wenzelm@16133
   890
    val spaces' = add_names sg typeK (map #1 abbrs') spaces;
wenzelm@14645
   891
    val decls = map (rd_abbr sg syn' tsig spaces') abbrs';
wenzelm@16133
   892
  in (syn', Type.add_abbrs decls tsig, ctab, (naming, spaces'), data) end;
wenzelm@386
   893
wenzelm@3791
   894
fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs;
wenzelm@3791
   895
fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs;
wenzelm@386
   896
wenzelm@386
   897
wenzelm@14784
   898
(* add nonterminals *)
wenzelm@14784
   899
wenzelm@16133
   900
fun ext_nonterminals sg (syn, tsig, ctab, (naming, spaces), data) bnames =
wenzelm@16133
   901
  let val names = map (full_name sg) bnames in
wenzelm@14784
   902
    (map_syntax (Syntax.extend_consts names) syn,
wenzelm@14784
   903
      Type.add_nonterminals names tsig, ctab,
wenzelm@16133
   904
      (naming, add_names sg typeK names spaces), data)
wenzelm@14784
   905
  end;
wenzelm@14784
   906
wenzelm@14784
   907
wenzelm@386
   908
(* add type arities *)
wenzelm@386
   909
wenzelm@16133
   910
fun ext_ars int prep_sort sg (syn, tsig, ctab, (naming, spaces), data) arities =
wenzelm@386
   911
  let
wenzelm@14645
   912
    val prepS = prep_sort sg syn tsig spaces;
wenzelm@8898
   913
    fun prep_arity (c, Ss, S) =
wenzelm@8898
   914
      (if int then intrn spaces typeK c else c, map prepS Ss, prepS S);
wenzelm@14828
   915
    val tsig' = Type.add_arities (pp sg) (map prep_arity arities) tsig;
wenzelm@16133
   916
  in (syn, tsig', ctab, (naming, spaces), data) end;
wenzelm@386
   917
wenzelm@14645
   918
fun ext_arities arg = ext_ars true rd_sort arg;
paulson@8927
   919
fun ext_arities_i arg = ext_ars false cert_sort arg;
wenzelm@8898
   920
wenzelm@386
   921
wenzelm@386
   922
(* add term constants and syntax *)
wenzelm@386
   923
wenzelm@16133
   924
fun const_name sg c mx =
wenzelm@16133
   925
  full_name sg (Syntax.const_name c mx);
wenzelm@3805
   926
wenzelm@386
   927
fun err_in_const c =
wenzelm@386
   928
  error ("in declaration of constant " ^ quote c);
wenzelm@386
   929
wenzelm@386
   930
fun err_dup_consts cs =
wenzelm@386
   931
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
wenzelm@386
   932
wenzelm@251
   933
wenzelm@16133
   934
fun read_cnst sg syn tsig (naming, spaces) (c, ty_src, mx) =
skalberg@15531
   935
  (c, rd_raw_typ (make_syntax sg syn) tsig spaces (K NONE) ty_src, mx)
wenzelm@16133
   936
    handle ERROR => err_in_const (const_name sg c mx);
wenzelm@386
   937
wenzelm@16133
   938
fun change_cnsts change_gram rd_const syn_only prmode sg (syn, tsig, ctab, (naming, spaces), data)
wenzelm@14645
   939
    raw_consts =
wenzelm@386
   940
  let
wenzelm@14784
   941
    val prep_typ = compress_type o Type.varifyT o Type.no_tvars o
wenzelm@14993
   942
     (if syn_only then Type.cert_typ_syntax tsig else Term.no_dummyT o Type.cert_typ tsig);
wenzelm@14784
   943
    fun prep_const (c, ty, mx) = (c, prep_typ ty, mx) handle TYPE (msg, _, _) =>
wenzelm@16133
   944
      (error_msg msg; err_in_const (const_name sg c mx));
wenzelm@386
   945
wenzelm@16133
   946
    val consts = map (prep_const o rd_const sg syn tsig (naming, spaces)) raw_consts;
wenzelm@386
   947
    val decls =
wenzelm@386
   948
      if syn_only then []
wenzelm@16133
   949
      else decls_of sg Syntax.const_name consts;
wenzelm@386
   950
  in
wenzelm@15746
   951
    (map_syntax (change_gram (is_logtype sg) prmode consts) syn, tsig,
wenzelm@14784
   952
      Symtab.extend (ctab, map (fn (c, T) => (c, (T, stamp ()))) decls)
wenzelm@3791
   953
        handle Symtab.DUPS cs => err_dup_consts cs,
wenzelm@16133
   954
      (naming, add_names sg constK (map fst decls) spaces), data)
wenzelm@386
   955
  end;
wenzelm@386
   956
wenzelm@15746
   957
fun ext_cnsts rd = change_cnsts Syntax.extend_const_gram rd;
wenzelm@15746
   958
fun rem_cnsts rd = change_cnsts Syntax.remove_const_gram rd;
wenzelm@15746
   959
wenzelm@14905
   960
fun ext_consts_i x = ext_cnsts no_read false Syntax.default_mode x;
wenzelm@15746
   961
fun ext_consts x   = ext_cnsts read_cnst false Syntax.default_mode x;
wenzelm@14905
   962
fun ext_syntax_i x = ext_cnsts no_read true Syntax.default_mode x;
wenzelm@15746
   963
fun ext_syntax x   = ext_cnsts read_cnst true Syntax.default_mode x;
wenzelm@15746
   964
wenzelm@15746
   965
fun ext_modesyntax_i x y (m, cs) = ext_cnsts no_read true m x y cs;
wenzelm@15746
   966
fun ext_modesyntax x y (m, cs)   = ext_cnsts read_cnst true m x y cs;
wenzelm@15746
   967
fun rem_modesyntax_i x y (m, cs) = rem_cnsts no_read true m x y cs;
wenzelm@15746
   968
fun rem_modesyntax x y (m, cs)   = rem_cnsts read_cnst true m x y cs;
wenzelm@386
   969
wenzelm@386
   970
wenzelm@386
   971
(* add type classes *)
wenzelm@386
   972
wenzelm@386
   973
fun const_of_class c = c ^ "_class";
wenzelm@386
   974
wenzelm@386
   975
fun class_of_const c_class =
wenzelm@386
   976
  let
skalberg@15570
   977
    val c = implode (Library.take (size c_class - size "_class", explode c_class));
wenzelm@386
   978
  in
wenzelm@386
   979
    if const_of_class c = c_class then c
wenzelm@3791
   980
    else raise TERM ("class_of_const: bad name " ^ quote c_class, [])
wenzelm@386
   981
  end;
wenzelm@386
   982
wenzelm@386
   983
wenzelm@16133
   984
fun ext_classes int sg (syn, tsig, ctab, (naming, spaces), data) classes =
wenzelm@386
   985
  let
wenzelm@562
   986
    val names = map fst classes;
wenzelm@386
   987
    val consts =
wenzelm@386
   988
      map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names;
wenzelm@3791
   989
wenzelm@16133
   990
    val full_names = map (full_name sg) names;
wenzelm@16133
   991
    val spaces' = add_names sg classK full_names spaces;
wenzelm@3791
   992
    val intrn = if int then map (intrn_class spaces') else I;
wenzelm@3791
   993
    val classes' =
wenzelm@3791
   994
      ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes);
wenzelm@386
   995
  in
wenzelm@14645
   996
    ext_consts_i sg
wenzelm@14645
   997
      (map_syntax (Syntax.extend_consts names) syn,
wenzelm@16133
   998
        Type.add_classes (pp sg) classes' tsig, ctab, (naming, spaces'), data)
wenzelm@3791
   999
    consts
wenzelm@386
  1000
  end;
wenzelm@386
  1001
wenzelm@386
  1002
wenzelm@2963
  1003
(* add to classrel *)
wenzelm@421
  1004
wenzelm@16133
  1005
fun ext_classrel int sg (syn, tsig, ctab, (naming, spaces), data) pairs =
wenzelm@3791
  1006
  let val intrn = if int then map (pairself (intrn_class spaces)) else I in
wenzelm@16133
  1007
    (syn, Type.add_classrel (pp sg) (intrn pairs) tsig, ctab, (naming, spaces), data)
wenzelm@3791
  1008
  end;
wenzelm@421
  1009
wenzelm@421
  1010
wenzelm@14645
  1011
(* add translation functions *)
wenzelm@14645
  1012
wenzelm@15746
  1013
local
wenzelm@15746
  1014
wenzelm@15746
  1015
fun mk trs = map Syntax.mk_trfun trs;
wenzelm@15746
  1016
wenzelm@14645
  1017
fun ext_trfs ext non_typed sg (syn, tsig, ctab, names, data) (atrs, trs, tr's, atr's) =
wenzelm@15746
  1018
  let val syn' = syn |> ext (mk atrs, mk trs, mk (map (apsnd non_typed) tr's), mk atr's)
wenzelm@14645
  1019
  in make_syntax sg syn'; (syn', tsig, ctab, names, data) end;
wenzelm@14645
  1020
wenzelm@14645
  1021
fun ext_trfsT ext sg (syn, tsig, ctab, names, data) tr's =
wenzelm@15746
  1022
  let val syn' = syn |> ext ([], [], mk tr's, [])
wenzelm@14645
  1023
  in make_syntax sg syn'; (syn', tsig, ctab, names, data) end;
wenzelm@14645
  1024
wenzelm@15746
  1025
in
wenzelm@15746
  1026
wenzelm@14645
  1027
fun ext_trfuns sg = ext_trfs (map_syntax o Syntax.extend_trfuns) Syntax.non_typed_tr' sg;
wenzelm@14645
  1028
fun ext_trfunsT sg = ext_trfsT (map_syntax o Syntax.extend_trfuns) sg;
wenzelm@14645
  1029
fun ext_advanced_trfuns sg = ext_trfs extend_trfuns Syntax.non_typed_tr'' sg;
wenzelm@14645
  1030
fun ext_advanced_trfunsT sg = ext_trfsT extend_trfuns sg;
wenzelm@14645
  1031
wenzelm@15746
  1032
end;
wenzelm@15746
  1033
wenzelm@14645
  1034
wenzelm@14645
  1035
(* add token translation functions *)
wenzelm@14645
  1036
wenzelm@14645
  1037
fun ext_tokentrfuns sg (syn, tsig, ctab, names, data) args =
wenzelm@14645
  1038
  (map_syntax (Syntax.extend_tokentrfuns args) syn, tsig, ctab, names, data);
wenzelm@14645
  1039
wenzelm@14645
  1040
wenzelm@4619
  1041
(* add translation rules *)
wenzelm@4619
  1042
wenzelm@16133
  1043
fun ext_trrules sg (syn, tsig, ctab, (naming, spaces), data) args =
wenzelm@14905
  1044
  (syn |> map_syntax (Syntax.extend_trrules (is_logtype sg) (make_syntax sg syn)
wenzelm@14645
  1045
      (map (Syntax.map_trrule (fn (root, str) => (intrn spaces typeK root, str))) args)),
wenzelm@16133
  1046
    tsig, ctab, (naming, spaces), data);
wenzelm@4619
  1047
wenzelm@14645
  1048
fun ext_trrules_i _ (syn, tsig, ctab, names, data) args =
wenzelm@14645
  1049
  (syn |> map_syntax (Syntax.extend_trrules_i args), tsig, ctab, names, data);
wenzelm@3791
  1050
wenzelm@3791
  1051
wenzelm@16133
  1052
(* change naming *)
wenzelm@3791
  1053
wenzelm@16133
  1054
fun change_naming f _ (syn, tsig, ctab, (naming, spaces), data) args =
wenzelm@16133
  1055
  (syn, tsig, ctab, (f args naming, spaces), data);
wenzelm@3791
  1056
wenzelm@3791
  1057
wenzelm@8725
  1058
(* change name space *)
wenzelm@8725
  1059
wenzelm@16133
  1060
fun ext_add_space sg (syn, tsig, ctab, (naming, spaces), data) (kind, names) =
wenzelm@16133
  1061
  (syn, tsig, ctab, (naming, add_names sg kind names spaces), data);
wenzelm@3791
  1062
wenzelm@16133
  1063
fun ext_hide_space _ (syn, tsig, ctab, (naming, spaces), data) (b, (kind, xnames)) =
wenzelm@16133
  1064
  (syn, tsig, ctab, (naming, hide_names b kind (map (intrn spaces kind) xnames) spaces), data);
wenzelm@8725
  1065
wenzelm@16133
  1066
fun ext_hide_space_i _ (syn, tsig, ctab, (naming, spaces), data) (b, (kind, names)) =
wenzelm@16133
  1067
  (syn, tsig, ctab, (naming, hide_names b kind names spaces), data);
wenzelm@386
  1068
wenzelm@386
  1069
wenzelm@3967
  1070
(* signature data *)
wenzelm@386
  1071
wenzelm@12310
  1072
fun ext_init_data sg (syn, tsig, ctab, names, data) (kind, (e, cp, ext, mrg, prt)) =
wenzelm@12310
  1073
  (syn, tsig, ctab, names, init_data_sg sg data kind e cp ext mrg prt);
wenzelm@386
  1074
wenzelm@4998
  1075
fun ext_put_data sg (syn, tsig, ctab, names, data) (kind, f, x) =
wenzelm@4998
  1076
  (syn, tsig, ctab, names, put_data_sg sg data kind f x);
wenzelm@386
  1077
wenzelm@386
  1078
wenzelm@12310
  1079
fun copy_data (k, (e, mths as (cp, _, _, _))) =
wenzelm@6961
  1080
  (k, (cp e handle exn => err_method "copy" (Object.name_of_kind k) exn, mths));
wenzelm@6546
  1081
wenzelm@16133
  1082
fun copy (sg as Sg ({id = _, stamps, syn}, {self, tsig, consts, naming, spaces, data})) =
wenzelm@6546
  1083
  let
wenzelm@6546
  1084
    val _ = check_stale sg;
skalberg@15531
  1085
    val self' = SgRef (SOME (ref sg));
wenzelm@6546
  1086
    val Data tab = data;
wenzelm@6546
  1087
    val data' = Data (Symtab.map copy_data tab);
wenzelm@16133
  1088
  in create_sign self' stamps "#" (syn, tsig, consts, (naming, spaces), data') end;
wenzelm@6546
  1089
wenzelm@6546
  1090
wenzelm@386
  1091
(* the external interfaces *)
wenzelm@386
  1092
wenzelm@14645
  1093
val add_classes          = extend_sign true (ext_classes true) "#";
wenzelm@14645
  1094
val add_classes_i        = extend_sign true (ext_classes false) "#";
wenzelm@14645
  1095
val add_classrel         = extend_sign true (ext_classrel true) "#";
wenzelm@14645
  1096
val add_classrel_i       = extend_sign true (ext_classrel false) "#";
wenzelm@14645
  1097
val add_defsort          = extend_sign true ext_defsort "#";
wenzelm@14645
  1098
val add_defsort_i        = extend_sign true ext_defsort_i "#";
wenzelm@14645
  1099
val add_types            = extend_sign true ext_types "#";
wenzelm@14645
  1100
val add_nonterminals     = extend_sign true ext_nonterminals "#";
wenzelm@14645
  1101
val add_tyabbrs          = extend_sign true ext_tyabbrs "#";
wenzelm@14645
  1102
val add_tyabbrs_i        = extend_sign true ext_tyabbrs_i "#";
wenzelm@14645
  1103
val add_arities          = extend_sign true ext_arities "#";
wenzelm@14645
  1104
val add_arities_i        = extend_sign true ext_arities_i "#";
wenzelm@14645
  1105
val add_consts           = extend_sign true ext_consts "#";
wenzelm@14645
  1106
val add_consts_i         = extend_sign true ext_consts_i "#";
wenzelm@14645
  1107
val add_syntax           = extend_sign true ext_syntax "#";
wenzelm@14645
  1108
val add_syntax_i         = extend_sign true ext_syntax_i "#";
wenzelm@14645
  1109
val add_modesyntax       = extend_sign true ext_modesyntax "#";
wenzelm@14645
  1110
val add_modesyntax_i     = extend_sign true ext_modesyntax_i "#";
wenzelm@15746
  1111
val del_modesyntax       = extend_sign true rem_modesyntax "#";
wenzelm@15746
  1112
val del_modesyntax_i     = extend_sign true rem_modesyntax_i "#";
wenzelm@14645
  1113
val add_trfuns           = extend_sign true ext_trfuns "#";
wenzelm@14645
  1114
val add_trfunsT          = extend_sign true ext_trfunsT "#";
wenzelm@14645
  1115
val add_advanced_trfuns  = extend_sign true ext_advanced_trfuns "#";
wenzelm@14645
  1116
val add_advanced_trfunsT = extend_sign true ext_advanced_trfunsT "#";
wenzelm@14645
  1117
val add_tokentrfuns      = extend_sign true ext_tokentrfuns "#";
wenzelm@14645
  1118
val add_trrules          = extend_sign true ext_trrules "#";
wenzelm@14645
  1119
val add_trrules_i        = extend_sign true ext_trrules_i "#";
wenzelm@16133
  1120
val add_path             = extend_sign true (change_naming NameSpace.add_path) "#";
wenzelm@16133
  1121
val qualified_names      = extend_sign true (change_naming (K NameSpace.qualified_names)) "#" ();
wenzelm@16133
  1122
val no_base_names        = extend_sign true (change_naming (K NameSpace.no_base_names)) "#" ();
wenzelm@16133
  1123
val custom_accesses      = extend_sign true (change_naming NameSpace.custom_accesses) "#";
wenzelm@16133
  1124
val set_policy           = extend_sign true (change_naming NameSpace.set_policy) "#";
wenzelm@16189
  1125
val restore_naming       = extend_sign true (change_naming (K o naming_of)) "#";
wenzelm@14645
  1126
val add_space            = extend_sign true ext_add_space "#";
wenzelm@14645
  1127
val hide_space           = curry (extend_sign true ext_hide_space "#");
wenzelm@14645
  1128
val hide_space_i         = curry (extend_sign true ext_hide_space_i "#");
wenzelm@14645
  1129
val init_data            = extend_sign true ext_init_data "#";
wenzelm@14645
  1130
fun put_data k f x       = extend_sign true ext_put_data "#" (k, f, x);
wenzelm@14645
  1131
fun add_name name        = extend_sign true (K K) name ();
wenzelm@14645
  1132
val prep_ext             = extend_sign false (K K) "#" ();
wenzelm@386
  1133
wenzelm@386
  1134
wenzelm@3867
  1135
wenzelm@10404
  1136
(** merge signatures **)        (*exception TERM*)
wenzelm@3867
  1137
wenzelm@4228
  1138
(* merge_stamps *)
wenzelm@4228
  1139
wenzelm@4228
  1140
fun merge_stamps stamps1 stamps2 =
wenzelm@12296
  1141
  let val stamps = merge_lists' stamps1 stamps2 in
wenzelm@4228
  1142
    (case duplicates (map ! stamps) of
wenzelm@4228
  1143
      [] => stamps
wenzelm@15826
  1144
    | dups => error ("Attempt to merge different versions of theories " ^ commas_quote dups))
wenzelm@4228
  1145
  end;
wenzelm@4228
  1146
wenzelm@4228
  1147
paulson@15889
  1148
(*** trivial merge ***)
paulson@15889
  1149
paulson@15889
  1150
(*For concise error messages: the last few components only*)
paulson@15889
  1151
fun abbrev_stamp_names_of sg = rev (List.take(map ! (stamps_of sg), 5));
paulson@15889
  1152
paulson@15889
  1153
fun abbrev_str_of sg = 
paulson@15889
  1154
  let val sts = "..., " ^ commas (abbrev_stamp_names_of sg)
paulson@15889
  1155
          handle Subscript => commas (stamp_names_of sg)
paulson@15889
  1156
  in "{" ^ sts ^ "}" end;
wenzelm@3877
  1157
skalberg@15531
  1158
fun merge_refs (sgr1 as SgRef (SOME (ref (sg1 as Sg ({stamps = s1, ...}, _)))),
skalberg@15531
  1159
        sgr2 as SgRef (SOME (ref (sg2 as Sg ({stamps = s2, ...}, _))))) =
wenzelm@15703
  1160
      if subsig (sg2, sg1) then sgr1
wenzelm@3967
  1161
      else if subsig (sg1, sg2) then sgr2
wenzelm@4228
  1162
      else (merge_stamps s1 s2; (*check for different versions*)
paulson@15889
  1163
            error ("Attempt to do non-trivial merge of signatures\n" ^ 
paulson@15889
  1164
                   abbrev_str_of sg1 ^ "\n" ^ 
paulson@15889
  1165
                   abbrev_str_of sg2 ^ "\n"))
wenzelm@3967
  1166
  | merge_refs _ = sys_error "Sign.merge_refs";
wenzelm@3867
  1167
wenzelm@4627
  1168
val merge = deref o merge_refs o pairself self_ref;
wenzelm@4627
  1169
wenzelm@3867
  1170
wenzelm@15703
  1171
(* merge_list *)              (*exception TERM/ERROR*)
wenzelm@15703
  1172
wenzelm@15703
  1173
local
wenzelm@10932
  1174
wenzelm@6961
  1175
fun nontriv_merge (sg1, sg2) =
wenzelm@15703
  1176
  if subsig_internal (sg2, sg1) then sg1
wenzelm@15703
  1177
  else if subsig_internal (sg1, sg2) then sg2
wenzelm@15703
  1178
  else
wenzelm@15801
  1179
    if exists_stamp CPureN sg1 <> exists_stamp CPureN sg2
wenzelm@15801
  1180
    then error "Cannot merge Pure and CPure developments"
wenzelm@251
  1181
  else
wenzelm@251
  1182
    let
wenzelm@14645
  1183
      val Sg ({id = _, stamps = stamps1, syn = Syn (syntax1, trfuns1)},
wenzelm@14784
  1184
          {self = _, tsig = tsig1, consts = consts1,
wenzelm@16133
  1185
            naming = _, spaces = spaces1, data = data1}) = sg1;
wenzelm@14645
  1186
      val Sg ({id = _, stamps = stamps2, syn = Syn (syntax2, trfuns2)},
wenzelm@14784
  1187
          {self = _, tsig = tsig2, consts = consts2,
wenzelm@16133
  1188
            naming = _, spaces = spaces2, data = data2}) = sg2;
wenzelm@386
  1189
wenzelm@4228
  1190
      val stamps = merge_stamps stamps1 stamps2;
wenzelm@14645
  1191
      val syntax = Syntax.merge_syntaxes syntax1 syntax2;
wenzelm@14645
  1192
      val trfuns = merge_trfuns trfuns1 trfuns2;
wenzelm@14784
  1193
      val consts = Symtab.merge eq_snd (consts1, consts2)
wenzelm@14784
  1194
        handle Symtab.DUPS cs => err_dup_consts cs;
wenzelm@3791
  1195
wenzelm@16133
  1196
      val naming = NameSpace.default_naming;
wenzelm@3791
  1197
      val kinds = distinct (map fst (spaces1 @ spaces2));
wenzelm@3791
  1198
      val spaces =
wenzelm@3791
  1199
        kinds ~~
wenzelm@3791
  1200
          ListPair.map NameSpace.merge
wenzelm@16133
  1201
            (map (get_space spaces1) kinds, map (get_space spaces2) kinds);
wenzelm@3867
  1202
wenzelm@4256
  1203
      val data = merge_data (data1, data2);
wenzelm@3967
  1204
skalberg@15531
  1205
      val pre_sign = make_sign (ref "", SgRef (SOME (ref dummy_sg)),
wenzelm@16133
  1206
        tsig1, consts, Syn (syntax, trfuns), naming, spaces, data, ref "#" :: stamps1);
wenzelm@14905
  1207
      val tsig = Type.merge_tsigs (pp pre_sign) (tsig1, tsig2);
wenzelm@14905
  1208
wenzelm@14905
  1209
      val self_ref = ref dummy_sg;
skalberg@15531
  1210
      val self = SgRef (SOME self_ref);
wenzelm@14905
  1211
      val sign = make_sign (ref "", self, tsig, consts, Syn (syntax, trfuns),
wenzelm@16133
  1212
        naming, spaces, data, stamps);
wenzelm@14645
  1213
    in self_ref := sign; syn_of sign; sign end;
wenzelm@3791
  1214
wenzelm@15703
  1215
in
wenzelm@15703
  1216
wenzelm@15703
  1217
fun prep_ext_merge sgs =
wenzelm@15703
  1218
  if null sgs then
wenzelm@15703
  1219
    error "Merge: no parent theories"
wenzelm@15703
  1220
  else if exists is_draft sgs then
wenzelm@15703
  1221
    error "Attempt to merge draft theories"
wenzelm@15703
  1222
  else
wenzelm@15703
  1223
    Library.foldl nontriv_merge (hd sgs, tl sgs)
wenzelm@15703
  1224
    |> prep_ext
wenzelm@15703
  1225
    |> add_path "/";
wenzelm@15703
  1226
clasohm@0
  1227
end;
wenzelm@15703
  1228
wenzelm@15703
  1229
end;