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