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