src/Pure/Isar/args.ML
author haftmann
Mon Nov 29 11:38:59 2010 +0100 (2010-11-29 ago)
changeset 40803 3f66ea311d44
parent 40291 012ed4426fda
child 42360 da8817d01e7c
permissions -rw-r--r--
tuned
     1 (*  Title:      Pure/Isar/args.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Parsing with implicit value assignment.  Concrete argument syntax of
     5 attributes, methods etc.
     6 *)
     7 
     8 signature ARGS =
     9 sig
    10   type src
    11   val src: (string * Token.T list) * Position.T -> src
    12   val dest_src: src -> (string * Token.T list) * Position.T
    13   val pretty_src: Proof.context -> src -> Pretty.T
    14   val map_name: (string -> string) -> src -> src
    15   val morph_values: morphism -> src -> src
    16   val assignable: src -> src
    17   val closure: src -> src
    18   val context: Proof.context context_parser
    19   val theory: theory context_parser
    20   val $$$ : string -> string parser
    21   val add: string parser
    22   val del: string parser
    23   val colon: string parser
    24   val query: string parser
    25   val bang: string parser
    26   val query_colon: string parser
    27   val bang_colon: string parser
    28   val parens: ('a parser) -> 'a parser
    29   val bracks: ('a parser) -> 'a parser
    30   val mode: string -> bool context_parser
    31   val maybe: 'a parser -> 'a option parser
    32   val name_source: string parser
    33   val name_source_position: (Symbol_Pos.text * Position.T) parser
    34   val name: string parser
    35   val binding: binding parser
    36   val alt_name: string parser
    37   val symbol: string parser
    38   val liberal_name: string parser
    39   val var: indexname parser
    40   val internal_text: string parser
    41   val internal_typ: typ parser
    42   val internal_term: term parser
    43   val internal_fact: thm list parser
    44   val internal_attribute: (morphism -> attribute) parser
    45   val named_text: (string -> string) -> string parser
    46   val named_typ: (string -> typ) -> typ parser
    47   val named_term: (string -> term) -> term parser
    48   val named_fact: (string -> thm list) -> thm list parser
    49   val named_attribute: (string -> morphism -> attribute) -> (morphism -> attribute) parser
    50   val typ_abbrev: typ context_parser
    51   val typ: typ context_parser
    52   val term: term context_parser
    53   val term_abbrev: term context_parser
    54   val prop: term context_parser
    55   val type_name: bool -> string context_parser
    56   val const: bool -> string context_parser
    57   val const_proper: bool -> string context_parser
    58   val goal_spec: ((int -> tactic) -> tactic) context_parser
    59   val parse: Token.T list parser
    60   val parse1: (string -> bool) -> Token.T list parser
    61   val attribs: (string -> string) -> src list parser
    62   val opt_attribs: (string -> string) -> src list parser
    63   val thm_name: (string -> string) -> string -> (binding * src list) parser
    64   val opt_thm_name: (string -> string) -> string -> (binding * src list) parser
    65   val syntax: string -> 'a context_parser -> src -> Context.generic -> 'a * Context.generic
    66   val context_syntax: string -> 'a context_parser -> src -> Proof.context -> 'a * Proof.context
    67 end;
    68 
    69 structure Args: ARGS =
    70 struct
    71 
    72 (** datatype src **)
    73 
    74 datatype src = Src of (string * Token.T list) * Position.T;
    75 
    76 val src = Src;
    77 fun dest_src (Src src) = src;
    78 
    79 fun pretty_src ctxt src =
    80   let
    81     val prt_thm = Pretty.backquote o Display.pretty_thm ctxt;
    82     fun prt arg =
    83       (case Token.get_value arg of
    84         SOME (Token.Text s) => Pretty.str (quote s)
    85       | SOME (Token.Typ T) => Syntax.pretty_typ ctxt T
    86       | SOME (Token.Term t) => Syntax.pretty_term ctxt t
    87       | SOME (Token.Fact ths) => Pretty.enclose "(" ")" (Pretty.breaks (map prt_thm ths))
    88       | _ => Pretty.str (Token.unparse arg));
    89     val (s, args) = #1 (dest_src src);
    90   in Pretty.block (Pretty.breaks (Pretty.str s :: map prt args)) end;
    91 
    92 fun map_name f (Src ((s, args), pos)) = Src ((f s, args), pos);
    93 fun map_args f (Src ((s, args), pos)) = Src ((s, map f args), pos);
    94 
    95 
    96 (* values *)
    97 
    98 fun morph_values phi = map_args (Token.map_value
    99   (fn Token.Text s => Token.Text s
   100     | Token.Typ T => Token.Typ (Morphism.typ phi T)
   101     | Token.Term t => Token.Term (Morphism.term phi t)
   102     | Token.Fact ths => Token.Fact (Morphism.fact phi ths)
   103     | Token.Attribute att => Token.Attribute (Morphism.transform phi att)));
   104 
   105 val assignable = map_args Token.assignable;
   106 val closure = map_args Token.closure;
   107 
   108 
   109 
   110 (** argument scanners **)
   111 
   112 (* context *)
   113 
   114 fun context x = (Scan.state >> Context.proof_of) x;
   115 fun theory x = (Scan.state >> Context.theory_of) x;
   116 
   117 
   118 (* basic *)
   119 
   120 fun token atom = Scan.ahead Parse.not_eof --| atom;
   121 
   122 val ident = token
   123   (Parse.short_ident || Parse.long_ident || Parse.sym_ident || Parse.term_var ||
   124     Parse.type_ident || Parse.type_var || Parse.number);
   125 
   126 val string = token (Parse.string || Parse.verbatim);
   127 val alt_string = token Parse.alt_string;
   128 val symbolic = token Parse.keyword_ident_or_symbolic;
   129 
   130 fun $$$ x = (ident >> Token.content_of || Parse.keyword)
   131   :|-- (fn y => if x = y then Scan.succeed x else Scan.fail);
   132 
   133 
   134 val named = ident || string;
   135 
   136 val add = $$$ "add";
   137 val del = $$$ "del";
   138 val colon = $$$ ":";
   139 val query = $$$ "?";
   140 val bang = $$$ "!";
   141 val query_colon = $$$ "?" ^^ $$$ ":";
   142 val bang_colon = $$$ "!" ^^ $$$ ":";
   143 
   144 fun parens scan = $$$ "(" |-- scan --| $$$ ")";
   145 fun bracks scan = $$$ "[" |-- scan --| $$$ "]";
   146 fun mode s = Scan.lift (Scan.optional (parens ($$$ s) >> K true) false);
   147 fun maybe scan = $$$ "_" >> K NONE || scan >> SOME;
   148 
   149 val name_source = named >> Token.source_of;
   150 val name_source_position = named >> Token.source_position_of;
   151 
   152 val name = named >> Token.content_of;
   153 val binding = Parse.position name >> Binding.make;
   154 val alt_name = alt_string >> Token.content_of;
   155 val symbol = symbolic >> Token.content_of;
   156 val liberal_name = symbol || name;
   157 
   158 val var = (ident >> Token.content_of) :|-- (fn x =>
   159   (case Lexicon.read_variable x of SOME v => Scan.succeed v | NONE => Scan.fail));
   160 
   161 
   162 (* values *)
   163 
   164 fun value dest = Scan.some (fn arg =>
   165   (case Token.get_value arg of SOME v => (SOME (dest v) handle Match => NONE) | NONE => NONE));
   166 
   167 fun evaluate mk eval arg =
   168   let val x = eval arg in (Token.assign (SOME (mk x)) arg; x) end;
   169 
   170 val internal_text = value (fn Token.Text s => s);
   171 val internal_typ = value (fn Token.Typ T => T);
   172 val internal_term = value (fn Token.Term t => t);
   173 val internal_fact = value (fn Token.Fact ths => ths);
   174 val internal_attribute = value (fn Token.Attribute att => att);
   175 
   176 fun named_text intern = internal_text || named >> evaluate Token.Text (intern o Token.content_of);
   177 fun named_typ readT = internal_typ || named >> evaluate Token.Typ (readT o Token.source_of);
   178 fun named_term read = internal_term || named >> evaluate Token.Term (read o Token.source_of);
   179 fun named_fact get = internal_fact || named >> evaluate Token.Fact (get o Token.content_of) ||
   180   alt_string >> evaluate Token.Fact (get o Token.source_of);
   181 fun named_attribute att =
   182   internal_attribute || named >> evaluate Token.Attribute (att o Token.content_of);
   183 
   184 
   185 (* terms and types *)
   186 
   187 val typ_abbrev = Scan.peek (named_typ o ProofContext.read_typ_abbrev o Context.proof_of);
   188 val typ = Scan.peek (named_typ o Syntax.read_typ o Context.proof_of);
   189 val term = Scan.peek (named_term o Syntax.read_term o Context.proof_of);
   190 val term_abbrev = Scan.peek (named_term o ProofContext.read_term_abbrev o Context.proof_of);
   191 val prop = Scan.peek (named_term o Syntax.read_prop o Context.proof_of);
   192 
   193 
   194 (* type and constant names *)
   195 
   196 fun type_name strict =
   197   Scan.peek (fn ctxt => named_typ (ProofContext.read_type_name (Context.proof_of ctxt) strict))
   198   >> (fn Type (c, _) => c | TFree (a, _) => a | _ => "");
   199 
   200 fun const strict =
   201   Scan.peek (fn ctxt => named_term (ProofContext.read_const (Context.proof_of ctxt) strict dummyT))
   202   >> (fn Const (c, _) => c | Free (x, _) => x | _ => "");
   203 
   204 fun const_proper strict =
   205   Scan.peek (fn ctxt => named_term (ProofContext.read_const_proper (Context.proof_of ctxt) strict))
   206   >> (fn Const (c, _) => c | _ => "");
   207 
   208 
   209 (* improper method arguments *)
   210 
   211 val from_to =
   212   Parse.nat -- ($$$ "-" |-- Parse.nat) >> (fn (i, j) => fn tac => Seq.INTERVAL tac i j) ||
   213   Parse.nat --| $$$ "-" >> (fn i => fn tac => fn st => Seq.INTERVAL tac i (Thm.nprems_of st) st) ||
   214   Parse.nat >> (fn i => fn tac => tac i) ||
   215   $$$ "!" >> K ALLGOALS;
   216 
   217 val goal = $$$ "[" |-- Parse.!!! (from_to --| $$$ "]");
   218 fun goal_spec x = Scan.lift (Scan.optional goal (fn tac => tac 1)) x;
   219 
   220 
   221 (* arguments within outer syntax *)
   222 
   223 fun parse_args is_symid =
   224   let
   225     val keyword_symid = token (Parse.keyword_with is_symid);
   226     fun atom blk = Parse.group "argument"
   227       (ident || keyword_symid || string || alt_string || token Parse.float_number ||
   228         (if blk then token (Parse.$$$ ",") else Scan.fail));
   229 
   230     fun args blk x = Scan.optional (args1 blk) [] x
   231     and args1 blk x =
   232       ((Scan.repeat1
   233         (Scan.repeat1 (atom blk) ||
   234           argsp "(" ")" ||
   235           argsp "[" "]")) >> flat) x
   236     and argsp l r x =
   237       (token (Parse.$$$ l) ::: Parse.!!! (args true @@@ (token (Parse.$$$ r) >> single))) x;
   238   in (args, args1) end;
   239 
   240 val parse = #1 (parse_args Token.ident_or_symbolic) false;
   241 fun parse1 is_symid = #2 (parse_args is_symid) false;
   242 
   243 
   244 (* attributes *)
   245 
   246 fun attribs intern =
   247   let
   248     val attrib_name = internal_text || (symbolic || named)
   249       >> evaluate Token.Text (intern o Token.content_of);
   250     val attrib = Parse.position (attrib_name -- Parse.!!! parse) >> src;
   251   in $$$ "[" |-- Parse.!!! (Parse.list attrib --| $$$ "]") end;
   252 
   253 fun opt_attribs intern = Scan.optional (attribs intern) [];
   254 
   255 
   256 (* theorem specifications *)
   257 
   258 fun thm_name intern s = binding -- opt_attribs intern --| $$$ s;
   259 
   260 fun opt_thm_name intern s =
   261   Scan.optional
   262     ((binding -- opt_attribs intern || attribs intern >> pair Binding.empty) --| $$$ s)
   263     (Binding.empty, []);
   264 
   265 
   266 
   267 (** syntax wrapper **)
   268 
   269 fun syntax kind scan (Src ((s, args), pos)) st =
   270   (case Scan.error (Scan.finite' Token.stopper (Scan.option scan)) (st, args) of
   271     (SOME x, (st', [])) => (x, st')
   272   | (_, (_, args')) =>
   273       error (kind ^ " " ^ quote s ^ Position.str_of pos ^ ": bad arguments\n  " ^
   274         space_implode " " (map Token.unparse args')));
   275 
   276 fun context_syntax kind scan src = apsnd Context.the_proof o syntax kind scan src o Context.Proof;
   277 
   278 end;