src/Pure/Syntax/syn_ext.ML
author wenzelm
Thu, 30 Oct 1997 17:05:20 +0100
changeset 4050 543df9687d7b
parent 2913 ce271fa4d8e2
child 4054 b33e02b3478e
permissions -rw-r--r--
added mixfix_args;
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
  val mixfix_args: string -> int
wenzelm@4050
    13
end;
wenzelm@240
    14
wenzelm@240
    15
signature SYN_EXT =
wenzelm@4050
    16
sig
wenzelm@240
    17
  include SYN_EXT0
paulson@1510
    18
  val logic: string
paulson@1510
    19
  val args: string
paulson@1510
    20
  val cargs: string
paulson@1510
    21
  val any: string
paulson@1510
    22
  val sprop: string
paulson@1510
    23
  val typ_to_nonterm: typ -> string
paulson@1510
    24
  datatype xsymb =
paulson@1510
    25
    Delim of string |
paulson@1510
    26
    Argument of string * int |
paulson@1510
    27
    Space of string |
paulson@1510
    28
    Bg of int | Brk of int | En
paulson@1510
    29
  datatype xprod = XProd of string * xsymb list * string * int
paulson@1510
    30
  val max_pri: int
paulson@1510
    31
  val chain_pri: int
paulson@1510
    32
  val delims_of: xprod list -> string 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@2382
    43
      print_translation: (string * (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@2694
    46
      token_translation: (string * string * (string -> string * int)) list}
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", []);
clasohm@330
    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@4050
   198
fun mixfix_args mx =
wenzelm@4050
   199
  foldl (fn (i, Argument _) => i + 1 | (i, _) => i) (0, scan_mixfix mx);
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
clasohm@764
   252
    (*replace logical types on rhs by "logic"*)
clasohm@764
   253
    fun unify_logtypes copy_prod (a as (Argument (s, p))) =
clasohm@764
   254
          if s mem logtypes then Argument (logic, p)
clasohm@764
   255
          else a
clasohm@764
   256
      | unify_logtypes _ a = a;
wenzelm@240
   257
wenzelm@2364
   258
wenzelm@4050
   259
    val raw_symbs = scan_mixfix sy handle ERROR => err "";
wenzelm@240
   260
    val (symbs, lhs) = add_args raw_symbs typ pris;
wenzelm@2364
   261
    val copy_prod =
wenzelm@2364
   262
      lhs mem ["prop", "logic"]
wenzelm@2364
   263
        andalso const <> ""
wenzelm@2364
   264
        andalso not (null symbs)
wenzelm@2364
   265
        andalso not (exists is_delim symbs);
wenzelm@2364
   266
    val lhs' =
wenzelm@2364
   267
      if convert andalso not copy_prod then
wenzelm@2364
   268
       (if lhs mem logtypes then logic
wenzelm@2364
   269
        else if lhs = "prop" then sprop else lhs)
wenzelm@2364
   270
      else lhs;
clasohm@764
   271
    val symbs' = map (unify_logtypes copy_prod) symbs;
clasohm@764
   272
    val xprod = XProd (lhs', symbs', const, pri);
wenzelm@240
   273
  in
wenzelm@240
   274
    seq check_pri pris;
wenzelm@240
   275
    check_pri pri;
clasohm@764
   276
    check_blocks symbs';
wenzelm@240
   277
wenzelm@4050
   278
    if is_terminal lhs' then err ("Illegal lhs: " ^ lhs')
wenzelm@240
   279
    else if const <> "" then xprod
