src/Pure/Syntax/syntax.ML
author wenzelm
Tue Apr 19 15:58:05 2011 +0200 (2011-04-19)
changeset 42403 38b29c9fc742
parent 42402 c7139609b67d
child 42408 282b7a3065d3
permissions -rw-r--r--
slightly more special eq_list/eq_set, with shortcut involving pointer_eq;
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
clasohm@0
     8
signature SYNTAX =
wenzelm@2383
     9
sig
wenzelm@42290
    10
  val const: string -> term
wenzelm@42290
    11
  val free: string -> term
wenzelm@42290
    12
  val var: indexname -> term
wenzelm@42382
    13
  type operations
wenzelm@42382
    14
  val install_operations: operations -> unit
wenzelm@42288
    15
  val root: string Config.T
wenzelm@42268
    16
  val positions_raw: Config.raw
wenzelm@42268
    17
  val positions: bool Config.T
wenzelm@42268
    18
  val ambiguity_enabled: bool Config.T
wenzelm@42268
    19
  val ambiguity_level_raw: Config.raw
wenzelm@42268
    20
  val ambiguity_level: int Config.T
wenzelm@42268
    21
  val ambiguity_limit: int Config.T
wenzelm@30573
    22
  val read_token: string -> Symbol_Pos.T list * Position.T
wenzelm@38238
    23
  val parse_token: Proof.context -> Markup.T -> string -> Symbol_Pos.T list * Position.T
wenzelm@24263
    24
  val parse_sort: Proof.context -> string -> sort
wenzelm@24263
    25
  val parse_typ: Proof.context -> string -> typ
wenzelm@24263
    26
  val parse_term: Proof.context -> string -> term
wenzelm@24263
    27
  val parse_prop: Proof.context -> string -> term
wenzelm@24768
    28
  val unparse_sort: Proof.context -> sort -> Pretty.T
wenzelm@24923
    29
  val unparse_classrel: Proof.context -> class list -> Pretty.T
wenzelm@24923
    30
  val unparse_arity: Proof.context -> arity -> Pretty.T
wenzelm@24768
    31
  val unparse_typ: Proof.context -> typ -> Pretty.T
wenzelm@24768
    32
  val unparse_term: Proof.context -> term -> Pretty.T
wenzelm@24768
    33
  val print_checks: Proof.context -> unit
wenzelm@42402
    34
  val context_typ_check: int -> string ->
wenzelm@25060
    35
    (typ list -> Proof.context -> (typ list * Proof.context) option) ->
wenzelm@24768
    36
    Context.generic -> Context.generic
wenzelm@42402
    37
  val context_term_check: int -> string ->
wenzelm@25060
    38
    (term list -> Proof.context -> (term list * Proof.context) option) ->
wenzelm@24768
    39
    Context.generic -> Context.generic
wenzelm@42402
    40
  val context_typ_uncheck: int -> string ->
wenzelm@25060
    41
    (typ list -> Proof.context -> (typ list * Proof.context) option) ->
wenzelm@24768
    42
    Context.generic -> Context.generic
wenzelm@42402
    43
  val context_term_uncheck: int -> string ->
wenzelm@25060
    44
    (term list -> Proof.context -> (term list * Proof.context) option) ->
wenzelm@24768
    45
    Context.generic -> Context.generic
wenzelm@42402
    46
  val add_typ_check: int -> string -> (Proof.context -> typ list -> typ list) ->
wenzelm@42402
    47
    Context.generic -> Context.generic
wenzelm@42402
    48
  val add_term_check: int -> string -> (Proof.context -> term list -> term list) ->
wenzelm@42402
    49
    Context.generic -> Context.generic
wenzelm@42402
    50
  val add_typ_uncheck: int -> string -> (Proof.context -> typ list -> typ list) ->
wenzelm@42402
    51
    Context.generic -> Context.generic
wenzelm@42402
    52
  val add_term_uncheck: int -> string -> (Proof.context -> term list -> term list) ->
wenzelm@42402
    53
    Context.generic -> Context.generic
wenzelm@42382
    54
  val typ_check: Proof.context -> typ list -> typ list
wenzelm@42382
    55
  val term_check: Proof.context -> term list -> term list
wenzelm@42382
    56
  val typ_uncheck: Proof.context -> typ list -> typ list
wenzelm@42382
    57
  val term_uncheck: Proof.context -> term list -> term list
wenzelm@24488
    58
  val check_sort: Proof.context -> sort -> sort
wenzelm@24512
    59
  val check_typ: Proof.context -> typ -> typ
wenzelm@24512
    60
  val check_term: Proof.context -> term -> term
wenzelm@24512
    61
  val check_prop: Proof.context -> term -> term
wenzelm@24488
    62
  val check_typs: Proof.context -> typ list -> typ list
wenzelm@24263
    63
  val check_terms: Proof.context -> term list -> term list
wenzelm@24263
    64
  val check_props: Proof.context -> term list -> term list
wenzelm@24923
    65
  val uncheck_sort: Proof.context -> sort -> sort
wenzelm@24923
    66
  val uncheck_arity: Proof.context -> arity -> arity
wenzelm@24923
    67
  val uncheck_classrel: Proof.context -> class list -> class list
wenzelm@24768
    68
  val uncheck_typs: Proof.context -> typ list -> typ list
wenzelm@24768
    69
  val uncheck_terms: Proof.context -> term list -> term list
wenzelm@24263
    70
  val read_sort: Proof.context -> string -> sort
wenzelm@24263
    71
  val read_typ: Proof.context -> string -> typ
wenzelm@24488
    72
  val read_term: Proof.context -> string -> term
wenzelm@24488
    73
  val read_prop: Proof.context -> string -> term
wenzelm@24263
    74
  val read_terms: Proof.context -> string list -> term list
wenzelm@24263
    75
  val read_props: Proof.context -> string list -> term list
wenzelm@24709
    76
  val read_sort_global: theory -> string -> sort
wenzelm@24709
    77
  val read_typ_global: theory -> string -> typ
wenzelm@24709
    78
  val read_term_global: theory -> string -> term
wenzelm@24709
    79
  val read_prop_global: theory -> string -> term
wenzelm@24923
    80
  val pretty_term: Proof.context -> term -> Pretty.T
wenzelm@24923
    81
  val pretty_typ: Proof.context -> typ -> Pretty.T
wenzelm@24923
    82
  val pretty_sort: Proof.context -> sort -> Pretty.T
wenzelm@24923
    83
  val pretty_classrel: Proof.context -> class list -> Pretty.T
