src/Pure/Isar/proof_context.ML
changeset 18971 f95650f3b5bf
parent 18953 93903be7ff66
child 19001 64e4b5bc6443
equal deleted inserted replaced
18970:d055a29ddd23 18971:f95650f3b5bf
   174 datatype ctxt =
   174 datatype ctxt =
   175   Ctxt of
   175   Ctxt of
   176    {syntax:                                      (*global/local syntax, structs, mixfixed*)
   176    {syntax:                                      (*global/local syntax, structs, mixfixed*)
   177       (Syntax.syntax * Syntax.syntax * (Syntax.syntax -> Syntax.syntax)) *
   177       (Syntax.syntax * Syntax.syntax * (Syntax.syntax -> Syntax.syntax)) *
   178       string list * string list,
   178       string list * string list,
       
   179     consts: Consts.T,                            (*const abbreviations*)
   179     fixes: bool * (string * string) list,        (*fixes: !!x. _ with proof body flag*)
   180     fixes: bool * (string * string) list,        (*fixes: !!x. _ with proof body flag*)
   180     assms:
   181     assms:
   181       ((cterm list * export) list *              (*assumes and views: A ==> _*)
   182       ((cterm list * export) list *              (*assumes and views: A ==> _*)
   182         (string * thm list) list),               (*prems: A |- A*)
   183         (string * thm list) list),               (*prems: A |- A*)
   183     binds: (typ * term) Vartab.table,            (*term bindings*)
   184     binds: (typ * term) Vartab.table,            (*term bindings*)
   188       typ Vartab.table *                         (*type constraints*)
   189       typ Vartab.table *                         (*type constraints*)
   189       sort Vartab.table *                        (*default sorts*)
   190       sort Vartab.table *                        (*default sorts*)
   190       string list *                              (*used type variables*)
   191       string list *                              (*used type variables*)
   191       term list Symtab.table};                   (*type variable occurrences*)
   192       term list Symtab.table};                   (*type variable occurrences*)
   192 
   193 
   193 fun make_ctxt (syntax, fixes, assms, binds, thms, cases, defaults) =
   194 fun make_ctxt (syntax, consts, fixes, assms, binds, thms, cases, defaults) =
   194   Ctxt {syntax = syntax, fixes = fixes, assms = assms, binds = binds,
   195   Ctxt {syntax = syntax, consts = consts, fixes = fixes, assms = assms, binds = binds,
   195     thms = thms, cases = cases, defaults = defaults};
   196     thms = thms, cases = cases, defaults = defaults};
   196 
   197 
   197 structure ContextData = ProofDataFun
   198 structure ContextData = ProofDataFun
   198 (
   199 (
   199   val name = "Isar/context";
   200   val name = "Isar/context";
   200   type T = ctxt;
   201   type T = ctxt;
   201   fun init thy =
   202   fun init thy =
   202     make_ctxt (((Sign.syn_of thy, Sign.syn_of thy, I), [], []), (false, []), ([], []),
   203     make_ctxt (((Sign.syn_of thy, Sign.syn_of thy, I), [], []), Sign.consts_of thy,
   203       Vartab.empty, (NameSpace.default_naming, NameSpace.empty_table, FactIndex.empty), [],
   204       (false, []), ([], []), Vartab.empty,
       
   205       (NameSpace.default_naming, NameSpace.empty_table, FactIndex.empty), [],
   204       (Vartab.empty, Vartab.empty, [], Symtab.empty));
   206       (Vartab.empty, Vartab.empty, [], Symtab.empty));
   205   fun print _ _ = ();
   207   fun print _ _ = ();
   206 );
   208 );
   207 
   209 
   208 val _ = Context.add_setup ContextData.init;
   210 val _ = Context.add_setup ContextData.init;
   209 
   211 
   210 fun rep_context ctxt = ContextData.get ctxt |> (fn Ctxt args => args);
   212 fun rep_context ctxt = ContextData.get ctxt |> (fn Ctxt args => args);
   211 
   213 
   212 fun map_context f =
   214 fun map_context f =
   213   ContextData.map (fn Ctxt {syntax, fixes, assms, binds, thms, cases, defaults} =>
   215   ContextData.map (fn Ctxt {syntax, consts, fixes, assms, binds, thms, cases, defaults} =>
   214     make_ctxt (f (syntax, fixes, assms, binds, thms, cases, defaults)));
   216     make_ctxt (f (syntax, consts, fixes, assms, binds, thms, cases, defaults)));
   215 
   217 
   216 fun map_syntax f = map_context (fn (syntax, fixes, assms, binds, thms, cases, defaults) =>
   218 fun map_syntax f = map_context (fn (syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
   217   (f syntax, fixes, assms, binds, thms, cases, defaults));
   219   (f syntax, consts, fixes, assms, binds, thms, cases, defaults));
   218 
   220 
   219 fun map_fixes f = map_context (fn (syntax, fixes, assms, binds, thms, cases, defaults) =>
   221 fun map_consts f = map_context (fn (syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
   220   (syntax, f fixes, assms, binds, thms, cases, defaults));
   222   (syntax, f consts, fixes, assms, binds, thms, cases, defaults));
   221 
   223 
   222 fun map_assms f = map_context (fn (syntax, fixes, assms, binds, thms, cases, defaults) =>
   224 fun map_fixes f = map_context (fn (syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
   223   (syntax, fixes, f assms, binds, thms, cases, defaults));
   225   (syntax, consts, f fixes, assms, binds, thms, cases, defaults));
   224 
   226 
   225 fun map_binds f = map_context (fn (syntax, fixes, assms, binds, thms, cases, defaults) =>
   227 fun map_assms f = map_context (fn (syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
   226   (syntax, fixes, assms, f binds, thms, cases, defaults));
   228   (syntax, consts, fixes, f assms, binds, thms, cases, defaults));
   227 
   229 
   228 fun map_thms f = map_context (fn (syntax, fixes, assms, binds, thms, cases, defaults) =>
   230 fun map_binds f = map_context (fn (syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
   229   (syntax, fixes, assms, binds, f thms, cases, defaults));
   231   (syntax, consts, fixes, assms, f binds, thms, cases, defaults));
       
   232 
       
   233 fun map_thms f = map_context (fn (syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
       
   234   (syntax, consts, fixes, assms, binds, f thms, cases, defaults));
   230 
   235 
   231 fun map_naming f = map_thms (fn (naming, table, index) => (f naming, table, index));
   236 fun map_naming f = map_thms (fn (naming, table, index) => (f naming, table, index));
   232 
   237 
   233 fun map_cases f = map_context (fn (syntax, fixes, assms, binds, thms, cases, defaults) =>
   238 fun map_cases f = map_context (fn (syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
   234   (syntax, fixes, assms, binds, thms, f cases, defaults));
   239   (syntax, consts, fixes, assms, binds, thms, f cases, defaults));
   235 
   240 
   236 fun map_defaults f = map_context (fn (syntax, fixes, assms, binds, thms, cases, defaults) =>
   241 fun map_defaults f = map_context (fn (syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
   237   (syntax, fixes, assms, binds, thms, cases, f defaults));
   242   (syntax, consts, fixes, assms, binds, thms, cases, f defaults));
   238 
   243 
   239 val syntax_of = #syntax o rep_context;
   244 val syntax_of = #syntax o rep_context;
       
   245 val consts_of = #consts o rep_context;
   240 
   246 
   241 val is_body = #1 o #fixes o rep_context;
   247 val is_body = #1 o #fixes o rep_context;
   242 fun set_body b = map_fixes (fn (_, fixes) => (b, fixes));
   248 fun set_body b = map_fixes (fn (_, fixes) => (b, fixes));
   243 fun restore_body ctxt = set_body (is_body ctxt);
   249 fun restore_body ctxt = set_body (is_body ctxt);
   244 
   250 
   354 
   360 
   355 
   361 
   356 (** pretty printing **)
   362 (** pretty printing **)
   357 
   363 
   358 fun pretty_term' thy ctxt t =
   364 fun pretty_term' thy ctxt t =
   359   Sign.pretty_term' (syn_of' thy ctxt) (Context.Proof ctxt) (context_tr' ctxt t);
   365   let
       
   366     val consts = consts_of ctxt;
       
   367     val t' = Pattern.rewrite_term thy (Consts.abbrevs_of consts) [] t;
       
   368   in Sign.pretty_term' (syn_of' thy ctxt) consts (Context.Proof ctxt) (context_tr' ctxt t) end;
       
   369 
   360 fun pretty_term ctxt t = pretty_term' (theory_of ctxt) ctxt (context_tr' ctxt t);
   370 fun pretty_term ctxt t = pretty_term' (theory_of ctxt) ctxt (context_tr' ctxt t);
   361 fun pretty_typ ctxt T = Sign.pretty_typ (theory_of ctxt) T;
   371 fun pretty_typ ctxt T = Sign.pretty_typ (theory_of ctxt) T;
   362 fun pretty_sort ctxt S = Sign.pretty_sort (theory_of ctxt) S;
   372 fun pretty_sort ctxt S = Sign.pretty_sort (theory_of ctxt) S;
   363 fun pretty_classrel ctxt cs = Sign.pretty_classrel (theory_of ctxt) cs;
   373 fun pretty_classrel ctxt cs = Sign.pretty_classrel (theory_of ctxt) cs;
   364 fun pretty_arity ctxt ar = Sign.pretty_arity (theory_of ctxt) ar;
   374 fun pretty_arity ctxt ar = Sign.pretty_arity (theory_of ctxt) ar;
   493 
   503 
   494 
   504 
   495 (* read wrt. theory *)     (*exception ERROR*)
   505 (* read wrt. theory *)     (*exception ERROR*)
   496 
   506 
   497 fun read_def_termTs freeze pp syn ctxt (types, sorts, used) sTs =
   507 fun read_def_termTs freeze pp syn ctxt (types, sorts, used) sTs =
   498   Sign.read_def_terms' pp (Sign.is_logtype (theory_of ctxt)) syn
   508   Sign.read_def_terms' pp (Sign.is_logtype (theory_of ctxt)) syn (consts_of ctxt)
   499     (Context.Proof ctxt) (types, sorts) used freeze sTs;
   509     (Context.Proof ctxt) (types, sorts) used freeze sTs;
   500 
   510 
   501 fun read_def_termT freeze pp syn ctxt defaults sT =
   511 fun read_def_termT freeze pp syn ctxt defaults sT =
   502   apfst hd (read_def_termTs freeze pp syn ctxt defaults [sT]);
   512   apfst hd (read_def_termTs freeze pp syn ctxt defaults [sT]);
   503 
   513 
   514   #1 o read_def_termTs freeze pp syn thy defaults o map (rpair propT);
   524   #1 o read_def_termTs freeze pp syn thy defaults o map (rpair propT);
   515 
   525 
   516 
   526 
   517 (* norm_term *)
   527 (* norm_term *)
   518 
   528 
   519 (*
       
   520   - expand abbreviations (polymorphic Consts)
       
   521   - expand term bindings (polymorphic Vars)
       
   522   - beta contraction
       
   523 *)
       
   524 
       
   525 fun norm_term ctxt schematic =
   529 fun norm_term ctxt schematic =
   526   let
   530   let
   527     val thy = theory_of ctxt;
   531     val binds = binds_of ctxt;
   528     val tsig = Sign.tsig_of thy;
   532     val tsig = Sign.tsig_of (theory_of ctxt);
   529     val expansion = Sign.const_expansion thy;
   533 
   530     val binding = Vartab.lookup (binds_of ctxt);
   534     fun norm_var (xi, T) =
   531 
   535       (case Vartab.lookup binds xi of
   532     exception SAME;
   536         SOME t => Envir.expand_atom tsig T t
   533     fun norm (Const c) =
   537       | NONE =>
   534           (case expansion c of
   538           if schematic then Var (xi, T)
   535             SOME u => (norm u handle SAME => u)
   539           else error ("Unbound schematic variable: " ^ Syntax.string_of_vname xi));
   536           | NONE => raise SAME)
   540   in Envir.beta_norm o Term.map_aterms (fn Var v => norm_var v | a => a) end;
   537       | norm (Var (xi, T)) =
       
   538           (case binding xi of
       
   539             SOME b =>
       
   540               let val u = Envir.expand_atom tsig T b
       
   541               in norm u handle SAME => u end
       
   542           | NONE =>
       
   543               if schematic then raise SAME
       
   544               else error ("Unbound schematic variable: " ^ Syntax.string_of_vname xi))
       
   545       | norm (Abs (a, T, body)) =  Abs (a, T, norm body)
       
   546       | norm (Abs (_, _, body) $ t) = normh (subst_bound (t, body))
       
   547       | norm (f $ t) =
       
   548           ((case norm f of
       
   549             Abs (_, _, body) => normh (subst_bound (t, body))
       
   550           | nf => nf $ (norm t handle SAME => t)) handle SAME => f $ norm t)
       
   551       | norm _ =  raise SAME
       
   552     and normh t = norm t handle SAME => t
       
   553   in normh end;
       
   554 
   541 
   555 
   542 
   556 (* dummy patterns *)
   543 (* dummy patterns *)
   557 
   544 
   558 val prepare_dummies =
   545 val prepare_dummies =
   612 
   599 
   613 (* certify terms *)
   600 (* certify terms *)
   614 
   601 
   615 local
   602 local
   616 
   603 
   617 fun gen_cert cert pattern schematic ctxt t = t
   604 fun gen_cert prop pattern schematic ctxt t = t
   618   |> (if pattern then I else norm_term ctxt schematic)
   605   |> (if pattern then I else norm_term ctxt schematic)
   619   |> (fn t' => cert (pp ctxt) (theory_of ctxt) t'
   606   |> (fn t' => #1 (Sign.certify false prop (pp ctxt) (theory_of ctxt) t')
   620     handle TYPE (msg, _, _) => error msg
   607     handle TYPE (msg, _, _) => error msg
   621       | TERM (msg, _) => error msg);
   608       | TERM (msg, _) => error msg);
   622 
   609 
   623 val certify_term = #1 ooo Sign.certify_term;
       
   624 val certify_prop = #1 ooo Sign.certify_prop;
       
   625 
       
   626 in
   610 in
   627 
   611 
   628 val cert_term = gen_cert certify_term false false;
   612 val cert_term = gen_cert false false false;
   629 val cert_prop = gen_cert certify_prop false false;
   613 val cert_prop = gen_cert true false false;
   630 val cert_props = map oo gen_cert certify_prop false;
   614 val cert_props = map oo gen_cert true false;
   631 
   615 
   632 fun cert_term_pats _ = map o gen_cert certify_term true false;
   616 fun cert_term_pats _ = map o gen_cert false true false;
   633 val cert_prop_pats = map o gen_cert certify_prop true false;
   617 val cert_prop_pats = map o gen_cert true true false;
   634 
   618 
   635 end;
   619 end;
   636 
   620 
   637 
   621 
   638 (* declare terms *)
   622 (* declare terms *)
   682 
   666 
   683 (* inferred types of parameters *)
   667 (* inferred types of parameters *)
   684 
   668 
   685 fun infer_type ctxt x =
   669 fun infer_type ctxt x =
   686   (case try (fn () =>
   670   (case try (fn () =>
   687       Sign.infer_types (pp ctxt) (theory_of ctxt) (def_type ctxt false) (def_sort ctxt)
   671       Sign.infer_types (pp ctxt) (theory_of ctxt) (consts_of ctxt) (def_type ctxt false)
   688         (used_types ctxt) true ([Free (x, dummyT)], TypeInfer.logicT)) () of
   672         (def_sort ctxt) (used_types ctxt) true ([Free (x, dummyT)], TypeInfer.logicT)) () of
   689     SOME (Free (_, T), _) => T
   673     SOME (Free (_, T), _) => T
   690   | _ => error ("Failed to infer type of fixed variable " ^ quote x));
   674   | _ => error ("Failed to infer type of fixed variable " ^ quote x));
   691 
   675 
   692 fun inferred_param x ctxt =
   676 fun inferred_param x ctxt =
   693   let val T = infer_type ctxt x
   677   let val T = infer_type ctxt x
   705   else Sign.read_tyname (theory_of ctxt) c;
   689   else Sign.read_tyname (theory_of ctxt) c;
   706 
   690 
   707 fun read_const ctxt c =
   691 fun read_const ctxt c =
   708   (case lookup_skolem ctxt c of
   692   (case lookup_skolem ctxt c of
   709     SOME c' => Free (c', dummyT)
   693     SOME c' => Free (c', dummyT)
   710   | NONE => Sign.read_const (theory_of ctxt) c);
   694   | NONE => Consts.read_const (consts_of ctxt) c);
   711 
   695 
   712 
   696 
   713 
   697 
   714 (** Hindley-Milner polymorphism **)
   698 (** Hindley-Milner polymorphism **)
   715 
   699 
  1115 fun gen_fixes prep raw_vars ctxt =
  1099 fun gen_fixes prep raw_vars ctxt =
  1116   let
  1100   let
  1117     val (ys, zs) = split_list (fixes_of ctxt);
  1101     val (ys, zs) = split_list (fixes_of ctxt);
  1118     val (vars, ctxt') = prep raw_vars ctxt;
  1102     val (vars, ctxt') = prep raw_vars ctxt;
  1119     val xs = map #1 vars;
  1103     val xs = map #1 vars;
  1120     val _ = no_dups ctxt (gen_duplicates (op =) xs);
  1104     val _ = no_dups ctxt (duplicates (op =) xs);
  1121     val xs' =
  1105     val xs' =
  1122       if is_body ctxt then Term.variantlist (map Syntax.skolem xs, zs)
  1106       if is_body ctxt then Term.variantlist (map Syntax.skolem xs, zs)
  1123       else (no_dups ctxt (xs inter_string ys); no_dups ctxt (xs inter_string zs); xs);
  1107       else (no_dups ctxt (xs inter_string ys); no_dups ctxt (xs inter_string zs); xs);
  1124     val vars' = map2 (fn x' => fn (_, T, mx) => (x', T, mx)) xs' vars;
  1108     val vars' = map2 (fn x' => fn (_, T, mx) => (x', T, mx)) xs' vars;
  1125   in
  1109   in
  1222 
  1206 
  1223 (* basic assumptions *)
  1207 (* basic assumptions *)
  1224 
  1208 
  1225 (*
  1209 (*
  1226     [A]
  1210     [A]
  1227      : 
  1211      :
  1228      B
  1212      B
  1229   --------
  1213   --------
  1230   #A ==> B
  1214   #A ==> B
  1231 *)
  1215 *)
  1232 fun assume_export true = Seq.single oo Drule.implies_intr_protected
  1216 fun assume_export true = Seq.single oo Drule.implies_intr_protected
  1233   | assume_export false = Seq.single oo Drule.implies_intr_list;
  1217   | assume_export false = Seq.single oo Drule.implies_intr_list;
  1234 
  1218 
  1235 (*
  1219 (*
  1236     [A]
  1220     [A]
  1237      : 
  1221      :
  1238      B
  1222      B
  1239   -------
  1223   -------
  1240   A ==> B
  1224   A ==> B
  1241 *)
  1225 *)
  1242 fun presume_export _ = assume_export false;
  1226 fun presume_export _ = assume_export false;
  1332 (* local syntax *)
  1316 (* local syntax *)
  1333 
  1317 
  1334 val print_syntax = Syntax.print_syntax o syn_of;
  1318 val print_syntax = Syntax.print_syntax o syn_of;
  1335 
  1319 
  1336 
  1320 
       
  1321 (* local consts *)
       
  1322 
       
  1323 fun pretty_consts ctxt =
       
  1324   let
       
  1325     val (space, consts) = #constants (Consts.dest (consts_of ctxt));
       
  1326     val globals = #2 (#constants (Consts.dest (Sign.consts_of (theory_of ctxt))));
       
  1327     fun local_abbrev (_, (_, NONE)) = I
       
  1328       | local_abbrev (c, (T, SOME t)) =
       
  1329           if Symtab.defined globals c then I else cons (c, Logic.mk_equals (Const (c, T), t));
       
  1330     val abbrevs = NameSpace.extern_table (space, Symtab.make (Symtab.fold local_abbrev consts []));
       
  1331   in
       
  1332     if null abbrevs andalso not (! verbose) then []
       
  1333     else [Pretty.big_list "abbreviations:" (map (pretty_term ctxt o #2) abbrevs)]
       
  1334   end;
       
  1335 
       
  1336 
  1337 (* term bindings *)
  1337 (* term bindings *)
  1338 
  1338 
  1339 fun pretty_binds ctxt =
  1339 fun pretty_binds ctxt =
  1340   let
  1340   let
  1341     val binds = binds_of ctxt;
  1341     val binds = binds_of ctxt;
  1456 
  1456 
  1457     val (types, sorts, used, _) = defaults_of ctxt;
  1457     val (types, sorts, used, _) = defaults_of ctxt;
  1458   in
  1458   in
  1459     verb single (K pretty_thy) @
  1459     verb single (K pretty_thy) @
  1460     pretty_ctxt ctxt @
  1460     pretty_ctxt ctxt @
       
  1461     verb pretty_consts (K ctxt) @
  1461     verb pretty_binds (K ctxt) @
  1462     verb pretty_binds (K ctxt) @
  1462     verb pretty_lthms (K ctxt) @
  1463     verb pretty_lthms (K ctxt) @
  1463     verb pretty_cases (K ctxt) @
  1464     verb pretty_cases (K ctxt) @
  1464     verb single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
  1465     verb single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
  1465     verb single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts))) @
  1466     verb single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts))) @