src/Pure/Syntax/syn_ext.ML
author paulson
Fri Feb 16 17:18:51 1996 +0100 (1996-02-16)
changeset 1510 4588ba1b1438
parent 1178 b28c6ecc3e6d
child 2201 7cffa6e6fc53
permissions -rw-r--r--
Elimination of fully-functorial style.
Type tactic changed to a type abbrevation (from a datatype).
Constructor tactic and function apply deleted.
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@780
   163
      (writeln ("Error in mixfix annotation " ^ quote sy ^ " for "
clasohm@764
   164
                ^ quote const);
wenzelm@240
   165
        error msg);
wenzelm@240
   166
wenzelm@240
   167
    fun check_pri p =
wenzelm@240
   168
      if p >= 0 andalso p <= max_pri then ()
wenzelm@240
   169
      else err ("precedence out of range: " ^ string_of_int p);
wenzelm@240
   170
wenzelm@240
   171
    fun blocks_ok [] 0 = true
wenzelm@240
   172
      | blocks_ok [] _ = false
wenzelm@240
   173
      | blocks_ok (Bg _ :: syms) n = blocks_ok syms (n + 1)
wenzelm@240
   174
      | blocks_ok (En :: _) 0 = false
wenzelm@240
   175
      | blocks_ok (En :: syms) n = blocks_ok syms (n - 1)
wenzelm@240
   176
      | blocks_ok (_ :: syms) n = blocks_ok syms n;
wenzelm@240
   177
wenzelm@240
   178
    fun check_blocks syms =
wenzelm@240
   179
      if blocks_ok syms 0 then ()
wenzelm@240
   180
      else err "unbalanced block parentheses";
wenzelm@240
   181
wenzelm@240
   182
wenzelm@911
   183
    local
wenzelm@911
   184
      fun is_meta c = c mem ["(", ")", "/", "_"];
wenzelm@240
   185
wenzelm@911
   186
      fun scan_delim_char ("'" :: c :: cs) =
wenzelm@911
   187
            if is_blank c then raise LEXICAL_ERROR else (c, cs)
wenzelm@911
   188
        | scan_delim_char ["'"] = err "trailing escape character"
wenzelm@911
   189
        | scan_delim_char (chs as c :: cs) =
wenzelm@911
   190
            if is_blank c orelse is_meta c then raise LEXICAL_ERROR else (c, cs)
wenzelm@911
   191
        | scan_delim_char [] = raise LEXICAL_ERROR;
wenzelm@240
   192
wenzelm@911
   193
      val scan_sym =
wenzelm@911
   194
        $$ "_" >> K (Argument ("", 0)) ||
wenzelm@911
   195
        $$ "(" -- scan_int >> (Bg o #2) ||
wenzelm@911
   196
        $$ ")" >> K En ||
wenzelm@911
   197
        $$ "/" -- $$ "/" >> K (Brk ~1) ||
wenzelm@911
   198
        $$ "/" -- scan_any is_blank >> (Brk o length o #2) ||
wenzelm@911
   199
        scan_any1 is_blank >> (Space o implode) ||
wenzelm@911
   200
        repeat1 scan_delim_char >> (Delim o implode);
wenzelm@911
   201
wenzelm@911
   202
      val scan_symb =
wenzelm@911
   203
        scan_sym >> Some ||
wenzelm@911
   204
        $$ "'" -- scan_one is_blank >> K None;
wenzelm@911
   205
    in
wenzelm@911
   206
      val scan_symbs = mapfilter I o #1 o repeat scan_symb;
wenzelm@911
   207
    end;
wenzelm@240
   208
wenzelm@240
   209
wenzelm@240
   210
    val cons_fst = apfst o cons;
wenzelm@240
   211
wenzelm@240
   212
    fun add_args [] ty [] = ([], typ_to_nonterm1 ty)
wenzelm@240
   213
      | add_args [] _ _ = err "too many precedences"
wenzelm@240
   214
      | add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) [] =
wenzelm@240
   215
          cons_fst (Argument (typ_to_nonterm ty, 0)) (add_args syms tys [])
wenzelm@240
   216
      | add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) (p :: ps) =
