src/Pure/Isar/locale.ML
changeset 21005 8f3896f0e5af
parent 20965 75ffb934929d
child 21035 326c15917a33
equal deleted inserted replaced
21004:081674431d68 21005:8f3896f0e5af
    85   (* Storing results *)
    85   (* Storing results *)
    86   val add_thmss: string -> string ->
    86   val add_thmss: string -> string ->
    87     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    87     ((string * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    88     Proof.context -> (string * thm list) list * Proof.context
    88     Proof.context -> (string * thm list) list * Proof.context
    89   val add_term_syntax: string -> (Proof.context -> Proof.context) -> Proof.context -> Proof.context
    89   val add_term_syntax: string -> (Proof.context -> Proof.context) -> Proof.context -> Proof.context
       
    90 
       
    91   val interpretation: (Proof.context -> Proof.context) ->
       
    92     string * Attrib.src list -> expr -> string option list ->
       
    93     theory -> Proof.state
       
    94   val interpretation_in_locale: (Proof.context -> Proof.context) ->
       
    95     xstring * expr -> theory -> Proof.state
       
    96   val interpret: (Proof.state -> Proof.state Seq.seq) ->
       
    97     string * Attrib.src list -> expr -> string option list ->
       
    98     bool -> Proof.state -> Proof.state
    90 
    99 
    91   val theorem: string -> Method.text option ->
   100   val theorem: string -> Method.text option ->
    92     (thm list list -> Proof.context -> Proof.context) ->
   101     (thm list list -> Proof.context -> Proof.context) ->
    93     string * Attrib.src list -> Element.context element list -> Element.statement ->
   102     string * Attrib.src list -> Element.context element list -> Element.statement ->
    94     theory -> Proof.state
   103     theory -> Proof.state
   105     string -> string * Attrib.src list -> Element.context_i element list ->
   114     string -> string * Attrib.src list -> Element.context_i element list ->
   106     Element.statement_i -> theory -> Proof.state
   115     Element.statement_i -> theory -> Proof.state
   107   val smart_theorem: string -> xstring option ->
   116   val smart_theorem: string -> xstring option ->
   108     string * Attrib.src list -> Element.context element list -> Element.statement ->
   117     string * Attrib.src list -> Element.context element list -> Element.statement ->
   109     theory -> Proof.state
   118     theory -> Proof.state
   110   val interpretation: (Proof.context -> Proof.context) ->
       
   111     string * Attrib.src list -> expr -> string option list ->
       
   112     theory -> Proof.state
       
   113   val interpretation_in_locale: (Proof.context -> Proof.context) ->
       
   114     xstring * expr -> theory -> Proof.state
       
   115   val interpret: (Proof.state -> Proof.state Seq.seq) ->
       
   116     string * Attrib.src list -> expr -> string option list ->
       
   117     bool -> Proof.state -> Proof.state
       
   118 end;
   119 end;
   119 
   120 
   120 structure Locale: LOCALE =
   121 structure Locale: LOCALE =
   121 struct
   122 struct
   122 
   123 
  1817   add_locale_i true "struct" empty [Fixes [(Name.internal "S", NONE, Structure)]] #>
  1818   add_locale_i true "struct" empty [Fixes [(Name.internal "S", NONE, Structure)]] #>
  1818   snd #> ProofContext.theory_of);
  1819   snd #> ProofContext.theory_of);
  1819 
  1820 
  1820 
  1821 
  1821 
  1822 
  1822 (** locale goals **)
       
  1823 
       
  1824 local
       
  1825 
       
  1826 fun intern_attrib thy = map_elem (Element.map_ctxt
       
  1827   {name = I, var = I, typ = I, term = I, fact = I, attrib = Attrib.intern_src thy});
       
  1828 
       
  1829 val global_goal = Proof.global_goal ProofDisplay.present_results
       
  1830   Attrib.attribute_i ProofContext.bind_propp_schematic_i;
       
  1831 
       
  1832 fun mk_shows prep_att stmt ctxt =
       
  1833   ((Attrib.map_specs prep_att stmt, []), fold (fold (Variable.fix_frees o fst) o snd) stmt ctxt);
       
  1834 
       
  1835 fun conclusion prep_att (Element.Shows concl) = (([], concl), mk_shows prep_att)
       
  1836   | conclusion _ (Element.Obtains cases) = apfst (apfst (map Elem)) (Obtain.statement cases);
       
  1837 
       
  1838 fun gen_theorem prep_src prep_elem prep_stmt
       
  1839     kind before_qed after_qed (name, raw_atts) raw_elems raw_concl thy =
       
  1840   let
       
  1841     val atts = map (prep_src thy) raw_atts;
       
  1842     val ((concl_elems, concl), mk_stmt) = conclusion (prep_src thy) raw_concl;
       
  1843 
       
  1844     val thy_ctxt = ProofContext.init thy;
       
  1845     val elems = map (prep_elem thy) (raw_elems @ concl_elems);
       
  1846     val (_, _, elems_ctxt, propp) = prep_stmt NONE elems (map snd concl) thy_ctxt;
       
  1847     val ((stmt, facts), goal_ctxt) = mk_stmt (map fst concl ~~ propp) elems_ctxt;
       
  1848 
       
  1849     fun after_qed' results goal_ctxt' =
       
  1850       thy_ctxt
       
  1851       |> ProofContext.transfer (ProofContext.theory_of goal_ctxt')
       
  1852       |> after_qed results;
       
  1853   in
       
  1854     global_goal kind before_qed after_qed' (SOME "") (name, atts) stmt goal_ctxt
       
  1855     |> Proof.refine_insert facts
       
  1856   end;
       
  1857 
       
  1858 fun gen_theorem_in_locale prep_locale prep_src prep_elem prep_stmt no_target
       
  1859     kind before_qed after_qed raw_loc (name, atts) raw_elems raw_concl thy =
       
  1860   let
       
  1861     val ((concl_elems, concl), mk_stmt) = conclusion (prep_src thy) raw_concl;
       
  1862     val loc = prep_locale thy raw_loc;
       
  1863     val loc_atts = map (prep_src thy) atts;
       
  1864     val loc_attss = map (map (prep_src thy) o snd o fst) concl;
       
  1865     val target = if no_target then NONE else SOME (extern thy loc);
       
  1866     val elems = map (prep_elem thy) (raw_elems @ concl_elems);
       
  1867     val names = map (fst o fst) concl;
       
  1868 
       
  1869     val thy_ctxt = init_term_syntax loc (ProofContext.init thy);
       
  1870     val (_, loc_ctxt, elems_ctxt, propp) =
       
  1871       prep_stmt (SOME raw_loc) elems (map snd concl) thy_ctxt;
       
  1872     val ((stmt, facts), goal_ctxt) = mk_stmt (map (apsnd (K []) o fst) concl ~~ propp) elems_ctxt;
       
  1873 
       
  1874     fun after_qed' results goal_ctxt' =
       
  1875       let
       
  1876         val loc_ctxt' = loc_ctxt |> ProofContext.transfer (ProofContext.theory_of goal_ctxt');
       
  1877         val loc_results = results |> burrow (ProofContext.export_standard goal_ctxt' loc_ctxt');
       
  1878       in
       
  1879         loc_ctxt'
       
  1880         |> locale_results kind loc ((names ~~ loc_attss) ~~ loc_results)
       
  1881         |-> (fn res =>
       
  1882           if name = "" andalso null loc_atts then I
       
  1883           else #2 o locale_results kind loc [((name, loc_atts), maps #2 res)])
       
  1884         |> after_qed loc_results results
       
  1885       end;
       
  1886   in
       
  1887     global_goal kind before_qed after_qed' target (name, []) stmt goal_ctxt
       
  1888     |> Proof.refine_insert facts
       
  1889   end;
       
  1890 
       
  1891 in
       
  1892 
       
  1893 val theorem = gen_theorem Attrib.intern_src intern_attrib read_context_statement;
       
  1894 val theorem_i = gen_theorem (K I) (K I) cert_context_statement;
       
  1895 
       
  1896 val theorem_in_locale = gen_theorem_in_locale intern Attrib.intern_src intern_attrib
       
  1897   read_context_statement false;
       
  1898 
       
  1899 val theorem_in_locale_i = gen_theorem_in_locale (K I) (K I) (K I)
       
  1900   cert_context_statement false;
       
  1901 
       
  1902 val theorem_in_locale_no_target = gen_theorem_in_locale (K I) (K I) (K I)
       
  1903   cert_context_statement true;
       
  1904 
       
  1905 fun smart_theorem kind NONE a [] (Element.Shows concl) =
       
  1906       Proof.theorem kind NONE (K I) (SOME "") a concl o ProofContext.init
       
  1907   | smart_theorem kind NONE a elems concl =
       
  1908       theorem kind NONE (K I) a elems concl
       
  1909   | smart_theorem kind (SOME loc) a elems concl =
       
  1910       theorem_in_locale kind NONE (K (K I)) loc a elems concl;
       
  1911 
       
  1912 end;
       
  1913 
       
  1914 
  1823 
  1915 (** Normalisation of locale statements ---
  1824 (** Normalisation of locale statements ---
  1916     discharges goals implied by interpretations **)
  1825     discharges goals implied by interpretations **)
  1917 
  1826 
  1918 local
  1827 local
  2244             |> fold activate_reg regs
  2153             |> fold activate_reg regs
  2245       end;
  2154       end;
  2246 
  2155 
  2247   in (propss, activate) end;
  2156   in (propss, activate) end;
  2248 
  2157 
  2249 fun prep_propp propss = propss |> map (fn ((name, _), props) =>
  2158 fun prep_propp propss = propss |> map (fn (_, props) =>
  2250   (("", []), map (rpair [] o Element.mark_witness) props));
  2159   (("", []), map (rpair [] o Element.mark_witness) props));
  2251 
  2160 
  2252 fun prep_result propps thmss =
  2161 fun prep_result propps thmss =
  2253   ListPair.map (fn ((_, props), thms) => map2 Element.make_witness props thms) (propps, thmss);
  2162   ListPair.map (fn ((_, props), thms) => map2 Element.make_witness props thms) (propps, thmss);
  2254 
  2163 
  2255 fun goal_name thy kind target propss =
  2164 fun goal_name thy kind target propss =
  2256     kind ^ Proof.goal_names (Option.map (extern thy) target) ""
  2165     kind ^ Proof.goal_names (Option.map (extern thy) target) ""
  2257       (propss |> map (fn ((name, _), _) => extern thy name));
  2166       (propss |> map (fn ((name, _), _) => extern thy name));
       
  2167 
       
  2168 val global_goal = Proof.global_goal ProofDisplay.present_results
       
  2169   Attrib.attribute_i ProofContext.bind_propp_schematic_i;
  2258 
  2170 
  2259 in
  2171 in
  2260 
  2172 
  2261 fun interpretation after_qed (prfx, atts) expr insts thy =
  2173 fun interpretation after_qed (prfx, atts) expr insts thy =
  2262   let
  2174   let
  2274 fun interpretation_in_locale after_qed (raw_target, expr) thy =
  2186 fun interpretation_in_locale after_qed (raw_target, expr) thy =
  2275   let
  2187   let
  2276     val target = intern thy raw_target;
  2188     val target = intern thy raw_target;
  2277     val (propss, activate) = prep_registration_in_locale target expr thy;
  2189     val (propss, activate) = prep_registration_in_locale target expr thy;
  2278     val kind = goal_name thy "interpretation" (SOME target) propss;
  2190     val kind = goal_name thy "interpretation" (SOME target) propss;
  2279     fun after_qed' _ results =
  2191     val raw_stmt = prep_propp propss;
       
  2192 
       
  2193     val (_, _, goal_ctxt, propp) = thy
       
  2194       |> ProofContext.init |> init_term_syntax target
       
  2195       |> cert_context_statement (SOME target) [] (map snd raw_stmt)
       
  2196 
       
  2197     fun after_qed' results =
  2280       ProofContext.theory (activate (prep_result propss results))
  2198       ProofContext.theory (activate (prep_result propss results))
  2281       #> after_qed;
  2199       #> after_qed;
  2282   in
  2200   in
  2283     thy
  2201     goal_ctxt
  2284     |> theorem_in_locale_no_target kind NONE after_qed' target ("", []) []
  2202     |> global_goal kind NONE after_qed' NONE ("", []) (map fst raw_stmt ~~ propp)
  2285       (Element.Shows (prep_propp propss))
       
  2286     |> Element.refine_witness |> Seq.hd
  2203     |> Element.refine_witness |> Seq.hd
  2287   end;
  2204   end;
  2288 
  2205 
  2289 fun interpret after_qed (prfx, atts) expr insts int state =
  2206 fun interpret after_qed (prfx, atts) expr insts int state =
  2290   let
  2207   let
  2303     |> Element.refine_witness |> Seq.hd
  2220     |> Element.refine_witness |> Seq.hd
  2304   end;
  2221   end;
  2305 
  2222 
  2306 end;
  2223 end;
  2307 
  2224 
       
  2225 
       
  2226 
       
  2227 (** locale goals **)
       
  2228 
       
  2229 local
       
  2230 
       
  2231 val global_goal = Proof.global_goal ProofDisplay.present_results
       
  2232   Attrib.attribute_i ProofContext.bind_propp_schematic_i;
       
  2233 
       
  2234 fun intern_attrib thy = map_elem (Element.map_ctxt
       
  2235   {name = I, var = I, typ = I, term = I, fact = I, attrib = Attrib.intern_src thy});
       
  2236 
       
  2237 fun mk_shows prep_att stmt ctxt =
       
  2238   ((Attrib.map_specs prep_att stmt, []), fold (fold (Variable.fix_frees o fst) o snd) stmt ctxt);
       
  2239 
       
  2240 fun conclusion prep_att (Element.Shows concl) = (([], concl), mk_shows prep_att)
       
  2241   | conclusion _ (Element.Obtains cases) = apfst (apfst (map Elem)) (Obtain.statement cases);
       
  2242 
       
  2243 fun gen_theorem prep_src prep_elem prep_stmt
       
  2244     kind before_qed after_qed (name, raw_atts) raw_elems raw_concl thy =
       
  2245   let
       
  2246     val atts = map (prep_src thy) raw_atts;
       
  2247     val ((concl_elems, concl), mk_stmt) = conclusion (prep_src thy) raw_concl;
       
  2248 
       
  2249     val thy_ctxt = ProofContext.init thy;
       
  2250     val elems = map (prep_elem thy) (raw_elems @ concl_elems);
       
  2251     val (_, _, elems_ctxt, propp) = prep_stmt NONE elems (map snd concl) thy_ctxt;
       
  2252     val ((stmt, facts), goal_ctxt) = mk_stmt (map fst concl ~~ propp) elems_ctxt;
       
  2253 
       
  2254     fun after_qed' results goal_ctxt' =
       
  2255       thy_ctxt
       
  2256       |> ProofContext.transfer (ProofContext.theory_of goal_ctxt')
       
  2257       |> after_qed results;
       
  2258   in
       
  2259     global_goal kind before_qed after_qed' (SOME "") (name, atts) stmt goal_ctxt
       
  2260     |> Proof.refine_insert facts
       
  2261   end;
       
  2262 
       
  2263 fun gen_theorem_in_locale prep_locale prep_src prep_elem prep_stmt
       
  2264     kind before_qed after_qed raw_loc (name, atts) raw_elems raw_concl thy =
       
  2265   let
       
  2266     val ((concl_elems, concl), mk_stmt) = conclusion (prep_src thy) raw_concl;
       
  2267     val loc = prep_locale thy raw_loc;
       
  2268     val loc_atts = map (prep_src thy) atts;
       
  2269     val loc_attss = map (map (prep_src thy) o snd o fst) concl;
       
  2270     val elems = map (prep_elem thy) (raw_elems @ concl_elems);
       
  2271     val names = map (fst o fst) concl;
       
  2272 
       
  2273     val thy_ctxt = init_term_syntax loc (ProofContext.init thy);
       
  2274     val (_, loc_ctxt, elems_ctxt, propp) =
       
  2275       prep_stmt (SOME raw_loc) elems (map snd concl) thy_ctxt;
       
  2276     val ((stmt, facts), goal_ctxt) = mk_stmt (map (apsnd (K []) o fst) concl ~~ propp) elems_ctxt;
       
  2277 
       
  2278     fun after_qed' results goal_ctxt' =
       
  2279       let
       
  2280         val loc_ctxt' = loc_ctxt |> ProofContext.transfer (ProofContext.theory_of goal_ctxt');
       
  2281         val loc_results = results |> burrow (ProofContext.export_standard goal_ctxt' loc_ctxt');
       
  2282       in
       
  2283         loc_ctxt'
       
  2284         |> locale_results kind loc ((names ~~ loc_attss) ~~ loc_results)
       
  2285         |-> (fn res =>
       
  2286           if name = "" andalso null loc_atts then I
       
  2287           else #2 o locale_results kind loc [((name, loc_atts), maps #2 res)])
       
  2288         |> after_qed loc_results results
       
  2289       end;
       
  2290   in
       
  2291     global_goal kind before_qed after_qed' (SOME (extern thy loc)) (name, []) stmt goal_ctxt
       
  2292     |> Proof.refine_insert facts
       
  2293   end;
       
  2294 
       
  2295 in
       
  2296 
       
  2297 val theorem = gen_theorem Attrib.intern_src intern_attrib read_context_statement;
       
  2298 val theorem_i = gen_theorem (K I) (K I) cert_context_statement;
       
  2299 
       
  2300 val theorem_in_locale =
       
  2301   gen_theorem_in_locale intern Attrib.intern_src intern_attrib read_context_statement;
       
  2302 
       
  2303 val theorem_in_locale_i =
       
  2304   gen_theorem_in_locale (K I) (K I) (K I) cert_context_statement;
       
  2305 
       
  2306 fun smart_theorem kind NONE a [] (Element.Shows concl) =
       
  2307       Proof.theorem kind NONE (K I) (SOME "") a concl o ProofContext.init
       
  2308   | smart_theorem kind NONE a elems concl =
       
  2309       theorem kind NONE (K I) a elems concl
       
  2310   | smart_theorem kind (SOME loc) a elems concl =
       
  2311       theorem_in_locale kind NONE (K (K I)) loc a elems concl;
       
  2312 
  2308 end;
  2313 end;
       
  2314 
       
  2315 end;