src/Pure/Syntax/syntax.ML
author wenzelm
Tue Oct 16 19:45:57 2007 +0200 (2007-10-16)
changeset 25060 17c313217998
parent 25043 32ed65dc1bf4
child 25122 f37d7dd25c88
permissions -rw-r--r--
Syntax.(un)check: explicit result option;
wenzelm@18
     1
(*  Title:      Pure/Syntax/syntax.ML
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@18
     4
wenzelm@24263
     5
Standard Isabelle syntax, based on arbitrary context-free grammars
wenzelm@24263
     6
(specified by mixfix declarations).
clasohm@0
     7
*)
clasohm@0
     8
wenzelm@260
     9
signature BASIC_SYNTAX =
wenzelm@2383
    10
sig
wenzelm@260
    11
  include AST0
wenzelm@556
    12
  include SYN_TRANS0
wenzelm@556
    13
  include MIXFIX0
wenzelm@260
    14
  include PRINTER0
wenzelm@2383
    15
end;
wenzelm@260
    16
clasohm@0
    17
signature SYNTAX =
wenzelm@2383
    18
sig
wenzelm@260
    19
  include AST1
clasohm@0
    20
  include LEXICON0
wenzelm@237
    21
  include SYN_EXT0
clasohm@0
    22
  include TYPE_EXT0
wenzelm@556
    23
  include SYN_TRANS1
wenzelm@556
    24
  include MIXFIX1
clasohm@0
    25
  include PRINTER0
