src/Pure/Isar/proof_context.ML
changeset 24684 80da599dea37
parent 24675 2be1253a20d3
child 24686 8113d0149304
equal deleted inserted replaced
24683:c62320337a4e 24684:80da599dea37
    56   val cert_typ_abbrev: Proof.context -> typ -> typ
    56   val cert_typ_abbrev: Proof.context -> typ -> typ
    57   val get_skolem: Proof.context -> string -> string
    57   val get_skolem: Proof.context -> string -> string
    58   val revert_skolem: Proof.context -> string -> string
    58   val revert_skolem: Proof.context -> string -> string
    59   val revert_skolems: Proof.context -> term -> term
    59   val revert_skolems: Proof.context -> term -> term
    60   val decode_term: Proof.context -> term -> term
    60   val decode_term: Proof.context -> term -> term
       
    61   val read_term_pattern: Proof.context -> string -> term
       
    62   val read_term_schematic: Proof.context -> string -> term
       
    63   val read_term_abbrev: Proof.context -> string -> term
    61   val read_termTs_schematic: Proof.context -> (string -> bool) -> (indexname -> typ option)
    64   val read_termTs_schematic: Proof.context -> (string -> bool) -> (indexname -> typ option)
    62     -> (indexname -> sort option) -> string list -> (string * typ) list
    65     -> (indexname -> sort option) -> string list -> (string * typ) list
    63     -> term list * (indexname * typ) list
    66     -> term list * (indexname * typ) list
    64   val read_prop_legacy: Proof.context -> string -> term
    67   val read_prop_legacy: Proof.context -> string -> term
    65   val read_termTs: Proof.context -> (string * typ) list -> term list
    68   val read_termTs: Proof.context -> (string * typ) list -> term list
    66   val read_term_pats: typ -> Proof.context -> string list -> term list
       
    67   val read_prop_pats: Proof.context -> string list -> term list
       
    68   val read_term_abbrev: Proof.context -> string -> term
       
    69   val cert_term: Proof.context -> term -> term
    69   val cert_term: Proof.context -> term -> term
    70   val cert_terms: Proof.context -> term list -> term list
       
    71   val cert_prop: Proof.context -> term -> term
    70   val cert_prop: Proof.context -> term -> term
    72   val cert_term_pats: typ -> Proof.context -> term list -> term list
       
    73   val cert_prop_pats: Proof.context -> term list -> term list
       
    74   val infer_type: Proof.context -> string -> typ
    71   val infer_type: Proof.context -> string -> typ
    75   val inferred_param: string -> Proof.context -> (string * typ) * Proof.context
    72   val inferred_param: string -> Proof.context -> (string * typ) * Proof.context
    76   val inferred_fixes: Proof.context -> (string * typ) list * Proof.context
    73   val inferred_fixes: Proof.context -> (string * typ) list * Proof.context
    77   val read_tyname: Proof.context -> string -> typ
    74   val read_tyname: Proof.context -> string -> typ
    78   val read_const: Proof.context -> string -> term
    75   val read_const: Proof.context -> string -> term
   426 
   423 
   427 
   424 
   428 
   425 
   429 (** prepare terms and propositions **)
   426 (** prepare terms and propositions **)
   430 
   427 
       
   428 (* read_term *)
       
   429 
       
   430 fun read_term_mode mode ctxt = Syntax.read_term (set_mode mode ctxt);
       
   431 
       
   432 val read_term_pattern   = read_term_mode mode_pattern;
       
   433 val read_term_schematic = read_term_mode mode_schematic;
       
   434 val read_term_abbrev    = read_term_mode mode_abbrev;
       
   435 
       
   436 
   431 (* read wrt. theory *)     (*exception ERROR*)
   437 (* read wrt. theory *)     (*exception ERROR*)
   432 
   438 
   433 fun read_def_termTs freeze pp syn ctxt (types, sorts, used) fixed sTs =
   439 fun read_def_termTs freeze pp syn ctxt (types, sorts, used) fixed sTs =
   434   Sign.read_def_terms' pp (Sign.is_logtype (theory_of ctxt)) syn (consts_of ctxt) fixed
   440   Sign.read_def_terms' pp (Sign.is_logtype (theory_of ctxt)) syn (consts_of ctxt) fixed
   435     ctxt (types, sorts) used freeze sTs;
   441     ctxt (types, sorts) used freeze sTs;
   503 fun reject_dummies t = Term.no_dummy_patterns t
   509 fun reject_dummies t = Term.no_dummy_patterns t
   504   handle TERM _ => error "Illegal dummy pattern(s) in term";
   510   handle TERM _ => error "Illegal dummy pattern(s) in term";
   505 
   511 
   506 in
   512 in
   507 
   513 
   508 fun prepare_pattern ctxt =
   514 fun prepare_patterns ctxt =
   509   let val Mode {pattern, ...} = get_mode ctxt in
   515   let val Mode {pattern, ...} = get_mode ctxt in
   510     Term.map_types (prepare_patternT ctxt) #>
   516     (if pattern then Variable.polymorphic ctxt else I) #>
   511     (if pattern then prepare_dummies else reject_dummies)
   517     (map o Term.map_types) (prepare_patternT ctxt) #>
       
   518     map (if pattern then prepare_dummies else reject_dummies)
   512   end;
   519   end;
   513 
   520 
   514 end;
   521 end;
   515 
   522 
   516 
   523 
   560   in
   567   in
   561     (read (pp ctxt) (syn_of ctxt) ctxt (types, sorts, used)
   568     (read (pp ctxt) (syn_of ctxt) ctxt (types, sorts, used)
   562         (legacy_intern_skolem ctxt internal types) s
   569         (legacy_intern_skolem ctxt internal types) s
   563       handle TERM (msg, _) => error msg)
   570       handle TERM (msg, _) => error msg)
   564     |> app (expand_abbrevs ctxt)
   571     |> app (expand_abbrevs ctxt)
   565     |> app (prepare_pattern ctxt)
   572     |> app (singleton (prepare_patterns ctxt))
   566   end;
   573   end;
   567 
   574 
   568 fun gen_read read app pattern schematic ctxt =
   575 fun gen_read read app pattern schematic ctxt =
   569   gen_read' read app pattern schematic ctxt (K false) (K NONE) (K NONE) [];
   576   gen_read' read app pattern schematic ctxt (K false) (K NONE) (K NONE) [];
   570 
   577 
   571 in
   578 in
   572 
   579 
   573 val read_termTs_schematic = gen_read' (read_def_termTs false) (apfst o map) false true;
   580 val read_termTs_schematic = gen_read' (read_def_termTs false) (apfst o map) false true;
   574 
   581 
   575 fun read_term_pats T ctxt =
       
   576   #1 o gen_read (read_def_termTs false) (apfst o map) true false ctxt o map (rpair T);
       
   577 val read_prop_pats = read_term_pats propT;
       
   578 
       
   579 fun read_prop_legacy ctxt =
   582 fun read_prop_legacy ctxt =
   580   gen_read' (read_prop_thy true) I false false ctxt (K true) (K NONE) (K NONE) [];
   583   gen_read' (read_prop_thy true) I false false ctxt (K true) (K NONE) (K NONE) [];
   581 
   584 
   582 val read_termTs          = gen_read (read_terms_thy true) map false false;
   585 val read_termTs          = gen_read (read_terms_thy true) map false false;
   583 fun read_props schematic = gen_read (read_props_thy true) map false schematic;
   586 fun read_props schematic = gen_read (read_props_thy true) map false schematic;
   587 
   590 
   588 (* certify terms *)
   591 (* certify terms *)
   589 
   592 
   590 local
   593 local
   591 
   594 
   592 fun gen_cert prop mode ctxt0 t =
   595 fun gen_cert prop ctxt t =
   593   let val ctxt = set_mode mode ctxt0 in
   596   t
   594     t
   597   |> expand_abbrevs ctxt
   595     |> expand_abbrevs ctxt
   598   |> (fn t' => #1 (Sign.certify' prop (pp ctxt) false (consts_of ctxt) (theory_of ctxt) t')
   596     |> (fn t' => #1 (Sign.certify' prop (pp ctxt) false (consts_of ctxt) (theory_of ctxt) t')
   599     handle TYPE (msg, _, _) => error msg
   597       handle TYPE (msg, _, _) => error msg
   600       | TERM (msg, _) => error msg);
   598         | TERM (msg, _) => error msg)
   601 
   599    end;
   602 in
   600 
   603 
   601 in
   604 val cert_term = gen_cert false;
   602 
   605 val cert_prop = gen_cert true;
   603 val cert_term = gen_cert false mode_default;
       
   604 val cert_terms = map o cert_term;
       
   605 val cert_prop = gen_cert true mode_default;
       
   606 fun cert_props schematic = map o gen_cert true (if schematic then mode_schematic else mode_default);
       
   607 
       
   608 fun cert_term_pats _ = map o gen_cert false mode_pattern;
       
   609 val cert_prop_pats = map o gen_cert true mode_pattern;
       
   610 
   606 
   611 end;
   607 end;
   612 
   608 
   613 
   609 
   614 (* type checking/inference *)
   610 (* type checking/inference *)
   625 local
   621 local
   626 
   622 
   627 fun standard_term_check ctxt =
   623 fun standard_term_check ctxt =
   628   standard_infer_types ctxt #>
   624   standard_infer_types ctxt #>
   629   map (expand_abbrevs ctxt) #>
   625   map (expand_abbrevs ctxt) #>
   630   map (prepare_pattern ctxt);
   626   prepare_patterns ctxt;
   631 
   627 
   632 fun standard_typ_check ctxt =
   628 fun standard_typ_check ctxt =
   633   map (cert_typ_mode (Type.get_mode ctxt) ctxt) #>
   629   map (cert_typ_mode (Type.get_mode ctxt) ctxt) #>
   634   map (prepare_patternT ctxt);
   630   map (prepare_patternT ctxt);
   635 
   631 
   689   handle ERROR msg => cat_error msg ("The error(s) above occurred in type " ^ quote str);
   685   handle ERROR msg => cat_error msg ("The error(s) above occurred in type " ^ quote str);
   690 
   686 
   691 fun parse_term T ctxt str =
   687 fun parse_term T ctxt str =
   692   let
   688   let
   693     val thy = theory_of ctxt;
   689     val thy = theory_of ctxt;
   694     fun infer t = singleton (standard_infer_types (Type.set_mode Type.mode_default ctxt))
   690     val infer = singleton (standard_infer_types (Type.set_mode Type.mode_default ctxt)) o
   695         (TypeInfer.constrain t T);
   691       TypeInfer.constrain T;
   696     fun check t = Exn.Result (infer t) handle ERROR msg => Exn.Exn (ERROR msg);
   692     fun check t = Exn.Result (infer t) handle ERROR msg => Exn.Exn (ERROR msg);
   697     val {get_sort, map_const, map_free, map_type, map_sort} = term_context ctxt;
   693     val {get_sort, map_const, map_free, map_type, map_sort} = term_context ctxt;
   698     val read = Syntax.standard_parse_term (pp ctxt) check get_sort map_const map_free map_type
   694     val read = Syntax.standard_parse_term (pp ctxt) check get_sort map_const map_free map_type
   699       map_sort ctxt (Sign.is_logtype thy) (syn_of ctxt) ((#1 (TypeInfer.paramify_dummies T 0)));
   695       map_sort ctxt (Sign.is_logtype thy) (syn_of ctxt) ((#1 (TypeInfer.paramify_dummies T 0)));
   700   in read str end;
   696   in read str end;
   758 
   754 
   759 (* match_bind(_i) *)
   755 (* match_bind(_i) *)
   760 
   756 
   761 local
   757 local
   762 
   758 
   763 fun prep_bind prep_pats (raw_pats, t) ctxt =
   759 fun gen_bind prep_terms gen raw_binds ctxt =
   764   let
   760   let
   765     val ctxt' = Variable.declare_term t ctxt;
   761     fun prep_bind (raw_pats, t) ctxt1 =
   766     val pats = prep_pats (Term.fastype_of t) ctxt' raw_pats;
   762       let
   767     val binds = simult_matches ctxt' (t, pats);
   763         val T = Term.fastype_of t;
   768   in (binds, ctxt') end;
   764         val ctxt2 = Variable.declare_term t ctxt1;
   769 
   765         val pats = prep_terms (set_mode mode_pattern ctxt2) T raw_pats;
   770 fun gen_binds prep_terms prep_pats gen raw_binds ctxt =
   766         val binds = simult_matches ctxt2 (t, pats);
   771   let
   767       in (binds, ctxt2) end;
   772     val ts = prep_terms (set_mode mode_default ctxt) (map snd raw_binds);
   768 
   773     val (binds, ctxt') =
   769     val ts = prep_terms ctxt dummyT (map #2 raw_binds);
   774       apfst flat (fold_map (prep_bind prep_pats) (map fst raw_binds ~~ ts) ctxt);
   770     val (binds, ctxt') = apfst flat (fold_map prep_bind (map #1 raw_binds ~~ ts) ctxt);
   775     val binds' =
   771     val binds' =
   776       if gen then map #1 binds ~~ Variable.exportT_terms ctxt' ctxt (map #2 binds)
   772       if gen then map #1 binds ~~ Variable.exportT_terms ctxt' ctxt (map #2 binds)
   777       else binds;
   773       else binds;
   778     val binds'' = map (apsnd SOME) binds';
   774     val binds'' = map (apsnd SOME) binds';
   779     val ctxt'' =
   775     val ctxt'' =
   783         else ctxt' |> add_binds_i binds'');
   779         else ctxt' |> add_binds_i binds'');
   784   in (ts, ctxt'') end;
   780   in (ts, ctxt'') end;
   785 
   781 
   786 in
   782 in
   787 
   783 
   788 val match_bind = gen_binds Syntax.read_terms read_term_pats;
   784 fun read_terms ctxt T =
   789 val match_bind_i = gen_binds cert_terms cert_term_pats;
   785   map (Syntax.parse_term ctxt #> TypeInfer.constrain T) #> Syntax.check_terms ctxt;
       
   786 
       
   787 val match_bind = gen_bind read_terms;
       
   788 val match_bind_i = gen_bind (fn ctxt => fn _ => map (cert_term ctxt));
   790 
   789 
   791 end;
   790 end;
   792 
   791 
   793 
   792 
   794 (* propositions with patterns *)
   793 (* propositions with patterns *)
   795 
   794 
   796 local
   795 local
   797 
   796 
   798 fun prep_propp schematic prep_props prep_pats (context, args) =
   797 fun prep_propp mode prep_props (context, args) =
   799   let
   798   let
   800     fun prep (_, raw_pats) (ctxt, prop :: props) =
   799     fun prep (_, raw_pats) (ctxt, prop :: props) =
   801           let val ctxt' = Variable.declare_term prop ctxt
   800       let val ctxt' = Variable.declare_term prop ctxt
   802           in ((prop, prep_pats ctxt' raw_pats), (ctxt', props)) end
   801       in ((prop, prep_props (set_mode mode_pattern ctxt') raw_pats), (ctxt', props)) end;
   803       | prep _ _ = sys_error "prep_propp";
   802 
   804     val (propp, (context', _)) = (fold_map o fold_map) prep args
   803     val (propp, (context', _)) = (fold_map o fold_map) prep args
   805       (context, prep_props schematic context (maps (map fst) args));
   804       (context, prep_props (set_mode mode context) (maps (map fst) args));
   806   in (context', propp) end;
   805   in (context', propp) end;
   807 
   806 
   808 fun gen_bind_propp prepp (ctxt, raw_args) =
   807 fun gen_bind_propp mode parse_prop (ctxt, raw_args) =
   809   let
   808   let
   810     val (ctxt', args) = prepp (ctxt, raw_args);
   809     val (ctxt', args) = prep_propp mode parse_prop (ctxt, raw_args);
   811     val binds = flat (flat (map (map (simult_matches ctxt')) args));
   810     val binds = flat (flat (map (map (simult_matches ctxt')) args));
   812     val propss = map (map #1) args;
   811     val propss = map (map #1) args;
   813 
   812 
   814     (*generalize result: context evaluated now, binds added later*)
   813     (*generalize result: context evaluated now, binds added later*)
   815     val gen = Variable.exportT_terms ctxt' ctxt;
   814     val gen = Variable.exportT_terms ctxt' ctxt;
   816     fun gen_binds c = c |> add_binds_i (map #1 binds ~~ map SOME (gen (map #2 binds)));
   815     fun gen_binds c = c |> add_binds_i (map #1 binds ~~ map SOME (gen (map #2 binds)));
   817   in (ctxt' |> add_binds_i (map (apsnd SOME) binds), (propss, gen_binds)) end;
   816   in (ctxt' |> add_binds_i (map (apsnd SOME) binds), (propss, gen_binds)) end;
   818 
   817 
   819 in
   818 in
   820 
   819 
   821 val read_propp = prep_propp false read_props read_prop_pats;
   820 val read_propp           = prep_propp mode_default Syntax.read_props;
   822 val cert_propp = prep_propp false cert_props cert_prop_pats;
   821 val cert_propp           = prep_propp mode_default (map o cert_prop);
   823 val read_propp_schematic = prep_propp true read_props read_prop_pats;
   822 val read_propp_schematic = prep_propp mode_schematic Syntax.read_props;
   824 val cert_propp_schematic = prep_propp true cert_props cert_prop_pats;
   823 val cert_propp_schematic = prep_propp mode_schematic (map o cert_prop);
   825 
   824 
   826 val bind_propp = gen_bind_propp read_propp;
   825 val bind_propp             = gen_bind_propp mode_default Syntax.read_props;
   827 val bind_propp_i = gen_bind_propp cert_propp;
   826 val bind_propp_i           = gen_bind_propp mode_default (map o cert_prop);
   828 val bind_propp_schematic = gen_bind_propp read_propp_schematic;
   827 val bind_propp_schematic   = gen_bind_propp mode_schematic Syntax.read_props;
   829 val bind_propp_schematic_i = gen_bind_propp cert_propp_schematic;
   828 val bind_propp_schematic_i = gen_bind_propp mode_schematic (map o cert_prop);
   830 
   829 
   831 end;
   830 end;
   832 
   831 
   833 
   832 
   834 
   833 
  1025   in Context.mapping (Sign.add_notation mode args') (add_notation mode args') end;
  1024   in Context.mapping (Sign.add_notation mode args') (add_notation mode args') end;
  1026 
  1025 
  1027 
  1026 
  1028 (* abbreviations *)
  1027 (* abbreviations *)
  1029 
  1028 
  1030 fun read_term_abbrev ctxt = Syntax.read_term (set_mode mode_abbrev ctxt);
       
  1031 
       
  1032 fun add_abbrev mode (c, raw_t) ctxt =
  1029 fun add_abbrev mode (c, raw_t) ctxt =
  1033   let
  1030   let
  1034     val t0 = cert_term (ctxt |> set_mode mode_abbrev) raw_t
  1031     val t0 = cert_term (ctxt |> set_mode mode_abbrev) raw_t
  1035       handle ERROR msg => cat_error msg ("in constant abbreviation " ^ quote c);
  1032       handle ERROR msg => cat_error msg ("in constant abbreviation " ^ quote c);
  1036     val [t] = Variable.exportT_terms (Variable.declare_term t0 ctxt) ctxt [t0];
  1033     val [t] = Variable.exportT_terms (Variable.declare_term t0 ctxt) ctxt [t0];