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