src/Pure/Syntax/syn_ext.ML
author wenzelm
Sat, 01 Sep 2001 00:20:44 +0200
changeset 11546 2b3f02227c35
parent 9380 63cca60b2cce
child 12513 0ffb824dc95c
permissions -rw-r--r--
tuned;
wenzelm@240
     1
(*  Title:      Pure/Syntax/syn_ext.ML
wenzelm@240
     2
    ID:         $Id$
wenzelm@911
     3
    Author:     Markus Wenzel and Carsten Clasohm, TU Muenchen
wenzelm@240
     4
wenzelm@240
     5
Syntax extension (internal interface).
wenzelm@240
     6
*)
wenzelm@240
     7
wenzelm@240
     8
signature SYN_EXT0 =
wenzelm@4050
     9
sig
wenzelm@6760
    10
  val dddot_indexname: indexname
wenzelm@6760
    11
  val constrainC: string
wenzelm@240
    12
  val typeT: typ
wenzelm@5690
    13
  val max_pri: int
wenzelm@4050
    14
end;
wenzelm@240
    15
wenzelm@240
    16
signature SYN_EXT =
wenzelm@4050
    17
sig
wenzelm@240
    18
  include SYN_EXT0
paulson@1510
    19
  val logic: string
paulson@1510
    20
  val args: string
paulson@1510
    21
  val cargs: string
paulson@1510
    22
  val any: string
paulson@1510
    23
  val sprop: string
paulson@1510
    24
  val typ_to_nonterm: typ -> string
paulson@1510
    25
  datatype xsymb =
paulson@1510
    26
    Delim of string |
paulson@1510
    27
    Argument of string * int |
paulson@1510
    28
    Space of string |
paulson@1510
    29
    Bg of int | Brk of int | En
paulson@1510
    30
  datatype xprod = XProd of string * xsymb list * string * int
paulson@1510
    31
  val chain_pri: int
wenzelm@4701
    32
  val delims_of: xprod list -> string list list
paulson@1510
    33
  datatype mfix = Mfix of string * typ * string * int list * int
paulson@1510
    34
  datatype syn_ext =
paulson@1510
    35
    SynExt of {
paulson@1510
    36
      logtypes: string list,
paulson@1510
    37
      xprods: xprod list,
paulson@1510
    38
      consts: string list,
wenzelm@2913
    39
      prmodes: string list,
paulson@1510
    40
      parse_ast_translation: (string * (Ast.ast list -> Ast.ast)) list,
paulson@1510
    41
      parse_rules: (Ast.ast * Ast.ast) list,
paulson@1510
    42
      parse_translation: (string * (term list -> term)) list,
wenzelm@4146
    43
      print_translation: (string * (bool -> typ -> term list -> term)) list,
paulson@1510
    44
      print_rules: (Ast.ast * Ast.ast) list,
wenzelm@2694
    45
      print_ast_translation: (string * (Ast.ast list -> Ast.ast)) list,
wenzelm@6322
    46
      token_translation: (string * string * (string -> string * real)) list}
wenzelm@4054
    47
  val mfix_args: string -> int
paulson@1510
    48
  val mk_syn_ext: bool -> string list -> mfix list ->
paulson@1510
    49
    string list -> (string * (Ast.ast list -> Ast.ast)) list *
paulson@1510
    50
    (string * (term list -> term)) list *
wenzelm@4146
    51
    (string * (bool -> typ -> term list -> term)) list * (string * (Ast.ast list -> Ast.ast)) list
wenzelm@6322
    52
    -> (string * string * (string -> string * real)) list
paulson@1510
    53
    -> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
paulson@1510
    54
  val syn_ext: string list -> mfix list -> string list ->
paulson@1510
    55
    (string * (Ast.ast list -> Ast.ast)) list * (string * (term list -> term)) list *
wenzelm@4146
    56
    (string * (bool -> typ -> term list -> term)) list * (string * (Ast.ast list -> Ast.ast)) list
