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