src/Pure/Isar/locale.ML
changeset 15598 4ab52355bb53
parent 15596 8665d08085df
child 15623 8b40f741597c
equal deleted inserted replaced
15597:b5f5722ed703 15598:4ab52355bb53
    88   val instantiate: string -> string * context attribute list
    88   val instantiate: string -> string * context attribute list
    89     -> thm list option -> context -> context
    89     -> thm list option -> context -> context
    90   val prep_registration:
    90   val prep_registration:
    91     string * theory attribute list -> expr -> string option list -> theory ->
    91     string * theory attribute list -> expr -> string option list -> theory ->
    92     theory * ((string * term list) * term list) list * (theory -> theory)
    92     theory * ((string * term list) * term list) list * (theory -> theory)
    93   val global_activate_thm:
    93   val global_add_witness:
    94     string * term list -> thm -> theory -> theory
    94     string * term list -> thm -> theory -> theory
    95 
    95 
    96   val setup: (theory -> theory) list
    96   val setup: (theory -> theory) list
    97 end;
    97 end;
    98 
    98 
   216       in
   216       in
   217         Symtab.update ((name, Termlisttab.update_new ((ps, (attn, [])), tab)),
   217         Symtab.update ((name, Termlisttab.update_new ((ps, (attn, [])), tab)),
   218           regs)
   218           regs)
   219       end handle Termlisttab.DUP _ => regs));
   219       end handle Termlisttab.DUP _ => regs));
   220 
   220 
   221 (* add theorem to registration in theory,
   221 (* add witness theorem to registration in theory,
   222    ignored if registration not present *)
   222    ignored if registration not present *)
   223 
   223 
   224 fun global_put_registration_thm (name, ps) thm =
   224 fun global_add_witness (name, ps) thm =
   225   LocalesData.map (fn (space, locs, regs) =>
   225   LocalesData.map (fn (space, locs, regs) =>
   226     (space, locs, let
   226     (space, locs, let
   227         val tab = valOf (Symtab.lookup (regs, name));
   227         val tab = valOf (Symtab.lookup (regs, name));
   228         val (x, thms) = valOf (Termlisttab.lookup (tab, ps));
   228         val (x, thms) = valOf (Termlisttab.lookup (tab, ps));
   229       in
   229       in
   269         Pretty.list "(" ")" (map prt_term ts)]
   269         Pretty.list "(" ")" (map prt_term ts)]
   270 end;
   270 end;
   271     val loc_regs = Symtab.lookup (regs, loc_int);
   271     val loc_regs = Symtab.lookup (regs, loc_int);
   272   in
   272   in
   273     (case loc_regs of
   273     (case loc_regs of
   274         NONE => Pretty.str "No registrations."
   274         NONE => Pretty.str "No interpretations."
   275       | SOME r => Pretty.big_list "registrations:"
   275       | SOME r => Pretty.big_list "interpretations:"
   276           (map prt_inst (Termlisttab.dest r)))
   276           (map prt_inst (Termlisttab.dest r)))
   277     |> Pretty.writeln
   277     |> Pretty.writeln
   278   end;
   278   end;
   279 
   279 
   280 
   280 
  1608 
  1608 
  1609 end;
  1609 end;
  1610 
  1610 
  1611 
  1611 
  1612 
  1612 
  1613 (** Registration commands **)
  1613 (** Interpretation commands **)
  1614 
  1614 
  1615 local
  1615 local
  1616 
  1616 
  1617 (** instantiate free vars **)
  1617 (** instantiate free vars **)
  1618 
  1618 
  1759   | activate_facts_elem _ _ (thy, Defines _) = thy
  1759   | activate_facts_elem _ _ (thy, Defines _) = thy
  1760   | activate_facts_elem disch (prfx, atts) (thy, Notes facts) =
  1760   | activate_facts_elem disch (prfx, atts) (thy, Notes facts) =
  1761       let
  1761       let
  1762         (* extract theory attributes *)
  1762         (* extract theory attributes *)
  1763         val (Notes facts) = map_attrib_element_i fst (Notes facts);
  1763         val (Notes facts) = map_attrib_element_i fst (Notes facts);
       
  1764         (* add attributs from registration *)
  1764         val facts' = map (apfst (apsnd (fn a => a @ atts))) facts;
  1765         val facts' = map (apfst (apsnd (fn a => a @ atts))) facts;
  1765         val facts'' = map (apsnd (map (apfst (map disch)))) facts';
  1766         (* discharge hyps and varify *)
       
  1767         val facts'' = map (apsnd (map (apfst (map (Drule.standard o disch))))) facts';
  1766       in
  1768       in
  1767         fst (note_thmss_qualified' "" prfx facts thy)
  1769         fst (note_thmss_qualified' "" prfx facts'' thy)
  1768       end;
  1770       end;
  1769 
  1771 
  1770 fun activate_facts_elems disch (thy, (id, elems)) =
  1772 fun activate_facts_elems disch (thy, (id, elems)) =
  1771       let
  1773       let
  1772         val ((prfx, atts2), _) = valOf (global_get_registration thy id)
  1774         val ((prfx, atts2), _) = valOf (global_get_registration thy id)
  1780       let
  1782       let
  1781         val prems = List.concat (List.mapPartial (fn (id, _) =>
  1783         val prems = List.concat (List.mapPartial (fn (id, _) =>
  1782               Option.map snd (global_get_registration thy id)
  1784               Option.map snd (global_get_registration thy id)
  1783                 handle Option => error ("(internal) unknown registration of " ^
  1785                 handle Option => error ("(internal) unknown registration of " ^
  1784                   quote (fst id) ^ " while activating facts.")) all_elemss);
  1786                   quote (fst id) ^ " while activating facts.")) all_elemss);
  1785         fun disch thm = let
  1787       in Library.foldl (activate_facts_elems (Drule.satisfy_hyps prems))
  1786           in Drule.satisfy_hyps prems thm end;
  1788         (thy, new_elemss) end;
  1787       in Library.foldl (activate_facts_elems disch) (thy, new_elemss) end;
       
  1788 
  1789 
  1789 in
  1790 in
  1790 
  1791 
  1791 fun prep_registration attn expr insts thy =
  1792 fun prep_registration attn expr insts thy =
  1792   let
  1793   let
  1801           read_elemss do_close ctxt [] raw_elemss [];
  1802           read_elemss do_close ctxt [] raw_elemss [];
  1802 
  1803 
  1803 
  1804 
  1804     (** compute instantiation **)
  1805     (** compute instantiation **)
  1805 
  1806 
  1806     (* user input *)
  1807     (* check user input *)
  1807     val insts = if length parms < length insts
  1808     val insts = if length parms < length insts
  1808          then error "More arguments than parameters in instantiation."
  1809          then error "More arguments than parameters in instantiation."
  1809          else insts @ replicate (length parms - length insts) NONE;
  1810          else insts @ replicate (length parms - length insts) NONE;
       
  1811 
  1810     val (ps, pTs) = split_list parms;
  1812     val (ps, pTs) = split_list parms;
  1811     val pvTs = map Type.varifyT pTs;
  1813     val pvTs = map Type.varifyT pTs;
       
  1814 
       
  1815     (* instantiations given by user *)
  1812     val given = List.mapPartial (fn (_, (NONE, _)) => NONE
  1816     val given = List.mapPartial (fn (_, (NONE, _)) => NONE
  1813          | (x, (SOME inst, T)) => SOME (x, (inst, T))) (ps ~~ (insts ~~ pvTs));
  1817          | (x, (SOME inst, T)) => SOME (x, (inst, T))) (ps ~~ (insts ~~ pvTs));
  1814     val (given_ps, given_insts) = split_list given;
  1818     val (given_ps, given_insts) = split_list given;
  1815     val tvars = foldr Term.add_typ_tvars [] pvTs;
  1819     val tvars = foldr Term.add_typ_tvars [] pvTs;
  1816     val used = foldr Term.add_typ_varnames [] pvTs;
  1820     val used = foldr Term.add_typ_varnames [] pvTs;
  1817     fun sorts (a, i) = assoc (tvars, (a, i));
  1821     fun sorts (a, i) = assoc (tvars, (a, i));
  1818     val (tvs, tvinst) = Sign.read_def_terms (sign, K NONE, sorts) used true
  1822     val (vs, vinst) = Sign.read_def_terms (sign, K NONE, sorts) used true
  1819          given_insts;
  1823          given_insts;
  1820     val tinst = Symtab.make (map (fn ((x, 0), T) => (x, Type.unvarifyT T)
  1824     (* replace new types (which are TFrees) by ones with new names *)
  1821                   | ((_, n), _) => error "Var in prep_registration") tvinst);
  1825     val new_Tnames = foldr Term.add_term_tfree_names [] vs;
  1822     val inst = Symtab.make (given_ps ~~ map Logic.unvarify tvs);
  1826     val new_Tnames' = Term.invent_names used "'a" (length new_Tnames);
       
  1827     val renameT = Term.map_type_tfree (fn (a, s) =>
       
  1828           TFree (valOf (assoc (new_Tnames ~~ new_Tnames', a)), s));
       
  1829     val rename = Term.map_term_types renameT;
       
  1830 
       
  1831     val tinst = Symtab.make (map
       
  1832                 (fn ((x, 0), T) => (x, T |> renameT |> Type.unvarifyT)
       
  1833                   | ((_, n), _) => error "Var in prep_registration") vinst);
       
  1834     val inst = Symtab.make (given_ps ~~ map (Logic.unvarify o rename) vs);
  1823 
  1835 
  1824     (* defined params without user input *)
  1836     (* defined params without user input *)
  1825     val not_given = List.mapPartial (fn (x, (NONE, T)) => SOME (x, T)
  1837     val not_given = List.mapPartial (fn (x, (NONE, T)) => SOME (x, T)
  1826          | (_, (SOME _, _)) => NONE) (ps ~~ (insts ~~ pTs));
  1838          | (_, (SOME _, _)) => NONE) (ps ~~ (insts ~~ pTs));
  1827     fun add_def ((inst, tinst), (p, pT)) =
  1839     fun add_def ((inst, tinst), (p, pT)) =
  1834     val (inst, tinst) = Library.foldl add_def ((inst, tinst), not_given);
  1846     val (inst, tinst) = Library.foldl add_def ((inst, tinst), not_given);
  1835   
  1847   
  1836 
  1848 
  1837     (** compute proof obligations **)
  1849     (** compute proof obligations **)
  1838 
  1850 
  1839     (* restore small ids *)
  1851     (* restore "small" ids *)
  1840     val ids' = map (fn ((n, ps), _) =>
  1852     val ids' = map (fn ((n, ps), _) =>
  1841           (n, map (fn p => Free (p, valOf (assoc (parms, p)))) ps)) ids;
  1853           (n, map (fn p => Free (p, valOf (assoc (parms, p)))) ps)) ids;
  1842 
  1854 
  1843     (* instantiate ids and elements *)
  1855     (* instantiate ids and elements *)
  1844     val inst_elemss = map
  1856     val inst_elemss = map
  1845           (fn (id, (_, elems)) => inst_elems sign (inst, tinst) (id, 
  1857           (fn (id, (_, elems)) => inst_elems sign (inst, tinst) (id, 
  1846             map (fn Int e => e) elems)) 
  1858             map (fn Int e => e) elems)) 
  1847           (ids' ~~ all_elemss);
  1859           (ids' ~~ all_elemss);
  1848 
  1860 
  1849 (*
       
  1850     (* varify ids, props are varified after they are proved *)
       
  1851     val inst_elemss' = map (fn ((n, ps), elems) =>
       
  1852           ((n, map Logic.varify ps), elems)) inst_elemss;
       
  1853 *)
       
  1854 
       
  1855     (* remove fragments already registered with theory *)
  1861     (* remove fragments already registered with theory *)
  1856     val new_inst_elemss = List.filter (fn (id, _) =>
  1862     val new_inst_elemss = List.filter (fn (id, _) =>
  1857           is_none (global_get_registration thy id)) inst_elemss;
  1863           is_none (global_get_registration thy id)) inst_elemss;
  1858 
  1864 
  1859 
       
  1860     val propss = extract_asms_elemss new_inst_elemss;
  1865     val propss = extract_asms_elemss new_inst_elemss;
  1861 
  1866 
  1862 
  1867 
  1863     (** add registrations to theory,
  1868     (** add registrations to theory,
  1864         without theorems, these are added after the proof **)
  1869         without theorems, these are added after the proof **)
  1865 
  1870 
  1866     val thy' = Library.foldl (prep_reg_global attn) (thy, new_inst_elemss);
  1871     val thy' = Library.foldl (prep_reg_global attn) (thy, new_inst_elemss);
  1867 
  1872 
  1868   in (thy', propss, activate_facts_elemss inst_elemss new_inst_elemss) end;
  1873   in (thy', propss, activate_facts_elemss inst_elemss new_inst_elemss) end;
  1869 
       
  1870 
       
  1871 (* Add registrations and theorems to theory context *)
       
  1872 
       
  1873 val global_activate_thm = global_put_registration_thm
       
  1874 
  1874 
  1875 end;  (* local *)
  1875 end;  (* local *)
  1876 
  1876 
  1877 
  1877 
  1878 
  1878