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