wenzelm@240
   217
          cons_fst (Argument (typ_to_nonterm ty, p)) (add_args syms tys ps)
wenzelm@240
   218
      | add_args (Argument _ :: _) _ _ =
wenzelm@240
   219
          err "more arguments than in corresponding type"
wenzelm@240
   220
      | add_args (sym :: syms) ty ps = cons_fst sym (add_args syms ty ps);
wenzelm@240
   221
wenzelm@240
   222
wenzelm@240
   223
    fun is_arg (Argument _) = true
wenzelm@240
   224
      | is_arg _ = false;
wenzelm@240
   225
wenzelm@240
   226
    fun is_term (Delim _) = true
wenzelm@240
   227
      | is_term (Argument (s, _)) = is_terminal s
wenzelm@240
   228
      | is_term _ = false;
wenzelm@240
   229
wenzelm@240
   230
    fun rem_pri (Argument (s, _)) = Argument (s, chain_pri)
wenzelm@240
   231
      | rem_pri sym = sym;
wenzelm@240
   232
clasohm@764
   233
    fun is_delim (Delim _) = true
clasohm@764
   234
      | is_delim _ = false;
clasohm@764
   235
clasohm@764
   236
    (*replace logical types on rhs by "logic"*)
clasohm@764
   237
    fun unify_logtypes copy_prod (a as (Argument (s, p))) =
clasohm@764
   238
          if s mem logtypes then Argument (logic, p)
clasohm@764
   239
          else a
clasohm@764
   240
      | unify_logtypes _ a = a;
wenzelm@240
   241
wenzelm@911
   242
    val raw_symbs = scan_symbs (explode sy);
wenzelm@240
   243
    val (symbs, lhs) = add_args raw_symbs typ pris;
clasohm@764
   244
    val copy_prod = lhs mem ["prop", "logic"]
clasohm@764
   245
          andalso const <> ""
clasohm@1145
   246
          andalso not (null symbs)
clasohm@764
   247
          andalso not (exists is_delim symbs);
clasohm@764
   248
    val lhs' = if convert andalso not copy_prod then
clasohm@764
   249
                 (if lhs mem logtypes then logic
clasohm@764
   250
                  else if lhs = "prop" then sprop else lhs)
clasohm@764
   251
               else lhs;
clasohm@764
   252
    val symbs' = map (unify_logtypes copy_prod) symbs;
clasohm@764
   253
    val xprod = XProd (lhs', symbs', const, pri);
wenzelm@240
   254
  in
wenzelm@240
   255
    seq check_pri pris;
wenzelm@240
   256
    check_pri pri;
clasohm@764
   257
    check_blocks symbs';
wenzelm@240
   258
clasohm@764
   259
    if is_terminal lhs' then err ("illegal lhs: " ^ lhs')
wenzelm@240
   260
    else if const <> "" then xprod
