src/Pure/Syntax/syntax_ext.ML
author wenzelm
Wed Dec 12 23:36:07 2012 +0100 (2012-12-12 ago)
changeset 50499 f496b2b7bafb
parent 44470 6c6c31ef6bb2
child 52143 36ffe23b25f8
permissions -rw-r--r--
rendering of selected dialog_result as active_result_color, depending on dynamic command status in output panel, but not static popups etc.;
wenzelm@42288
     1
(*  Title:      Pure/Syntax/syntax_ext.ML
wenzelm@911
     2
    Author:     Markus Wenzel and Carsten Clasohm, TU Muenchen
wenzelm@240
     3
wenzelm@42288
     4
Syntax extension.
wenzelm@240
     5
*)
wenzelm@240
     6
wenzelm@42288
     7
signature SYNTAX_EXT =
wenzelm@4050
     8
sig
wenzelm@6760
     9
  val dddot_indexname: indexname
wenzelm@35351
    10
  datatype mfix = Mfix of string * typ * string * int list * int
wenzelm@35351
    11
  val err_in_mfix: string -> mfix -> 'a
paulson@1510
    12
  val typ_to_nonterm: typ -> string
paulson@1510
    13
  datatype xsymb =
paulson@1510
    14
    Delim of string |
paulson@1510
    15
    Argument of string * int |
paulson@1510
    16
    Space of string |
paulson@1510
    17
    Bg of int | Brk of int | En
paulson@1510
    18
  datatype xprod = XProd of string * xsymb list * string * int
paulson@1510
    19
  val chain_pri: int
wenzelm@4701
    20
  val delims_of: xprod list -> string list list
paulson@1510
    21
  datatype syn_ext =