wenzelm@6322
    57
    -> (string * string * (string -> string * real)) list
paulson@1510
    58
    -> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
paulson@1510
    59
  val syn_ext_logtypes: string list -> syn_ext
paulson@1510
    60
  val syn_ext_const_names: string list -> string list -> syn_ext
paulson@1510
    61
  val syn_ext_rules: string list -> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
wenzelm@4146
    62
  val fix_tr': (term list -> term) -> bool -> typ -> term list -> term
paulson@1510
    63
  val syn_ext_trfuns: string list ->
paulson@1510
    64
    (string * (Ast.ast list -> Ast.ast)) list * (string * (term list -> term)) list *
paulson@1510
    65
    (string * (term list -> term)) list * (string * (Ast.ast list -> Ast.ast)) list
paulson@1510
    66
    -> syn_ext
wenzelm@4146
    67
  val syn_ext_trfunsT: string list ->
wenzelm@4146
    68
    (string * (bool -> typ -> term list -> term)) list -> syn_ext
wenzelm@2694
    69
  val syn_ext_tokentrfuns: string list
wenzelm@6322
    70
    -> (string * string * (string -> string * real)) list -> syn_ext
paulson@1510
    71
  val pure_ext: syn_ext
wenzelm@4050
    72
end;
wenzelm@240
    73
paulson@1510
    74
structure SynExt : SYN_EXT =
wenzelm@240
    75
struct
wenzelm@240
    76
wenzelm@2694
    77
wenzelm@240
    78
(** misc definitions **)
wenzelm@240
    79
wenzelm@7472
    80
val dddot_indexname = ("dddot", 0);
wenzelm@6760
    81
val constrainC = "_constrain";
wenzelm@6760
    82
wenzelm@6760
    83
wenzelm@240
    84
(* syntactic categories *)
wenzelm@240
    85
wenzelm@240
    86
val logic = "logic";
wenzelm@240
    87
val logicT = Type (logic, []);
wenzelm@240
    88
wenzelm@240
    89
val args = "args";
clasohm@1178
    90
val cargs = "cargs";
wenzelm@240
    91
clasohm@330
    92
val typeT = Type ("type", []);
clasohm@330
    93
clasohm@764
    94
val sprop = "#prop";
clasohm@764
    95
val spropT = Type (sprop, []);
wenzelm@240
    96
clasohm@764
    97
val any = "any";
clasohm@624
    98
val anyT = Type (any, []);
clasohm@624
    99
wenzelm@780
   100
wenzelm@240
   101
wenzelm@240
   102
(** datatype xprod **)
wenzelm@240
   103
wenzelm@240
   104
(*Delim s: delimiter s
wenzelm@240
   105
  Argument (s, p): nonterminal s requiring priority >= p, or valued token
wenzelm@240
   106
  Space s: some white space for printing
wenzelm@240
   107
  Bg, Brk, En: blocks and breaks for pretty printing*)
wenzelm@240
   108
wenzelm@240
   109
datatype xsymb =
wenzelm@240
   110
  Delim of string |
wenzelm@240
   111
  Argument of string * int |
wenzelm@240
   112
  Space of string |
wenzelm@240
   113
  Bg of int | Brk of int | En;
wenzelm@240
   114
wenzelm@240
   115
wenzelm@240
   116
(*XProd (lhs, syms, c, p):
wenzelm@240
   117
    lhs: name of nonterminal on the lhs of the production
wenzelm@240
   118
    syms: list of symbols on the rhs of the production
wenzelm@240
   119
    c: head of parse tree
wenzelm@240
   120
    p: priority of this production*)
wenzelm@240
   121
wenzelm@240
   122
datatype xprod = XProd of string * xsymb list * string * int;
wenzelm@240
   123
wenzelm@240
   124
val max_pri = 1000;   (*maximum legal priority*)
wenzelm@240
   125
