src/Pure/Isar/locale.ML
changeset 18899 a8e913c93578
parent 18890 34a9e466201f
child 18907 f984f22f1cb4
equal deleted inserted replaced
18898:e3d2aa8ba0e1 18899:a8e913c93578
    50   val local_asms_of: theory -> string ->
    50   val local_asms_of: theory -> string ->
    51     ((string * Attrib.src list) * term list) list
    51     ((string * Attrib.src list) * term list) list
    52   val global_asms_of: theory -> string ->
    52   val global_asms_of: theory -> string ->
    53     ((string * Attrib.src list) * term list) list
    53     ((string * Attrib.src list) * term list) list
    54 
    54 
    55   (* Processing of locale statements *)  (* FIXME export more abstract version *)
    55   (* Processing of locale statements *)
    56   val read_context_statement: xstring option -> Element.context element list ->
    56   val read_context_statement: xstring option -> Element.context element list ->
    57     (string * (string list * string list)) list list -> Proof.context ->
    57     (string * (string list * string list)) list list -> Proof.context ->
    58     string option * (cterm list * Proof.context) * (cterm list * Proof.context) *
    58     string option * (cterm list * Proof.context) * (cterm list * Proof.context) *
    59       (term * (term list * term list)) list list
    59       (term * (term list * term list)) list list
    60   val cert_context_statement: string option -> Element.context_i element list ->
    60   val cert_context_statement: string option -> Element.context_i element list ->
    90     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    90     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    91     cterm list * Proof.context ->
    91     cterm list * Proof.context ->
    92     ((string * thm list) list * (string * thm list) list) * Proof.context
    92     ((string * thm list) list * (string * thm list) list) * Proof.context
    93 
    93 
    94   val theorem: string -> Method.text option -> (thm list list -> theory -> theory) ->
    94   val theorem: string -> Method.text option -> (thm list list -> theory -> theory) ->
    95     string * Attrib.src list -> Element.context element list ->
    95     string * Attrib.src list -> Element.context element list -> Element.statement ->
    96     ((string * Attrib.src list) * (string * (string list * string list)) list) list ->
       
    97     theory -> Proof.state
    96     theory -> Proof.state
    98   val theorem_i: string -> Method.text option -> (thm list list -> theory -> theory) ->
    97   val theorem_i: string -> Method.text option -> (thm list list -> theory -> theory) ->
    99     string * attribute list -> Element.context_i element list ->
    98     string * attribute list -> Element.context_i element list -> Element.statement_i ->
   100     ((string * attribute list) * (term * (term list * term list)) list) list ->
       
   101     theory -> Proof.state
    99     theory -> Proof.state
   102   val theorem_in_locale: string -> Method.text option ->
   100   val theorem_in_locale: string -> Method.text option ->
   103     (thm list list -> thm list list -> theory -> theory) ->
   101     (thm list list -> thm list list -> theory -> theory) ->
   104     xstring -> string * Attrib.src list -> Element.context element list ->
   102     xstring -> string * Attrib.src list -> Element.context element list -> Element.statement ->
   105     ((string * Attrib.src list) * (string * (string list * string list)) list) list ->
       
   106     theory -> Proof.state
   103     theory -> Proof.state
   107   val theorem_in_locale_i: string -> Method.text option ->
   104   val theorem_in_locale_i: string -> Method.text option ->
   108     (thm list list -> thm list list -> theory -> theory) ->
   105     (thm list list -> thm list list -> theory -> theory) ->
   109     string -> string * Attrib.src list -> Element.context_i element list ->
   106     string -> string * Attrib.src list -> Element.context_i element list ->
   110     ((string * Attrib.src list) * (term * (term list * term list)) list) list ->
   107     (typ, term, Attrib.src) Element.stmt -> theory -> Proof.state
   111     theory -> Proof.state
       
   112   val smart_theorem: string -> xstring option ->
   108   val smart_theorem: string -> xstring option ->
   113     string * Attrib.src list -> Element.context element list ->
   109     string * Attrib.src list -> Element.context element list -> Element.statement ->
   114     ((string * Attrib.src list) * (string * (string list * string list)) list) list ->
       
   115     theory -> Proof.state
   110     theory -> Proof.state
   116   val interpretation: string * Attrib.src list -> expr -> string option list ->
   111   val interpretation: string * Attrib.src list -> expr -> string option list ->
   117     theory -> Proof.state
   112     theory -> Proof.state
   118   val interpretation_in_locale: xstring * expr -> theory -> Proof.state
   113   val interpretation_in_locale: xstring * expr -> theory -> Proof.state
   119   val interpret: string * Attrib.src list -> expr -> string option list ->
   114   val interpret: string * Attrib.src list -> expr -> string option list ->
  1106       end;
  1101       end;
  1107 
  1102 
  1108 
  1103 
  1109 fun finish_ext_elem parms _ (Fixes fixes, _) = Fixes (map (fn (x, _, mx) =>
  1104 fun finish_ext_elem parms _ (Fixes fixes, _) = Fixes (map (fn (x, _, mx) =>
  1110       (x, AList.lookup (op =) parms x, mx)) fixes)
  1105       (x, AList.lookup (op =) parms x, mx)) fixes)
  1111   | finish_ext_elem parms _ (Constrains csts, _) =
  1106   | finish_ext_elem parms _ (Constrains _, _) = Constrains []
  1112       (* FIXME fails if x is not a parameter *)
       
  1113       Constrains (map (fn (x, _) => (x, (the o AList.lookup (op =) parms) x)) csts)
       
  1114   | finish_ext_elem _ close (Assumes asms, propp) =
  1107   | finish_ext_elem _ close (Assumes asms, propp) =
  1115       close (Assumes (map #1 asms ~~ propp))
  1108       close (Assumes (map #1 asms ~~ propp))
  1116   | finish_ext_elem _ close (Defines defs, propp) =
  1109   | finish_ext_elem _ close (Defines defs, propp) =
  1117       close (Defines (map #1 defs ~~ map (fn [(t, (ps, []))] => (t, ps)) propp))
  1110       close (Defines (map #1 defs ~~ map (fn [(t, (ps, []))] => (t, ps)) propp))
  1118   | finish_ext_elem _ _ (Notes facts, _) = Notes facts;
  1111   | finish_ext_elem _ _ (Notes facts, _) = Notes facts;
  1744   {name = I, var = I, typ = I, term = I, fact = I, attrib = Attrib.intern_src thy});
  1737   {name = I, var = I, typ = I, term = I, fact = I, attrib = Attrib.intern_src thy});
  1745 
  1738 
  1746 fun global_goal prep_att =
  1739 fun global_goal prep_att =
  1747   Proof.global_goal ProofDisplay.present_results prep_att ProofContext.bind_propp_schematic_i;
  1740   Proof.global_goal ProofDisplay.present_results prep_att ProofContext.bind_propp_schematic_i;
  1748 
  1741 
  1749 fun gen_theorem prep_att prep_elem prep_stmt kind before_qed after_qed a raw_elems concl thy =
  1742 fun insert_facts [] = I
  1750   let
  1743   | insert_facts ths = Seq.hd o Proof.refine (Method.Basic (K (Method.insert ths)));
       
  1744 
       
  1745 fun conclusion (Element.Shows concl) = (([], concl), fn stmt => fn ctxt => ((stmt, []), ctxt))
       
  1746   | conclusion (Element.Obtains cases) = apfst (apfst (map Elem)) (Obtain.statement cases);
       
  1747 
       
  1748 fun gen_theorem prep_att prep_elem prep_stmt kind before_qed after_qed a raw_elems raw_concl thy =
       
  1749   let
       
  1750     val ((concl_elems, concl), mk_stmt) = conclusion raw_concl;
  1751     val thy_ctxt = ProofContext.init thy;
  1751     val thy_ctxt = ProofContext.init thy;
  1752     val elems = map (prep_elem thy) raw_elems;
  1752     val elems = map (prep_elem thy) (raw_elems @ concl_elems);
  1753     val (_, _, (view, ctxt), propp) = prep_stmt NONE elems (map snd concl) thy_ctxt;
  1753     val (_, _, (view, ctxt), propp) = prep_stmt NONE elems (map snd concl) thy_ctxt;
  1754     val ctxt' = ctxt |> ProofContext.add_view thy_ctxt view;
  1754     val ((stmt, facts), goal_ctxt) = ctxt
  1755     val stmt = map fst concl ~~ propp;
  1755       |> ProofContext.add_view thy_ctxt view
  1756   in global_goal prep_att kind before_qed after_qed (SOME "") a stmt ctxt' end;
  1756       |> mk_stmt (map fst concl ~~ propp);
       
  1757   in
       
  1758     global_goal prep_att kind before_qed after_qed (SOME "") a stmt goal_ctxt
       
  1759     |> insert_facts facts
       
  1760   end;
  1757 
  1761 
  1758 fun gen_theorem_in_locale prep_locale prep_src prep_elem prep_stmt no_target
  1762 fun gen_theorem_in_locale prep_locale prep_src prep_elem prep_stmt no_target
  1759     kind before_qed after_qed raw_loc (name, atts) raw_elems concl thy =
  1763     kind before_qed after_qed raw_loc (name, atts) raw_elems raw_concl thy =
  1760   let
  1764   let
       
  1765     val ((concl_elems, concl), mk_stmt) = conclusion raw_concl;
  1761     val loc = prep_locale thy raw_loc;
  1766     val loc = prep_locale thy raw_loc;
  1762     val loc_atts = map (prep_src thy) atts;
  1767     val loc_atts = map (prep_src thy) atts;
  1763     val loc_attss = map (map (prep_src thy) o snd o fst) concl;
  1768     val loc_attss = map (map (prep_src thy) o snd o fst) concl;
  1764     val target = if no_target then NONE else SOME (extern thy loc);
  1769     val target = if no_target then NONE else SOME (extern thy loc);
  1765     val elems = map (prep_elem thy) raw_elems;
  1770     val elems = map (prep_elem thy) (raw_elems @ concl_elems);
  1766     val names = map (fst o fst) concl;
  1771     val names = map (fst o fst) concl;
  1767 
  1772 
  1768     val thy_ctxt = ProofContext.init thy;
  1773     val thy_ctxt = ProofContext.init thy;
  1769     val (_, (loc_view, loc_ctxt), (elems_view, elems_ctxt), propp) =
  1774     val (_, (loc_view, loc_ctxt), (elems_view, elems_ctxt), propp) =
  1770       prep_stmt (SOME raw_loc) elems (map snd concl) thy_ctxt;
  1775       prep_stmt (SOME raw_loc) elems (map snd concl) thy_ctxt;
  1772       |> ProofContext.add_view loc_ctxt elems_view
  1777       |> ProofContext.add_view loc_ctxt elems_view
  1773       |> ProofContext.add_view thy_ctxt loc_view;
  1778       |> ProofContext.add_view thy_ctxt loc_view;
  1774     val loc_ctxt' = loc_ctxt
  1779     val loc_ctxt' = loc_ctxt
  1775       |> ProofContext.add_view thy_ctxt loc_view;
  1780       |> ProofContext.add_view thy_ctxt loc_view;
  1776 
  1781 
  1777     val stmt = map (apsnd (K []) o fst) concl ~~ propp;
  1782     val ((stmt, facts), goal_ctxt) =
       
  1783       elems_ctxt' |> mk_stmt (map (apsnd (K []) o fst) concl ~~ propp);
  1778 
  1784 
  1779     fun after_qed' results =
  1785     fun after_qed' results =
  1780       let val loc_results = results |> (map o map)
  1786       let val loc_results = results |> (map o map)
  1781           (ProofContext.export_standard elems_ctxt' loc_ctxt') in
  1787           (ProofContext.export_standard goal_ctxt loc_ctxt') in
  1782         curry (locale_results kind loc ((names ~~ loc_attss) ~~ loc_results)) loc_ctxt
  1788         curry (locale_results kind loc ((names ~~ loc_attss) ~~ loc_results)) loc_ctxt
  1783         #-> (fn res =>
  1789         #-> (fn res =>
  1784           if name = "" andalso null loc_atts then I
  1790           if name = "" andalso null loc_atts then I
  1785           else #2 o locale_results kind loc [((name, loc_atts), List.concat (map #2 res))])
  1791           else #2 o locale_results kind loc [((name, loc_atts), List.concat (map #2 res))])
  1786         #> #2
  1792         #> #2
  1787         #> after_qed loc_results results
  1793         #> after_qed loc_results results
  1788       end;
  1794       end;
  1789   in global_goal (K I) kind before_qed after_qed' target (name, []) stmt elems_ctxt' end;
  1795   in
       
  1796     global_goal (K I) kind before_qed after_qed' target (name, []) stmt goal_ctxt
       
  1797     |> insert_facts facts
       
  1798   end;
  1790 
  1799 
  1791 in
  1800 in
  1792 
  1801 
  1793 val theorem = gen_theorem Attrib.attribute intern_attrib read_context_statement;
  1802 val theorem = gen_theorem Attrib.attribute intern_attrib read_context_statement;
  1794 val theorem_i = gen_theorem (K I) (K I) cert_context_statement;
  1803 val theorem_i = gen_theorem (K I) (K I) cert_context_statement;
  1800   cert_context_statement false;
  1809   cert_context_statement false;
  1801 
  1810 
  1802 val theorem_in_locale_no_target = gen_theorem_in_locale (K I) (K I) (K I)
  1811 val theorem_in_locale_no_target = gen_theorem_in_locale (K I) (K I) (K I)
  1803   cert_context_statement true;
  1812   cert_context_statement true;
  1804 
  1813 
  1805 fun smart_theorem kind NONE a [] concl =
  1814 fun smart_theorem kind NONE a [] (Element.Shows concl) =
  1806       Proof.theorem kind NONE (K I) (SOME "") a concl o ProofContext.init
  1815       Proof.theorem kind NONE (K I) (SOME "") a concl o ProofContext.init
  1807   | smart_theorem kind NONE a elems concl =
  1816   | smart_theorem kind NONE a elems concl =
  1808       theorem kind NONE (K I) a elems concl
  1817       theorem kind NONE (K I) a elems concl
  1809   | smart_theorem kind (SOME loc) a elems concl =
  1818   | smart_theorem kind (SOME loc) a elems concl =
  1810       theorem_in_locale kind NONE (K (K I)) loc a elems concl;
  1819       theorem_in_locale kind NONE (K (K I)) loc a elems concl;
  2142     val (propss, activate) = prep_registration_in_locale target expr thy;
  2151     val (propss, activate) = prep_registration_in_locale target expr thy;
  2143     val kind = goal_name thy "interpretation" (SOME target) propss;
  2152     val kind = goal_name thy "interpretation" (SOME target) propss;
  2144     val after_qed = K (activate o prep_result propss);
  2153     val after_qed = K (activate o prep_result propss);
  2145   in
  2154   in
  2146     thy
  2155     thy
  2147     |> theorem_in_locale_no_target kind NONE after_qed target ("", []) [] (prep_propp propss)
  2156     |> theorem_in_locale_no_target kind NONE after_qed target ("", []) []
       
  2157       (Element.Shows (prep_propp propss))
  2148     |> refine_protected
  2158     |> refine_protected
  2149   end;
  2159   end;
  2150 
  2160 
  2151 fun interpret (prfx, atts) expr insts int state =
  2161 fun interpret (prfx, atts) expr insts int state =
  2152   let
  2162   let