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