val chain_pri = ~1;   (*dummy for chain productions*)
wenzelm@240
   126
wenzelm@240
   127
wenzelm@240
   128
(* delims_of *)
wenzelm@240
   129
wenzelm@240
   130
fun delims_of xprods =
wenzelm@240
   131
  let
wenzelm@240
   132
    fun del_of (Delim s) = Some s
wenzelm@240
   133
      | del_of _ = None;
wenzelm@240
   134
wenzelm@240
   135
    fun dels_of (XProd (_, xsymbs, _, _)) =
wenzelm@240
   136
      mapfilter del_of xsymbs;
wenzelm@240
   137
  in
wenzelm@4701
   138
    map Symbol.explode (distinct (flat (map dels_of xprods)))
wenzelm@240
   139
  end;
wenzelm@240
   140
wenzelm@240
   141
wenzelm@240
   142
wenzelm@240
   143
(** datatype mfix **)
wenzelm@240
   144
wenzelm@240
   145
(*Mfix (sy, ty, c, ps, p):
wenzelm@240
   146
    sy: rhs of production as symbolic string
wenzelm@240
   147
    ty: type description of production
wenzelm@240
   148
    c: head of parse tree
wenzelm@240
   149
    ps: priorities of arguments in sy
wenzelm@240
   150
    p: priority of production*)
wenzelm@240
   151
wenzelm@240
   152
datatype mfix = Mfix of string * typ * string * int list * int;
wenzelm@240
   153
wenzelm@240
   154
wenzelm@240
   155
(* typ_to_nonterm *)
wenzelm@240
   156
clasohm@865
   157
fun typ_to_nt _ (Type (c, _)) = c
clasohm@865
   158
  | typ_to_nt default _ = default;
clasohm@865
   159
clasohm@764
   160
(*get nonterminal for rhs*)
clasohm@865
   161
val typ_to_nonterm = typ_to_nt any;
wenzelm@240
   162
clasohm@764
   163
(*get nonterminal for lhs*)
clasohm@865
   164
val typ_to_nonterm1 = typ_to_nt logic;
wenzelm@240
   165
wenzelm@240
   166
wenzelm@4701
   167
(* read_mixfix, mfix_args *)
wenzelm@4050
   168
wenzelm@4050
   169
local
wenzelm@4050
   170
  fun is_meta c = c mem ["(", ")", "/", "_"];
wenzelm@4050
   171
wenzelm@4701
   172
  val scan_delim_char =
wenzelm@4701
   173
    $$ "'" |-- Scan.one ((not o Symbol.is_blank) andf Symbol.not_eof) ||
wenzelm@4701
   174
    Scan.one ((not o is_meta) andf (not o Symbol.is_blank) andf Symbol.not_eof);
wenzelm@4050
   175
wenzelm@4050
   176
  val scan_sym =
wenzelm@4050
   177
    $$ "_" >> K (Argument ("", 0)) ||
