src/Pure/Syntax/syn_ext.ML
author wenzelm
Mon Nov 16 10:41:27 1998 +0100 (1998-11-16)
changeset 5870 5d4fc952be47
parent 5690 4b056ee5435c
child 6322 7047300264c9
permissions -rw-r--r--
Scan.read;
     1 (*  Title:      Pure/Syntax/syn_ext.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel and Carsten Clasohm, TU Muenchen
     4 
     5 Syntax extension (internal interface).
     6 *)
     7 
     8 signature SYN_EXT0 =
     9 sig
    10   val typeT: typ
    11   val constrainC: string
    12   val max_pri: int
    13 end;
    14 
    15 signature SYN_EXT =
    16 sig
    17   include SYN_EXT0
    18   val logic: string
    19   val args: string
    20   val cargs: string
    21   val any: string
    22   val sprop: string
    23   val typ_to_nonterm: typ -> string
    24   datatype xsymb =
    25     Delim of string |
    26     Argument of string * int |
    27     Space of string |
    28     Bg of int | Brk of int | En
    29   datatype xprod = XProd of string * xsymb list * string * int
    30   val chain_pri: int
    31   val delims_of: xprod list -> string list list
    32   datatype mfix = Mfix of string * typ * string * int list * int
    33   datatype syn_ext =
    34     SynExt of {
    35       logtypes: string list,
    36       xprods: xprod list,
    37       consts: string list,
    38       prmodes: string list,
    39       parse_ast_translation: (string * (Ast.ast list -> Ast.ast)) list,
    40       parse_rules: (Ast.ast * Ast.ast) list,
    41       parse_translation: (string * (term list -> term)) list,
    42       print_translation: (string * (bool -> typ -> term list -> term)) list,
    43       print_rules: (Ast.ast * Ast.ast) list,
    44       print_ast_translation: (string * (Ast.ast list -> Ast.ast)) list,
    45       token_translation: (string * string * (string -> string * int)) list}
    46   val mfix_args: string -> int
    47   val mk_syn_ext: bool -> string list -> mfix list ->
    48     string list -> (string * (Ast.ast list -> Ast.ast)) list *
    49     (string * (term list -> term)) list *
    50     (string * (bool -> typ -> term list -> term)) list * (string * (Ast.ast list -> Ast.ast)) list
    51     -> (string * string * (string -> string * int)) list
    52     -> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
    53   val syn_ext: string list -> mfix list -> string list ->
    54     (string * (Ast.ast list -> Ast.ast)) list * (string * (term list -> term)) list *
    55     (string * (bool -> typ -> term list -> term)) list * (string * (Ast.ast list -> Ast.ast)) list
    56     -> (string * string * (string -> string * int)) list
    57     -> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
    58   val syn_ext_logtypes: string list -> syn_ext
    59   val syn_ext_const_names: string list -> string list -> syn_ext
    60   val syn_ext_rules: string list -> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
    61   val fix_tr': (term list -> term) -> bool -> typ -> term list -> term
    62   val syn_ext_trfuns: string list ->
    63     (string * (Ast.ast list -> Ast.ast)) list * (string * (term list -> term)) list *
    64     (string * (term list -> term)) list * (string * (Ast.ast list -> Ast.ast)) list
    65     -> syn_ext
    66   val syn_ext_trfunsT: string list ->
    67     (string * (bool -> typ -> term list -> term)) list -> syn_ext
    68   val syn_ext_tokentrfuns: string list
    69     -> (string * string * (string -> string * int)) list -> syn_ext
    70   val pure_ext: syn_ext
    71 end;
    72 
    73 structure SynExt : SYN_EXT =
    74 struct
    75 
    76 
    77 (** misc definitions **)
    78 
    79 (* syntactic categories *)
    80 
    81 val logic = "logic";
    82 val logicT = Type (logic, []);
    83 
    84 val args = "args";
    85 val cargs = "cargs";
    86 
    87 val typeT = Type ("type", []);
    88 
    89 val sprop = "#prop";
    90 val spropT = Type (sprop, []);
    91 
    92 val any = "any";
    93 val anyT = Type (any, []);
    94 
    95 
    96 (* constants *)
    97 
    98 val constrainC = "_constrain";
    99 
   100 
   101 
   102 (** datatype xprod **)
   103 
   104 (*Delim s: delimiter s
   105   Argument (s, p): nonterminal s requiring priority >= p, or valued token
   106   Space s: some white space for printing
   107   Bg, Brk, En: blocks and breaks for pretty printing*)
   108 
   109 datatype xsymb =
   110   Delim of string |
   111   Argument of string * int |
   112   Space of string |
   113   Bg of int | Brk of int | En;
   114 
   115 
   116 (*XProd (lhs, syms, c, p):
   117     lhs: name of nonterminal on the lhs of the production
   118     syms: list of symbols on the rhs of the production
   119     c: head of parse tree
   120     p: priority of this production*)
   121 
   122 datatype xprod = XProd of string * xsymb list * string * int;
   123 
   124 val max_pri = 1000;   (*maximum legal priority*)
   125 val chain_pri = ~1;   (*dummy for chain productions*)
   126 
   127 
   128 (* delims_of *)
   129 
   130 fun delims_of xprods =
   131   let
   132     fun del_of (Delim s) = Some s
   133       | del_of _ = None;
   134 
   135     fun dels_of (XProd (_, xsymbs, _, _)) =
   136       mapfilter del_of xsymbs;
   137   in
   138     map Symbol.explode (distinct (flat (map dels_of xprods)))
   139   end;
   140 
   141 
   142 
   143 (** datatype mfix **)
   144 
   145 (*Mfix (sy, ty, c, ps, p):
   146     sy: rhs of production as symbolic string
   147     ty: type description of production
   148     c: head of parse tree
   149     ps: priorities of arguments in sy
   150     p: priority of production*)
   151 
   152 datatype mfix = Mfix of string * typ * string * int list * int;
   153 
   154 
   155 (* typ_to_nonterm *)
   156 
   157 fun typ_to_nt _ (Type (c, _)) = c
   158   | typ_to_nt default _ = default;
   159 
   160 (*get nonterminal for rhs*)
   161 val typ_to_nonterm = typ_to_nt any;
   162 
   163 (*get nonterminal for lhs*)
   164 val typ_to_nonterm1 = typ_to_nt logic;
   165 
   166 
   167 (* read_mixfix, mfix_args *)
   168 
   169 local
   170   fun is_meta c = c mem ["(", ")", "/", "_"];
   171 
   172   val scan_delim_char =
   173     $$ "'" |-- Scan.one ((not o Symbol.is_blank) andf Symbol.not_eof) ||
   174     Scan.one ((not o is_meta) andf (not o Symbol.is_blank) andf Symbol.not_eof);
   175 
   176   val scan_sym =
   177     $$ "_" >> K (Argument ("", 0)) ||
   178     $$ "(" |-- Scan.any Symbol.is_digit >> (Bg o #1 o Term.read_int) ||
   179     $$ ")" >> K En ||
   180     $$ "/" -- $$ "/" >> K (Brk ~1) ||
   181     $$ "/" |-- Scan.any Symbol.is_blank >> (Brk o length) ||
   182     Scan.any1 Symbol.is_blank >> (Space o implode) ||
   183     Scan.repeat1 scan_delim_char >> (Delim o implode);
   184 
   185   val scan_symb =
   186     scan_sym >> Some ||
   187     $$ "'" -- Scan.one Symbol.is_blank >> K None;
   188 
   189   val scan_symbs = Scan.repeat scan_symb --| Scan.ahead (Scan.one (not_equal "'"));
   190   val read_symbs = mapfilter I o the o Scan.read Symbol.stopper scan_symbs;
   191 in
   192   val read_mixfix = read_symbs o Symbol.explode;
   193 end;
   194 
   195 fun mfix_args sy =
   196   foldl (fn (i, Argument _) => i + 1 | (i, _) => i) (0, read_mixfix sy);
   197 
   198 
   199 (* mfix_to_xprod *)
   200 
   201 fun mfix_to_xprod convert logtypes (Mfix (sy, typ, const, pris, pri)) =
   202   let
   203     fun err msg =
   204       (if msg = "" then () else error_msg msg;
   205         error ("in mixfix annotation " ^ quote sy ^ " for " ^ quote const));
   206 
   207     fun check_pri p =
   208       if p >= 0 andalso p <= max_pri then ()
   209       else err ("Precedence out of range: " ^ string_of_int p);
   210 
   211     fun blocks_ok [] 0 = true
   212       | blocks_ok [] _ = false
   213       | blocks_ok (Bg _ :: syms) n = blocks_ok syms (n + 1)
   214       | blocks_ok (En :: _) 0 = false
   215       | blocks_ok (En :: syms) n = blocks_ok syms (n - 1)
   216       | blocks_ok (_ :: syms) n = blocks_ok syms n;
   217 
   218     fun check_blocks syms =
   219       if blocks_ok syms 0 then ()
   220       else err "Unbalanced block parentheses";
   221 
   222 
   223     val cons_fst = apfst o cons;
   224 
   225     fun add_args [] ty [] = ([], typ_to_nonterm1 ty)
   226       | add_args [] _ _ = err "Too many precedences"
   227       | add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) [] =
   228           cons_fst (Argument (typ_to_nonterm ty, 0)) (add_args syms tys [])
   229       | add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) (p :: ps) =
   230           cons_fst (Argument (typ_to_nonterm ty, p)) (add_args syms tys ps)
   231       | add_args (Argument _ :: _) _ _ =
   232           err "More arguments than in corresponding type"
   233       | add_args (sym :: syms) ty ps = cons_fst sym (add_args syms ty ps);
   234 
   235 
   236     fun is_arg (Argument _) = true
   237       | is_arg _ = false;
   238 
   239     fun is_term (Delim _) = true
   240       | is_term (Argument (s, _)) = Lexicon.is_terminal s
   241       | is_term _ = false;
   242 
   243     fun rem_pri (Argument (s, _)) = Argument (s, chain_pri)
   244       | rem_pri sym = sym;
   245 
   246     fun is_delim (Delim _) = true
   247       | is_delim _ = false;
   248 
   249     fun logify_types copy_prod (a as (Argument (s, p))) =
   250           if s mem logtypes then Argument (logic, p) else a
   251       | logify_types _ a = a;
   252 
   253 
   254     val raw_symbs = read_mixfix sy handle ERROR => err "";
   255     val (symbs, lhs) = add_args raw_symbs typ pris;
   256     val copy_prod =
   257       lhs mem ["prop", "logic"]
   258         andalso const <> ""
   259         andalso not (null symbs)
   260         andalso not (exists is_delim symbs);
   261     val lhs' =
   262       if convert andalso not copy_prod then
   263        (if lhs mem logtypes then logic
   264         else if lhs = "prop" then sprop else lhs)
   265       else lhs;
   266     val symbs' = map (logify_types copy_prod) symbs;
   267     val xprod = XProd (lhs', symbs', const, pri);
   268   in
   269     seq check_pri pris;
   270     check_pri pri;
   271     check_blocks symbs';
   272 
   273     if Lexicon.is_terminal lhs' then err ("Illegal lhs: " ^ lhs')
   274     else if const <> "" then xprod
   275     else if length (filter is_arg symbs') <> 1 then
   276       err "Copy production must have exactly one argument"
   277     else if exists is_term symbs' then xprod
   278     else XProd (lhs', map rem_pri symbs', "", chain_pri)
   279   end;
   280 
   281 
   282 (** datatype syn_ext **)
   283 
   284 datatype syn_ext =
   285   SynExt of {
   286     logtypes: string list,
   287     xprods: xprod list,
   288     consts: string list,
   289     prmodes: string list,
   290     parse_ast_translation: (string * (Ast.ast list -> Ast.ast)) list,
   291     parse_rules: (Ast.ast * Ast.ast) list,
   292     parse_translation: (string * (term list -> term)) list,
   293     print_translation: (string * (bool -> typ -> term list -> term)) list,
   294     print_rules: (Ast.ast * Ast.ast) list,
   295     print_ast_translation: (string * (Ast.ast list -> Ast.ast)) list,
   296     token_translation: (string * string * (string -> string * int)) list}
   297 
   298 
   299 (* syn_ext *)
   300 
   301 fun mk_syn_ext convert logtypes mfixes consts trfuns tokentrfuns rules =
   302   let
   303     val (parse_ast_translation, parse_translation, print_translation,
   304       print_ast_translation) = trfuns;
   305     val (parse_rules, print_rules) = rules;
   306     val logtypes' = logtypes \ "prop";
   307 
   308     val mfix_consts = distinct (map (fn (Mfix (_, _, c, _, _)) => c) mfixes);
   309     val xprods = map (mfix_to_xprod convert logtypes') mfixes;
   310   in
   311     SynExt {
   312       logtypes = logtypes',
   313       xprods = xprods,
   314       consts = filter Lexicon.is_xid (consts union mfix_consts),
   315       prmodes = distinct (map (fn (m, _, _) => m) tokentrfuns),
   316       parse_ast_translation = parse_ast_translation,
   317       parse_rules = parse_rules,
   318       parse_translation = parse_translation,
   319       print_translation = print_translation,
   320       print_rules = print_rules,
   321       print_ast_translation = print_ast_translation,
   322       token_translation = tokentrfuns}
   323   end;
   324 
   325 
   326 val syn_ext = mk_syn_ext true;
   327 
   328 fun syn_ext_logtypes logtypes =
   329   syn_ext logtypes [] [] ([], [], [], []) [] ([], []);
   330 
   331 fun syn_ext_const_names logtypes cs =
   332   syn_ext logtypes [] cs ([], [], [], []) [] ([], []);
   333 
   334 fun syn_ext_rules logtypes rules =
   335   syn_ext logtypes [] [] ([], [], [], []) [] rules;
   336 
   337 fun fix_tr' f _ _ ts = f ts;
   338 
   339 fun syn_ext_trfuns logtypes (atrs, trs, tr's, atr's) =
   340   syn_ext logtypes [] [] (atrs, trs, map (apsnd fix_tr') tr's, atr's) [] ([], []);
   341 
   342 fun syn_ext_trfunsT logtypes tr's =
   343   syn_ext logtypes [] [] ([], [], tr's, []) [] ([], []);
   344 
   345 fun syn_ext_tokentrfuns logtypes tokentrfuns =
   346   syn_ext logtypes [] [] ([], [], [], []) tokentrfuns ([], []);
   347 
   348 
   349 (* pure_ext *)
   350 
   351 val pure_ext = mk_syn_ext false []
   352   [Mfix ("_", spropT --> propT, "", [0], 0),
   353    Mfix ("_", logicT --> anyT, "", [0], 0),
   354    Mfix ("_", spropT --> anyT, "", [0], 0),
   355    Mfix ("'(_')", logicT --> logicT, "", [0], max_pri),
   356    Mfix ("'(_')", spropT --> spropT, "", [0], max_pri),
   357    Mfix ("_::_",  [logicT, typeT] ---> logicT, "_constrain", [4, 0], 3),
   358    Mfix ("_::_",  [spropT, typeT] ---> spropT, "_constrain", [4, 0], 3)]
   359   []
   360   ([], [], [], [])
   361   []
   362   ([], []);
   363 
   364 end;