wenzelm@15755
    26
  val extend_trtab: string -> (string * ('a * stamp)) list ->
wenzelm@15755
    27
    ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table
wenzelm@15755
    28
  val merge_trtabs: string -> ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table ->
wenzelm@15755
    29
    ('a * stamp) Symtab.table
wenzelm@14648
    30
  val merge_tr'tabs: ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table
wenzelm@14648
    31
    -> ('a * stamp) list Symtab.table
wenzelm@15755
    32
  val extend_tr'tab: (string * ('a * stamp)) list ->
wenzelm@15755
    33
    ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table
wenzelm@1158
    34
  datatype 'a trrule =
wenzelm@3526
    35
    ParseRule of 'a * 'a |
wenzelm@3526
    36
    PrintRule of 'a * 'a |
wenzelm@3526
    37
    ParsePrintRule of 'a * 'a
wenzelm@556
    38
  type syntax
wenzelm@17079
    39
  val eq_syntax: syntax * syntax -> bool
wenzelm@14687
    40
  val is_keyword: syntax -> string -> bool
wenzelm@20784
    41
  type mode
wenzelm@24970
    42
  val mode_default: mode
wenzelm@24970
    43
  val mode_input: mode
wenzelm@21826
    44
  val internalM: string
wenzelm@12073
    45
  val extend_type_gram: (string * int * mixfix) list -> syntax -> syntax
wenzelm@14904
    46
  val extend_const_gram: (string -> bool) ->
wenzelm@20784
    47
    mode -> (string * typ * mixfix) list -> syntax -> syntax
wenzelm@12073
    48
  val extend_consts: string list -> syntax -> syntax
wenzelm@12073
    49
  val extend_trfuns:
wenzelm@15755
    50
    (string * ((ast list -> ast) * stamp)) list *
wenzelm@15755
    51
    (string * ((term list -> term) * stamp)) list *
wenzelm@15755
    52
    (string * ((bool -> typ -> term list -> term) * stamp)) list *
wenzelm@15755
    53
    (string * ((ast list -> ast) * stamp)) list -> syntax -> syntax
wenzelm@16613
    54
  val extend_advanced_trfuns:
wenzelm@21772
    55
    (string * ((Proof.context -> ast list -> ast) * stamp)) list *
wenzelm@21772
    56
    (string * ((Proof.context -> term list -> term) * stamp)) list *
wenzelm@21772
    57
    (string * ((Proof.context -> bool -> typ -> term list -> term) * stamp)) list *
wenzelm@21772
    58
    (string * ((Proof.context -> ast list -> ast) * stamp)) list -> syntax -> syntax
wenzelm@23660
    59
  val extend_tokentrfuns: (string * string * (string -> output * int)) list -> syntax -> syntax
wenzelm@19262
    60
  val remove_const_gram: (string -> bool) ->
wenzelm@20784
    61
    mode -> (string * typ * mixfix) list -> syntax -> syntax
wenzelm@21772
    62
  val extend_trrules: Proof.context -> (string -> bool) -> syntax ->
wenzelm@14904
    63
    (string * string) trrule list -> syntax -> syntax
wenzelm@21772
    64
  val remove_trrules: Proof.context -> (string -> bool) -> syntax ->
wenzelm@19262
    65
    (string * string) trrule list -> syntax -> syntax
wenzelm@19262
    66
  val extend_trrules_i: ast trrule list -> syntax -> syntax
wenzelm@19262
    67
  val remove_trrules_i: ast trrule list -> syntax -> syntax
wenzelm@4618
    68
  val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
wenzelm@556
    69
  val merge_syntaxes: syntax -> syntax -> syntax
wenzelm@18720
    70
  val basic_syn: syntax
wenzelm@18720
    71
  val basic_nonterms: string list
wenzelm@556
    72
  val print_gram: syntax -> unit
wenzelm@556
    73
  val print_trans: syntax -> unit
wenzelm@556
    74
  val print_syntax: syntax -> unit
wenzelm@21772
    75
  val read: Proof.context -> (string -> bool) -> syntax -> typ -> string -> term list
wenzelm@24372
    76
  val standard_parse_term: Pretty.pp -> (term -> term Exn.result) ->
wenzelm@22703
    77
    (((string * int) * sort) list -> string * int -> Term.sort) ->
wenzelm@22703
    78
    (string -> string option) -> (string -> string option) ->
wenzelm@22703
    79
    (typ -> typ) -> (sort -> sort) -> Proof.context ->
wenzelm@24372
    80
    (string -> bool) -> syntax -> typ -> string -> term
wenzelm@24278
    81
  val standard_parse_typ: Proof.context -> syntax -> ((indexname * sort) list -> indexname -> sort) ->
wenzelm@16613
    82
    (sort -> sort) -> string -> typ
wenzelm@24278
    83
  val standard_parse_sort: Proof.context -> syntax -> (sort -> sort) -> string -> sort
wenzelm@24923
    84
  val standard_unparse_term: (string -> xstring) -> Proof.context -> syntax -> bool -> term -> Pretty.T
wenzelm@24923
    85
  val standard_unparse_typ: Proof.context -> syntax -> typ -> Pretty.T
wenzelm@24923
    86
  val standard_unparse_sort: Proof.context -> syntax -> sort -> Pretty.T
clasohm@882
    87
  val ambiguity_level: int ref
skalberg@14177
    88
  val ambiguity_is_error: bool ref
wenzelm@24263
    89
  val parse_sort: Proof.context -> string -> sort
wenzelm@24263
    90
  val parse_typ: Proof.context -> string -> typ
wenzelm@24263
    91
  val parse_term: Proof.context -> string -> term
wenzelm@24263
    92
  val parse_prop: Proof.context -> string -> term
wenzelm@24768
    93
  val unparse_sort: Proof.context -> sort -> Pretty.T
wenzelm@24923
    94
  val unparse_classrel: Proof.context -> class list -> Pretty.T
wenzelm@24923
    95
  val unparse_arity: Proof.context -> arity -> Pretty.T
wenzelm@24768
    96
  val unparse_typ: Proof.context -> typ -> Pretty.T
wenzelm@24768
    97
  val unparse_term: Proof.context -> term -> Pretty.T
wenzelm@24768
    98
  val install_operations:
wenzelm@24768
    99
   {parse_sort: Proof.context -> string -> sort,
wenzelm@24768
   100
    parse_typ: Proof.context -> string -> typ,
wenzelm@24768
   101
    parse_term: Proof.context -> string -> term,
wenzelm@24768
   102
    parse_prop: Proof.context -> string -> term,
wenzelm@24768
   103
    unparse_sort: Proof.context -> sort -> Pretty.T,
wenzelm@24768
   104
    unparse_typ: Proof.context -> typ -> Pretty.T,
wenzelm@24768
   105
    unparse_term: Proof.context -> term -> Pretty.T} -> unit
wenzelm@24768
   106
  val print_checks: Proof.context -> unit
wenzelm@24768
   107
  val add_typ_check: int -> string ->
wenzelm@25060
   108
    (typ list -> Proof.context -> (typ list * Proof.context) option) ->
wenzelm@24768
   109
    Context.generic -> Context.generic
wenzelm@24768
   110
  val add_term_check: int -> string ->
wenzelm@25060
   111
    (term list -> Proof.context -> (term list * Proof.context) option) ->
wenzelm@24768
   112
    Context.generic -> Context.generic
wenzelm@24768
   113
  val add_typ_uncheck: int -> string ->
wenzelm@25060
   114
    (typ list -> Proof.context -> (typ list * Proof.context) option) ->
wenzelm@24768
   115
    Context.generic -> Context.generic
wenzelm@24768
   116
  val add_term_uncheck: int -> string ->
wenzelm@25060
   117
    (term list -> Proof.context -> (term list * Proof.context) option) ->
wenzelm@24768
   118
    Context.generic -> Context.generic
wenzelm@24488
   119
  val check_sort: Proof.context -> sort -> sort
wenzelm@24512
   120
  val check_typ: Proof.context -> typ -> typ
wenzelm@24512
   121
  val check_term: Proof.context -> term -> term
wenzelm@24512
   122
  val check_prop: Proof.context -> term -> term
wenzelm@24488
   123
  val check_typs: Proof.context -> typ list -> typ list
wenzelm@24263
   124
  val check_terms: Proof.context -> term list -> term list
wenzelm@24263
   125
  val check_props: Proof.context -> term list -> term list
wenzelm@24923
   126
  val uncheck_sort: Proof.context -> sort -> sort
wenzelm@24923
   127
  val uncheck_arity: Proof.context -> arity -> arity
wenzelm@24923
   128
  val uncheck_classrel: Proof.context -> class list -> class list
wenzelm@24768
   129
  val uncheck_typs: Proof.context -> typ list -> typ list
wenzelm@24768
   130
  val uncheck_terms: Proof.context -> term list -> term list
wenzelm@24263
   131
  val read_sort: Proof.context -> string -> sort
wenzelm@24263
   132
  val read_typ: Proof.context -> string -> typ
wenzelm@24488
   133
  val read_term: Proof.context -> string -> term
wenzelm@24488
   134
  val read_prop: Proof.context -> string -> term
wenzelm@24263
   135
  val read_terms: Proof.context -> string list -> term list
wenzelm@24263
   136
  val read_props: Proof.context -> string list -> term list
wenzelm@24709
   137
  val read_sort_global: theory -> string -> sort
wenzelm@24709
   138
  val read_typ_global: theory -> string -> typ
wenzelm@24709
   139
  val read_term_global: theory -> string -> term
wenzelm@24709
   140
  val read_prop_global: theory -> string -> term
wenzelm@24923
   141
  val pretty_term: Proof.context -> term -> Pretty.T
wenzelm@24923
   142
  val pretty_typ: Proof.context -> typ -> Pretty.T
wenzelm@24923
   143
  val pretty_sort: Proof.context -> sort -> Pretty.T
wenzelm@24923
   144
  val pretty_classrel: Proof.context -> class list -> Pretty.T
wenzelm@24923
   145
  val pretty_arity: Proof.context -> arity -> Pretty.T
wenzelm@24923
   146
  val string_of_term: Proof.context -> term -> string
wenzelm@24923
   147
  val string_of_typ: Proof.context -> typ -> string
wenzelm@24923
   148
  val string_of_sort: Proof.context -> sort -> string
wenzelm@24923
   149
  val string_of_classrel: Proof.context -> class list -> string
wenzelm@24923
   150
  val string_of_arity: Proof.context -> arity -> string
wenzelm@24970
   151
  val pp: Proof.context -> Pretty.pp
wenzelm@2383
   152
end;
clasohm@0
   153
wenzelm@12094
   154
structure Syntax: SYNTAX =
clasohm@0
   155
struct
clasohm@0
   156
wenzelm@237
   157
(** tables of translation functions **)
wenzelm@237
   158
wenzelm@5692
   159
(* parse (ast) translations *)
wenzelm@5692
   160
wenzelm@17412
   161
fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c);
wenzelm@237
   162
wenzelm@23655
   163
fun err_dup_trfun name c =
wenzelm@23655
   164
  error ("More than one " ^ name ^ " for " ^ quote c);
wenzelm@237
   165
wenzelm@21536
   166
fun remove_trtab trfuns = fold (Symtab.remove SynExt.eq_trfun) trfuns;
wenzelm@21536
   167
wenzelm@21536
   168
fun extend_trtab name trfuns tab = Symtab.extend (remove_trtab trfuns tab, trfuns)
wenzelm@23655
   169
  handle Symtab.DUP c => err_dup_trfun name c;
wenzelm@237
   170
wenzelm@15755
   171
fun merge_trtabs name tab1 tab2 = Symtab.merge SynExt.eq_trfun (tab1, tab2)
wenzelm@23655
   172
  handle Symtab.DUP c => err_dup_trfun name c;
wenzelm@237
   173
wenzelm@237
   174
wenzelm@5692
   175
(* print (ast) translations *)
wenzelm@5692
   176
wenzelm@18931
   177
fun lookup_tr' tab c = map fst (Symtab.lookup_list tab c);
wenzelm@18931
   178
fun extend_tr'tab trfuns = fold_rev Symtab.update_list trfuns;
wenzelm@18931
   179
fun remove_tr'tab trfuns = fold (Symtab.remove_list SynExt.eq_trfun) trfuns;
wenzelm@18931
   180
fun merge_tr'tabs tab1 tab2 = Symtab.merge_list SynExt.eq_trfun (tab1, tab2);
wenzelm@5692
   181
wenzelm@5692
   182
wenzelm@237
   183
wenzelm@2700
   184
(** tables of token translation functions **)
wenzelm@2700
   185
wenzelm@2700
   186
fun lookup_tokentr tabs modes =
wenzelm@19482
   187
  let val trs = distinct (eq_fst (op =)) (maps (these o AList.lookup (op =) tabs) (modes @ [""]))
haftmann@17314
   188
  in fn c => Option.map fst (AList.lookup (op =) trs c) end;
wenzelm@2700
   189
wenzelm@2700
   190
fun merge_tokentrtabs tabs1 tabs2 =
wenzelm@2700
   191
  let
wenzelm@2700
   192
    fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2;
wenzelm@2700
   193
wenzelm@4703
   194
    fun name (s, _) = implode (tl (Symbol.explode s));
wenzelm@2706
   195
wenzelm@2700
   196
    fun merge mode =
wenzelm@2700
   197
      let
wenzelm@17213
   198
        val trs1 = these (AList.lookup (op =) tabs1 mode);
wenzelm@17213
   199
        val trs2 = these (AList.lookup (op =) tabs2 mode);
wenzelm@19046
   200
        val trs = distinct eq_tr (trs1 @ trs2);
wenzelm@2700
   201
      in
wenzelm@18964
   202
        (case duplicates (eq_fst (op =)) trs of
wenzelm@2700
   203
          [] => (mode, trs)
wenzelm@2700
   204
        | dups => error ("More than one token translation function in mode " ^
wenzelm@2706
   205
            quote mode ^ " for " ^ commas_quote (map name dups)))
wenzelm@2700
   206
      end;
wenzelm@19046
   207
  in map merge (distinct (op =) (map fst (tabs1 @ tabs2))) end;
wenzelm@2700
   208
wenzelm@15755
   209
fun extend_tokentrtab tokentrs tabs =
wenzelm@2700
   210
  let
wenzelm@17213
   211
    fun ins_tokentr (m, c, f) =
wenzelm@17213
   212
      AList.default (op =) (m, [])
wenzelm@17213
   213
      #> AList.map_entry (op =) m (cons ("_" ^ c, (f, stamp ())));
wenzelm@15759
   214
  in merge_tokentrtabs tabs (fold ins_tokentr tokentrs []) end;
wenzelm@2700
   215
wenzelm@2700
   216
wenzelm@2700
   217
wenzelm@237
   218
(** tables of translation rules **)
wenzelm@237
   219
wenzelm@5692
   220
type ruletab = (Ast.ast * Ast.ast) list Symtab.table;
wenzelm@237
   221
wenzelm@19482
   222
fun dest_ruletab tab = maps snd (Symtab.dest tab);
wenzelm@237
   223
wenzelm@237
   224
wenzelm@237
   225
(* empty, extend, merge ruletabs *)
wenzelm@237
   226
wenzelm@18931
   227
val extend_ruletab = fold_rev (fn r => Symtab.update_list (Ast.head_of_rule r, r));
wenzelm@18931
   228
val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r));
wenzelm@18931
   229
fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2);
clasohm@0
   230
clasohm@0
   231
clasohm@0
   232
clasohm@0
   233
(** datatype syntax **)
clasohm@0
   234
wenzelm@237
   235
datatype syntax =
wenzelm@237
   236
  Syntax of {
wenzelm@15755
   237
    input: SynExt.xprod list,
wenzelm@4703
   238
    lexicon: Scan.lexicon,
wenzelm@5692
   239
    gram: Parser.gram,
wenzelm@18
   240
    consts: string list,
wenzelm@2913
   241
    prmodes: string list,
wenzelm@21772
   242
    parse_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) Symtab.table,
wenzelm@237
   243
    parse_ruletab: ruletab,
wenzelm@21772
   244
    parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table,
wenzelm@21772
   245
    print_trtab: ((Proof.context -> bool -> typ -> term list -> term) * stamp) list Symtab.table,
wenzelm@237
   246
    print_ruletab: ruletab,
wenzelm@21772
   247
    print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
wenzelm@23660
   248
    tokentrtab: (string * (string * ((string -> output * int) * stamp)) list) list,
wenzelm@17079
   249
    prtabs: Printer.prtabs} * stamp;
clasohm@0
   250
wenzelm@17079
   251
fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2;
wenzelm@17079
   252
wenzelm@17079
   253
fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode;
wenzelm@14687
   254
wenzelm@20784
   255
type mode = string * bool;
wenzelm@24970
   256
val mode_default = ("", true);
wenzelm@24970
   257
val mode_input = ("input", true);
wenzelm@21826
   258
val internalM = "internal";
wenzelm@15755
   259
wenzelm@18
   260
wenzelm@237
   261
(* empty_syntax *)
wenzelm@18
   262
wenzelm@17079
   263
val empty_syntax = Syntax
wenzelm@17079
   264
  ({input = [],
wenzelm@4703
   265
    lexicon = Scan.empty_lexicon,
wenzelm@5692
   266
    gram = Parser.empty_gram,
wenzelm@167
   267
    consts = [],
wenzelm@2913
   268
    prmodes = [],
wenzelm@5692
   269
    parse_ast_trtab = Symtab.empty,
wenzelm@5692
   270
    parse_ruletab = Symtab.empty,
wenzelm@5692
   271
    parse_trtab = Symtab.empty,
wenzelm@5692
   272
    print_trtab = Symtab.empty,
wenzelm@5692
   273
    print_ruletab = Symtab.empty,
wenzelm@5692
   274
    print_ast_trtab = Symtab.empty,
wenzelm@2700
   275
    tokentrtab = [],
wenzelm@17079
   276
    prtabs = Printer.empty_prtabs}, stamp ());
wenzelm@167
   277
wenzelm@167
   278
wenzelm@237
   279
(* extend_syntax *)
wenzelm@167
   280
wenzelm@17079
   281
fun extend_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
wenzelm@167
   282
  let
wenzelm@15755
   283
    val {input, lexicon, gram, consts = consts1, prmodes = prmodes1,
wenzelm@2366
   284
      parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
wenzelm@2700
   285
      print_ast_trtab, tokentrtab, prtabs} = tabs;
wenzelm@14904
   286
    val SynExt.SynExt {xprods, consts = consts2, prmodes = prmodes2,
wenzelm@2913
   287
      parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
wenzelm@2700
   288
      print_ast_translation, token_translation} = syn_ext;
wenzelm@19546
   289
    fun if_inout xs = if inout then xs else [];
wenzelm@167
   290
  in
wenzelm@17079
   291
    Syntax
wenzelm@19546
   292
    ({input = if_inout xprods @ input,
wenzelm@22121
   293
      lexicon = Scan.extend_lexicon (if_inout (SynExt.delims_of xprods)) lexicon,
wenzelm@19546
   294
      gram = Parser.extend_gram gram (if_inout xprods),
wenzelm@19546
   295
      consts = Library.merge (op =) (consts1, filter_out NameSpace.is_qualified consts2),
wenzelm@18921
   296
      prmodes = insert (op =) mode (Library.merge (op =) (prmodes1, prmodes2)),
wenzelm@167
   297
      parse_ast_trtab =
wenzelm@19546
   298
        extend_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab,
wenzelm@19546
   299
      parse_ruletab = extend_ruletab (if_inout parse_rules) parse_ruletab,
wenzelm@19546
   300
      parse_trtab = extend_trtab "parse translation" (if_inout parse_translation) parse_trtab,
wenzelm@15755
   301
      print_trtab = extend_tr'tab print_translation print_trtab,
wenzelm@15755
   302
      print_ruletab = extend_ruletab print_rules print_ruletab,
wenzelm@15755
   303
      print_ast_trtab = extend_tr'tab print_ast_translation print_ast_trtab,
wenzelm@15755
   304
      tokentrtab = extend_tokentrtab token_translation tokentrtab,
wenzelm@17079
   305
      prtabs = Printer.extend_prtabs mode xprods prtabs}, stamp ())