wenzelm@24923
    84
  val pretty_arity: Proof.context -> arity -> Pretty.T
wenzelm@24923
    85
  val string_of_term: Proof.context -> term -> string
wenzelm@24923
    86
  val string_of_typ: Proof.context -> typ -> string
wenzelm@24923
    87
  val string_of_sort: Proof.context -> sort -> string
wenzelm@24923
    88
  val string_of_classrel: Proof.context -> class list -> string
wenzelm@24923
    89
  val string_of_arity: Proof.context -> arity -> string
wenzelm@26951
    90
  val is_pretty_global: Proof.context -> bool
wenzelm@26951
    91
  val set_pretty_global: bool -> Proof.context -> Proof.context
wenzelm@26951
    92
  val init_pretty_global: theory -> Proof.context
wenzelm@42383
    93
  val init_pretty: Context.pretty -> Proof.context
wenzelm@26951
    94
  val pretty_term_global: theory -> term -> Pretty.T
wenzelm@26951
    95
  val pretty_typ_global: theory -> typ -> Pretty.T
wenzelm@26951
    96
  val pretty_sort_global: theory -> sort -> Pretty.T
wenzelm@26951
    97
  val string_of_term_global: theory -> term -> string
wenzelm@26951
    98
  val string_of_typ_global: theory -> typ -> string
wenzelm@26951
    99
  val string_of_sort_global: theory -> sort -> string
wenzelm@39135
   100
  type syntax
wenzelm@39135
   101
  val eq_syntax: syntax * syntax -> bool
wenzelm@42298
   102
  val lookup_const: syntax -> string -> string option
wenzelm@39135
   103
  val is_keyword: syntax -> string -> bool
wenzelm@42251
   104
  val tokenize: syntax -> bool -> Symbol_Pos.T list -> Lexicon.token list
wenzelm@42251
   105
  val parse: Proof.context -> syntax -> string -> Lexicon.token list -> Parser.parsetree list
wenzelm@42253
   106
  val parse_ast_translation: syntax -> string -> (Proof.context -> Ast.ast list -> Ast.ast) option
wenzelm@42255
   107
  val parse_rules: syntax -> string -> (Ast.ast * Ast.ast) list
wenzelm@42253
   108
  val parse_translation: syntax -> string -> (Proof.context -> term list -> term) option
wenzelm@42254
   109
  val print_translation: syntax -> string ->
wenzelm@42254
   110
    Proof.context -> typ -> term list -> term  (*exception Match*)
wenzelm@42255
   111
  val print_rules: syntax -> string -> (Ast.ast * Ast.ast) list
wenzelm@42254
   112
  val print_ast_translation: syntax -> string ->
wenzelm@42254
   113
    Proof.context -> Ast.ast list -> Ast.ast  (*exception Match*)
wenzelm@42255
   114
  val prtabs: syntax -> Printer.prtabs
wenzelm@39135
   115
  type mode
wenzelm@39135
   116
  val mode_default: mode
wenzelm@39135
   117
  val mode_input: mode
wenzelm@42294
   118
  val empty_syntax: syntax
wenzelm@39135
   119
  val merge_syntaxes: syntax -> syntax -> syntax
wenzelm@42294
   120
  val token_markers: string list
wenzelm@39135
   121
  val basic_nonterms: string list
wenzelm@39135
   122
  val print_gram: syntax -> unit
wenzelm@39135
   123
  val print_trans: syntax -> unit
wenzelm@39135
   124
  val print_syntax: syntax -> unit
wenzelm@39135
   125
  val guess_infix: syntax -> string -> mixfix option
wenzelm@39135
   126
  datatype 'a trrule =
wenzelm@42204
   127
    Parse_Rule of 'a * 'a |
wenzelm@42204
   128
    Print_Rule of 'a * 'a |
wenzelm@42204
   129
    Parse_Print_Rule of 'a * 'a
wenzelm@39135
   130
  val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
wenzelm@39135
   131
  val update_trfuns:
wenzelm@42224
   132
    (string * ((Ast.ast list -> Ast.ast) * stamp)) list *
wenzelm@39135
   133
    (string * ((term list -> term) * stamp)) list *
wenzelm@42247
   134
    (string * ((typ -> term list -> term) * stamp)) list *
wenzelm@42224
   135
    (string * ((Ast.ast list -> Ast.ast) * stamp)) list -> syntax -> syntax
wenzelm@39135
   136
  val update_advanced_trfuns:
wenzelm@42224
   137
    (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list *
wenzelm@39135
   138
    (string * ((Proof.context -> term list -> term) * stamp)) list *
wenzelm@42247
   139
    (string * ((Proof.context -> typ -> term list -> term) * stamp)) list *
wenzelm@42224
   140
    (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list -> syntax -> syntax
wenzelm@39135
   141
  val update_type_gram: bool -> mode -> (string * typ * mixfix) list -> syntax -> syntax
wenzelm@39135
   142
  val update_const_gram: bool -> (string -> bool) ->
wenzelm@39135
   143
    mode -> (string * typ * mixfix) list -> syntax -> syntax
wenzelm@42224
   144
  val update_trrules: Ast.ast trrule list -> syntax -> syntax
wenzelm@42224
   145
  val remove_trrules: Ast.ast trrule list -> syntax -> syntax
wenzelm@2383
   146
end;
clasohm@0
   147
wenzelm@12094
   148
structure Syntax: SYNTAX =
clasohm@0
   149
struct
clasohm@0
   150
wenzelm@42290
   151
val const = Lexicon.const;
wenzelm@42290
   152
val free = Lexicon.free;
wenzelm@42290
   153
val var = Lexicon.var;
wenzelm@42290
   154
wenzelm@42290
   155
wenzelm@42288
   156
wenzelm@39135
   157
(** inner syntax operations **)
wenzelm@39135
   158
wenzelm@42382
   159
(* back-patched operations *)
wenzelm@42382
   160
wenzelm@42382
   161
type operations =
wenzelm@42382
   162
 {parse_sort: Proof.context -> string -> sort,
wenzelm@42382
   163
  parse_typ: Proof.context -> string -> typ,
wenzelm@42382
   164
  parse_term: Proof.context -> string -> term,
wenzelm@42382
   165
  parse_prop: Proof.context -> string -> term,
wenzelm@42382
   166
  unparse_sort: Proof.context -> sort -> Pretty.T,
wenzelm@42382
   167
  unparse_typ: Proof.context -> typ -> Pretty.T,
wenzelm@42382
   168
  unparse_term: Proof.context -> term -> Pretty.T,
wenzelm@42382
   169
  check_typs: Proof.context -> typ list -> typ list,
wenzelm@42382
   170
  check_terms: Proof.context -> term list -> term list,
wenzelm@42382
   171
  check_props: Proof.context -> term list -> term list,
wenzelm@42382
   172
  uncheck_typs: Proof.context -> typ list -> typ list,
wenzelm@42382
   173
  uncheck_terms: Proof.context -> term list -> term list};
wenzelm@42382
   174
wenzelm@42382
   175
val operations: operations Single_Assignment.var = Single_Assignment.var "Syntax.operations";
wenzelm@42382
   176
fun install_operations ops = Single_Assignment.assign operations ops;
wenzelm@42382
   177
wenzelm@42382
   178
fun operation which ctxt x =
wenzelm@42382
   179
  (case Single_Assignment.peek operations of
wenzelm@42382
   180
    NONE => raise Fail "Inner syntax operations not installed"
wenzelm@42382
   181
  | SOME ops => which ops ctxt x);
wenzelm@42382
   182
wenzelm@42382
   183
wenzelm@42268
   184
(* configuration options *)
wenzelm@42268
   185
wenzelm@42293
   186
val root = Config.string (Config.declare "syntax_root" (K (Config.String "any")));
wenzelm@42288
   187
wenzelm@42268
   188
val positions_raw = Config.declare "syntax_positions" (fn _ => Config.Bool true);
wenzelm@42268
   189
val positions = Config.bool positions_raw;
wenzelm@42268
   190
wenzelm@42268
   191
val ambiguity_enabled =
wenzelm@42268
   192
  Config.bool (Config.declare "syntax_ambiguity_enabled" (fn _ => Config.Bool true));
wenzelm@42268
   193
wenzelm@42268
   194
val ambiguity_level_raw = Config.declare "syntax_ambiguity_level" (fn _ => Config.Int 1);
wenzelm@42268
   195
val ambiguity_level = Config.int ambiguity_level_raw;
wenzelm@42268
   196
wenzelm@42268
   197
val ambiguity_limit =
wenzelm@42268
   198
  Config.int (Config.declare "syntax_ambiguity_limit" (fn _ => Config.Int 10));
wenzelm@42268
   199
wenzelm@42268
   200
wenzelm@39135
   201
(* read token -- with optional YXML encoding of position *)
wenzelm@39135
   202
wenzelm@39135
   203
fun read_token str =
wenzelm@39135
   204
  let
wenzelm@39135
   205
    val tree = YXML.parse str handle Fail msg => error msg;
wenzelm@39555
   206
    val text = XML.content_of [tree];
wenzelm@39135
   207
    val pos =
wenzelm@39135
   208
      (case tree of
wenzelm@39135
   209
        XML.Elem ((name, props), _) =>
wenzelm@39135
   210
          if name = Markup.tokenN then Position.of_properties props
wenzelm@39135
   211
          else Position.none
wenzelm@39135
   212
      | XML.Text _ => Position.none);
wenzelm@39135
   213
  in (Symbol_Pos.explode (text, pos), pos) end;
wenzelm@39135
   214
wenzelm@39135
   215
wenzelm@39135
   216
(* (un)parsing *)
wenzelm@39135
   217
wenzelm@39135
   218
fun parse_token ctxt markup str =
wenzelm@39135
   219
  let
wenzelm@39135
   220
    val (syms, pos) = read_token str;
wenzelm@39507
   221
    val _ = Context_Position.report ctxt pos markup;
wenzelm@39135
   222
  in (syms, pos) end;
wenzelm@39135
   223
wenzelm@39135
   224
val parse_sort = operation #parse_sort;
wenzelm@39135
   225
val parse_typ = operation #parse_typ;
wenzelm@39135
   226
val parse_term = operation #parse_term;
wenzelm@39135
   227
val parse_prop = operation #parse_prop;
wenzelm@39135
   228
val unparse_sort = operation #unparse_sort;
wenzelm@39135
   229
val unparse_typ = operation #unparse_typ;
wenzelm@39135
   230
val unparse_term = operation #unparse_term;
wenzelm@39135
   231
wenzelm@39135
   232
wenzelm@39135
   233
(* context-sensitive (un)checking *)
wenzelm@39135
   234
wenzelm@39135
   235
type key = int * bool;
wenzelm@39135
   236
wenzelm@39135
   237
structure Checks = Generic_Data
wenzelm@39135
   238
(
wenzelm@42402
   239
  type 'a check = 'a list -> Proof.context -> ('a list * Proof.context) option;
wenzelm@39135
   240
  type T =
wenzelm@39135
   241
    ((key * ((string * typ check) * stamp) list) list *
wenzelm@39135
   242
     (key * ((string * term check) * stamp) list) list);
wenzelm@39135
   243
  val empty = ([], []);
wenzelm@39135
   244
  val extend = I;
wenzelm@39135
   245
  fun merge ((typ_checks1, term_checks1), (typ_checks2, term_checks2)) : T =
wenzelm@39135
   246
    (AList.join (op =) (K (Library.merge (eq_snd (op =)))) (typ_checks1, typ_checks2),
wenzelm@39135
   247
     AList.join (op =) (K (Library.merge (eq_snd (op =)))) (term_checks1, term_checks2));
wenzelm@39135
   248
);
wenzelm@39135
   249
wenzelm@39135
   250
fun print_checks ctxt =
wenzelm@39135
   251
  let
wenzelm@39135
   252
    fun split_checks checks =
wenzelm@39135
   253
      List.partition (fn ((_, un), _) => not un) checks
wenzelm@39135
   254
      |> pairself (map (fn ((i, _), fs) => (i, map (fst o fst) fs))
wenzelm@39135
   255
          #> sort (int_ord o pairself fst));
wenzelm@39135
   256
    fun pretty_checks kind checks =
wenzelm@39135
   257
      checks |> map (fn (i, names) => Pretty.block
wenzelm@39135
   258
        [Pretty.str (kind ^ " (stage " ^ signed_string_of_int i ^ "):"),
wenzelm@39135
   259
          Pretty.brk 1, Pretty.strs names]);
wenzelm@39135
   260
wenzelm@39135
   261
    val (typs, terms) = Checks.get (Context.Proof ctxt);
wenzelm@39135
   262
    val (typ_checks, typ_unchecks) = split_checks typs;
wenzelm@39135
   263
    val (term_checks, term_unchecks) = split_checks terms;
wenzelm@39135
   264
  in
wenzelm@39135
   265
    pretty_checks "typ_checks" typ_checks @
wenzelm@39135
   266
    pretty_checks "term_checks" term_checks @
wenzelm@39135
   267
    pretty_checks "typ_unchecks" typ_unchecks @
wenzelm@39135
   268
    pretty_checks "term_unchecks" term_unchecks
wenzelm@39135
   269
  end |> Pretty.chunks |> Pretty.writeln;
wenzelm@39135
   270
wenzelm@42402
   271
wenzelm@42402
   272
local
wenzelm@42402
   273
wenzelm@42402
   274
fun context_check which (key: key) name f =
wenzelm@42402
   275
  Checks.map (which (AList.map_default op = (key, []) (cons ((name, f), stamp ()))));
wenzelm@42402
   276
wenzelm@42402
   277
fun simple_check eq f xs ctxt =
wenzelm@42403
   278
  let val xs' = f ctxt xs
wenzelm@42403
   279
  in if eq_list eq (xs, xs') then NONE else SOME (xs', ctxt) end;
wenzelm@42402
   280
wenzelm@42402
   281
in
wenzelm@42402
   282
wenzelm@42402
   283
fun context_typ_check stage = context_check apfst (stage, false);
wenzelm@42402
   284
fun context_term_check stage = context_check apsnd (stage, false);
wenzelm@42402
   285
fun context_typ_uncheck stage = context_check apfst (stage, true);
wenzelm@42402
   286
fun context_term_uncheck stage = context_check apsnd (stage, true);
wenzelm@39135
   287
wenzelm@42402
   288
fun add_typ_check key name f = context_typ_check key name (simple_check (op =) f);
wenzelm@42402
   289
fun add_term_check key name f = context_term_check key name (simple_check (op aconv) f);
wenzelm@42402
   290
fun add_typ_uncheck key name f = context_typ_uncheck key name (simple_check (op =) f);
wenzelm@42402
   291
fun add_term_uncheck key name f = context_term_uncheck key name (simple_check (op aconv) f);
wenzelm@42402
   292
wenzelm@42402
   293
end;
wenzelm@42402
   294
wenzelm@42402
   295
wenzelm@42402
   296
local
wenzelm@42402
   297
wenzelm@42402
   298
fun check_stage fs = perhaps_loop (perhaps_apply (map uncurry fs));
wenzelm@42402
   299
fun check_all fs = perhaps_apply (map check_stage fs);
wenzelm@42402
   300
wenzelm@42402
   301
fun check which uncheck ctxt0 xs0 =
wenzelm@42402
   302
  let
wenzelm@42402
   303
    val funs = which (Checks.get (Context.Proof ctxt0))
wenzelm@42402
   304
      |> map_filter (fn ((i, u), fs) => if uncheck = u then SOME (i, map (snd o fst) fs) else NONE)
wenzelm@42402
   305
      |> Library.sort (int_ord o pairself fst) |> map snd
wenzelm@42402
   306
      |> not uncheck ? map rev;
wenzelm@42402
   307
  in #1 (perhaps (check_all funs) (xs0, ctxt0)) end;
wenzelm@42402
   308
wenzelm@42402
   309
in
wenzelm@42402
   310
wenzelm@42402
   311
val typ_check = check fst false;
wenzelm@42402
   312
val term_check = check snd false;
wenzelm@42402
   313
val typ_uncheck = check fst true;
wenzelm@42402
   314
val term_uncheck = check snd true;
wenzelm@42382
   315
wenzelm@42382
   316
val check_typs = operation #check_typs;
wenzelm@42382
   317
val check_terms = operation #check_terms;
wenzelm@42382
   318
val check_props = operation #check_props;
wenzelm@39135
   319
wenzelm@39135
   320
val check_typ = singleton o check_typs;
wenzelm@39135
   321
val check_term = singleton o check_terms;
wenzelm@39135
   322
val check_prop = singleton o check_props;
wenzelm@39135
   323
wenzelm@42382
   324
val uncheck_typs = operation #uncheck_typs;
wenzelm@42382
   325
val uncheck_terms = operation #uncheck_terms;
wenzelm@42402
   326
wenzelm@42402
   327
end;
wenzelm@42402
   328
wenzelm@42402
   329
wenzelm@42402
   330
(* derived operations for algebra of sorts *)
wenzelm@42402
   331
wenzelm@42402
   332
local
wenzelm@42402
   333
wenzelm@42402
   334
fun map_sort f S =
wenzelm@42402
   335
  (case f (TFree ("", S)) of
wenzelm@42402
   336
    TFree ("", S') => S'
wenzelm@42402
   337
  | _ => raise TYPE ("map_sort", [TFree ("", S)], []));
wenzelm@42402
   338
wenzelm@42402
   339
in
wenzelm@42402
   340
wenzelm@42402
   341
val check_sort = map_sort o check_typ;
wenzelm@39135
   342
val uncheck_sort = map_sort o singleton o uncheck_typs;
wenzelm@39135
   343
wenzelm@39135
   344
end;
wenzelm@39135
   345
wenzelm@39135
   346
wenzelm@39135
   347
val uncheck_classrel = map o singleton o uncheck_sort;
wenzelm@39135
   348
wenzelm@39135
   349
fun unparse_classrel ctxt cs = Pretty.block (flat
wenzelm@39135
   350
  (separate [Pretty.str " <", Pretty.brk 1] (map (single o unparse_sort ctxt o single) cs)));
wenzelm@39135
   351
wenzelm@39135
   352
fun uncheck_arity ctxt (a, Ss, S) =
wenzelm@39135
   353
  let
wenzelm@39135
   354
    val T = Type (a, replicate (length Ss) dummyT);
wenzelm@39135
   355
    val a' =
wenzelm@39135
   356
      (case singleton (uncheck_typs ctxt) T of
wenzelm@39135
   357
        Type (a', _) => a'
wenzelm@39135
   358
      | T => raise TYPE ("uncheck_arity", [T], []));
wenzelm@39135
   359
    val Ss' = map (uncheck_sort ctxt) Ss;
wenzelm@39135
   360
    val S' = uncheck_sort ctxt S;
wenzelm@39135
   361
  in (a', Ss', S') end;
wenzelm@39135
   362
wenzelm@39135
   363
fun unparse_arity ctxt (a, Ss, S) =
wenzelm@39135
   364
  let
wenzelm@39135
   365
    val prtT = unparse_typ ctxt (Type (a, []));
wenzelm@39135
   366
    val dom =
wenzelm@39135
   367
      if null Ss then []
wenzelm@39135
   368
      else [Pretty.list "(" ")" (map (unparse_sort ctxt) Ss), Pretty.brk 1];
wenzelm@39135
   369
  in Pretty.block ([prtT, Pretty.str " ::", Pretty.brk 1] @ dom @ [unparse_sort ctxt S]) end;
wenzelm@39135
   370
wenzelm@39135
   371
wenzelm@39135
   372
(* read = parse + check *)
wenzelm@39135
   373
wenzelm@39135
   374
fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt;
wenzelm@39135
   375
fun read_typ ctxt = parse_typ ctxt #> singleton (check_typs ctxt);
wenzelm@39135
   376
wenzelm@39135
   377
fun read_terms ctxt = map (parse_term ctxt) #> check_terms ctxt;
wenzelm@39135
   378
fun read_props ctxt = map (parse_prop ctxt) #> check_props ctxt;
wenzelm@39135
   379
wenzelm@39135
   380
val read_term = singleton o read_terms;
wenzelm@39135
   381
val read_prop = singleton o read_props;
wenzelm@39135
   382
wenzelm@42360
   383
val read_sort_global = read_sort o Proof_Context.init_global;
wenzelm@42360
   384
val read_typ_global = read_typ o Proof_Context.init_global;
wenzelm@42360
   385
val read_term_global = read_term o Proof_Context.init_global;
wenzelm@42360
   386
val read_prop_global = read_prop o Proof_Context.init_global;
wenzelm@39135
   387
wenzelm@39135
   388
wenzelm@39135
   389
(* pretty = uncheck + unparse *)
wenzelm@39135
   390
wenzelm@39135
   391
fun pretty_term ctxt = singleton (uncheck_terms ctxt) #> unparse_term ctxt;
wenzelm@39135
   392
fun pretty_typ ctxt = singleton (uncheck_typs ctxt) #> unparse_typ ctxt;
wenzelm@39135
   393
fun pretty_sort ctxt = uncheck_sort ctxt #> unparse_sort ctxt;
wenzelm@39135
   394
fun pretty_classrel ctxt = uncheck_classrel ctxt #> unparse_classrel ctxt;
wenzelm@39135
   395
fun pretty_arity ctxt = uncheck_arity ctxt #> unparse_arity ctxt;
wenzelm@39135
   396
wenzelm@39135
   397
val string_of_term = Pretty.string_of oo pretty_term;
wenzelm@39135
   398
val string_of_typ = Pretty.string_of oo pretty_typ;
wenzelm@39135
   399
val string_of_sort = Pretty.string_of oo pretty_sort;
wenzelm@39135
   400
val string_of_classrel = Pretty.string_of oo pretty_classrel;
wenzelm@39135
   401
val string_of_arity = Pretty.string_of oo pretty_arity;
wenzelm@39135
   402
wenzelm@39135
   403
wenzelm@39135
   404
(* global pretty printing *)
wenzelm@39135
   405
wenzelm@39508
   406
val pretty_global = Config.bool (Config.declare "Syntax.pretty_global" (K (Config.Bool false)));
wenzelm@39508
   407
fun is_pretty_global ctxt = Config.get ctxt pretty_global;
wenzelm@39508
   408
val set_pretty_global = Config.put pretty_global;
wenzelm@42360
   409
val init_pretty_global = set_pretty_global true o Proof_Context.init_global;
wenzelm@42383
   410
val init_pretty = Context.pretty_context init_pretty_global;
wenzelm@39135
   411
wenzelm@39135
   412
val pretty_term_global = pretty_term o init_pretty_global;
wenzelm@39135
   413
val pretty_typ_global = pretty_typ o init_pretty_global;
wenzelm@39135
   414
val pretty_sort_global = pretty_sort o init_pretty_global;
wenzelm@39135
   415
wenzelm@39135
   416
val string_of_term_global = string_of_term o init_pretty_global;
wenzelm@39135
   417
val string_of_typ_global = string_of_typ o init_pretty_global;
wenzelm@39135
   418
val string_of_sort_global = string_of_sort o init_pretty_global;
wenzelm@39135
   419
wenzelm@39135
   420
wenzelm@39135
   421
wenzelm@237
   422
(** tables of translation functions **)
wenzelm@237
   423
wenzelm@5692
   424
(* parse (ast) translations *)
wenzelm@5692
   425
wenzelm@23655
   426
fun err_dup_trfun name c =
wenzelm@23655
   427
  error ("More than one " ^ name ^ " for " ^ quote c);
wenzelm@237
   428
wenzelm@42253
   429
fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c);
wenzelm@42253
   430
wenzelm@42288
   431
fun remove_trtab trfuns = fold (Symtab.remove Syntax_Ext.eq_trfun) trfuns;
wenzelm@21536
   432
haftmann@29004
   433
fun update_trtab name trfuns tab = fold Symtab.update_new trfuns (remove_trtab trfuns tab)
wenzelm@23655
   434
  handle Symtab.DUP c => err_dup_trfun name c;
wenzelm@237
   435
wenzelm@42288
   436
fun merge_trtabs name tab1 tab2 = Symtab.merge Syntax_Ext.eq_trfun (tab1, tab2)
wenzelm@23655
   437
  handle Symtab.DUP c => err_dup_trfun name c;
wenzelm@237
   438
wenzelm@237
   439
wenzelm@5692
   440
(* print (ast) translations *)
wenzelm@5692
   441
wenzelm@42254
   442
fun apply_tr' tab c ctxt T args =
wenzelm@42254
   443
  let
wenzelm@42254
   444
    val fns = map fst (Symtab.lookup_list tab c);
wenzelm@42254
   445
    fun app_first [] = raise Match
wenzelm@42254
   446
      | app_first (f :: fs) = f ctxt T args handle Match => app_first fs;
wenzelm@42254
   447
  in app_first fns end;
wenzelm@42254
   448
wenzelm@42254
   449
fun apply_ast_tr' tab c ctxt args =
wenzelm@42254
   450
  let
wenzelm@42254
   451
    val fns = map fst (Symtab.lookup_list tab c);
wenzelm@42254
   452
    fun app_first [] = raise Match
wenzelm@42254
   453
      | app_first (f :: fs) = f ctxt args handle Match => app_first fs;
wenzelm@42254
   454
  in app_first fns end;
wenzelm@42253
   455
wenzelm@42288
   456
fun update_tr'tab trfuns = fold_rev (Symtab.update_list Syntax_Ext.eq_trfun) trfuns;
wenzelm@42288
   457
fun remove_tr'tab trfuns = fold (Symtab.remove_list Syntax_Ext.eq_trfun) trfuns;
wenzelm@42288
   458
fun merge_tr'tabs tab1 tab2 = Symtab.merge_list Syntax_Ext.eq_trfun (tab1, tab2);
wenzelm@5692
   459
wenzelm@5692
   460
wenzelm@237
   461
wenzelm@237
   462
(** tables of translation rules **)
wenzelm@237
   463
wenzelm@5692
   464
type ruletab = (Ast.ast * Ast.ast) list Symtab.table;
wenzelm@237
   465
wenzelm@19482
   466
fun dest_ruletab tab = maps snd (Symtab.dest tab);
wenzelm@237
   467
wenzelm@25394
   468
val update_ruletab = fold_rev (fn r => Symtab.update_list (op =) (Ast.head_of_rule r, r));
wenzelm@18931
   469
val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r));
wenzelm@18931
   470
fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2);
clasohm@0
   471
clasohm@0
   472
clasohm@0
   473
clasohm@0
   474
(** datatype syntax **)
clasohm@0
   475
wenzelm@237
   476
datatype syntax =
wenzelm@237
   477
  Syntax of {
wenzelm@42288
   478
    input: Syntax_Ext.xprod list,
wenzelm@4703
   479
    lexicon: Scan.lexicon,
wenzelm@5692
   480
    gram: Parser.gram,
wenzelm@42298
   481
    consts: string Symtab.table,
wenzelm@2913
   482
    prmodes: string list,
wenzelm@21772
   483
    parse_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) Symtab.table,
wenzelm@237
   484
    parse_ruletab: ruletab,
wenzelm@21772
   485
    parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table,
wenzelm@42247
   486
    print_trtab: ((Proof.context -> typ -> term list -> term) * stamp) list Symtab.table,
wenzelm@237
   487
    print_ruletab: ruletab,
wenzelm@21772
   488
    print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
wenzelm@17079
   489
    prtabs: Printer.prtabs} * stamp;
clasohm@0
   490
wenzelm@17079
   491
fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2;
wenzelm@17079
   492
wenzelm@42298
   493
fun lookup_const (Syntax ({consts, ...}, _)) = Symtab.lookup consts;
wenzelm@17079
   494
fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode;
wenzelm@42251
   495
fun tokenize (Syntax ({lexicon, ...}, _)) = Lexicon.tokenize lexicon;
wenzelm@42251
   496
fun parse ctxt (Syntax ({gram, ...}, _)) = Parser.parse ctxt gram;
wenzelm@14687
   497
wenzelm@42253
   498
fun parse_ast_translation (Syntax ({parse_ast_trtab, ...}, _)) = lookup_tr parse_ast_trtab;
wenzelm@42253
   499
fun parse_translation (Syntax ({parse_trtab, ...}, _)) = lookup_tr parse_trtab;
wenzelm@42255
   500
fun parse_rules (Syntax ({parse_ruletab, ...}, _)) = Symtab.lookup_list parse_ruletab;
wenzelm@42254
   501
fun print_translation (Syntax ({print_trtab, ...}, _)) = apply_tr' print_trtab;
wenzelm@42255
   502
fun print_rules (Syntax ({print_ruletab, ...}, _)) = Symtab.lookup_list print_ruletab;
wenzelm@42254
   503
fun print_ast_translation (Syntax ({print_ast_trtab, ...}, _)) = apply_ast_tr' print_ast_trtab;
wenzelm@42253
   504
wenzelm@42255
   505
fun prtabs (Syntax ({prtabs, ...}, _)) = prtabs;
wenzelm@42255
   506
wenzelm@20784
   507
type mode = string * bool;
wenzelm@24970
   508
val mode_default = ("", true);
wenzelm@37146
   509
val mode_input = (Print_Mode.input, true);
wenzelm@15755
   510
wenzelm@18
   511
wenzelm@237
   512
(* empty_syntax *)
wenzelm@18
   513
wenzelm@17079
   514
val empty_syntax = Syntax
wenzelm@17079
   515
  ({input = [],
wenzelm@4703
   516
    lexicon = Scan.empty_lexicon,
wenzelm@5692
   517
    gram = Parser.empty_gram,
wenzelm@42298
   518
    consts = Symtab.empty,
wenzelm@2913
   519
    prmodes = [],
wenzelm@5692
   520
    parse_ast_trtab = Symtab.empty,
wenzelm@5692
   521
    parse_ruletab = Symtab.empty,
wenzelm@5692
   522
    parse_trtab = Symtab.empty,
wenzelm@5692
   523
    print_trtab = Symtab.empty,
wenzelm@5692
   524
    print_ruletab = Symtab.empty,
wenzelm@5692
   525
    print_ast_trtab = Symtab.empty,
wenzelm@17079
   526
    prtabs = Printer.empty_prtabs}, stamp ());
wenzelm@167
   527
wenzelm@167
   528
wenzelm@25394
   529
(* update_syntax *)
wenzelm@167
   530
wenzelm@42298
   531
fun update_const (c, b) tab =
wenzelm@42298
   532
  if c = "" orelse (b = "" andalso (Lexicon.is_marked c orelse Symtab.defined tab c))
wenzelm@42298
   533
  then tab
wenzelm@42298
   534
  else Symtab.update (c, b) tab;
wenzelm@42298
   535
wenzelm@25394
   536
fun update_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
wenzelm@167
   537
  let
wenzelm@42268
   538
    val {input, lexicon, gram, consts = consts1, prmodes, parse_ast_trtab, parse_ruletab,
wenzelm@42268
   539
      parse_trtab, print_trtab, print_ruletab, print_ast_trtab, prtabs} = tabs;
wenzelm@42288
   540
    val Syntax_Ext.Syn_Ext {xprods, consts = consts2, parse_ast_translation,
wenzelm@42268
   541
      parse_rules, parse_translation, print_translation, print_rules,
wenzelm@42268
   542
      print_ast_translation} = syn_ext;
wenzelm@36208
   543
    val new_xprods =
wenzelm@36208
   544
      if inout then distinct (op =) (filter_out (member (op =) input) xprods) else [];
wenzelm@19546
   545
    fun if_inout xs = if inout then xs else [];
wenzelm@167
   546
  in
wenzelm@17079
   547
    Syntax
wenzelm@36208
   548
    ({input = new_xprods @ input,
wenzelm@42288
   549
      lexicon = fold Scan.extend_lexicon (Syntax_Ext.delims_of new_xprods) lexicon,
wenzelm@37684
   550
      gram = Parser.extend_gram new_xprods gram,
wenzelm@42298
   551
      consts = fold update_const consts2 consts1,
wenzelm@42268
   552
      prmodes = insert (op =) mode prmodes,
wenzelm@167
   553
      parse_ast_trtab =
wenzelm@25394
   554
        update_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab,
wenzelm@25394
   555
      parse_ruletab = update_ruletab (if_inout parse_rules) parse_ruletab,
wenzelm@25394
   556
      parse_trtab = update_trtab "parse translation" (if_inout parse_translation) parse_trtab,
wenzelm@25394
   557
      print_trtab = update_tr'tab print_translation print_trtab,
wenzelm@25394
   558
      print_ruletab = update_ruletab print_rules print_ruletab,
wenzelm@25394
   559
      print_ast_trtab = update_tr'tab print_ast_translation print_ast_trtab,
wenzelm@25394
   560
      prtabs = Printer.update_prtabs mode xprods prtabs}, stamp ())
wenzelm@18
   561
  end;
wenzelm@18
   562
wenzelm@15755
   563
wenzelm@15755
   564
(* remove_syntax *)
wenzelm@15755
   565
wenzelm@17079
   566
fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
wenzelm@15755
   567
  let
wenzelm@42288
   568
    val Syntax_Ext.Syn_Ext {xprods, consts = _, parse_ast_translation, parse_rules,
wenzelm@42268
   569
      parse_translation, print_translation, print_rules, print_ast_translation} = syn_ext;
wenzelm@42268
   570
    val {input, lexicon, gram, consts, prmodes, parse_ast_trtab, parse_ruletab,
wenzelm@42268
   571
      parse_trtab, print_trtab, print_ruletab, print_ast_trtab, prtabs} = tabs;
wenzelm@19300
   572
    val input' = if inout then subtract (op =) xprods input else input;
wenzelm@25394
   573
    val changed = length input <> length input';
wenzelm@19546
   574
    fun if_inout xs = if inout then xs else [];
wenzelm@15755
   575
  in
wenzelm@17079
   576
    Syntax
wenzelm@17079
   577
    ({input = input',
wenzelm@42288
   578
      lexicon = if changed then Scan.make_lexicon (Syntax_Ext.delims_of input') else lexicon,
wenzelm@42217
   579
      gram = if changed then Parser.extend_gram input' Parser.empty_gram else gram,
wenzelm@15755
   580
      consts = consts,
wenzelm@15755
   581
      prmodes = prmodes,
wenzelm@19546
   582
      parse_ast_trtab = remove_trtab (if_inout parse_ast_translation) parse_ast_trtab,
wenzelm@19546
   583
      parse_ruletab = remove_ruletab (if_inout parse_rules) parse_ruletab,
wenzelm@19546
   584
      parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab,
wenzelm@15755
   585
      print_trtab = remove_tr'tab print_translation print_trtab,
wenzelm@15755
   586
      print_ruletab = remove_ruletab print_rules print_ruletab,
wenzelm@15755
   587
      print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
wenzelm@17079
   588
      prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ())
wenzelm@15755
   589
  end;
wenzelm@14904
   590
wenzelm@18
   591
wenzelm@237
   592
(* merge_syntaxes *)
clasohm@0
   593
wenzelm@17079
   594
fun merge_syntaxes (Syntax (tabs1, _)) (Syntax (tabs2, _)) =
clasohm@0
   595
  let
wenzelm@15755
   596
    val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
wenzelm@42268
   597
      prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1,
wenzelm@42268
   598
      parse_trtab = parse_trtab1, print_trtab = print_trtab1, print_ruletab = print_ruletab1,
wenzelm@42268
   599
      print_ast_trtab = print_ast_trtab1, prtabs = prtabs1} = tabs1;
wenzelm@237
   600
wenzelm@15755
   601
    val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
wenzelm@42268
   602
      prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2,
wenzelm@42268
   603
      parse_trtab = parse_trtab2, print_trtab = print_trtab2, print_ruletab = print_ruletab2,
wenzelm@42268
   604
      print_ast_trtab = print_ast_trtab2, prtabs = prtabs2} = tabs2;
clasohm@0
   605
  in
wenzelm@17079
   606
    Syntax
wenzelm@18921
   607
    ({input = Library.merge (op =) (input1, input2),
wenzelm@27768
   608
      lexicon = Scan.merge_lexicons (lexicon1, lexicon2),
wenzelm@37684
   609
      gram = Parser.merge_gram (gram1, gram2),
wenzelm@42298
   610
      consts = Symtab.merge (K true) (consts1, consts2),
wenzelm@18921
   611
      prmodes = Library.merge (op =) (prmodes1, prmodes2),
wenzelm@237
   612
      parse_ast_trtab =
wenzelm@15755
   613
        merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
wenzelm@237
   614
      parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
wenzelm@15755
   615
      parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
wenzelm@5692
   616
      print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
wenzelm@237
   617
      print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
wenzelm@5692
   618
      print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
wenzelm@17079
   619
      prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ())
clasohm@0
   620
  end;
clasohm@0
   621
clasohm@0
   622
wenzelm@18720
   623
(* basic syntax *)
wenzelm@260
   624
wenzelm@42294
   625
val token_markers =
wenzelm@42294
   626
  ["_tfree", "_tvar", "_free", "_bound", "_var", "_numeral", "_inner_string"];
wenzelm@260
   627
wenzelm@18720
   628
val basic_nonterms =
wenzelm@42293
   629
  (Lexicon.terminals @ ["logic", "type", "types", "sort", "classes",
wenzelm@42293
   630
    "args", "cargs", "pttrn", "pttrns", "idt", "idts", "aprop", "asms",
wenzelm@42293
   631
    "any", "prop'", "num_const", "float_const", "index", "struct",
wenzelm@42293
   632
    "id_position", "longid_position", "type_name", "class_name"]);
wenzelm@18720
   633
clasohm@0
   634
wenzelm@4887
   635
wenzelm@15759
   636
(** print syntax **)
wenzelm@15759
   637
wenzelm@15759
   638
local
clasohm@0
   639
wenzelm@260
   640
fun pretty_strs_qs name strs =
wenzelm@28840
   641
  Pretty.strs (name :: map quote (sort_strings strs));
clasohm@0
   642
wenzelm@17079
   643
fun pretty_gram (Syntax (tabs, _)) =
clasohm@0
   644
  let
wenzelm@32784
   645
    val {lexicon, prmodes, gram, ...} = tabs;
wenzelm@28375
   646
    val prmodes' = sort_strings (filter_out (fn s => s = "") prmodes);
clasohm@0
   647
  in
wenzelm@8720
   648
    [pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon),
wenzelm@8720
   649
      Pretty.big_list "prods:" (Parser.pretty_gram gram),
wenzelm@8720
   650
      pretty_strs_qs "print modes:" prmodes']
clasohm@0
   651
  end;
clasohm@0
   652
wenzelm@17079
   653
fun pretty_trans (Syntax (tabs, _)) =
clasohm@0
   654
  let
wenzelm@42298
   655
    fun pretty_tab name tab =
wenzelm@42298
   656
      pretty_strs_qs name (sort_strings (Symtab.keys tab));
clasohm@0
   657
wenzelm@260
   658
    fun pretty_ruletab name tab =
wenzelm@5692
   659
      Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab));
clasohm@0
   660
wenzelm@237
   661
    val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
wenzelm@42268
   662
      print_ruletab, print_ast_trtab, ...} = tabs;