clasohm@764
   280
    else if length (filter is_arg symbs') <> 1 then
wenzelm@4050
   281
      err "Copy production must have exactly one argument"
clasohm@764
   282
    else if exists is_term symbs' then xprod
clasohm@764
   283
    else XProd (lhs', map rem_pri symbs', "", chain_pri)
wenzelm@240
   284
  end;
wenzelm@240
   285
wenzelm@240
   286
wenzelm@240
   287
(** datatype syn_ext **)
wenzelm@240
   288
wenzelm@240
   289
datatype syn_ext =
wenzelm@240
   290
  SynExt of {
clasohm@764
   291
    logtypes: string list,
wenzelm@240
   292
    xprods: xprod list,
wenzelm@240
   293
    consts: string list,
wenzelm@2913
   294
    prmodes: string list,
paulson@1510
   295
    parse_ast_translation: (string * (Ast.ast list -> Ast.ast)) list,
paulson@1510
   296
    parse_rules: (Ast.ast * Ast.ast) list,
wenzelm@240
   297
    parse_translation: (string * (term list -> term)) list,
wenzelm@2382
   298
    print_translation: (string * (typ -> term list -> term)) list,
paulson@1510
   299
    print_rules: (Ast.ast * Ast.ast) list,
wenzelm@2694
   300
    print_ast_translation: (string * (Ast.ast list -> Ast.ast)) list,
wenzelm@2913
   301
    token_translation: (string * string * (string -> string * int)) list}
wenzelm@240
   302
wenzelm@240
   303
wenzelm@240
   304
(* syn_ext *)
wenzelm@240
   305
wenzelm@2694
   306
fun mk_syn_ext convert logtypes mfixes consts trfuns tokentrfuns rules =
wenzelm@240
   307
  let
wenzelm@240
   308
    val (parse_ast_translation, parse_translation, print_translation,
wenzelm@240
   309
      print_ast_translation) = trfuns;
wenzelm@240
   310
    val (parse_rules, print_rules) = rules;
clasohm@764
   311
    val logtypes' = logtypes \ "prop";
wenzelm@240
   312
clasohm@624
   313
    val mfix_consts = distinct (map (fn (Mfix (_, _, c, _, _)) => c) mfixes);
clasohm@764
   314
    val xprods = map (mfix_to_xprod convert logtypes') mfixes;
wenzelm@240
   315
  in
wenzelm@240
   316
    SynExt {
clasohm@764
   317
      logtypes = logtypes',
clasohm@624
   318
      xprods = xprods,
clasohm@368
   319
      consts = filter is_xid (consts union mfix_consts),
wenzelm@2913
   320
      prmodes = distinct (map (fn (m, _, _) => m) tokentrfuns),
wenzelm@240
   321
      parse_ast_translation = parse_ast_translation,
wenzelm@240
   322
      parse_rules = parse_rules,
wenzelm@240
   323
      parse_translation = parse_translation,
wenzelm@240
   324
      print_translation = print_translation,
wenzelm@240
   325
      print_rules = print_rules,
wenzelm@2694
   326
      print_ast_translation = print_ast_translation,
wenzelm@2694
   327
      token_translation = tokentrfuns}
wenzelm@240
   328
  end;
wenzelm@240
   329
wenzelm@2382
   330
clasohm@764
   331
val syn_ext = mk_syn_ext true;
wenzelm@240
   332
clasohm@764
   333
fun syn_ext_logtypes logtypes =
wenzelm@2694
   334
  syn_ext logtypes [] [] ([], [], [], []) [] ([], []);
wenzelm@555
   335
clasohm@764
   336
fun syn_ext_const_names logtypes cs =
wenzelm@2694
   337
  syn_ext logtypes [] cs ([], [], [], []) [] ([], []);
wenzelm@240
   338
clasohm@764
   339
fun syn_ext_rules logtypes rules =
wenzelm@2694
   340
  syn_ext logtypes [] [] ([], [], [], []) [] rules;
wenzelm@240
   341
wenzelm@2382
   342
fun fix_tr' f _ args = f args;
wenzelm@2382
   343
wenzelm@2382
   344
fun syn_ext_trfuns logtypes (atrs, trs, tr's, atr's) =
wenzelm@2694
   345
  syn_ext logtypes [] [] (atrs, trs, map (apsnd fix_tr') tr's, atr's) [] ([], []);
wenzelm@2382
   346
wenzelm@2382
   347
fun syn_ext_trfunsT logtypes tr's =
wenzelm@2694
   348
  syn_ext logtypes [] [] ([], [], tr's, []) [] ([], []);
wenzelm@2694
   349
wenzelm@2694
   350
fun syn_ext_tokentrfuns logtypes tokentrfuns =
wenzelm@2694
   351
  syn_ext logtypes [] [] ([], [], [], []) tokentrfuns ([], []);
wenzelm@2382
   352
wenzelm@240
   353
clasohm@764
   354
(* pure_ext *)
clasohm@764
   355
clasohm@764
   356
val pure_ext = mk_syn_ext false []
clasohm@764
   357
  [Mfix ("_", spropT --> propT, "", [0], 0),
clasohm@764
   358
   Mfix ("_", logicT --> anyT, "", [0], 0),
clasohm@764
   359
   Mfix ("_", spropT --> anyT, "", [0], 0),
clasohm@764
   360
   Mfix ("'(_')", logicT --> logicT, "", [0], max_pri),
clasohm@764
   361
   Mfix ("'(_')", spropT --> spropT, "", [0], max_pri),
clasohm@764
   362
   Mfix ("_::_",  [logicT, typeT] ---> logicT, "_constrain", [4, 0], 3),
clasohm@764
   363
   Mfix ("_::_",  [spropT, typeT] ---> spropT, "_constrain", [4, 0], 3)]
clasohm@764
   364
  []
clasohm@764
   365
  ([], [], [], [])
wenzelm@2694
   366
  []
clasohm@764
   367
  ([], []);
wenzelm@240
   368
wenzelm@240
   369
end;