wenzelm@18
   306
  end;
wenzelm@18
   307
wenzelm@15755
   308
wenzelm@15755
   309
(* remove_syntax *)
wenzelm@15755
   310
wenzelm@17079
   311
fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
wenzelm@15755
   312
  let
wenzelm@15755
   313
    val SynExt.SynExt {xprods, consts = _, prmodes = _,
wenzelm@15755
   314
      parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
wenzelm@15755
   315
      print_ast_translation, token_translation = _} = syn_ext;
wenzelm@15755
   316
    val {input, lexicon, gram, consts, prmodes,
wenzelm@15755
   317
      parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
wenzelm@15755
   318
      print_ast_trtab, tokentrtab, prtabs} = tabs;
wenzelm@19300
   319
    val input' = if inout then subtract (op =) xprods input else input;
wenzelm@19546
   320
    fun if_inout xs = if inout then xs else [];
wenzelm@15755
   321
  in
wenzelm@17079
   322
    Syntax
wenzelm@17079
   323
    ({input = input',
wenzelm@15755
   324
      lexicon = if inout then Scan.make_lexicon (SynExt.delims_of input') else lexicon,
wenzelm@15755
   325
      gram = if inout then Parser.make_gram input' else gram,
wenzelm@15755
   326
      consts = consts,
wenzelm@15755
   327
      prmodes = prmodes,
wenzelm@19546
   328
      parse_ast_trtab = remove_trtab (if_inout parse_ast_translation) parse_ast_trtab,
wenzelm@19546
   329
      parse_ruletab = remove_ruletab (if_inout parse_rules) parse_ruletab,
wenzelm@19546
   330
      parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab,
wenzelm@15755
   331
      print_trtab = remove_tr'tab print_translation print_trtab,
wenzelm@15755
   332
      print_ruletab = remove_ruletab print_rules print_ruletab,
wenzelm@15755
   333
      print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
wenzelm@15755
   334
      tokentrtab = tokentrtab,
wenzelm@17079
   335
      prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ())
wenzelm@15755
   336
  end;
wenzelm@14904
   337
wenzelm@18
   338
wenzelm@237
   339
(* merge_syntaxes *)
clasohm@0
   340
wenzelm@17079
   341
fun merge_syntaxes (Syntax (tabs1, _)) (Syntax (tabs2, _)) =
clasohm@0
   342
  let
wenzelm@15755
   343
    val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
wenzelm@15755
   344
      prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1,
wenzelm@15755
   345
      parse_ruletab = parse_ruletab1, parse_trtab = parse_trtab1,
wenzelm@15755
   346
      print_trtab = print_trtab1, print_ruletab = print_ruletab1,
wenzelm@15755
   347
      print_ast_trtab = print_ast_trtab1, tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
wenzelm@237
   348
wenzelm@15755
   349
    val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
wenzelm@15755
   350
      prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2,
wenzelm@15755
   351
      parse_ruletab = parse_ruletab2, parse_trtab = parse_trtab2,
wenzelm@15755
   352
      print_trtab = print_trtab2, print_ruletab = print_ruletab2,
wenzelm@15755
   353
      print_ast_trtab = print_ast_trtab2, tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
clasohm@0
   354
  in
wenzelm@17079
   355
    Syntax
wenzelm@18921
   356
    ({input = Library.merge (op =) (input1, input2),
wenzelm@4703
   357
      lexicon = Scan.merge_lexicons lexicon1 lexicon2,
wenzelm@5692
   358
      gram = Parser.merge_grams gram1 gram2,
wenzelm@18428
   359
      consts = sort_distinct string_ord (consts1 @ consts2),
wenzelm@18921
   360
      prmodes = Library.merge (op =) (prmodes1, prmodes2),
wenzelm@237
   361
      parse_ast_trtab =
wenzelm@15755
   362
        merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
wenzelm@237
   363
      parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
wenzelm@15755
   364
      parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
wenzelm@5692
   365
      print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
wenzelm@237
   366
      print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
wenzelm@5692
   367
      print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
wenzelm@2700
   368
      tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2,
wenzelm@17079
   369
      prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ())
clasohm@0
   370
  end;
clasohm@0
   371
clasohm@0
   372
wenzelm@18720
   373
(* basic syntax *)
wenzelm@260
   374
wenzelm@18720
   375
val basic_syn =
wenzelm@17168
   376
  empty_syntax
wenzelm@24970
   377
  |> extend_syntax mode_default TypeExt.type_ext
wenzelm@24970
   378
  |> extend_syntax mode_default SynExt.pure_ext;
wenzelm@260
   379
wenzelm@18720
   380
val basic_nonterms =
wenzelm@18720
   381
  (Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes",
wenzelm@18720
   382
    SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
wenzelm@22573
   383
    "asms", SynExt.any_, SynExt.sprop, "num_const", "index", "struct"]);
wenzelm@18720
   384
clasohm@0
   385
wenzelm@4887
   386
wenzelm@15759
   387
(** print syntax **)
wenzelm@15759
   388
wenzelm@15759
   389
local
clasohm@0
   390
wenzelm@260
   391
fun pretty_strs_qs name strs =
berghofe@14598
   392
  Pretty.strs (name :: map Library.quote (sort_strings strs));
clasohm@0
   393
wenzelm@17079
   394
fun pretty_gram (Syntax (tabs, _)) =
clasohm@0
   395
  let
wenzelm@14904
   396
    val {lexicon, prmodes, gram, prtabs, ...} = tabs;
wenzelm@2913
   397
    val prmodes' = sort_strings (filter_out (equal "") prmodes);
clasohm@0
   398
  in
wenzelm@8720
   399
    [pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon),
wenzelm@8720
   400
      Pretty.big_list "prods:" (Parser.pretty_gram gram),
wenzelm@8720
   401
      pretty_strs_qs "print modes:" prmodes']
clasohm@0
   402
  end;
clasohm@0
   403
wenzelm@17079
   404
fun pretty_trans (Syntax (tabs, _)) =
clasohm@0
   405
  let
wenzelm@260
   406
    fun pretty_trtab name tab =
wenzelm@5692
   407
      pretty_strs_qs name (Symtab.keys tab);
clasohm@0
   408
wenzelm@260
   409
    fun pretty_ruletab name tab =
wenzelm@5692
   410
      Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab));
clasohm@0
   411
berghofe@14598
   412
    fun pretty_tokentr (mode, trs) = Pretty.strs (Library.quote mode ^ ":" :: map fst trs);
wenzelm@4703
   413
wenzelm@237
   414
    val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
wenzelm@4703
   415
      print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs;