clasohm@0
   663
  in
wenzelm@42298
   664
   [pretty_tab "consts:" consts,
wenzelm@42298
   665
    pretty_tab "parse_ast_translation:" parse_ast_trtab,
wenzelm@42298
   666
    pretty_ruletab "parse_rules:" parse_ruletab,
wenzelm@42298
   667
    pretty_tab "parse_translation:" parse_trtab,
wenzelm@42298
   668
    pretty_tab "print_translation:" print_trtab,
wenzelm@42298
   669
    pretty_ruletab "print_rules:" print_ruletab,
wenzelm@42298
   670
    pretty_tab "print_ast_translation:" print_ast_trtab]
clasohm@0
   671
  end;
clasohm@0
   672
wenzelm@15759
   673
in
clasohm@0
   674
wenzelm@15759
   675
fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn));
wenzelm@15759
   676
fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn));
wenzelm@15759
   677
fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn));
clasohm@0
   678
wenzelm@15759
   679
end;
clasohm@0
   680
clasohm@0
   681
wenzelm@26951
   682
(* reconstructing infixes -- educated guessing *)
wenzelm@26951
   683
wenzelm@26951
   684
fun guess_infix (Syntax ({gram, ...}, _)) c =
wenzelm@26951
   685
  (case Parser.guess_infix_lr gram c of
wenzelm@26951
   686
    SOME (s, l, r, j) => SOME
wenzelm@35130
   687
     (if l then Mixfix.Infixl (s, j)
wenzelm@35130
   688
      else if r then Mixfix.Infixr (s, j)
wenzelm@35130
   689
      else Mixfix.Infix (s, j))
wenzelm@26951
   690
  | NONE => NONE);
