src/Pure/Syntax/syntax.ML
author wenzelm
Mon Oct 04 15:30:49 1993 +0100 (1993-10-04)
changeset 18 c9ec452ff08f
parent 0 a5a9c433f639
child 47 0af9dbb93529
permissions -rw-r--r--
lots of internal cleaning and tuning;
removed {parse,print}_{pre,post}_proc;
new lexer: now human readable due to scanner combinators;
new parser installed, but still inactive (due to grammar ambiguities);
added Syntax.test_read;
typ_of_term: sorts now made distinct and sorted;
mixfix: added forced line breaks (//);
PROP now printed before subterm of type prop with non-const head;
wenzelm@18
     1
(*  Title:      Pure/Syntax/syntax.ML
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@18
     4
wenzelm@18
     5
Root of Isabelle's syntax module.
wenzelm@18
     6
wenzelm@18
     7
TODO:
wenzelm@18
     8
  extend_tables (requires extend_gram) (roots!)
wenzelm@18
     9
  replace add_synrules by extend_tables
wenzelm@18
    10
  extend, merge: make roots handling more robust
wenzelm@18
    11
  extend: read_typ (incl check) as arg, remove def_sort
wenzelm@18
    12
  extend: use extend_tables
clasohm@0
    13
*)
clasohm@0
    14
clasohm@0
    15
signature SYNTAX =
clasohm@0
    16
sig
wenzelm@18
    17
  include AST0
clasohm@0
    18
  include LEXICON0
clasohm@0
    19
  include EXTENSION0
clasohm@0
    20
  include TYPE_EXT0
clasohm@0
    21
  include SEXTENSION1
clasohm@0
    22
  include PRINTER0
wenzelm@18
    23
  type syntax
wenzelm@18
    24
  val type_syn: syntax
wenzelm@18
    25
  val extend: syntax * (indexname -> sort) -> string list * string list * sext
wenzelm@18
    26
    -> syntax
wenzelm@18
    27
  val merge: syntax * syntax -> syntax
wenzelm@18
    28
  val print_gram: syntax -> unit
wenzelm@18
    29
  val print_trans: syntax -> unit
wenzelm@18
    30
  val print_syntax: syntax -> unit
wenzelm@18
    31
  val test_read: syntax -> string -> string -> unit
wenzelm@18
    32
  val read: syntax -> typ -> string -> term
wenzelm@18
    33
  val pretty_term: syntax -> term -> Pretty.T
wenzelm@18
    34
  val pretty_typ: syntax -> typ -> Pretty.T
wenzelm@18
    35
  val string_of_term: syntax -> term -> string
wenzelm@18
    36
  val string_of_typ: syntax -> typ -> string
clasohm@0
    37
end;
clasohm@0
    38
clasohm@0
    39
functor SyntaxFun(structure Symtab: SYMTAB and TypeExt: TYPE_EXT
clasohm@0
    40
  and Parser: PARSER and SExtension: SEXTENSION and Printer: PRINTER
wenzelm@18
    41
  sharing TypeExt.Extension = SExtension.Extension
wenzelm@18
    42
  and Parser.XGram = TypeExt.Extension.XGram = Printer.XGram
wenzelm@18
    43
  and Parser.XGram.Ast = Parser.ParseTree.Ast)(*: SYNTAX *) = (* FIXME *)
clasohm@0
    44
struct
clasohm@0
    45
clasohm@0
    46
structure Extension = TypeExt.Extension;
clasohm@0
    47
structure XGram = Extension.XGram;
clasohm@0
    48
structure Lexicon = Parser.ParseTree.Lexicon;
wenzelm@18
    49
open Lexicon Parser Parser.ParseTree Extension TypeExt SExtension Printer
wenzelm@18
    50
  XGram XGram.Ast;
clasohm@0
    51
clasohm@0
    52
clasohm@0
    53
fun lookup tab a = Symtab.lookup (tab, a);
clasohm@0
    54
clasohm@0
    55
clasohm@0
    56
clasohm@0
    57
(** datatype syntax **)
clasohm@0
    58
clasohm@0
    59
datatype tables =
wenzelm@18
    60
  Tabs of {
wenzelm@18
    61
    lexicon: lexicon,
wenzelm@18
    62
    roots: string list,
wenzelm@18
    63
    gram: gram,
wenzelm@18
    64
    consts: string list,
clasohm@0
    65
    parse_ast_trtab: (ast list -> ast) Symtab.table,
clasohm@0
    66
    parse_ruletab: (ast * ast) list Symtab.table,
clasohm@0
    67
    parse_trtab: (term list -> term) Symtab.table,
clasohm@0
    68
    print_trtab: (term list -> term) Symtab.table,
clasohm@0
    69
    print_ruletab: (ast * ast) list Symtab.table,
wenzelm@18
    70
    prtab: prtab};
clasohm@0
    71
clasohm@0
    72
datatype gramgraph =
clasohm@0
    73
  EmptyGG |
clasohm@0
    74
  ExtGG of gramgraph ref * (ext * synrules) |
clasohm@0
    75
  MergeGG of gramgraph ref * gramgraph ref;
clasohm@0
    76
clasohm@0
    77
datatype syntax = Syntax of gramgraph ref * tables;
clasohm@0
    78
clasohm@0
    79
clasohm@0
    80
clasohm@0
    81
(*** compile syntax ***)
clasohm@0
    82
wenzelm@18
    83
(* translation funs *)
wenzelm@18
    84
wenzelm@18
    85
fun extend_trtab tab trfuns name =
wenzelm@18
    86
  Symtab.balance (Symtab.st_of_alist (trfuns, tab)) handle Symtab.DUPLICATE s
wenzelm@18
    87
    => error ("More than one " ^ name ^ " for " ^ quote s);
wenzelm@18
    88
wenzelm@18
    89
val mk_trtab = extend_trtab Symtab.null;
wenzelm@18
    90
wenzelm@18
    91
wenzelm@18
    92
(* translation rules *)
wenzelm@18
    93
wenzelm@18
    94
fun mk_ruletab rules =
wenzelm@18
    95
  let
wenzelm@18
    96
    fun add_rule (r, tab) =
wenzelm@18
    97
      let val a = head_of_rule r;
wenzelm@18
    98
      in
wenzelm@18
    99
        (case lookup tab a of
wenzelm@18
   100
          None => Symtab.update ((a, [r]), tab)
wenzelm@18
   101
        | Some rs => Symtab.update ((a, r :: rs), tab))
wenzelm@18
   102
      end;
wenzelm@18
   103
  in
wenzelm@18
   104
    Symtab.balance (foldr add_rule (rules, Symtab.null))
wenzelm@18
   105
  end;
wenzelm@18
   106
wenzelm@18
   107
fun extend_ruletab tab rules =
wenzelm@18
   108
  mk_ruletab (flat (map #2 (Symtab.alist_of tab)) @ rules);
wenzelm@18
   109
wenzelm@18
   110
wenzelm@18
   111
(* mk_tables *)
wenzelm@18
   112
wenzelm@18
   113
fun mk_tables (XGram xgram) =
wenzelm@18
   114
  let
wenzelm@18
   115
    val {roots, prods, consts, parse_ast_translation, parse_rules,
wenzelm@18
   116
      parse_translation, print_translation, print_rules,
wenzelm@18
   117
      print_ast_translation} = xgram;
wenzelm@18
   118
  in
wenzelm@18
   119
    Tabs {
wenzelm@18
   120
      lexicon = mk_lexicon (literals_of prods),
wenzelm@18
   121
      roots = roots,
wenzelm@18
   122
      gram = mk_gram roots prods,
wenzelm@18
   123
      consts = consts,
wenzelm@18
   124
      parse_ast_trtab = mk_trtab parse_ast_translation "parse ast translation",
wenzelm@18
   125
      parse_ruletab = mk_ruletab parse_rules,
wenzelm@18
   126
      parse_trtab = mk_trtab parse_translation "parse translation",
wenzelm@18
   127
      print_trtab = mk_trtab print_translation "print translation",
wenzelm@18
   128
      print_ruletab = mk_ruletab print_rules,
wenzelm@18
   129
      prtab = mk_prtab prods print_ast_translation}
wenzelm@18
   130
  end;
wenzelm@18
   131
wenzelm@18
   132
wenzelm@18
   133
(* add_synrules *)
wenzelm@18
   134
wenzelm@18
   135
fun add_synrules (Tabs tabs) (SynRules rules) =
wenzelm@18
   136
  let
wenzelm@18
   137
    val {lexicon, roots, gram, consts, parse_ast_trtab, parse_ruletab,
wenzelm@18
   138
      parse_trtab, print_trtab, print_ruletab, prtab} = tabs;
wenzelm@18
   139
    val {parse_rules, print_rules} = rules;
wenzelm@18
   140
  in
wenzelm@18
   141
    Tabs {
wenzelm@18
   142
      lexicon = lexicon, roots = roots, gram = gram, consts = consts,
wenzelm@18
   143
      parse_ast_trtab = parse_ast_trtab,
wenzelm@18
   144
      parse_ruletab = extend_ruletab parse_ruletab parse_rules,
wenzelm@18
   145
      parse_trtab = parse_trtab,
wenzelm@18
   146
      print_trtab = print_trtab,
wenzelm@18
   147
      print_ruletab = extend_ruletab print_ruletab print_rules,
wenzelm@18
   148
      prtab = prtab}
wenzelm@18
   149
  end;
wenzelm@18
   150
wenzelm@18
   151
clasohm@0
   152
(* ggr_to_xgram *)
clasohm@0
   153
clasohm@0
   154
fun ggr_to_xgram ggr =
clasohm@0
   155
  let
clasohm@0
   156
    fun flatGG ggr (xg, v) =
clasohm@0
   157
      if ggr mem v then (xg, v) else flatGG' ggr (xg, ggr :: v)
clasohm@0
   158
    and flatGG' (ref EmptyGG) xgv = xgv
clasohm@0
   159
      | flatGG' (ref (ExtGG (ggr, ext))) xgv =
clasohm@0
   160
          let
wenzelm@18
   161
            val (xg', v') = flatGG ggr xgv;
clasohm@0
   162
          in
wenzelm@18
   163
            (extend_xgram xg' ext, v')
clasohm@0
   164
          end
clasohm@0
   165
      | flatGG' (ref (MergeGG (ggr1, ggr2))) xgv =
clasohm@0
   166
          flatGG ggr1 (flatGG ggr2 xgv);
clasohm@0
   167
  in
wenzelm@18
   168
    #1 (flatGG ggr (empty_xgram, []))
clasohm@0
   169
  end;
clasohm@0
   170
clasohm@0
   171
clasohm@0
   172
(* make_syntax *)
clasohm@0
   173
wenzelm@18
   174
fun make_syntax ggr = Syntax (ggr, mk_tables (ggr_to_xgram ggr));
clasohm@0
   175
clasohm@0
   176
clasohm@0
   177
clasohm@0
   178
(*** inspect syntax ***)
clasohm@0
   179
clasohm@0
   180
fun xgram_of (Syntax (ggr, _)) = ggr_to_xgram ggr;
clasohm@0
   181
clasohm@0
   182
fun string_of_big_list name prts =
clasohm@0
   183
  Pretty.string_of (Pretty.blk (2,
clasohm@0
   184
    separate Pretty.fbrk (Pretty.str name :: prts)));
clasohm@0
   185
wenzelm@18
   186
fun string_of_strings name strs =
wenzelm@18
   187
  Pretty.string_of (Pretty.blk (2,
wenzelm@18
   188
    separate (Pretty.brk 1)
wenzelm@18
   189
      (map Pretty.str (name :: map quote (sort_strings strs)))));
clasohm@0
   190
wenzelm@18
   191
wenzelm@18
   192
(* print_gram *)
clasohm@0
   193
clasohm@0
   194
fun prt_gram (XGram {roots, prods, ...}) =
clasohm@0
   195
  let
clasohm@0
   196
    fun pretty_name name = [Pretty.str (name ^ " ="), Pretty.brk 1];
clasohm@0
   197
clasohm@0
   198
    fun pretty_xsymbs (Terminal s :: xs) =
clasohm@0
   199
          Pretty.str (quote s) :: Pretty.brk 1 :: pretty_xsymbs xs
clasohm@0
   200
      | pretty_xsymbs (Nonterminal (s, p) :: xs) =
clasohm@0
   201
          (if is_terminal s then Pretty.str s
clasohm@0
   202
          else Pretty.str (s ^ "[" ^ string_of_int p ^ "]"))
clasohm@0
   203
            :: Pretty.brk 1 :: pretty_xsymbs xs
clasohm@0
   204
      | pretty_xsymbs (_ :: xs) = pretty_xsymbs xs
clasohm@0
   205
      | pretty_xsymbs [] = [];
clasohm@0
   206
clasohm@0
   207
    fun pretty_const "" = [Pretty.brk 1]
clasohm@0
   208
      | pretty_const c = [Pretty.str (" => " ^ quote c), Pretty.brk 1];
clasohm@0
   209
clasohm@0
   210
    fun pretty_pri p = [Pretty.str ("(" ^ string_of_int p ^ ")")];
clasohm@0
   211
clasohm@0
   212
    fun pretty_prod (Prod (name, xsymbs, const, pri)) =
clasohm@0
   213
      Pretty.blk (2, pretty_name name @ pretty_xsymbs xsymbs @
clasohm@0
   214
        pretty_const const @ pretty_pri pri);
clasohm@0
   215
  in
wenzelm@18
   216
    writeln (string_of_strings "lexicon:" (literals_of prods));
clasohm@0
   217
    writeln (Pretty.string_of (Pretty.blk (2,
clasohm@0
   218
      separate (Pretty.brk 1) (map Pretty.str ("roots:" :: roots)))));
clasohm@0
   219
    writeln (string_of_big_list "prods:" (map pretty_prod prods))
clasohm@0
   220
  end;
clasohm@0
   221
clasohm@0
   222
val print_gram = prt_gram o xgram_of;
clasohm@0
   223
clasohm@0
   224
wenzelm@18
   225
(* print_trans *)
clasohm@0
   226
clasohm@0
   227
fun prt_trans (XGram xgram) =
clasohm@0
   228
  let
clasohm@0
   229
    fun string_of_trs name trs = string_of_strings name (map fst trs);
clasohm@0
   230
clasohm@0
   231
    fun string_of_rules name rules =
clasohm@0
   232
      string_of_big_list name (map pretty_rule rules);
clasohm@0
   233
wenzelm@18
   234
    val {consts, parse_ast_translation, parse_rules, parse_translation,
wenzelm@18
   235
      print_translation, print_rules, print_ast_translation, ...} = xgram;
clasohm@0
   236
  in
clasohm@0
   237
    writeln (string_of_strings "consts:" consts);
clasohm@0
   238
    writeln (string_of_trs "parse_ast_translation:" parse_ast_translation);
clasohm@0
   239
    writeln (string_of_rules "parse_rules:" parse_rules);
clasohm@0
   240
    writeln (string_of_trs "parse_translation:" parse_translation);
clasohm@0
   241
    writeln (string_of_trs "print_translation:" print_translation);
clasohm@0
   242
    writeln (string_of_rules "print_rules:" print_rules);
clasohm@0
   243
    writeln (string_of_trs "print_ast_translation:" print_ast_translation)
clasohm@0
   244
  end;
clasohm@0
   245
clasohm@0
   246
val print_trans = prt_trans o xgram_of;
clasohm@0
   247
clasohm@0
   248
clasohm@0
   249
(* print_syntax *)
clasohm@0
   250
clasohm@0
   251
fun print_syntax syn =
clasohm@0
   252
  let
clasohm@0
   253
    val xgram = xgram_of syn;
clasohm@0
   254
  in
clasohm@0
   255
    prt_gram xgram; prt_trans xgram
clasohm@0
   256
  end;
clasohm@0
   257
clasohm@0
   258
clasohm@0
   259
clasohm@0
   260
(*** parsing and printing ***)
clasohm@0
   261
wenzelm@18
   262
(* mk_get_rules *)
clasohm@0
   263
wenzelm@18
   264
fun mk_get_rules ruletab =
clasohm@0
   265
  let
clasohm@0
   266
    fun get_rules a =
clasohm@0
   267
      (case lookup ruletab a of
clasohm@0
   268
        Some rules => rules
clasohm@0
   269
      | None => []);
clasohm@0
   270
  in
wenzelm@18
   271
    if Symtab.is_null ruletab then None else Some get_rules
wenzelm@18
   272
  end;
wenzelm@18
   273
wenzelm@18
   274
wenzelm@18
   275
(* read_ast *)
wenzelm@18
   276
wenzelm@18
   277
fun read_ast (Syntax (_, tabs)) xids root str =
wenzelm@18
   278
  let
wenzelm@18
   279
    val Tabs {lexicon, gram, parse_ast_trtab, ...} = tabs;
wenzelm@18
   280
  in
wenzelm@18
   281
    pt_to_ast (lookup parse_ast_trtab)
wenzelm@18
   282
      (parse gram root (tokenize lexicon xids str))
clasohm@0
   283
  end;
clasohm@0
   284
clasohm@0
   285
clasohm@0
   286
wenzelm@18
   287
(** test_read **)
wenzelm@18
   288
wenzelm@18
   289
fun test_read (Syntax (_, tabs)) root str =
wenzelm@18
   290
  let
wenzelm@18
   291
    val Tabs {lexicon, gram, parse_ast_trtab, parse_ruletab, ...} = tabs;
wenzelm@18
   292
wenzelm@18
   293
    val toks = tokenize lexicon false str;
wenzelm@18
   294
    val _ = writeln ("tokens: " ^ space_implode " " (map display_token toks));
wenzelm@18
   295
wenzelm@18
   296
    val pt = parse gram root toks;
wenzelm@18
   297
    val raw_ast = pt_to_ast (K None) pt;
wenzelm@18
   298
    val _ = writeln ("raw: " ^ str_of_ast raw_ast);
wenzelm@18
   299
wenzelm@18
   300
    val pre_ast = pt_to_ast (lookup parse_ast_trtab) pt;
wenzelm@18
   301
    val _ = normalize true true (mk_get_rules parse_ruletab) pre_ast;
wenzelm@18
   302
  in () end;
wenzelm@18
   303
wenzelm@18
   304
wenzelm@18
   305
clasohm@0
   306
(** read **)
clasohm@0
   307
wenzelm@18
   308
fun read (syn as Syntax (_, tabs)) ty str =
clasohm@0
   309
  let
wenzelm@18
   310
    val Tabs {parse_ruletab, parse_trtab, ...} = tabs;
wenzelm@18
   311
    val ast = read_ast syn false (typ_to_nonterm ty) str;
clasohm@0
   312
  in
clasohm@0
   313
    ast_to_term (lookup parse_trtab)
wenzelm@18
   314
      (normalize_ast (mk_get_rules parse_ruletab) ast)
clasohm@0
   315
  end;
clasohm@0
   316
clasohm@0
   317
clasohm@0
   318
wenzelm@18
   319
(** read_rule **)
wenzelm@18
   320
wenzelm@18
   321
fun read_rule syn (xrule as ((_, lhs_src), (_, rhs_src))) =
wenzelm@18
   322
  let
wenzelm@18
   323
    val Syntax (_, Tabs {consts, ...}) = syn;
wenzelm@18
   324
wenzelm@18
   325
    fun constantify (ast as Constant _) = ast
wenzelm@18
   326
      | constantify (ast as Variable x) =
wenzelm@18
   327
          if x mem consts then Constant x else ast
wenzelm@18
   328
      | constantify (Appl asts) = Appl (map constantify asts);
clasohm@0
   329
wenzelm@18
   330
    fun read_pat (root, str) =
wenzelm@18
   331
      constantify (read_ast syn true root str)
wenzelm@18
   332
        handle ERROR => error ("The error above occurred in " ^ quote str);
wenzelm@18
   333
wenzelm@18
   334
    val rule as (lhs, rhs) = (pairself read_pat) xrule;
wenzelm@18
   335
  in
wenzelm@18
   336
    (case rule_error rule of
wenzelm@18
   337
      Some msg =>
wenzelm@18
   338
        error ("Error in syntax translation rule: " ^ msg ^
wenzelm@18
   339
          "\nexternal: " ^ quote lhs_src ^ "  ->  " ^ quote rhs_src ^
wenzelm@18
   340
          "\ninternal: " ^ str_of_ast lhs ^ "  ->  " ^ str_of_ast rhs)
wenzelm@18
   341
    | None => rule)
wenzelm@18
   342
  end;
clasohm@0
   343
clasohm@0
   344
clasohm@0
   345
wenzelm@18
   346
(** read_xrules **)
wenzelm@18
   347
wenzelm@18
   348
fun read_xrules syn xrules =
wenzelm@18
   349
  let
wenzelm@18
   350
    fun right_rule (xpat1 |-> xpat2) = Some (xpat1, xpat2)
wenzelm@18
   351
      | right_rule (xpat1 <-| xpat2) = None
wenzelm@18
   352
      | right_rule (xpat1 <-> xpat2) = Some (xpat1, xpat2);
clasohm@0
   353
wenzelm@18
   354
    fun left_rule (xpat1 |-> xpat2) = None
wenzelm@18
   355
      | left_rule (xpat1 <-| xpat2) = Some (xpat2, xpat1)
wenzelm@18
   356
      | left_rule (xpat1 <-> xpat2) = Some (xpat2, xpat1);
wenzelm@18
   357
  in
wenzelm@18
   358
    (map (read_rule syn) (mapfilter right_rule xrules),
wenzelm@18
   359
     map (read_rule syn) (mapfilter left_rule xrules))
wenzelm@18
   360
  end;
wenzelm@18
   361
wenzelm@18
   362
wenzelm@18
   363
wenzelm@18
   364
(** pretty terms or typs **)
wenzelm@18
   365
wenzelm@18
   366
fun pretty_t t_to_ast pretty_t (syn as Syntax (_, tabs)) t =
clasohm@0
   367
  let
wenzelm@18
   368
    val Tabs {print_trtab, print_ruletab, prtab, ...} = tabs;
clasohm@0
   369
    val ast = t_to_ast (lookup print_trtab) t;
clasohm@0
   370
  in
wenzelm@18
   371
    pretty_t prtab (normalize_ast (mk_get_rules print_ruletab) ast)
clasohm@0
   372
  end;
clasohm@0
   373
clasohm@0
   374
val pretty_term = pretty_t term_to_ast pretty_term_ast;
clasohm@0
   375
clasohm@0
   376
val pretty_typ = pretty_t typ_to_ast pretty_typ_ast;
clasohm@0
   377
clasohm@0
   378
fun string_of_term syn t = Pretty.string_of (pretty_term syn t);
clasohm@0
   379
clasohm@0
   380
fun string_of_typ syn ty = Pretty.string_of (pretty_typ syn ty);
clasohm@0
   381
clasohm@0
   382
clasohm@0
   383
clasohm@0
   384
(*** build syntax ***)
clasohm@0
   385
clasohm@0
   386
(* type_syn *)
clasohm@0
   387
clasohm@0
   388
val type_syn = make_syntax (ref (ExtGG (ref EmptyGG, (type_ext, empty_synrules))));
clasohm@0
   389
clasohm@0
   390
wenzelm@18
   391
(** extend **)
clasohm@0
   392
clasohm@0
   393
fun extend (old_syn as Syntax (ggr, _), def_sort) (roots, xconsts, sext) =
clasohm@0
   394
  let
clasohm@0
   395
    fun read_typ s = typ_of_term def_sort (read old_syn typeT s);
clasohm@0
   396
    val ext = ext_of_sext roots xconsts read_typ sext;
clasohm@0
   397
clasohm@0
   398
    val (tmp_syn as Syntax (_, tmp_tabs)) =
clasohm@0
   399
      make_syntax (ref (ExtGG (ggr, (ext, empty_synrules))));
clasohm@0
   400
wenzelm@18
   401
    val (parse_rules, print_rules) = read_xrules tmp_syn (xrules_of sext);
wenzelm@18
   402
    val rules =
clasohm@0
   403
      SynRules {
wenzelm@18
   404
        parse_rules = parse_rules,
wenzelm@18
   405
        print_rules = print_rules};
clasohm@0
   406
  in
wenzelm@18
   407
    Syntax (ref (ExtGG (ggr, (ext, rules))), add_synrules tmp_tabs rules)
clasohm@0
   408
  end;
clasohm@0
   409
clasohm@0
   410
clasohm@0
   411
(* merge *)
clasohm@0
   412
clasohm@0
   413
fun merge (Syntax (ggr1, _), Syntax (ggr2, _)) =
clasohm@0
   414
  make_syntax (ref (MergeGG (ggr1, ggr2)));
clasohm@0
   415
clasohm@0
   416
clasohm@0
   417
end;
clasohm@0
   418