wenzelm@37216
    22
    Syn_Ext of {
paulson@1510
    23
      xprods: xprod list,
wenzelm@42298
    24
      consts: (string * string) list,
wenzelm@21772
    25
      parse_ast_translation: (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list,
paulson@1510
    26
      parse_rules: (Ast.ast * Ast.ast) list,
wenzelm@21772
    27
      parse_translation: (string * ((Proof.context -> term list -> term) * stamp)) list,
wenzelm@42247
    28
      print_translation: (string * ((Proof.context -> typ -> term list -> term) * stamp)) list,
paulson@1510
    29
      print_rules: (Ast.ast * Ast.ast) list,
wenzelm@42268
    30
      print_ast_translation: (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list}
wenzelm@19004
    31
  val mfix_delims: string -> string list
wenzelm@4054
    32
  val mfix_args: string -> int
wenzelm@42288
    33
  val escape: string -> string
wenzelm@42293
    34
  val syn_ext': (string -> bool) -> mfix list ->
wenzelm@42298
    35
    (string * string) list -> (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list *
wenzelm@21772
    36
    (string * ((Proof.context -> term list -> term) * stamp)) list *
wenzelm@42247
    37
    (string * ((Proof.context -> typ -> term list -> term) * stamp)) list *
wenzelm@42268
    38
    (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list ->
wenzelm@42268
    39
    (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
wenzelm@42298
    40
  val syn_ext: mfix list -> (string * string) list ->
wenzelm@21772
    41
    (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list *
wenzelm@21772
    42
    (string * ((Proof.context -> term list -> term) * stamp)) list *
wenzelm@42247
    43
    (string * ((Proof.context -> typ -> term list -> term) * stamp)) list *
wenzelm@42268
    44
    (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list ->
wenzelm@42268
    45
    (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
wenzelm@14903
    46
  val syn_ext_rules: (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
wenzelm@14903
    47
  val syn_ext_trfuns:
wenzelm@15754
    48
    (string * ((Ast.ast list -> Ast.ast) * stamp)) list *
wenzelm@15754
    49
    (string * ((term list -> term) * stamp)) list *
wenzelm@42247
    50
    (string * ((typ -> term list -> term) * stamp)) list *
wenzelm@15754
    51
    (string * ((Ast.ast list -> Ast.ast) * stamp)) list -> syn_ext
wenzelm@16610
    52
  val syn_ext_advanced_trfuns:
wenzelm@21772
    53
    (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list *
wenzelm@21772
    54
    (string * ((Proof.context -> term list -> term) * stamp)) list *
wenzelm@42247
    55
    (string * ((Proof.context -> typ -> term list -> term) * stamp)) list *
wenzelm@21772
    56
    (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list -> syn_ext
wenzelm@42288
    57
  val stamp_trfun: stamp -> string * 'a -> string * ('a * stamp)
wenzelm@42288
    58
  val mk_trfun: string * 'a -> string * ('a * stamp)
wenzelm@42288
    59
  val eq_trfun: ('a * stamp) * ('a * stamp) -> bool
wenzelm@4050
    60
end;
wenzelm@240
    61
wenzelm@42288
    62
structure Syntax_Ext: SYNTAX_EXT =
wenzelm@240
    63
struct
wenzelm@240
    64
wenzelm@2694
    65
wenzelm@240
    66
(** misc definitions **)
wenzelm@240
    67
wenzelm@7472
    68
val dddot_indexname = ("dddot", 0);
wenzelm@6760
    69
wenzelm@6760
    70
wenzelm@240
    71
(** datatype xprod **)
wenzelm@240
    72
wenzelm@240
    73
(*Delim s: delimiter s
wenzelm@240
    74
  Argument (s, p): nonterminal s requiring priority >= p, or valued token
wenzelm@240
    75
  Space s: some white space for printing
wenzelm@240
    76
  Bg, Brk, En: blocks and breaks for pretty printing*)
wenzelm@240
    77
wenzelm@240
    78
datatype xsymb =
wenzelm@240
    79
  Delim of string |
wenzelm@240
    80
  Argument of string * int |
wenzelm@240
    81
  Space of string |
wenzelm@240
    82
  Bg of int | Brk of int | En;
wenzelm@240
    83
wenzelm@12513
    84
fun is_delim (Delim _) = true
wenzelm@12513
    85
  | is_delim _ = false;
wenzelm@12513
    86
wenzelm@12513
    87
fun is_terminal (Delim _) = true
wenzelm@12513
    88
  | is_terminal (Argument (s, _)) = Lexicon.is_terminal s
wenzelm@12513
    89
  | is_terminal _ = false;
wenzelm@12513
    90
wenzelm@12513
    91
fun is_argument (Argument _) = true
wenzelm@12513
    92
  | is_argument _ = false;
wenzelm@12513
    93
wenzelm@12513
    94
fun is_index (Argument ("index", _)) = true
wenzelm@12513
    95
  | is_index _ = false;
wenzelm@12513
    96
wenzelm@12513
    97
val index = Argument ("index", 1000);
wenzelm@12513
    98
wenzelm@240
    99
wenzelm@240
   100
(*XProd (lhs, syms, c, p):
wenzelm@240
   101
    lhs: name of nonterminal on the lhs of the production
wenzelm@240
   102
    syms: list of symbols on the rhs of the production
wenzelm@240
   103
    c: head of parse tree
wenzelm@240
   104
    p: priority of this production*)
wenzelm@240
   105
wenzelm@240
   106
datatype xprod = XProd of string * xsymb list * string * int;
wenzelm@240
   107
wenzelm@240
   108
val chain_pri = ~1;   (*dummy for chain productions*)
wenzelm@240
   109
wenzelm@240
   110
fun delims_of xprods =
wenzelm@19004
   111
  fold (fn XProd (_, xsymbs, _, _) =>
wenzelm@19004
   112
    fold (fn Delim s => insert (op =) s | _ => I) xsymbs) xprods []
wenzelm@19004
   113
  |> map Symbol.explode;
wenzelm@240
   114
wenzelm@240
   115
wenzelm@240
   116
wenzelm@240
   117
(** datatype mfix **)
wenzelm@240
   118
wenzelm@240
   119
(*Mfix (sy, ty, c, ps, p):
wenzelm@240
   120
    sy: rhs of production as symbolic string
wenzelm@240
   121
    ty: type description of production
wenzelm@240
   122
    c: head of parse tree
wenzelm@240
   123
    ps: priorities of arguments in sy
wenzelm@240
   124
    p: priority of production*)
wenzelm@240
   125
wenzelm@240
   126
datatype mfix = Mfix of string * typ * string * int list * int;
wenzelm@240
   127
wenzelm@12513
   128
fun err_in_mfix msg (Mfix (sy, _, const, _, _)) =
wenzelm@18678
   129
  cat_error msg ("in mixfix annotation " ^ quote sy ^ " for " ^ quote const);
wenzelm@12513
   130
wenzelm@240
   131
wenzelm@240
   132
(* typ_to_nonterm *)
wenzelm@240
   133
clasohm@865
   134
fun typ_to_nt _ (Type (c, _)) = c
clasohm@865
   135
  | typ_to_nt default _ = default;
clasohm@865
   136
clasohm@764
   137
(*get nonterminal for rhs*)
wenzelm@42293
   138
val typ_to_nonterm = typ_to_nt "any";
wenzelm@240
   139
clasohm@764
   140
(*get nonterminal for lhs*)
wenzelm@42293
   141
val typ_to_nonterm1 = typ_to_nt "logic";
wenzelm@240
   142
wenzelm@240
   143
wenzelm@19004
   144
(* read mixfix annotations *)
wenzelm@4050
   145
wenzelm@4050
   146
local
wenzelm@19004
   147
wenzelm@20675
   148
val is_meta = member (op =) ["(", ")", "/", "_", "\\<index>"];
wenzelm@19004
   149
wenzelm@19004
   150
val scan_delim_char =
wenzelm@23784
   151
  $$ "'" |-- Scan.one ((not o Symbol.is_blank) andf Symbol.is_regular) ||
wenzelm@23784
   152
  Scan.one ((not o is_meta) andf (not o Symbol.is_blank) andf Symbol.is_regular);
wenzelm@19004
   153
wenzelm@19004
   154
fun read_int ["0", "0"] = ~1
wenzelm@19004
   155
  | read_int cs = #1 (Library.read_int cs);
wenzelm@4050
   156
wenzelm@19004
   157
val scan_sym =
wenzelm@19004
   158
  $$ "_" >> K (Argument ("", 0)) ||
wenzelm@19004
   159
  $$ "\\<index>" >> K index ||
wenzelm@21858
   160
  $$ "(" |-- Scan.many Symbol.is_digit >> (Bg o read_int) ||
wenzelm@19004
   161
  $$ ")" >> K En ||
wenzelm@19004
   162
  $$ "/" -- $$ "/" >> K (Brk ~1) ||
wenzelm@21858
   163
  $$ "/" |-- Scan.many Symbol.is_blank >> (Brk o length) ||
wenzelm@21858
   164
  Scan.many1 Symbol.is_blank >> (Space o implode) ||
wenzelm@19004
   165
  Scan.repeat1 scan_delim_char >> (Delim o implode);
wenzelm@14819
   166
wenzelm@19004
   167
val scan_symb =
wenzelm@19004
   168
  scan_sym >> SOME ||
wenzelm@19004
   169
  $$ "'" -- Scan.one Symbol.is_blank >> K NONE;
wenzelm@19004
   170
wenzelm@19305
   171
val scan_symbs = Scan.repeat scan_symb --| Scan.ahead (~$$ "'");
wenzelm@19482
   172
val read_symbs = map_filter I o the o Scan.read Symbol.stopper scan_symbs;
wenzelm@4050
   173
wenzelm@19004
   174
fun unique_index xsymbs =
wenzelm@33317
   175
  if length (filter is_index xsymbs) <= 1 then xsymbs
wenzelm@19004
   176
  else error "Duplicate index arguments (\\<index>)";
wenzelm@4050
   177
wenzelm@19004
   178
in
wenzelm@12513
   179
wenzelm@19004
   180
val read_mfix = unique_index o read_symbs o Symbol.explode;
wenzelm@19004
   181
wenzelm@19004
   182
fun mfix_delims sy = fold_rev (fn Delim s => cons s | _ => I) (read_mfix sy) [];
wenzelm@33317
   183
val mfix_args = length o filter is_argument o read_mfix;
wenzelm@19004
   184
wenzelm@35390
   185
val escape = implode o map (fn s => if is_meta s then "'" ^ s else s) o Symbol.explode;
wenzelm@19004
   186
wenzelm@4050
   187
end;
wenzelm@4050
   188
wenzelm@4050
   189
wenzelm@240
   190
(* mfix_to_xprod *)
wenzelm@240
   191
wenzelm@42293
   192
fun mfix_to_xprod is_logtype (mfix as Mfix (sy, typ, const, pris, pri)) =
wenzelm@240
   193
  let
wenzelm@240
   194
    fun check_pri p =
wenzelm@42297
   195
      if p >= 0 andalso p <= 1000 then ()
wenzelm@12513
   196
      else err_in_mfix ("Precedence out of range: " ^ string_of_int p) mfix;
wenzelm@240
   197
wenzelm@240
   198
    fun blocks_ok [] 0 = true
wenzelm@240
   199
      | blocks_ok [] _ = false
wenzelm@240
   200
      | blocks_ok (Bg _ :: syms) n = blocks_ok syms (n + 1)
wenzelm@240
   201
      | blocks_ok (En :: _) 0 = false
wenzelm@240
   202
      | blocks_ok (En :: syms) n = blocks_ok syms (n - 1)
wenzelm@240
   203
      | blocks_ok (_ :: syms) n = blocks_ok syms n;
wenzelm@240
   204
wenzelm@240
   205
    fun check_blocks syms =
wenzelm@240
   206
      if blocks_ok syms 0 then ()
wenzelm@12513
   207
      else err_in_mfix "Unbalanced block parentheses" mfix;
wenzelm@240
   208
wenzelm@240
   209
wenzelm@240
   210
    val cons_fst = apfst o cons;
wenzelm@240
   211
wenzelm@240
   212
    fun add_args [] ty [] = ([], typ_to_nonterm1 ty)
wenzelm@12513
   213
      | add_args [] _ _ = err_in_mfix "Too many precedences" mfix
wenzelm@12513
   214
      | add_args ((arg as Argument ("index", _)) :: syms) ty ps =
wenzelm@12513
   215
          cons_fst arg (add_args syms ty ps)
wenzelm@240
   216
      | add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) [] =
wenzelm@240
   217
          cons_fst (Argument (typ_to_nonterm ty, 0)) (add_args syms tys [])
wenzelm@240
   218
      | add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) (p :: ps) =
wenzelm@240
   219
          cons_fst (Argument (typ_to_nonterm ty, p)) (add_args syms tys ps)
wenzelm@240
   220
      | add_args (Argument _ :: _) _ _ =
wenzelm@12513
   221
          err_in_mfix "More arguments than in corresponding type" mfix
wenzelm@240
   222
      | add_args (sym :: syms) ty ps = cons_fst sym (add_args syms ty ps);
wenzelm@240
   223
wenzelm@240
   224
    fun rem_pri (Argument (s, _)) = Argument (s, chain_pri)
wenzelm@240
   225
      | rem_pri sym = sym;
wenzelm@240
   226
wenzelm@32785
   227
    fun logify_types (a as (Argument (s, p))) =
wenzelm@42293
   228
          if s <> "prop" andalso is_logtype s then Argument ("logic", p) else a
wenzelm@32785
   229
      | logify_types a = a;
wenzelm@240
   230
wenzelm@2364
   231
wenzelm@19004
   232
    val raw_symbs = read_mfix sy handle ERROR msg => err_in_mfix msg mfix;
wenzelm@33317
   233
    val args = filter (fn Argument _ => true | _ => false) raw_symbs;
wenzelm@44470
   234
    val (const', typ', syntax_consts, parse_rules) =
wenzelm@44470
   235
      if not (exists is_index args) then (const, typ, NONE, NONE)
wenzelm@12513
   236
      else
wenzelm@12513
   237
        let
wenzelm@35429
   238
          val indexed_const =
wenzelm@35429
   239
            if const <> "" then const ^ "_indexed"
wenzelm@12513
   240
            else err_in_mfix "Missing constant name for indexed syntax" mfix;
wenzelm@14697
   241
          val rangeT = Term.range_type typ handle Match =>
wenzelm@12513
   242
            err_in_mfix "Missing structure argument for indexed syntax" mfix;
wenzelm@12513
   243
wenzelm@43329
   244
          val xs = map Ast.Variable (Name.invent Name.context "xa" (length args - 1));
wenzelm@19012
   245
          val (xs1, xs2) = chop (find_index is_index args) xs;
wenzelm@12513
   246
          val i = Ast.Variable "i";
wenzelm@14697
   247
          val lhs = Ast.mk_appl (Ast.Constant indexed_const)
wenzelm@14697
   248
            (xs1 @ [Ast.mk_appl (Ast.Constant "_index") [i]] @ xs2);
wenzelm@14697
   249
          val rhs = Ast.mk_appl (Ast.Constant const) (i :: xs);
wenzelm@44470
   250
        in (indexed_const, rangeT, SOME (indexed_const, const), SOME (lhs, rhs)) end;
wenzelm@12513
   251
wenzelm@12513
   252
    val (symbs, lhs) = add_args raw_symbs typ' pris;
wenzelm@12513
   253
wenzelm@2364
   254
    val copy_prod =
wenzelm@20675
   255
      (lhs = "prop" orelse lhs = "logic")
wenzelm@2364
   256
        andalso const <> ""
wenzelm@2364
   257
        andalso not (null symbs)
wenzelm@2364
   258
        andalso not (exists is_delim symbs);
wenzelm@2364
   259
    val lhs' =
wenzelm@42293
   260
      if copy_prod orelse lhs = "prop" andalso symbs = [Argument ("prop'", 0)] then lhs
wenzelm@42293
   261
      else if lhs = "prop" then "prop'"
wenzelm@42293
   262
      else if is_logtype lhs then "logic"
wenzelm@2364
   263
      else lhs;
wenzelm@32785
   264
    val symbs' = map logify_types symbs;
wenzelm@12513
   265
    val xprod = XProd (lhs', symbs', const', pri);
wenzelm@240
   266
skalberg@15570
   267
    val _ = (List.app check_pri pris; check_pri pri; check_blocks symbs');
wenzelm@12513
   268
    val xprod' =
wenzelm@12513
   269
      if Lexicon.is_terminal lhs' then err_in_mfix ("Illegal lhs: " ^ lhs') mfix
wenzelm@12513
   270
      else if const <> "" then xprod
wenzelm@33317
   271
      else if length (filter is_argument symbs') <> 1 then
wenzelm@12513
   272
        err_in_mfix "Copy production must have exactly one argument" mfix
wenzelm@12513
   273
      else if exists is_terminal symbs' then xprod
wenzelm@12513
   274
      else XProd (lhs', map rem_pri symbs', "", chain_pri);
wenzelm@12513
   275
wenzelm@44470
   276
  in (xprod', syntax_consts, parse_rules) end;
wenzelm@12513
   277
wenzelm@240
   278
wenzelm@240
   279
wenzelm@240
   280
(** datatype syn_ext **)
wenzelm@240
   281
wenzelm@240
   282
datatype syn_ext =
wenzelm@37216
   283
  Syn_Ext of {
wenzelm@240
   284
    xprods: xprod list,
wenzelm@42298
   285
    consts: (string * string) list,
wenzelm@21772
   286
    parse_ast_translation: (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list,
paulson@1510
   287
    parse_rules: (Ast.ast * Ast.ast) list,
wenzelm@21772
   288
    parse_translation: (string * ((Proof.context -> term list -> term) * stamp)) list,
wenzelm@42247
   289
    print_translation: (string * ((Proof.context -> typ -> term list -> term) * stamp)) list,
paulson@1510
   290
    print_rules: (Ast.ast * Ast.ast) list,
wenzelm@42268
   291
    print_ast_translation: (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list};
wenzelm@240
   292
wenzelm@240
   293
wenzelm@240
   294
(* syn_ext *)
wenzelm@240
   295
wenzelm@42293
   296
fun syn_ext' is_logtype mfixes consts trfuns (parse_rules, print_rules) =
wenzelm@240
   297
  let
wenzelm@240
   298
    val (parse_ast_translation, parse_translation, print_translation,
wenzelm@240
   299
      print_ast_translation) = trfuns;
wenzelm@240
   300
wenzelm@44470
   301
    val xprod_results = map (mfix_to_xprod is_logtype) mfixes;
wenzelm@44470
   302
    val xprods = map #1 xprod_results;
wenzelm@44470
   303
    val consts' = map_filter #2 xprod_results;
wenzelm@44470
   304
    val parse_rules' = rev (map_filter #3 xprod_results);
wenzelm@42298
   305
    val mfix_consts = map (fn Mfix x => (#3 x, "")) mfixes @ map (fn XProd x => (#3 x, "")) xprods;
wenzelm@240
   306
  in
wenzelm@37216
   307
    Syn_Ext {
clasohm@624
   308
      xprods = xprods,
wenzelm@44470
   309
      consts = mfix_consts @ consts' @ consts,
wenzelm@240
   310
      parse_ast_translation = parse_ast_translation,
wenzelm@12513
   311
      parse_rules = parse_rules' @ parse_rules,
wenzelm@240
   312
      parse_translation = parse_translation,
wenzelm@240
   313
      print_translation = print_translation,
wenzelm@12513
   314
      print_rules = map swap parse_rules' @ print_rules,
wenzelm@42268
   315
      print_ast_translation = print_ast_translation}
wenzelm@240
   316
  end;
wenzelm@240
   317
wenzelm@2382
   318
wenzelm@42293
   319
val syn_ext = syn_ext' (K false);
wenzelm@555
   320
wenzelm@42268
   321
fun syn_ext_rules rules = syn_ext [] [] ([], [], [], []) rules;
wenzelm@42268
   322
fun syn_ext_advanced_trfuns trfuns = syn_ext [] [] trfuns ([], []);
wenzelm@2382
   323
wenzelm@16610
   324
fun syn_ext_trfuns (atrs, trs, tr's, atr's) =
wenzelm@16610
   325
  let fun simple (name, (f, s)) = (name, (K f, s)) in
wenzelm@16610
   326
    syn_ext_advanced_trfuns (map simple atrs, map simple trs, map simple tr's, map simple atr's)
wenzelm@16610
   327
  end;
wenzelm@16610
   328
wenzelm@15754
   329
fun stamp_trfun s (c, f) = (c, (f, s));
wenzelm@15754
   330
fun mk_trfun tr = stamp_trfun (stamp ()) tr;
wenzelm@29318
   331
fun eq_trfun ((_, s1: stamp), (_, s2)) = s1 = s2;
wenzelm@15754
   332
wenzelm@240
   333
end;