wenzelm@4701
   178
    $$ "(" |-- Scan.any Symbol.is_digit >> (Bg o #1 o Term.read_int) ||
wenzelm@4050
   179
    $$ ")" >> K En ||
wenzelm@4050
   180
    $$ "/" -- $$ "/" >> K (Brk ~1) ||
wenzelm@4701
   181
    $$ "/" |-- Scan.any Symbol.is_blank >> (Brk o length) ||
wenzelm@4701
   182
    Scan.any1 Symbol.is_blank >> (Space o implode) ||
wenzelm@4701
   183
    Scan.repeat1 scan_delim_char >> (Delim o implode);
wenzelm@4050
   184
wenzelm@4050
   185
  val scan_symb =
wenzelm@4050
   186
    scan_sym >> Some ||
wenzelm@4701
   187
    $$ "'" -- Scan.one Symbol.is_blank >> K None;
wenzelm@4050
   188
wenzelm@4701
   189
  val scan_symbs = Scan.repeat scan_symb --| Scan.ahead (Scan.one (not_equal "'"));
wenzelm@5870
   190
  val read_symbs = mapfilter I o the o Scan.read Symbol.stopper scan_symbs;
wenzelm@4050
   191
in
wenzelm@4701
   192
  val read_mixfix = read_symbs o Symbol.explode;
wenzelm@4050
   193
end;
wenzelm@4050
   194
wenzelm@4054
   195
fun mfix_args sy =
wenzelm@4701
   196
  foldl (fn (i, Argument _) => i + 1 | (i, _) => i) (0, read_mixfix sy);
wenzelm@4050
   197
wenzelm@4050
   198
wenzelm@240
   199
(* mfix_to_xprod *)
wenzelm@240
   200
clasohm@764
   201
fun mfix_to_xprod convert logtypes (Mfix (sy, typ, const, pris, pri)) =
wenzelm@240
   202
  let
wenzelm@240
   203
    fun err msg =
wenzelm@4050
   204
      (if msg = "" then () else error_msg msg;
wenzelm@4050
   205
        error ("in mixfix annotation " ^ quote sy ^ " for " ^ quote const));
wenzelm@240
   206
wenzelm@240
   207
    fun check_pri p =
wenzelm@240
   208
      if p >= 0 andalso p <= max_pri then ()
wenzelm@4050
   209
      else err ("Precedence out of range: " ^ string_of_int p);
wenzelm@240
   210
wenzelm@240
   211
    fun blocks_ok [] 0 = true
wenzelm@240
   212
      | blocks_ok [] _ = false
wenzelm@240
   213
      | blocks_ok (Bg _ :: syms) n = blocks_ok syms (n + 1)
wenzelm@240
   214
      | blocks_ok (En :: _) 0 = false
wenzelm@240
   215
      | blocks_ok (En :: syms) n = blocks_ok syms (n - 1)
wenzelm@240
   216
      | blocks_ok (_ :: syms) n = blocks_ok syms n;
wenzelm@240
   217
wenzelm@240
   218
    fun check_blocks syms =
wenzelm@240
   219
      if blocks_ok syms 0 then ()
wenzelm@4050
   220
      else err "Unbalanced block parentheses";
wenzelm@240
   221
wenzelm@240
   222
wenzelm@240
   223
    val cons_fst = apfst o cons;
wenzelm@240
   224
wenzelm@240
   225
    fun add_args [] ty [] = ([], typ_to_nonterm1 ty)
wenzelm@4050
   226
      | add_args [] _ _ = err "Too many precedences"
wenzelm@240
   227
      | add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) [] =
wenzelm@240
   228
          cons_fst (Argument (typ_to_nonterm ty, 0)) (add_args syms tys [])
wenzelm@240
   229
      | add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) (p :: ps) =
wenzelm@240
   230
          cons_fst (Argument (typ_to_nonterm ty, p)) (add_args syms tys ps)
wenzelm@240
   231
      | add_args (Argument _ :: _) _ _ =
wenzelm@4050
   232
          err "More arguments than in corresponding type"
wenzelm@240
   233
      | add_args (sym :: syms) ty ps = cons_fst sym (add_args syms ty ps);
wenzelm@240
   234
wenzelm@240
   235
wenzelm@240
   236
    fun is_arg (Argument _) = true
wenzelm@240
   237
      | is_arg _ = false;
wenzelm@240
   238
wenzelm@240
   239
    fun is_term (Delim _) = true
wenzelm@5690
   240
      | is_term (Argument (s, _)) = Lexicon.is_terminal s
wenzelm@240
   241
      | is_term _ = false;
wenzelm@240
   242
wenzelm@240
   243
    fun rem_pri (Argument (s, _)) = Argument (s, chain_pri)
wenzelm@240
   244
      | rem_pri sym = sym;
wenzelm@240
   245
clasohm@764
   246
    fun is_delim (Delim _) = true
clasohm@764
   247
      | is_delim _ = false;
