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