wenzelm@26951
   691
wenzelm@26951
   692
clasohm@0
   693
wenzelm@1158
   694
(** prepare translation rules **)
wenzelm@1158
   695
wenzelm@42204
   696
(* rules *)
wenzelm@42204
   697
wenzelm@1158
   698
datatype 'a trrule =
wenzelm@42204
   699
  Parse_Rule of 'a * 'a |
wenzelm@42204
   700
  Print_Rule of 'a * 'a |
wenzelm@42204
   701
  Parse_Print_Rule of 'a * 'a;
wenzelm@888
   702
wenzelm@42204
   703
fun map_trrule f (Parse_Rule (x, y)) = Parse_Rule (f x, f y)
wenzelm@42204
   704
  | map_trrule f (Print_Rule (x, y)) = Print_Rule (f x, f y)
wenzelm@42204
   705
  | map_trrule f (Parse_Print_Rule (x, y)) = Parse_Print_Rule (f x, f y);
wenzelm@1158
   706
wenzelm@42204
   707
fun parse_rule (Parse_Rule pats) = SOME pats
wenzelm@42204
   708
  | parse_rule (Print_Rule _) = NONE
wenzelm@42204
   709
  | parse_rule (Parse_Print_Rule pats) = SOME pats;
wenzelm@1158
   710
