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