src/Pure/Isar/expression.ML
changeset 38108 b4115423c049
parent 38107 3a46cebd7983
child 38211 8ed3a5fb4d25
equal deleted inserted replaced
38107:3a46cebd7983 38108:b4115423c049
    41     (term list list * (string * morphism) list * morphism) * Proof.context
    41     (term list list * (string * morphism) list * morphism) * Proof.context
    42   val sublocale: string -> expression_i -> theory -> Proof.state
    42   val sublocale: string -> expression_i -> theory -> Proof.state
    43   val sublocale_cmd: string -> expression -> theory -> Proof.state
    43   val sublocale_cmd: string -> expression -> theory -> Proof.state
    44   val interpretation: expression_i -> (Attrib.binding * term) list -> theory -> Proof.state
    44   val interpretation: expression_i -> (Attrib.binding * term) list -> theory -> Proof.state
    45   val interpretation_cmd: expression -> (Attrib.binding * string) list -> theory -> Proof.state
    45   val interpretation_cmd: expression -> (Attrib.binding * string) list -> theory -> Proof.state
    46   val interpret: expression_i -> bool -> Proof.state -> Proof.state
    46   val interpret: expression_i -> (Attrib.binding * term) list -> bool -> Proof.state -> Proof.state
    47   val interpret_cmd: expression -> bool -> Proof.state -> Proof.state
    47   val interpret_cmd: expression -> (Attrib.binding * string) list -> bool -> Proof.state -> Proof.state
    48 end;
    48 end;
    49 
    49 
    50 structure Expression : EXPRESSION =
    50 structure Expression : EXPRESSION =
    51 struct
    51 struct
    52 
    52 
   788 end;
   788 end;
   789 
   789 
   790 
   790 
   791 (*** Interpretation ***)
   791 (*** Interpretation ***)
   792 
   792 
   793 (** Interpretation in theories **)
   793 (** Interpretation in theories and proof contexts **)
   794 
   794 
   795 local
   795 local
       
   796 
       
   797 fun note_eqns_register deps witss attrss eqns export export' context =
       
   798   let
       
   799     fun meta_rewrite context =
       
   800       map (Local_Defs.meta_rewrite_rule (Context.proof_of context) #> Drule.abs_def) o
       
   801         maps snd;
       
   802   in
       
   803     context
       
   804     |> Element.generic_note_thmss Thm.lemmaK
       
   805       (attrss ~~ map (fn eqn => [([Morphism.thm (export' $> export) eqn],[])]) eqns)
       
   806     |-> (fn facts => `(fn context => meta_rewrite context facts))
       
   807     |-> (fn eqns => fold (fn ((dep, morph), wits) =>
       
   808       fn context =>
       
   809         Locale.add_registration (dep, morph $> Element.satisfy_morphism
       
   810             (map (Element.morph_witness export') wits))
       
   811           (Element.eq_morphism (Context.theory_of context) eqns |>
       
   812             Option.map (rpair true))
       
   813           export context) (deps ~~ witss))
       
   814   end;
   796 
   815 
   797 fun gen_interpretation prep_expr parse_prop prep_attr
   816 fun gen_interpretation prep_expr parse_prop prep_attr
   798     expression equations theory =
   817     expression equations theory =
   799   let
   818   let
   800     val ((propss, deps, export), expr_ctxt) = ProofContext.init_global theory
   819     val ((propss, deps, export), expr_ctxt) = ProofContext.init_global theory
   803     val eqns = map (parse_prop expr_ctxt o snd) equations |> Syntax.check_terms expr_ctxt;
   822     val eqns = map (parse_prop expr_ctxt o snd) equations |> Syntax.check_terms expr_ctxt;
   804     val attrss = map ((apsnd o map) (prep_attr theory) o fst) equations;
   823     val attrss = map ((apsnd o map) (prep_attr theory) o fst) equations;
   805     val goal_ctxt = fold Variable.auto_fixes eqns expr_ctxt;
   824     val goal_ctxt = fold Variable.auto_fixes eqns expr_ctxt;
   806     val export' = Variable.export_morphism goal_ctxt expr_ctxt;
   825     val export' = Variable.export_morphism goal_ctxt expr_ctxt;
   807 
   826 
   808     fun note_eqns raw_eqns thy =
   827     fun after_qed witss eqns = (ProofContext.theory o Context.theory_map)
   809       let
   828       (note_eqns_register deps witss attrss eqns export export');
   810         val eqns = map (Morphism.thm (export' $> export)) raw_eqns;
       
   811         val eqn_attrss = map2 (fn attrs => fn eqn =>
       
   812           ((apsnd o map) (Attrib.attribute_i thy) attrs, [([eqn], [])])) attrss eqns;
       
   813         fun meta_rewrite thy =
       
   814           map (Local_Defs.meta_rewrite_rule (ProofContext.init_global thy) #> Drule.abs_def) o
       
   815             maps snd;
       
   816       in
       
   817         thy
       
   818         |> PureThy.note_thmss Thm.lemmaK eqn_attrss
       
   819         |-> (fn facts => `(fn thy => meta_rewrite thy facts))
       
   820       end;
       
   821 
       
   822     fun after_qed witss eqns = ProofContext.theory (note_eqns eqns
       
   823       #-> (fn eqns => fold (fn ((dep, morph), wits) =>
       
   824         fn thy => Context.theory_map
       
   825           (Locale.add_registration (dep, morph $> Element.satisfy_morphism
       
   826               (map (Element.morph_witness export') wits))
       
   827             (Element.eq_morphism thy eqns |> Option.map (rpair true))
       
   828             export) thy) (deps ~~ witss)));
       
   829 
   829 
   830   in Element.witness_proof_eqs after_qed propss eqns goal_ctxt end;
   830   in Element.witness_proof_eqs after_qed propss eqns goal_ctxt end;
       
   831 
       
   832 fun gen_interpret prep_expr parse_prop prep_attr
       
   833     expression equations int state =
       
   834   let
       
   835     val _ = Proof.assert_forward_or_chain state;
       
   836     val ctxt = Proof.context_of state;
       
   837     val theory = ProofContext.theory_of ctxt;
       
   838 
       
   839     val ((propss, deps, export), expr_ctxt) = prep_expr expression ctxt;
       
   840 
       
   841     val eqns = map (parse_prop expr_ctxt o snd) equations |> Syntax.check_terms expr_ctxt;
       
   842     val attrss = map ((apsnd o map) (prep_attr theory) o fst) equations;
       
   843     val goal_ctxt = fold Variable.auto_fixes eqns expr_ctxt;
       
   844     val export' = Variable.export_morphism goal_ctxt expr_ctxt;
       
   845 
       
   846     fun after_qed witss eqns = (Proof.map_context o Context.proof_map)
       
   847       (note_eqns_register deps witss attrss eqns export export');
       
   848   in
       
   849     state
       
   850     |> Element.witness_local_proof_eqs after_qed "interpret" propss eqns goal_ctxt int
       
   851   end;
   831 
   852 
   832 in
   853 in
   833 
   854 
   834 fun interpretation x = gen_interpretation cert_goal_expression (K I) (K I) x;
   855 fun interpretation x = gen_interpretation cert_goal_expression (K I) (K I) x;
   835 fun interpretation_cmd x = gen_interpretation read_goal_expression
   856 fun interpretation_cmd x = gen_interpretation read_goal_expression
       
   857   Syntax.parse_prop Attrib.intern_src x;
       
   858 
       
   859 fun interpret x = gen_interpret cert_goal_expression (K I) (K I) x;
       
   860 fun interpret_cmd x = gen_interpret read_goal_expression
   836   Syntax.parse_prop Attrib.intern_src x;
   861   Syntax.parse_prop Attrib.intern_src x;
   837 
   862 
   838 end;
   863 end;
   839 
   864 
   840 
   865 
   857 fun sublocale x = gen_sublocale cert_goal_expression (K I) x;
   882 fun sublocale x = gen_sublocale cert_goal_expression (K I) x;
   858 fun sublocale_cmd x = gen_sublocale read_goal_expression Locale.intern x;
   883 fun sublocale_cmd x = gen_sublocale read_goal_expression Locale.intern x;
   859 
   884 
   860 end;
   885 end;
   861 
   886 
   862 
   887 end;
   863 (** Interpretation in proof contexts **)
   888 
   864 
       
   865 local
       
   866 
       
   867 fun gen_interpret prep_expr expression int state =
       
   868   let
       
   869     val _ = Proof.assert_forward_or_chain state;
       
   870     val ctxt = Proof.context_of state;
       
   871 
       
   872     val ((propss, regs, export), goal_ctxt) = prep_expr expression ctxt;
       
   873 
       
   874     fun store_reg (name, morph) thms =
       
   875       let
       
   876         val morph' = morph $> Element.satisfy_morphism thms $> export;
       
   877       in Context.proof_map (Locale.activate_facts (name, morph')) end;
       
   878 
       
   879     fun after_qed wits =
       
   880       Proof.map_context (fold2 store_reg regs wits);
       
   881   in
       
   882     state
       
   883     |> Element.witness_local_proof after_qed "interpret" propss goal_ctxt int
       
   884   end;
       
   885 
       
   886 in
       
   887 
       
   888 fun interpret x = gen_interpret cert_goal_expression x;
       
   889 fun interpret_cmd x = gen_interpret read_goal_expression x;
       
   890 
       
   891 end;
       
   892 
       
   893 end;
       
   894