clasohm@764
   261
    else if length (filter is_arg symbs') <> 1 then
wenzelm@240
   262
      err "copy production must have exactly one argument"
clasohm@764
   263
    else if exists is_term symbs' then xprod
clasohm@764
   264
    else XProd (lhs', map rem_pri symbs', "", chain_pri)
wenzelm@240
   265
  end;
wenzelm@240
   266
wenzelm@240
   267
wenzelm@240
   268
(** datatype syn_ext **)
wenzelm@240
   269
wenzelm@240
   270
datatype syn_ext =
wenzelm@240
   271
  SynExt of {
clasohm@764
   272
    logtypes: string list,
wenzelm@240
   273
    xprods: xprod list,
wenzelm@240
   274
    consts: string list,
paulson@1510
   275
    parse_ast_translation: (string * (Ast.ast list -> Ast.ast)) list,
paulson@1510
   276
    parse_rules: (Ast.ast * Ast.ast) list,
wenzelm@240
   277
    parse_translation: (string * (term list -> term)) list,
wenzelm@240
   278
    print_translation: (string * (term list -> term)) list,
paulson@1510
   279
    print_rules: (Ast.ast * Ast.ast) list,
paulson@1510
   280
    print_ast_translation: (string * (Ast.ast list -> Ast.ast)) list};
wenzelm@240
   281
wenzelm@240
   282
wenzelm@240
   283
(* syn_ext *)
wenzelm@240
   284
clasohm@764
   285
fun mk_syn_ext convert logtypes mfixes consts trfuns rules =
wenzelm@240
   286
  let
wenzelm@240
   287
    val (parse_ast_translation, parse_translation, print_translation,
wenzelm@240
   288
      print_ast_translation) = trfuns;
wenzelm@240
   289
    val (parse_rules, print_rules) = rules;
clasohm@764
   290
    val logtypes' = logtypes \ "prop";
wenzelm@240
   291
clasohm@624
   292
    val mfix_consts = distinct (map (fn (Mfix (_, _, c, _, _)) => c) mfixes);
clasohm@764
   293
    val xprods = map (mfix_to_xprod convert logtypes') mfixes;
wenzelm@240
   294
  in
wenzelm@240
   295
    SynExt {
clasohm@764
   296
      logtypes = logtypes',
clasohm@624
   297
      xprods = xprods,
clasohm@368
   298
      consts = filter is_xid (consts union mfix_consts),
wenzelm@240
   299
      parse_ast_translation = parse_ast_translation,
wenzelm@240
   300
      parse_rules = parse_rules,
wenzelm@240
   301
      parse_translation = parse_translation,
wenzelm@240
   302
      print_translation = print_translation,
wenzelm@240
   303
      print_rules = print_rules,
wenzelm@240
   304
      print_ast_translation = print_ast_translation}
wenzelm@240
   305
  end;
wenzelm@240
   306
clasohm@764
   307
val syn_ext = mk_syn_ext true;
wenzelm@240
   308
clasohm@764
   309
fun syn_ext_logtypes logtypes =
clasohm@764
   310
  syn_ext logtypes [] [] ([], [], [], []) ([], []);
clasohm@764
   311
clasohm@764
   312
fun syn_ext_const_names logtypes cs =
clasohm@764
   313
  syn_ext logtypes [] cs ([], [], [], []) ([], []);
wenzelm@555
   314
clasohm@764
   315
fun syn_ext_rules logtypes rules =
clasohm@764
   316
  syn_ext logtypes [] [] ([], [], [], []) rules;
clasohm@764
   317
clasohm@764
   318
fun syn_ext_trfuns logtypes trfuns =
clasohm@764
   319
  syn_ext logtypes [] [] trfuns ([], []);
wenzelm@240
   320
clasohm@764
   321
(* pure_ext *)
wenzelm@240
   322
clasohm@764
   323
val pure_ext = mk_syn_ext false []
clasohm@764
   324
  [Mfix ("_", spropT --> propT, "", [0], 0),
clasohm@764
   325
   Mfix ("_", logicT --> anyT, "", [0], 0),
clasohm@764
   326
   Mfix ("_", spropT --> anyT, "", [0], 0),
clasohm@764
   327
   Mfix ("'(_')", logicT --> logicT, "", [0], max_pri),
clasohm@764
   328
   Mfix ("'(_')", spropT --> spropT, "", [0], max_pri),
clasohm@764
   329
   Mfix ("_::_",  [logicT, typeT] ---> logicT, "_constrain", [4, 0], 3),
clasohm@764
   330
   Mfix ("_::_",  [spropT, typeT] ---> spropT, "_constrain", [4, 0], 3)]
clasohm@764
   331
  []
clasohm@764
   332
  ([], [], [], [])
clasohm@764
   333
  ([], []);
wenzelm@240
   334
wenzelm@240
   335
end;
paulson@1510
   336