src/Pure/Syntax/syn_ext.ML
author wenzelm
Wed, 29 Jun 2005 15:13:40 +0200
changeset 16610 58bf09036a6d
parent 15973 5fd94d84470f
child 18566 20dd2f7dca4b
permissions -rw-r--r--
accomodate advanced trfuns;
     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 dddot_indexname: indexname
    11   val constrainC: string
    12   val typeT: typ
    13   val max_pri: int
    14   val stamp_trfun: stamp -> string * 'a -> string * ('a * stamp)
    15   val mk_trfun: string * 'a -> string * ('a * stamp)
    16   val eq_trfun: ('a * stamp) * ('a * stamp) -> bool
    17   val tokentrans_mode: string -> (string * (string -> string * real)) list ->
    18     (string * string * (string -> string * real)) list
    19   val standard_token_classes: string list
    20 end;
    21 
    22 signature SYN_EXT =
    23 sig
    24   include SYN_EXT0
    25   val logic: string
    26   val args: string
    27   val cargs: string
    28   val any: string
    29   val sprop: string
    30   val typ_to_nonterm: typ -> string
    31   datatype xsymb =
    32     Delim of string |
    33     Argument of string * int |
    34     Space of string |
    35     Bg of int | Brk of int | En
    36   datatype xprod = XProd of string * xsymb list * string * int
    37   val chain_pri: int
    38   val delims_of: xprod list -> string list list
    39   datatype mfix = Mfix of string * typ * string * int list * int
    40   datatype syn_ext =
    41     SynExt of {
    42       xprods: xprod list,
    43       consts: string list,
    44       prmodes: string list,
    45       parse_ast_translation: (string * ((theory -> Ast.ast list -> Ast.ast) * stamp)) list,
    46       parse_rules: (Ast.ast * Ast.ast) list,
    47       parse_translation: (string * ((theory -> term list -> term) * stamp)) list,
    48       print_translation: (string * ((theory -> bool -> typ -> term list -> term) * stamp)) list,
    49       print_rules: (Ast.ast * Ast.ast) list,
    50       print_ast_translation: (string * ((theory -> Ast.ast list -> Ast.ast) * stamp)) list,
    51       token_translation: (string * string * (string -> string * real)) list}
    52   val mfix_args: string -> int
    53   val escape_mfix: string -> string
    54   val syn_ext': bool -> (string -> bool) -> mfix list ->
    55     string list -> (string * ((theory -> Ast.ast list -> Ast.ast) * stamp)) list *
    56     (string * ((theory -> term list -> term) * stamp)) list *
    57     (string * ((theory -> bool -> typ -> term list -> term) * stamp)) list *
    58     (string * ((theory -> Ast.ast list -> Ast.ast) * stamp)) list
    59     -> (string * string * (string -> string * real)) list
    60     -> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
    61   val syn_ext: mfix list -> string list ->
    62     (string * ((theory -> Ast.ast list -> Ast.ast) * stamp)) list *
    63     (string * ((theory -> term list -> term) * stamp)) list *
    64     (string * ((theory -> bool -> typ -> term list -> term) * stamp)) list *
    65     (string * ((theory -> Ast.ast list -> Ast.ast) * stamp)) list
    66     -> (string * string * (string -> string * real)) list
    67     -> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
    68   val syn_ext_const_names: string list -> syn_ext
    69   val syn_ext_rules: (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
    70   val syn_ext_trfuns:
    71     (string * ((Ast.ast list -> Ast.ast) * stamp)) list *
    72     (string * ((term list -> term) * stamp)) list *
    73     (string * ((bool -> typ -> term list -> term) * stamp)) list *
    74     (string * ((Ast.ast list -> Ast.ast) * stamp)) list -> syn_ext
    75   val syn_ext_advanced_trfuns:
    76     (string * ((theory -> Ast.ast list -> Ast.ast) * stamp)) list *
    77     (string * ((theory -> term list -> term) * stamp)) list *
    78     (string * ((theory -> bool -> typ -> term list -> term) * stamp)) list *
    79     (string * ((theory -> Ast.ast list -> Ast.ast) * stamp)) list -> syn_ext
    80   val syn_ext_tokentrfuns: (string * string * (string -> string * real)) list -> syn_ext
    81   val standard_token_markers: string list
    82   val pure_ext: syn_ext
    83 end;
    84 
    85 structure SynExt: SYN_EXT =
    86 struct
    87 
    88 
    89 (** misc definitions **)
    90 
    91 val dddot_indexname = ("dddot", 0);
    92 val constrainC = "_constrain";
    93 
    94 
    95 (* syntactic categories *)
    96 
    97 val logic = "logic";
    98 val logicT = Type (logic, []);
    99 
   100 val args = "args";
   101 val cargs = "cargs";
   102 
   103 val typeT = Type ("type", []);
   104 
   105 val sprop = "#prop";
   106 val spropT = Type (sprop, []);
   107 
   108 val any = "any";
   109 val anyT = Type (any, []);
   110 
   111 
   112 
   113 (** datatype xprod **)
   114 
   115 (*Delim s: delimiter s
   116   Argument (s, p): nonterminal s requiring priority >= p, or valued token
   117   Space s: some white space for printing
   118   Bg, Brk, En: blocks and breaks for pretty printing*)
   119 
   120 datatype xsymb =
   121   Delim of string |
   122   Argument of string * int |
   123   Space of string |
   124   Bg of int | Brk of int | En;
   125 
   126 fun is_delim (Delim _) = true
   127   | is_delim _ = false;
   128 
   129 fun is_terminal (Delim _) = true
   130   | is_terminal (Argument (s, _)) = Lexicon.is_terminal s
   131   | is_terminal _ = false;
   132 
   133 fun is_argument (Argument _) = true
   134   | is_argument _ = false;
   135 
   136 fun is_index (Argument ("index", _)) = true
   137   | is_index _ = false;
   138 
   139 val index = Argument ("index", 1000);
   140 
   141 
   142 (*XProd (lhs, syms, c, p):
   143     lhs: name of nonterminal on the lhs of the production
   144     syms: list of symbols on the rhs of the production
   145     c: head of parse tree
   146     p: priority of this production*)
   147 
   148 datatype xprod = XProd of string * xsymb list * string * int;
   149 
   150 val max_pri = 1000;   (*maximum legal priority*)
   151 val chain_pri = ~1;   (*dummy for chain productions*)
   152 
   153 
   154 (* delims_of *)
   155 
   156 fun delims_of xprods =
   157   let
   158     fun del_of (Delim s) = SOME s
   159       | del_of _ = NONE;
   160 
   161     fun dels_of (XProd (_, xsymbs, _, _)) =
   162       List.mapPartial del_of xsymbs;
   163   in
   164     map Symbol.explode (distinct (List.concat (map dels_of xprods)))
   165   end;
   166 
   167 
   168 
   169 (** datatype mfix **)
   170 
   171 (*Mfix (sy, ty, c, ps, p):
   172     sy: rhs of production as symbolic string
   173     ty: type description of production
   174     c: head of parse tree
   175     ps: priorities of arguments in sy
   176     p: priority of production*)
   177 
   178 datatype mfix = Mfix of string * typ * string * int list * int;
   179 
   180 fun err_in_mfix msg (Mfix (sy, _, const, _, _)) =
   181   error ((if msg = "" then "" else msg ^ "\n") ^
   182     "in mixfix annotation " ^ quote sy ^ " for " ^ quote const);
   183 
   184 
   185 (* typ_to_nonterm *)
   186 
   187 fun typ_to_nt _ (Type (c, _)) = c
   188   | typ_to_nt default _ = default;
   189 
   190 (*get nonterminal for rhs*)
   191 val typ_to_nonterm = typ_to_nt any;
   192 
   193 (*get nonterminal for lhs*)
   194 val typ_to_nonterm1 = typ_to_nt logic;
   195 
   196 
   197 (* read_mixfix *)
   198 
   199 local
   200   fun is_meta c = c mem ["(", ")", "/", "_", "\\<index>"];
   201 
   202   val scan_delim_char =
   203     $$ "'" |-- Scan.one ((not o Symbol.is_blank) andf Symbol.not_eof) ||
   204     Scan.one ((not o is_meta) andf (not o Symbol.is_blank) andf Symbol.not_eof);
   205 
   206   fun read_int ["0", "0"] = ~1
   207     | read_int cs = #1 (Library.read_int cs);
   208 
   209   val scan_sym =
   210     $$ "_" >> K (Argument ("", 0)) ||
   211     $$ "\\<index>" >> K index ||
   212     $$ "(" |-- Scan.any Symbol.is_digit >> (Bg o read_int) ||
   213     $$ ")" >> K En ||
   214     $$ "/" -- $$ "/" >> K (Brk ~1) ||
   215     $$ "/" |-- Scan.any Symbol.is_blank >> (Brk o length) ||
   216     Scan.any1 Symbol.is_blank >> (Space o implode) ||
   217     Scan.repeat1 scan_delim_char >> (Delim o implode);
   218 
   219   val scan_symb =
   220     scan_sym >> SOME ||
   221     $$ "'" -- Scan.one Symbol.is_blank >> K NONE;
   222 
   223   val scan_symbs = Scan.repeat scan_symb --| Scan.ahead (Scan.one (not_equal "'"));
   224   val read_symbs = List.mapPartial I o the o Scan.read Symbol.stopper scan_symbs;
   225 
   226   fun unique_index xsymbs =
   227     if length (List.filter is_index xsymbs) <= 1 then xsymbs
   228     else error "Duplicate index arguments (\\<index>)";
   229 in
   230   val read_mixfix = unique_index o read_symbs o Symbol.explode;
   231   val mfix_args = length o List.filter is_argument o read_mixfix;
   232   val escape_mfix = implode o map (fn s => if is_meta s then "'" ^ s else s) o Symbol.explode;
   233 end;
   234 
   235 
   236 (* mfix_to_xprod *)
   237 
   238 fun mfix_to_xprod convert is_logtype (mfix as Mfix (sy, typ, const, pris, pri)) =
   239   let
   240     fun check_pri p =
   241       if p >= 0 andalso p <= max_pri then ()
   242       else err_in_mfix ("Precedence out of range: " ^ string_of_int p) mfix;
   243 
   244     fun blocks_ok [] 0 = true
   245       | blocks_ok [] _ = false
   246       | blocks_ok (Bg _ :: syms) n = blocks_ok syms (n + 1)
   247       | blocks_ok (En :: _) 0 = false
   248       | blocks_ok (En :: syms) n = blocks_ok syms (n - 1)
   249       | blocks_ok (_ :: syms) n = blocks_ok syms n;
   250 
   251     fun check_blocks syms =
   252       if blocks_ok syms 0 then ()
   253       else err_in_mfix "Unbalanced block parentheses" mfix;
   254 
   255 
   256     val cons_fst = apfst o cons;
   257 
   258     fun add_args [] ty [] = ([], typ_to_nonterm1 ty)
   259       | add_args [] _ _ = err_in_mfix "Too many precedences" mfix
   260       | add_args ((arg as Argument ("index", _)) :: syms) ty ps =
   261           cons_fst arg (add_args syms ty ps)
   262       | add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) [] =
   263           cons_fst (Argument (typ_to_nonterm ty, 0)) (add_args syms tys [])
   264       | add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) (p :: ps) =
   265           cons_fst (Argument (typ_to_nonterm ty, p)) (add_args syms tys ps)
   266       | add_args (Argument _ :: _) _ _ =
   267           err_in_mfix "More arguments than in corresponding type" mfix
   268       | add_args (sym :: syms) ty ps = cons_fst sym (add_args syms ty ps);
   269 
   270     fun rem_pri (Argument (s, _)) = Argument (s, chain_pri)
   271       | rem_pri sym = sym;
   272 
   273     fun logify_types copy_prod (a as (Argument (s, p))) =
   274           if s <> "prop" andalso is_logtype s then Argument (logic, p) else a
   275       | logify_types _ a = a;
   276 
   277 
   278     val raw_symbs = read_mixfix sy handle ERROR => err_in_mfix "" mfix;
   279     val args = List.filter (fn Argument _ => true | _ => false) raw_symbs;
   280     val (const', typ', parse_rules) =
   281       if not (exists is_index args) then (const, typ, [])
   282       else
   283         let
   284           val indexed_const = if const <> "" then "_indexed_" ^ const
   285             else err_in_mfix "Missing constant name for indexed syntax" mfix;
   286           val rangeT = Term.range_type typ handle Match =>
   287             err_in_mfix "Missing structure argument for indexed syntax" mfix;
   288 
   289           val xs = map Ast.Variable (Term.invent_names [] "xa" (length args - 1));
   290           val (xs1, xs2) = Library.splitAt (Library.find_index is_index args, xs);
   291           val i = Ast.Variable "i";
   292           val lhs = Ast.mk_appl (Ast.Constant indexed_const)
   293             (xs1 @ [Ast.mk_appl (Ast.Constant "_index") [i]] @ xs2);
   294           val rhs = Ast.mk_appl (Ast.Constant const) (i :: xs);
   295         in (indexed_const, rangeT, [(lhs, rhs)]) end;
   296 
   297     val (symbs, lhs) = add_args raw_symbs typ' pris;
   298 
   299     val copy_prod =
   300       lhs mem ["prop", "logic"]
   301         andalso const <> ""
   302         andalso not (null symbs)
   303         andalso not (exists is_delim symbs);
   304     val lhs' =
   305       if convert andalso not copy_prod then
   306        (if lhs = "prop" then sprop else if is_logtype lhs then logic else lhs)
   307       else lhs;
   308     val symbs' = map (logify_types copy_prod) symbs;
   309     val xprod = XProd (lhs', symbs', const', pri);
   310 
   311     val _ = (List.app check_pri pris; check_pri pri; check_blocks symbs');
   312     val xprod' =
   313       if Lexicon.is_terminal lhs' then err_in_mfix ("Illegal lhs: " ^ lhs') mfix
   314       else if const <> "" then xprod
   315       else if length (List.filter is_argument symbs') <> 1 then
   316         err_in_mfix "Copy production must have exactly one argument" mfix
   317       else if exists is_terminal symbs' then xprod
   318       else XProd (lhs', map rem_pri symbs', "", chain_pri);
   319 
   320   in (xprod', parse_rules) end;
   321 
   322 
   323 
   324 (** datatype syn_ext **)
   325 
   326 datatype syn_ext =
   327   SynExt of {
   328     xprods: xprod list,
   329     consts: string list,
   330     prmodes: string list,
   331     parse_ast_translation: (string * ((theory -> Ast.ast list -> Ast.ast) * stamp)) list,
   332     parse_rules: (Ast.ast * Ast.ast) list,
   333     parse_translation: (string * ((theory -> term list -> term) * stamp)) list,
   334     print_translation: (string * ((theory -> bool -> typ -> term list -> term) * stamp)) list,
   335     print_rules: (Ast.ast * Ast.ast) list,
   336     print_ast_translation: (string * ((theory -> Ast.ast list -> Ast.ast) * stamp)) list,
   337     token_translation: (string * string * (string -> string * real)) list};
   338 
   339 
   340 (* syn_ext *)
   341 
   342 fun syn_ext' convert is_logtype mfixes consts trfuns tokentrfuns (parse_rules, print_rules) =
   343   let
   344     val (parse_ast_translation, parse_translation, print_translation,
   345       print_ast_translation) = trfuns;
   346 
   347     val (xprods, parse_rules') = map (mfix_to_xprod convert is_logtype) mfixes
   348       |> split_list |> apsnd (rev o List.concat);
   349     val mfix_consts = distinct (map (fn Mfix x => #3 x) mfixes @ map (fn XProd x => #3 x) xprods);
   350   in
   351     SynExt {
   352       xprods = xprods,
   353       consts = consts union_string mfix_consts,
   354       prmodes = distinct (map (fn (m, _, _) => m) tokentrfuns),
   355       parse_ast_translation = parse_ast_translation,
   356       parse_rules = parse_rules' @ parse_rules,
   357       parse_translation = parse_translation,
   358       print_translation = print_translation,
   359       print_rules = map swap parse_rules' @ print_rules,
   360       print_ast_translation = print_ast_translation,
   361       token_translation = tokentrfuns}
   362   end;
   363 
   364 
   365 val syn_ext = syn_ext' true (K false);
   366 
   367 fun syn_ext_const_names cs = syn_ext [] cs ([], [], [], []) [] ([], []);
   368 fun syn_ext_rules rules = syn_ext [] [] ([], [], [], []) [] rules;
   369 fun syn_ext_advanced_trfuns trfuns = syn_ext [] [] trfuns [] ([], []);
   370 fun syn_ext_tokentrfuns trfuns = syn_ext [] [] ([], [], [], []) trfuns ([], []);
   371 
   372 fun syn_ext_trfuns (atrs, trs, tr's, atr's) =
   373   let fun simple (name, (f, s)) = (name, (K f, s)) in
   374     syn_ext_advanced_trfuns (map simple atrs, map simple trs, map simple tr's, map simple atr's)
   375   end;
   376 
   377 fun stamp_trfun s (c, f) = (c, (f, s));
   378 fun mk_trfun tr = stamp_trfun (stamp ()) tr;
   379 fun eq_trfun ((_, s1:stamp), (_, s2)) = s1 = s2;
   380 
   381 
   382 (* token translations *)
   383 
   384 fun tokentrans_mode m trs = map (fn (s, f) => (m, s, f)) trs;
   385 
   386 val standard_token_classes =
   387   ["class", "tfree", "tvar", "free", "bound", "var", "num", "xnum", "xstr"];
   388 
   389 val standard_token_markers = map (fn s => "_" ^ s) standard_token_classes;
   390 
   391 
   392 (* pure_ext *)
   393 
   394 val pure_ext = syn_ext' false (K false)
   395   [Mfix ("_", spropT --> propT, "", [0], 0),
   396    Mfix ("_", logicT --> anyT, "", [0], 0),
   397    Mfix ("_", spropT --> anyT, "", [0], 0),
   398    Mfix ("'(_')", logicT --> logicT, "", [0], max_pri),
   399    Mfix ("'(_')", spropT --> spropT, "", [0], max_pri),
   400    Mfix ("_::_",  [logicT, typeT] ---> logicT, "_constrain", [4, 0], 3),
   401    Mfix ("_::_",  [spropT, typeT] ---> spropT, "_constrain", [4, 0], 3)]
   402   []
   403   ([], [], [], [])
   404   []
   405   ([], []);
   406 
   407 end;