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