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