clasohm@0
   416
  in
wenzelm@8720
   417
    [pretty_strs_qs "consts:" consts,
wenzelm@8720
   418
      pretty_trtab "parse_ast_translation:" parse_ast_trtab,
wenzelm@8720
   419
      pretty_ruletab "parse_rules:" parse_ruletab,
wenzelm@8720
   420
      pretty_trtab "parse_translation:" parse_trtab,
wenzelm@8720
   421
      pretty_trtab "print_translation:" print_trtab,
wenzelm@8720
   422
      pretty_ruletab "print_rules:" print_ruletab,
wenzelm@8720
   423
      pretty_trtab "print_ast_translation:" print_ast_trtab,
wenzelm@8720
   424
      Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab)]
clasohm@0
   425
  end;
clasohm@0
   426
wenzelm@15759
   427
in
clasohm@0
   428
wenzelm@15759
   429
fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn));
wenzelm@15759
   430
fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn));
wenzelm@15759
   431
fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn));
clasohm@0
   432
wenzelm@15759
   433
end;
clasohm@0
   434
clasohm@0
   435
clasohm@0
   436
wenzelm@237
   437
(** read **)
wenzelm@18
   438
wenzelm@260
   439
(* read_ast *)
wenzelm@260
   440
clasohm@882
   441
val ambiguity_level = ref 1;
wenzelm@24263
   442
val ambiguity_is_error = ref false;
clasohm@882
   443
wenzelm@21772
   444
fun read_asts ctxt is_logtype (Syntax (tabs, _)) xids root str =
wenzelm@260
   445
  let
wenzelm@14904
   446
    val {lexicon, gram, parse_ast_trtab, ...} = tabs;
wenzelm@14904
   447
    val root' = if root <> "prop" andalso is_logtype root then SynExt.logic else root;
wenzelm@4703
   448
    val chars = Symbol.explode str;
wenzelm@5692
   449
    val pts = Parser.parse gram root' (Lexicon.tokenize lexicon xids chars);
clasohm@330
   450
wenzelm@12292
   451
    fun show_pt pt =
wenzelm@21772
   452
      Pretty.string_of (Ast.pretty_ast (hd (SynTrans.pts_to_asts ctxt (K NONE) [pt])));
wenzelm@260
   453
  in
wenzelm@21962
   454
    if length pts > ! ambiguity_level then
wenzelm@16613
   455
      if ! ambiguity_is_error then error ("Ambiguous input " ^ quote str)
wenzelm@16661
   456
      else (warning ("Ambiguous input " ^ quote str ^ "\n" ^
wenzelm@16716
   457
          "produces " ^ string_of_int (length pts) ^ " parse trees.");
wenzelm@21962
   458
         List.app (warning o show_pt) pts)
wenzelm@21962
   459
    else ();
wenzelm@21772
   460
    SynTrans.pts_to_asts ctxt (lookup_tr parse_ast_trtab) pts
wenzelm@260
   461
  end;
wenzelm@260
   462
wenzelm@260
   463
wenzelm@237
   464
(* read *)
clasohm@0
   465
wenzelm@21772
   466
fun read ctxt is_logtype (syn as Syntax (tabs, _)) ty str =
clasohm@0
   467
  let
wenzelm@237
   468
    val {parse_ruletab, parse_trtab, ...} = tabs;
wenzelm@21772
   469
    val asts = read_asts ctxt is_logtype syn false (SynExt.typ_to_nonterm ty) str;
clasohm@0
   470
  in
wenzelm@21772
   471
    SynTrans.asts_to_terms ctxt (lookup_tr parse_trtab)
wenzelm@18931
   472
      (map (Ast.normalize_ast (Symtab.lookup_list parse_ruletab)) asts)
clasohm@0
   473
  end;
clasohm@0
   474
clasohm@0
   475
wenzelm@22703
   476
(* read terms *)
wenzelm@22703
   477
wenzelm@24372
   478
(*brute-force disambiguation via type-inference*)
wenzelm@24372
   479
fun disambig _ _ [t] = t
wenzelm@24372
   480
  | disambig pp check ts =
wenzelm@24372
   481
      let
wenzelm@24372
   482
        val err_results = map check ts;
wenzelm@24372
   483
        val errs = map_filter (fn Exn.Exn (ERROR msg) => SOME msg | _ => NONE) err_results;
wenzelm@24372
   484
        val results = map_filter Exn.get_result err_results;
wenzelm@24372
   485
wenzelm@24372
   486
        val ambiguity = length ts;
wenzelm@24372
   487
        fun ambig_msg () =
wenzelm@24372
   488
          if ambiguity > 1 andalso ambiguity <= ! ambiguity_level then
wenzelm@24372
   489
            "Got more than one parse tree.\n\
wenzelm@24372
   490
            \Retry with smaller ambiguity_level for more information."
wenzelm@24372
   491
          else "";
wenzelm@24372
   492
      in
wenzelm@24372
   493
        if null results then cat_error (ambig_msg ()) (cat_lines errs)
wenzelm@24372
   494
        else if length results = 1 then
wenzelm@24372
   495
          (if ambiguity > ! ambiguity_level then
wenzelm@24372
   496
            warning "Fortunately, only one parse tree is type correct.\n\
wenzelm@24372
   497
              \You may still want to disambiguate your grammar or your input."
wenzelm@24372
   498
          else (); hd results)
wenzelm@24372
   499
        else cat_error (ambig_msg ()) ("More than one term is type correct:\n" ^
wenzelm@24372
   500
          cat_lines (map (Pretty.string_of_term pp) ts))
wenzelm@24372
   501
      end;
wenzelm@24372
   502
wenzelm@24372
   503
fun standard_parse_term pp check get_sort map_const map_free map_type map_sort
wenzelm@24372
   504
    ctxt is_logtype syn ty str =
wenzelm@24372
   505
  read ctxt is_logtype syn ty str
wenzelm@24372
   506
  |> map (TypeExt.decode_term get_sort map_const map_free map_type map_sort)
wenzelm@24372
   507
  |> disambig (Printer.pp_show_brackets pp) check;
wenzelm@22703
   508
wenzelm@22703
   509
wenzelm@237
   510
(* read types *)
clasohm@0
   511
wenzelm@24278
   512
fun standard_parse_typ ctxt syn get_sort map_sort str =
wenzelm@21772
   513
  (case read ctxt (K false) syn SynExt.typeT str of
wenzelm@22703
   514
    [t] => TypeExt.typ_of_term (get_sort (TypeExt.term_sorts map_sort t)) map_sort t
wenzelm@8894
   515
  | _ => error "read_typ: ambiguous syntax");
wenzelm@144
   516
wenzelm@144
   517
wenzelm@8894
   518
(* read sorts *)
wenzelm@8894
   519
wenzelm@24278
   520
fun standard_parse_sort ctxt syn map_sort str =
wenzelm@21772
   521
  (case read ctxt (K false) syn TypeExt.sortT str of
wenzelm@22703
   522
    [t] => TypeExt.sort_of_term map_sort t
wenzelm@8894
   523
  | _ => error "read_sort: ambiguous syntax");
wenzelm@8894
   524
wenzelm@8894
   525