wenzelm@42204
   711
fun print_rule (Parse_Rule _) = NONE
wenzelm@42204
   712
  | print_rule (Print_Rule pats) = SOME (swap pats)
wenzelm@42204
   713
  | print_rule (Parse_Print_Rule pats) = SOME (swap pats);
wenzelm@1158
   714
wenzelm@1158
   715
wenzelm@42204
   716
(* check_rules *)
wenzelm@42204
   717
wenzelm@19262
   718
local
wenzelm@19262
   719
wenzelm@42048
   720
fun check_rule rule =
wenzelm@5692
   721
  (case Ast.rule_error rule of
skalberg@15531
   722
    SOME msg =>
wenzelm@1158
   723
      error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
wenzelm@42048
   724
        Pretty.string_of (Ast.pretty_rule rule))
skalberg@15531
   725
  | NONE => rule);
wenzelm@888
   726
wenzelm@42204
   727
in
wenzelm@42204
   728
wenzelm@42204
   729
fun check_rules rules =
wenzelm@42204
   730
 (map check_rule (map_filter parse_rule rules),
wenzelm@42204
   731
  map check_rule (map_filter print_rule rules));
wenzelm@42204
   732
wenzelm@42204
   733
end;
wenzelm@42204
   734
wenzelm@42204
   735
wenzelm@18
   736
wenzelm@19262
   737
(** modify syntax **)
wenzelm@383
   738
wenzelm@25394
   739
fun ext_syntax f decls = update_syntax mode_default (f decls);
wenzelm@383
   740
wenzelm@42288
   741
val update_trfuns = ext_syntax Syntax_Ext.syn_ext_trfuns;
wenzelm@42288
   742
val update_advanced_trfuns = ext_syntax Syntax_Ext.syn_ext_advanced_trfuns;
wenzelm@5692
   743
wenzelm@35412
   744
fun update_type_gram add prmode decls =
wenzelm@35412
   745
  (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_types decls);
wenzelm@25387
   746
wenzelm@35412
   747
fun update_const_gram add is_logtype prmode decls =
wenzelm@35412
   748
  (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_consts is_logtype decls);
wenzelm@15755
   749
wenzelm@42288
   750
val update_trrules = ext_syntax Syntax_Ext.syn_ext_rules o check_rules;
wenzelm@42288
   751
val remove_trrules = remove_syntax mode_default o Syntax_Ext.syn_ext_rules o check_rules;
wenzelm@5692
   752
clasohm@0
   753
end;
wenzelm@5692
   754