src/Pure/Isar/proof_context.ML
changeset 12093 1b890f1e0b4d
parent 12086 742b9c3740dc
child 12100 bb10ac677955
equal deleted inserted replaced
12092:d1896409ff13 12093:1b890f1e0b4d
    11   type context
    11   type context
    12   type exporter
    12   type exporter
    13   exception CONTEXT of string * context
    13   exception CONTEXT of string * context
    14   val theory_of: context -> theory
    14   val theory_of: context -> theory
    15   val sign_of: context -> Sign.sg
    15   val sign_of: context -> Sign.sg
    16   val syntax_of: context -> Syntax.syntax * string list * string list  (* FIXME *)
    16   val syntax_of: context -> Syntax.syntax * string list * string list
    17   val fixed_names_of: context -> string list
    17   val fixed_names_of: context -> string list
    18   val assumptions_of: context -> (cterm list * exporter) list
    18   val assumptions_of: context -> (cterm list * exporter) list
    19   val prems_of: context -> thm list
    19   val prems_of: context -> thm list
    20   val print_proof_data: theory -> unit
    20   val print_proof_data: theory -> unit
    21   val init: theory -> context
    21   val init: theory -> context
   272       Symtab.empty, [], (Vartab.empty, Vartab.empty, ([], Symtab.empty)))
   272       Symtab.empty, [], (Vartab.empty, Vartab.empty, ([], Symtab.empty)))
   273   end;
   273   end;
   274 
   274 
   275 
   275 
   276 
   276 
       
   277 
       
   278 (** local syntax **)
       
   279 
       
   280 val fixedN = "\\<^fixed>";
       
   281 val structN = "\\<^struct>";
       
   282 
       
   283 
       
   284 (* add_syntax and syn_of *)
       
   285 
       
   286 local
       
   287 
       
   288 val aT = TFree ("'a", logicS);
       
   289 
       
   290 fun mixfix x None mx = (fixedN ^ x, replicate (Syntax.mixfix_args mx) aT ---> aT, mx)
       
   291   | mixfix x (Some T) mx = (fixedN ^ x, T, mx);
       
   292 
       
   293 fun prep_mixfix (_, _, None) = None
       
   294   | prep_mixfix (x, opt_T, Some mx) = Some (mixfix x opt_T mx);
       
   295 
       
   296 fun prep_mixfix' (_, _, None) = None
       
   297   | prep_mixfix' (x, _, Some Syntax.NoSyn) = None
       
   298   | prep_mixfix' (x, opt_T, _) = Some (x, mixfix x opt_T (Syntax.Delimfix x));
       
   299 
       
   300 fun fixed_tr x = (fixedN ^ x, curry Term.list_comb (Syntax.free x));
       
   301 
       
   302 fun index_tr (Const ("_structs", _) $ Const ("_struct", _) $ idx) = index_tr idx + 1
       
   303   | index_tr (Const ("_struct", _)) = 0
       
   304   | index_tr t = raise TERM ("index_tr", [t]);
       
   305 
       
   306 fun struct_app_tr structs [idx, f] =
       
   307       let val i = index_tr idx in
       
   308         if i < length structs then f $ Syntax.free (Library.nth_elem (i, structs))
       
   309         else error ("Illegal reference to structure #" ^ string_of_int (i + 1))
       
   310       end
       
   311   | struct_app_tr _ ts = raise TERM ("struct_app_tr", ts);
       
   312 
       
   313 fun prep_struct (x, _, None) = Some x
       
   314   | prep_struct _ = None;
       
   315 
       
   316 in
       
   317 
       
   318 fun add_syntax decls =
       
   319   map_context (fn (thy, (syn, structs, mixfixed), data, asms, binds, thms, cases, defs) =>
       
   320     let
       
   321       val structs' = structs @ mapfilter prep_struct decls;
       
   322       val mxs = mapfilter prep_mixfix decls;
       
   323       val (fixed, mxs_output) = Library.split_list (mapfilter prep_mixfix' decls);
       
   324       val trs = map fixed_tr fixed;
       
   325       val syn' = syn
       
   326         |> Syntax.extend_const_gram ("", false) mxs_output
       
   327         |> Syntax.extend_const_gram ("", true) mxs
       
   328         |> Syntax.extend_trfuns ([], trs, [], []);
       
   329     in (thy, (syn', structs', fixed @ mixfixed), data, asms, binds, thms, cases, defs) end)
       
   330 
       
   331 fun syn_of (Context {syntax = (syn, structs, _), ...}) =
       
   332   syn |> Syntax.extend_trfuns ([], [("_struct_app", struct_app_tr structs)], [], []);
       
   333 
       
   334 
       
   335 end;
       
   336 
       
   337 
       
   338 (* context_tr' *)
       
   339 
       
   340 fun context_tr' (Context {syntax = (_, structs, mixfixed), ...}) =
       
   341   let
       
   342     fun structs_tr' 0 t = t
       
   343       | structs_tr' i t = Syntax.const "_structs" $ Syntax.const "_struct" $ structs_tr' (i - 1) t;
       
   344 
       
   345     fun tr' (f $ (t as Free (x, T))) =
       
   346           let val i = Library.find_index (equal x) structs in
       
   347             if i < 0 then tr' f $ tr' t
       
   348             else Syntax.const "_struct_app" $ structs_tr' i (Syntax.const "_struct") $ f
       
   349           end
       
   350       | tr' (t as Free (x, T)) = if x mem_string mixfixed then Const (fixedN ^ x, T) else t
       
   351       | tr' (t $ u) = tr' t $ tr' u
       
   352       | tr' (Abs (x, T, t)) = Abs (x, T, tr' t)
       
   353       | tr' a = a;
       
   354   in tr' end;
       
   355 
       
   356 
       
   357 
   277 (** default sorts and types **)
   358 (** default sorts and types **)
   278 
   359 
   279 fun def_sort (Context {defs = (_, sorts, _), ...}) xi = Vartab.lookup (sorts, xi);
   360 fun def_sort (Context {defs = (_, sorts, _), ...}) xi = Vartab.lookup (sorts, xi);
   280 
   361 
   281 fun def_type (Context {binds, defs = (types, _, _), ...}) is_pat xi =
   362 fun def_type (Context {binds, defs = (types, _, _), ...}) is_pat xi =
   292 (** prepare types **)
   373 (** prepare types **)
   293 
   374 
   294 local
   375 local
   295 
   376 
   296 fun read_typ_aux read ctxt s =
   377 fun read_typ_aux read ctxt s =
   297   transform_error (read (#1 (syntax_of ctxt)) (sign_of ctxt, def_sort ctxt)) s
   378   transform_error (read (syn_of ctxt) (sign_of ctxt, def_sort ctxt)) s
   298     handle ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt);
   379     handle ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt);
   299 
   380 
   300 fun cert_typ_aux cert ctxt raw_T =
   381 fun cert_typ_aux cert ctxt raw_T =
   301   cert (sign_of ctxt) raw_T
   382   cert (sign_of ctxt) raw_T
   302     handle TYPE (msg, _, _) => raise CONTEXT (msg, ctxt);
   383     handle TYPE (msg, _, _) => raise CONTEXT (msg, ctxt);
   434 (* read terms *)
   515 (* read terms *)
   435 
   516 
   436 local
   517 local
   437 
   518 
   438 fun gen_read read app is_pat schematic (ctxt as Context {defs = (_, _, (used, _)), ...}) s =
   519 fun gen_read read app is_pat schematic (ctxt as Context {defs = (_, _, (used, _)), ...}) s =
   439   (transform_error (read (#1 (syntax_of ctxt))
   520   (transform_error (read (syn_of ctxt)
   440       (sign_of ctxt) (def_type ctxt is_pat, def_sort ctxt, used)) s
   521       (sign_of ctxt) (def_type ctxt is_pat, def_sort ctxt, used)) s
   441     handle TERM (msg, _) => raise CONTEXT (msg, ctxt)
   522     handle TERM (msg, _) => raise CONTEXT (msg, ctxt)
   442     | ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt))
   523     | ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt))
   443   |> app (intern_skolem ctxt)
   524   |> app (intern_skolem ctxt)
   444   |> app (if is_pat then I else norm_term ctxt schematic)
   525   |> app (if is_pat then I else norm_term ctxt schematic)
   527 
   608 
   528 end;
   609 end;
   529 
   610 
   530 
   611 
   531 
   612 
   532 (** local syntax **)
       
   533 
       
   534 val fixedN = "\\<^fixed>";
       
   535 val structN = "\\<^struct>";
       
   536 
       
   537 
       
   538 (* add_syntax *)
       
   539 
       
   540 local
       
   541 
       
   542 val aT = TFree ("'a", logicS);
       
   543 
       
   544 fun mixfix x None mx = (fixedN ^ x, replicate (Syntax.mixfix_args mx) aT ---> aT, mx)
       
   545   | mixfix x (Some T) mx = (fixedN ^ x, T, mx);
       
   546 
       
   547 fun prep_mixfix (_, _, None) = None
       
   548   | prep_mixfix (x, opt_T, Some mx) = Some (mixfix x opt_T mx);
       
   549 
       
   550 fun prep_mixfix' (_, _, None) = None
       
   551   | prep_mixfix' (x, _, Some Syntax.NoSyn) = None
       
   552   | prep_mixfix' (x, opt_T, _) = Some (x, mixfix x opt_T (Syntax.Delimfix x));
       
   553 
       
   554 fun fixed_tr x = (fixedN ^ x, curry Term.list_comb (Syntax.free x));
       
   555 
       
   556 fun prep_struct (x, _, None) = Some x
       
   557   | prep_struct _ = None;
       
   558 
       
   559 in
       
   560 
       
   561 fun add_syntax decls =
       
   562   map_context (fn (thy, (syn, structs, mixfixed), data, asms, binds, thms, cases, defs) =>
       
   563     let
       
   564       val structs' = mapfilter prep_struct decls @ structs;
       
   565       val mxs = mapfilter prep_mixfix decls;
       
   566       val (fixed, mxs_output) = Library.split_list (mapfilter prep_mixfix' decls);
       
   567       val trs = map fixed_tr fixed;
       
   568       val syn' = syn
       
   569         |> Syntax.extend_const_gram ("", false) mxs_output
       
   570         |> Syntax.extend_const_gram ("", true) mxs
       
   571         |> Syntax.extend_trfuns ([], trs, [], []);
       
   572     in (thy, (syn', structs', fixed @ mixfixed), data, asms, binds, thms, cases, defs) end)
       
   573 
       
   574 end;
       
   575 
       
   576 
       
   577 (* annotate_term *)  (* FIXME structs *)
       
   578 
       
   579 fun annotate_term ctxt =
       
   580   let
       
   581     val (_, structs, mixfixed) = syntax_of ctxt;
       
   582     fun annotate (t as Free (x, T)) = if x mem_string mixfixed then Const (fixedN ^ x, T) else t
       
   583       | annotate (t $ u) = annotate t $ annotate u
       
   584       | annotate (Abs (x, T, t)) = Abs (x, T, annotate t)
       
   585       | annotate a = a;
       
   586   in annotate end;
       
   587 
       
   588 
       
   589 
       
   590 (** pretty printing **)
   613 (** pretty printing **)
   591 
   614 
   592 fun pretty_term ctxt =
   615 fun pretty_term ctxt = Sign.pretty_term' (syn_of ctxt) (sign_of ctxt) o context_tr' ctxt;
   593   Sign.pretty_term' (#1 (syntax_of ctxt)) (sign_of ctxt) o annotate_term ctxt;
       
   594 
       
   595 val pretty_typ = Sign.pretty_typ o sign_of;
   616 val pretty_typ = Sign.pretty_typ o sign_of;
   596 val pretty_sort = Sign.pretty_sort o sign_of;
   617 val pretty_sort = Sign.pretty_sort o sign_of;
   597 
   618 
   598 val string_of_term = Pretty.string_of oo pretty_term;
   619 val string_of_term = Pretty.string_of oo pretty_term;
   599 
   620 
   951       err "Arguments of lhs must be distinct free or fixed variables");
   972       err "Arguments of lhs must be distinct free or fixed variables");
   952     conditional (f mem Term.term_frees rhs) (fn () =>
   973     conditional (f mem Term.term_frees rhs) (fn () =>
   953       err "Element to be defined occurs on rhs");
   974       err "Element to be defined occurs on rhs");
   954     conditional (not (null extra_frees)) (fn () =>
   975     conditional (not (null extra_frees)) (fn () =>
   955       err ("Extra free variables on rhs: " ^ commas_quote (map (#1 o dest_Free) extra_frees)));
   976       err ("Extra free variables on rhs: " ^ commas_quote (map (#1 o dest_Free) extra_frees)));
   956     (* FIXME check for extra type variables on rhs *)
       
   957     (c, Term.list_all_free (mapfilter (try Term.dest_Free) xs, eq))
   977     (c, Term.list_all_free (mapfilter (try Term.dest_Free) xs, eq))
   958   end;
   978   end;
   959 
   979 
   960 fun head_of_def cprop =
   980 fun head_of_def cprop =
   961   #1 (Term.strip_comb (#1 (Logic.dest_equals (Term.strip_all_body (Thm.term_of cprop)))))
   981   #1 (Term.strip_comb (#1 (Logic.dest_equals (Term.strip_all_body (Thm.term_of cprop)))))
  1133   end;
  1153   end;
  1134 
  1154 
  1135 
  1155 
  1136 (* local syntax *)
  1156 (* local syntax *)
  1137 
  1157 
  1138 val print_syntax = Syntax.print_syntax o #1 o syntax_of;
  1158 val print_syntax = Syntax.print_syntax o syn_of;
  1139 
  1159 
  1140 
  1160 
  1141 (* term bindings *)
  1161 (* term bindings *)
  1142 
  1162 
  1143 val smash_option = fn (_, None) => None | (xi, Some b) => Some (xi, b);
  1163 val smash_option = fn (_, None) => None | (xi, Some b) => Some (xi, b);
  1206 
  1226 
  1207 fun pretty_asms ctxt =
  1227 fun pretty_asms ctxt =
  1208   let
  1228   let
  1209     val prt_term = pretty_term ctxt;
  1229     val prt_term = pretty_term ctxt;
  1210 
  1230 
       
  1231     (*structures*)
       
  1232     val (_, structs, _) = syntax_of ctxt;
       
  1233     val prt_structs = if null structs then []
       
  1234       else [Pretty.block (Pretty.str "structures:" :: Pretty.brk 1 ::
       
  1235         Pretty.commas (map Pretty.str structs))];
       
  1236 
  1211     (*fixes*)
  1237     (*fixes*)
  1212     fun prt_fix (x, x') =
  1238     fun prt_fix (x, x') =
  1213       if x = x' then Pretty.str x
  1239       if x = x' then Pretty.str x
  1214       else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')];
  1240       else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')];
  1215     fun prt_fixes [] = []
  1241     val fixes = rev (filter_out
  1216       | prt_fixes xs = [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
  1242       ((can Syntax.dest_internal o #1) orf (fn (_, x') => x' mem_string structs)) (fixes_of ctxt));
  1217           Pretty.commas (map prt_fix xs))];
  1243     val prt_fixes = if null fixes then []
       
  1244       else [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
       
  1245         Pretty.commas (map prt_fix fixes))];
  1218 
  1246 
  1219     (*prems*)
  1247     (*prems*)
  1220     val limit = ! prems_limit;
  1248     val limit = ! prems_limit;
  1221     val prems = prems_of ctxt;
  1249     val prems = prems_of ctxt;
  1222     val len = length prems;
  1250     val len = length prems;
  1223     val prt_prems =
  1251     val prt_prems = if null prems then []
  1224       (if len <= limit then [] else [Pretty.str "..."]) @
  1252       else [Pretty.big_list "prems:" ((if len <= limit then [] else [Pretty.str "..."]) @
  1225       map (pretty_thm ctxt) (Library.drop (len - limit, prems));
  1253         map (pretty_thm ctxt) (Library.drop (len - limit, prems)))];
  1226   in
  1254 
  1227     prt_fixes (rev (filter_out (can Syntax.dest_internal o #1) (fixes_of ctxt))) @
  1255   in prt_structs @ prt_fixes @ prt_prems end;
  1228     (if null prems then [] else [Pretty.big_list "prems:" prt_prems])
       
  1229   end;
       
  1230 
  1256 
  1231 
  1257 
  1232 (* main context *)
  1258 (* main context *)
  1233 
  1259 
  1234 fun pretty_context (ctxt as Context {cases, defs = (types, sorts, (used, _)), ...}) =
  1260 fun pretty_context (ctxt as Context {cases, defs = (types, sorts, (used, _)), ...}) =