wenzelm@18
   526
wenzelm@1158
   527
(** prepare translation rules **)
wenzelm@1158
   528
wenzelm@1158
   529
datatype 'a trrule =
wenzelm@3526
   530
  ParseRule of 'a * 'a |
wenzelm@3526
   531
  PrintRule of 'a * 'a |
wenzelm@3526
   532
  ParsePrintRule of 'a * 'a;
wenzelm@888
   533
wenzelm@4618
   534
fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y)
wenzelm@4618
   535
  | map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y)
wenzelm@4618
   536
  | map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y);
wenzelm@1158
   537
skalberg@15531
   538
fun parse_rule (ParseRule pats) = SOME pats
skalberg@15531
   539
  | parse_rule (PrintRule _) = NONE
skalberg@15531
   540
  | parse_rule (ParsePrintRule pats) = SOME pats;
wenzelm@1158
   541
skalberg@15531
   542
fun print_rule (ParseRule _) = NONE
skalberg@15531
   543
  | print_rule (PrintRule pats) = SOME (swap pats)
skalberg@15531
   544
  | print_rule (ParsePrintRule pats) = SOME (swap pats);
wenzelm@1158
   545
wenzelm@1158
   546
wenzelm@19262
   547
local
wenzelm@19262
   548
wenzelm@1158
   549
fun check_rule (rule as (lhs, rhs)) =
wenzelm@5692
   550
  (case Ast.rule_error rule of
skalberg@15531
   551
    SOME msg =>
wenzelm@1158
   552
      error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
wenzelm@5692
   553
        Ast.str_of_ast lhs ^ "  ->  " ^ Ast.str_of_ast rhs)
skalberg@15531
   554
  | NONE => rule);
wenzelm@888
   555
wenzelm@21772
   556
fun read_pattern ctxt is_logtype syn (root, str) =
wenzelm@18
   557
  let
wenzelm@17079
   558
    val Syntax ({consts, ...}, _) = syn;
wenzelm@18
   559
wenzelm@5692
   560
    fun constify (ast as Ast.Constant _) = ast
wenzelm@5692
   561
      | constify (ast as Ast.Variable x) =
wenzelm@20664
   562
          if member (op =) consts x orelse NameSpace.is_qualified x then Ast.Constant x
wenzelm@3830
   563
          else ast
wenzelm@5692
   564
      | constify (Ast.Appl asts) = Ast.Appl (map constify asts);
wenzelm@18
   565
  in
wenzelm@21772
   566
    (case read_asts ctxt is_logtype syn true root str of
wenzelm@888
   567
      [ast] => constify ast
wenzelm@888
   568
    | _ => error ("Syntactically ambiguous input: " ^ quote str))
wenzelm@18678
   569
  end handle ERROR msg =>
wenzelm@18678
   570
    cat_error msg ("The error(s) above occurred in translation pattern " ^
wenzelm@888
   571
      quote str);
clasohm@0
   572
wenzelm@1158
   573
fun prep_rules rd_pat raw_rules =
wenzelm@4618
   574
  let val rules = map (map_trrule rd_pat) raw_rules in
wenzelm@19482
   575
    (map check_rule (map_filter parse_rule rules),
wenzelm@19482
   576
      map check_rule (map_filter print_rule rules))
wenzelm@1158
   577
  end
wenzelm@18
   578
wenzelm@19262
   579
in
wenzelm@19262
   580
wenzelm@19262
   581
val cert_rules = prep_rules I;
wenzelm@19262
   582
wenzelm@21772
   583
fun read_rules ctxt is_logtype syn =
wenzelm@21772
   584
  prep_rules (read_pattern ctxt is_logtype syn);
wenzelm@19262
   585
wenzelm@19262
   586
end;
wenzelm@19262
   587
wenzelm@18
   588
wenzelm@18
   589
wenzelm@24923
   590
(** unparse terms, typs, sorts **)
wenzelm@18
   591
wenzelm@24923
   592
local
wenzelm@24923
   593
wenzelm@24923
   594
fun unparse_t t_to_ast prt_t markup ctxt (syn as Syntax (tabs, _)) curried t =
clasohm@0
   595
  let
wenzelm@2700
   596
    val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs;