clasohm@764
   248
wenzelm@4054
   249
    fun logify_types copy_prod (a as (Argument (s, p))) =
wenzelm@4054
   250
          if s mem logtypes then Argument (logic, p) else a
wenzelm@4054
   251
      | logify_types _ a = a;
wenzelm@240
   252
wenzelm@2364
   253
wenzelm@4701
   254
    val raw_symbs = read_mixfix sy handle ERROR => err "";
wenzelm@240
   255
    val (symbs, lhs) = add_args raw_symbs typ pris;
wenzelm@2364
   256
    val copy_prod =
wenzelm@2364
   257
      lhs mem ["prop", "logic"]
wenzelm@2364
   258
        andalso const <> ""
wenzelm@2364
   259
        andalso not (null symbs)
wenzelm@2364
   260
        andalso not (exists is_delim symbs);
wenzelm@2364
   261
    val lhs' =
wenzelm@2364
   262
      if convert andalso not copy_prod then
wenzelm@2364
   263
       (if lhs mem logtypes then logic
wenzelm@2364
   264
        else if lhs = "prop" then sprop else lhs)
wenzelm@2364
   265
      else lhs;
wenzelm@4054
   266
    val symbs' = map (logify_types copy_prod) symbs;
clasohm@764
   267
    val xprod = XProd (lhs', symbs', const, pri);
wenzelm@240
   268
  in
wenzelm@240
   269
    seq check_pri pris;
wenzelm@240
   270
    check_pri pri;
clasohm@764
   271
    check_blocks symbs';
wenzelm@240
   272
wenzelm@5690
   273
    if Lexicon.is_terminal lhs' then err ("Illegal lhs: " ^ lhs')
wenzelm@240
   274
    else if const <> "" then xprod
