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