wenzelm@21772
   597
    val ast = t_to_ast ctxt (lookup_tr' print_trtab) t;
clasohm@0
   598
  in
wenzelm@23631
   599
    Pretty.markup markup (prt_t ctxt curried prtabs (lookup_tr' print_ast_trtab)
wenzelm@24613
   600
      (lookup_tokentr tokentrtab (print_mode_value ()))
wenzelm@23631
   601
      (Ast.normalize_ast (Symtab.lookup_list print_ruletab) ast))
clasohm@0
   602
  end;
clasohm@0
   603
wenzelm@24923
   604
in
wenzelm@24923
   605
wenzelm@24923
   606
fun standard_unparse_term extern =
wenzelm@24923
   607
  unparse_t Printer.term_to_ast (Printer.pretty_term_ast extern) Markup.term;
wenzelm@24923
   608
wenzelm@24923
   609
fun standard_unparse_typ ctxt syn =
wenzelm@24923
   610
  unparse_t Printer.typ_to_ast Printer.pretty_typ_ast Markup.typ ctxt syn false;
wenzelm@24923
   611
wenzelm@24923
   612
fun standard_unparse_sort ctxt syn =
wenzelm@24923
   613
  unparse_t Printer.sort_to_ast Printer.pretty_typ_ast Markup.sort ctxt syn false;
wenzelm@24923
   614
wenzelm@24923
   615
end;
clasohm@0
   616
clasohm@0
   617
clasohm@0
   618
wenzelm@19262
   619
(** modify syntax **)
wenzelm@383
   620
wenzelm@14904
   621
fun ext_syntax' f is_logtype prmode decls = extend_syntax prmode (f is_logtype decls);
wenzelm@24970
   622
fun ext_syntax f = ext_syntax' (K f) (K false) mode_default;
wenzelm@383
   623
wenzelm@16613
   624
val extend_type_gram       = ext_syntax Mixfix.syn_ext_types;
wenzelm@16613
   625
val extend_const_gram      = ext_syntax' Mixfix.syn_ext_consts;
wenzelm@16613
   626
val extend_consts          = ext_syntax SynExt.syn_ext_const_names;
wenzelm@16613
   627
val extend_trfuns          = ext_syntax SynExt.syn_ext_trfuns;
wenzelm@16613
   628
val extend_advanced_trfuns = ext_syntax SynExt.syn_ext_advanced_trfuns;
wenzelm@16613
   629
val extend_tokentrfuns     = ext_syntax SynExt.syn_ext_tokentrfuns;
wenzelm@5692
   630
wenzelm@15755
   631
fun remove_const_gram is_logtype prmode decls =
wenzelm@15755
   632
  remove_syntax prmode (Mixfix.syn_ext_consts is_logtype decls);
wenzelm@15755
   633
wenzelm@21772
   634
fun extend_trrules ctxt is_logtype syn =
wenzelm@21772
   635
  ext_syntax SynExt.syn_ext_rules o read_rules ctxt is_logtype syn;
wenzelm@19262
   636
wenzelm@21772
   637
fun remove_trrules ctxt is_logtype syn =
wenzelm@24970
   638
  remove_syntax mode_default o SynExt.syn_ext_rules o read_rules ctxt is_logtype syn;
wenzelm@19262
   639
wenzelm@19262
   640
val extend_trrules_i = ext_syntax SynExt.syn_ext_rules o cert_rules;
wenzelm@24970
   641
val remove_trrules_i = remove_syntax mode_default o SynExt.syn_ext_rules o cert_rules;
wenzelm@5692
   642
wenzelm@5692
   643
wenzelm@24263
   644
wenzelm@24768
   645
(** inner syntax operations **)
wenzelm@24263
   646
wenzelm@24768
   647
(* (un)parsing *)
wenzelm@24263
   648
wenzelm@24263
   649
local
wenzelm@24768
   650
  type operations =
wenzelm@24768
   651
   {parse_sort: Proof.context -> string -> sort,
wenzelm@24768
   652
    parse_typ: Proof.context -> string -> typ,
wenzelm@24768
   653
    parse_term: Proof.context -> string -> term,
wenzelm@24768
   654
    parse_prop: Proof.context -> string -> term,
wenzelm@24768
   655
    unparse_sort: Proof.context -> sort -> Pretty.T,
wenzelm@24768
   656
    unparse_typ: Proof.context -> typ -> Pretty.T,
wenzelm@24768
   657
    unparse_term: Proof.context -> term -> Pretty.T};
wenzelm@24768
   658
wenzelm@24768
   659
  val operations = ref (NONE: operations option);
wenzelm@24768
   660
wenzelm@24768
   661
  fun operation which ctxt x =
wenzelm@24768
   662
    (case ! operations of
wenzelm@24768
   663
      NONE => error "Inner syntax operations not yet installed"
wenzelm@24768
   664
    | SOME ops => which ops ctxt x);
wenzelm@24263
   665
in
wenzelm@24263
   666
wenzelm@24768
   667
val parse_sort = operation #parse_sort;
wenzelm@24768
   668
val parse_typ = operation #parse_typ;
wenzelm@24768
   669
val parse_term = operation #parse_term;
wenzelm@24768
   670
val parse_prop = operation #parse_prop;
wenzelm@24768
   671
val unparse_sort = operation #unparse_sort;
wenzelm@24768
   672
val unparse_typ = operation #unparse_typ;
wenzelm@24768
   673
val unparse_term = operation #unparse_term;
wenzelm@24768
   674
wenzelm@24768
   675
fun install_operations ops = CRITICAL (fn () =>
wenzelm@24768
   676
  if is_some (! operations) then error "Inner syntax operations already installed"
wenzelm@24768
   677
  else operations := SOME ops);
wenzelm@24768
   678
wenzelm@24263
   679
end;
wenzelm@24263
   680
wenzelm@24263
   681
wenzelm@24768
   682
(* context-sensitive (un)checking *)
wenzelm@24263
   683
wenzelm@24488
   684
local
wenzelm@24488
   685
wenzelm@24768
   686
type key = int * bool;
wenzelm@25060
   687
type 'a check = 'a list -> Proof.context -> ('a list * Proof.context) option;
wenzelm@24768
   688
wenzelm@24488
   689
structure Checks = GenericDataFun
wenzelm@24263
   690
(
wenzelm@24488
   691
  type T =
wenzelm@24768
   692
    ((key * ((string * typ check) * stamp) list) list *
wenzelm@24768
   693
     (key * ((string * term check) * stamp) list) list);
wenzelm@24488
   694
  val empty = ([], []);
wenzelm@24263
   695
  val extend = I;
wenzelm@24488
   696
  fun merge _ ((typ_checks1, term_checks1), (typ_checks2, term_checks2)) : T =
wenzelm@24768
   697
    (AList.join (op =) (K (Library.merge (eq_snd (op =)))) (typ_checks1, typ_checks2),
wenzelm@24768
   698
     AList.join (op =) (K (Library.merge (eq_snd (op =)))) (term_checks1, term_checks2));
wenzelm@24263
   699
);
wenzelm@24263
   700
wenzelm@24768
   701
fun gen_add which (key: key) name f =
wenzelm@24768
   702
  Checks.map (which (AList.map_default op = (key, []) (cons ((name, f), stamp ()))));
wenzelm@24768
   703
wenzelm@25060
   704
fun check_stage fs = perhaps_loop (perhaps_apply (map uncurry fs));
wenzelm@25060
   705
wenzelm@25060
   706
fun gen_check which uncheck ctxt0 xs0 =
wenzelm@24263
   707
  let
wenzelm@24768
   708
    val funs = which (Checks.get (Context.Proof ctxt0))
wenzelm@24768
   709
      |> map_filter (fn ((i, u), fs) => if uncheck = u then SOME (i, map (snd o fst) fs) else NONE)
wenzelm@25060
   710
      |> Library.sort (int_ord o pairself fst) |> map snd
wenzelm@25060
   711
      |> not uncheck ? map rev;
wenzelm@25060
   712
    val check_all = perhaps_apply (map check_stage funs);
wenzelm@25060
   713
  in #1 (perhaps check_all (xs0, ctxt0)) end;
wenzelm@24768
   714
wenzelm@24768
   715
fun map_sort f S =
wenzelm@24768
   716
  (case f (TFree ("", S)) of
wenzelm@24768
   717
    TFree ("", S') => S'
wenzelm@24923
   718
  | _ => raise TYPE ("map_sort", [TFree ("", S)], []));
wenzelm@24488
   719
wenzelm@24488
   720
in
wenzelm@24263
   721
wenzelm@24768
   722
fun print_checks ctxt =
wenzelm@24768
   723
  let
wenzelm@24768
   724
    fun split_checks checks =
wenzelm@24768
   725
      List.partition (fn ((_, un), _) => not un) checks
wenzelm@24768
   726
      |> pairself (map (fn ((i, _), fs) => (i, map (fst o fst) fs))
wenzelm@24768
   727
          #> sort (int_ord o pairself fst));
wenzelm@24768
   728
    fun pretty_checks kind checks =
wenzelm@24768
   729
      checks |> map (fn (i, names) => Pretty.block
wenzelm@24768
   730
        [Pretty.str (kind ^ " (stage " ^ signed_string_of_int i ^ "):"),
wenzelm@24768
   731
          Pretty.brk 1, Pretty.strs names]);
wenzelm@24768
   732
wenzelm@24768
   733
    val (typs, terms) = Checks.get (Context.Proof ctxt);
wenzelm@24768
   734
    val (typ_checks, typ_unchecks) = split_checks typs;
wenzelm@24768
   735
    val (term_checks, term_unchecks) = split_checks terms;
wenzelm@24768
   736
  in
wenzelm@24768
   737
    pretty_checks "typ_checks" typ_checks @
wenzelm@24768
   738
    pretty_checks "term_checks" term_checks @
wenzelm@24768
   739
    pretty_checks "typ_unchecks" typ_unchecks @
wenzelm@24768
   740
    pretty_checks "term_unchecks" term_unchecks
wenzelm@24768
   741
  end |> Pretty.chunks |> Pretty.writeln;
wenzelm@24768
   742
wenzelm@24768
   743
fun add_typ_check stage = gen_add apfst (stage, false);
wenzelm@24768
   744
fun add_term_check stage = gen_add apsnd (stage, false);
wenzelm@24768
   745
fun add_typ_uncheck stage = gen_add apfst (stage, true);
wenzelm@24768
   746
fun add_term_uncheck stage = gen_add apsnd (stage, true);
wenzelm@24768
   747
wenzelm@25060
   748
val check_typs = gen_check fst false;
wenzelm@25060
   749
val check_terms = gen_check snd false;
wenzelm@24680
   750
fun check_props ctxt = map (TypeInfer.constrain propT) #> check_terms ctxt;
wenzelm@24488
   751
wenzelm@24512
   752
val check_typ = singleton o check_typs;
wenzelm@24512
   753
val check_term = singleton o check_terms;
wenzelm@24512
   754
val check_prop = singleton o check_props;
wenzelm@24768
   755
val check_sort = map_sort o check_typ;
wenzelm@24263
   756
wenzelm@25060
   757
val uncheck_typs = gen_check fst true;
wenzelm@25060
   758
val uncheck_terms = gen_check snd true;
wenzelm@24768
   759
val uncheck_sort = map_sort o singleton o uncheck_typs;
wenzelm@24488
   760
wenzelm@24488
   761
end;
wenzelm@24488
   762
wenzelm@24263
   763
wenzelm@24923
   764
(* derived operations for classrel and arity *)
wenzelm@24923
   765
wenzelm@24923
   766
val uncheck_classrel = map o singleton o uncheck_sort;
wenzelm@24923
   767
wenzelm@24923
   768
fun unparse_classrel ctxt cs = Pretty.block (flat
wenzelm@24923
   769
  (separate [Pretty.str " <", Pretty.brk 1] (map (single o unparse_sort ctxt o single) cs)));
wenzelm@24923
   770
wenzelm@24923
   771
fun uncheck_arity ctxt (a, Ss, S) =
wenzelm@24923
   772
  let
wenzelm@24923
   773
    val T = Type (a, replicate (length Ss) dummyT);
wenzelm@24923
   774
    val a' =
wenzelm@24923
   775
      (case singleton (uncheck_typs ctxt) T of
wenzelm@24923
   776
        Type (a', _) => a'
wenzelm@24923
   777
      | T => raise TYPE ("uncheck_arity", [T], []));
wenzelm@24923
   778
    val Ss' = map (uncheck_sort ctxt) Ss;
wenzelm@24923
   779
    val S' = uncheck_sort ctxt S;
wenzelm@24923
   780
  in (a', Ss', S') end;
wenzelm@24923
   781
wenzelm@24923
   782
fun unparse_arity ctxt (a, Ss, S) =
wenzelm@24923
   783
  let
wenzelm@25043
   784
    val prtT = unparse_typ ctxt (Type (a, []));
wenzelm@24923
   785
    val dom =
wenzelm@24923
   786
      if null Ss then []
wenzelm@24923
   787
      else [Pretty.list "(" ")" (map (unparse_sort ctxt) Ss), Pretty.brk 1];
wenzelm@25043
   788
  in Pretty.block ([prtT, Pretty.str " ::", Pretty.brk 1] @ dom @ [unparse_sort ctxt S]) end;
wenzelm@24923
   789
wenzelm@24923
   790
wenzelm@24923
   791
(* read = parse + check *)
wenzelm@24263
   792
wenzelm@24263
   793
fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt;
wenzelm@24263
   794
fun read_typ ctxt = parse_typ ctxt #> singleton (check_typs ctxt);
wenzelm@24263
   795
wenzelm@24263
   796
fun read_terms ctxt = map (parse_term ctxt) #> check_terms ctxt;
wenzelm@24263
   797
fun read_props ctxt = map (parse_prop ctxt) #> check_props ctxt;
wenzelm@24263
   798
wenzelm@24263
   799
val read_term = singleton o read_terms;
wenzelm@24263
   800
val read_prop = singleton o read_props;
wenzelm@24263
   801
wenzelm@24709
   802
val read_sort_global = read_sort o ProofContext.init;
wenzelm@24709
   803
val read_typ_global = read_typ o ProofContext.init;
wenzelm@24709
   804
val read_term_global = read_term o ProofContext.init;
wenzelm@24709
   805
val read_prop_global = read_prop o ProofContext.init;
wenzelm@24278
   806
wenzelm@24263
   807
wenzelm@24923
   808
(* pretty = uncheck + unparse *)
wenzelm@24923
   809
wenzelm@24923
   810
fun pretty_sort ctxt = uncheck_sort ctxt #> unparse_sort ctxt;
wenzelm@24923
   811
fun pretty_classrel ctxt = uncheck_classrel ctxt #> unparse_classrel ctxt;
wenzelm@24923
   812
fun pretty_arity ctxt = uncheck_arity ctxt #> unparse_arity ctxt;
wenzelm@24923
   813
fun pretty_typ ctxt = singleton (uncheck_typs ctxt) #> unparse_typ ctxt;
wenzelm@24923
   814
fun pretty_term ctxt = singleton (uncheck_terms ctxt) #> unparse_term ctxt;
wenzelm@24923
   815
wenzelm@24923
   816
val string_of_sort = Pretty.string_of oo pretty_sort;
wenzelm@24923
   817
val string_of_classrel = Pretty.string_of oo pretty_classrel;
wenzelm@24923
   818
val string_of_arity = Pretty.string_of oo pretty_arity;
wenzelm@24923
   819
val string_of_typ = Pretty.string_of oo pretty_typ;
wenzelm@24923
   820
val string_of_term = Pretty.string_of oo pretty_term;
wenzelm@24923
   821
wenzelm@24970
   822
(*pp operations -- deferred evaluation*)
wenzelm@24970
   823
fun pp ctxt = Pretty.pp
wenzelm@24970
   824
 (fn x => pretty_term ctxt x,
wenzelm@24970
   825
  fn x => pretty_typ ctxt x,
wenzelm@24970
   826
  fn x => pretty_sort ctxt x,
wenzelm@24970
   827
  fn x => pretty_classrel ctxt x,
wenzelm@24970
   828
  fn x => pretty_arity ctxt x);
wenzelm@24923
   829
wenzelm@15833
   830
(*export parts of internal Syntax structures*)
wenzelm@15833
   831
open Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer;
wenzelm@2366
   832
clasohm@0
   833
end;
wenzelm@5692
   834
wenzelm@5692
   835
structure BasicSyntax: BASIC_SYNTAX = Syntax;
wenzelm@5692
   836
open BasicSyntax;
wenzelm@23923
   837
wenzelm@23923
   838
structure Hidden = struct end;
wenzelm@23923
   839
structure Ast = Hidden;
wenzelm@23923
   840
structure SynExt = Hidden;
wenzelm@23923
   841
structure Parser = Hidden;
wenzelm@23923
   842
structure TypeExt = Hidden;
wenzelm@23923
   843
structure SynTrans = Hidden;
wenzelm@23923
   844
structure Mixfix = Hidden;
wenzelm@23923
   845
structure Printer = Hidden;