clasohm@764
   275
    else if length (filter is_arg symbs') <> 1 then
wenzelm@4050
   276
      err "Copy production must have exactly one argument"
clasohm@764
   277
    else if exists is_term symbs' then xprod
clasohm@764
   278
    else XProd (lhs', map rem_pri symbs', "", chain_pri)
wenzelm@240
   279
  end;
wenzelm@240
   280
wenzelm@240
   281
wenzelm@240
   282
(** datatype syn_ext **)
wenzelm@240
   283
wenzelm@240
   284
datatype syn_ext =
wenzelm@240
   285
  SynExt of {
clasohm@764
   286
    logtypes: string list,
wenzelm@240
   287
    xprods: xprod list,
wenzelm@240
   288
    consts: string list,
wenzelm@2913
   289
    prmodes: string list,
paulson@1510
   290
    parse_ast_translation: (string * (Ast.ast list -> Ast.ast)) list,
paulson@1510
   291
    parse_rules: (Ast.ast * Ast.ast) list,
wenzelm@240
   292
    parse_translation: (string * (term list -> term)) list,
wenzelm@4146
   293
    print_translation: (string * (bool -> typ -> term list -> term)) list,
paulson@1510
   294
    print_rules: (Ast.ast * Ast.ast) list,
wenzelm@2694
   295
    print_ast_translation: (string * (Ast.ast list -> Ast.ast)) list,
wenzelm@6322
   296
    token_translation: (string * string * (string -> string * real)) list}
wenzelm@240
   297
wenzelm@240
   298
wenzelm@240
   299
(* syn_ext *)
wenzelm@240
   300
wenzelm@2694
   301
fun mk_syn_ext convert logtypes mfixes consts trfuns tokentrfuns rules =
wenzelm@240
   302
  let
wenzelm@240
   303
    val (parse_ast_translation, parse_translation, print_translation,
wenzelm@240
   304
      print_ast_translation) = trfuns;
wenzelm@240
   305
    val (parse_rules, print_rules) = rules;
clasohm@764
   306
    val logtypes' = logtypes \ "prop";
wenzelm@240
   307
clasohm@624
   308
    val mfix_consts = distinct (map (fn (Mfix (_, _, c, _, _)) => c) mfixes);
clasohm@764
   309
    val xprods = map (mfix_to_xprod convert logtypes') mfixes;
wenzelm@240
   310
  in
wenzelm@240
   311
    SynExt {
clasohm@764
   312
      logtypes = logtypes',
clasohm@624
   313
      xprods = xprods,
wenzelm@11546
   314
      consts = consts union_string mfix_consts,
wenzelm@2913
   315
      prmodes = distinct (map (fn (m, _, _) => m) tokentrfuns),
wenzelm@240
   316
      parse_ast_translation = parse_ast_translation,
wenzelm@240
   317
      parse_rules = parse_rules,
wenzelm@240
   318
      parse_translation = parse_translation,
wenzelm@240
   319
      print_translation = print_translation,
wenzelm@240
   320
      print_rules = print_rules,
wenzelm@2694
   321
      print_ast_translation = print_ast_translation,
wenzelm@2694
   322
      token_translation = tokentrfuns}
wenzelm@240
   323
  end;
wenzelm@240
   324
wenzelm@2382
   325
clasohm@764
   326
val syn_ext = mk_syn_ext true;
wenzelm@240
   327
clasohm@764
   328
fun syn_ext_logtypes logtypes =
wenzelm@2694
   329
  syn_ext logtypes [] [] ([], [], [], []) [] ([], []);
wenzelm@555
   330
clasohm@764
   331
fun syn_ext_const_names logtypes cs =
wenzelm@2694
   332
  syn_ext logtypes [] cs ([], [], [], []) [] ([], []);
wenzelm@240
   333
clasohm@764
   334
fun syn_ext_rules logtypes rules =
wenzelm@2694
   335
  syn_ext logtypes [] [] ([], [], [], []) [] rules;
wenzelm@240
   336
wenzelm@4146
   337
fun fix_tr' f _ _ ts = f ts;
wenzelm@2382
   338
wenzelm@2382
   339
fun syn_ext_trfuns logtypes (atrs, trs, tr's, atr's) =
wenzelm@2694
   340
  syn_ext logtypes [] [] (atrs, trs, map (apsnd fix_tr') tr's, atr's) [] ([], []);
wenzelm@2382
   341
wenzelm@2382
   342
fun syn_ext_trfunsT logtypes tr's =
wenzelm@2694
   343
  syn_ext logtypes [] [] ([], [], tr's, []) [] ([], []);
wenzelm@2694
   344
wenzelm@2694
   345
fun syn_ext_tokentrfuns logtypes tokentrfuns =
wenzelm@2694
   346
  syn_ext logtypes [] [] ([], [], [], []) tokentrfuns ([], []);
wenzelm@2382
   347
wenzelm@240
   348
clasohm@764
   349
(* pure_ext *)
clasohm@764
   350
clasohm@764
   351
val pure_ext = mk_syn_ext false []
clasohm@764
   352
  [Mfix ("_", spropT --> propT, "", [0], 0),
clasohm@764
   353
   Mfix ("_", logicT --> anyT, "", [0], 0),
clasohm@764
   354
   Mfix ("_", spropT --> anyT, "", [0], 0),
clasohm@764
   355
   Mfix ("'(_')", logicT --> logicT, "", [0], max_pri),
clasohm@764
   356
   Mfix ("'(_')", spropT --> spropT, "", [0], max_pri),
clasohm@764
   357
   Mfix ("_::_",  [logicT, typeT] ---> logicT, "_constrain", [4, 0], 3),
clasohm@764
   358
   Mfix ("_::_",  [spropT, typeT] ---> spropT, "_constrain", [4, 0], 3)]
clasohm@764
   359
  []
clasohm@764
   360
  ([], [], [], [])
wenzelm@2694
   361
  []
clasohm@764
   362
  ([], []);
wenzelm@240
   363
wenzelm@240
   364
end;