src/Pure/Isar/locale.ML
changeset 13297 e4ae0732e2be
parent 13211 aabdb4b33625
child 13308 1dbed9ea764b
equal deleted inserted replaced
13296:ba142aa29694 13297:e4ae0732e2be
    73   Fixes of (string * 'typ option * mixfix option) list |
    73   Fixes of (string * 'typ option * mixfix option) list |
    74   Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list |
    74   Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list |
    75   Defines of ((string * 'att list) * ('term * 'term list)) list |
    75   Defines of ((string * 'att list) * ('term * 'term list)) list |
    76   Notes of ((string * 'att list) * ('fact * 'att list) list) list;
    76   Notes of ((string * 'att list) * ('fact * 'att list) list) list;
    77 
    77 
    78 datatype fact_kind = Assume | Define | Note;
       
    79 
       
    80 datatype expr =
    78 datatype expr =
    81   Locale of string |
    79   Locale of string |
    82   Rename of expr * string option list |
    80   Rename of expr * string option list |
    83   Merge of expr list;
    81   Merge of expr list;
    84 
    82 
   407 
   405 
   408 (* activate elements *)
   406 (* activate elements *)
   409 
   407 
   410 local
   408 local
   411 
   409 
   412 fun activate_elem (ctxt, Fixes fixes) =
   410 fun activate_elem (ctxt, Fixes fixes) = (ctxt |> ProofContext.add_fixes fixes, [])
   413       (ctxt |> ProofContext.add_fixes fixes, [])
       
   414   | activate_elem (ctxt, Assumes asms) =
   411   | activate_elem (ctxt, Assumes asms) =
   415       ctxt |> ProofContext.fix_frees (flat (map (map #1 o #2) asms))
   412       ctxt |> ProofContext.fix_frees (flat (map (map #1 o #2) asms))
   416       |> ProofContext.assume_i ProofContext.export_assume asms
   413       |> ProofContext.assume_i ProofContext.export_assume asms
   417       |> apsnd (map (pair Assume))
       
   418   | activate_elem (ctxt, Defines defs) =
   414   | activate_elem (ctxt, Defines defs) =
   419       ctxt |> ProofContext.assume_i ProofContext.export_def
   415       ctxt |> ProofContext.assume_i ProofContext.export_def
   420         (map (fn ((name, atts), (t, ps)) =>
   416         (map (fn ((name, atts), (t, ps)) =>
   421           let val (c, t') = ProofContext.cert_def ctxt t
   417           let val (c, t') = ProofContext.cert_def ctxt t
   422           in ((if name = "" then Thm.def_name c else name, atts), [(t', (ps, []))]) end) defs)
   418           in ((if name = "" then Thm.def_name c else name, atts), [(t', (ps, []))]) end) defs)
   423       |> apsnd (map (pair Define))
   419   | activate_elem (ctxt, Notes facts) = ctxt |> ProofContext.have_thmss_i facts;
   424   | activate_elem (ctxt, Notes facts) =
       
   425       ctxt |> ProofContext.have_thmss_i facts
       
   426       |> apsnd (map (pair Note));
       
   427 
   420 
   428 fun activate_elems ((name, ps), elems) = ProofContext.qualified_result (fn ctxt =>
   421 fun activate_elems ((name, ps), elems) = ProofContext.qualified_result (fn ctxt =>
   429   foldl_map activate_elem (ctxt, elems) handle ProofContext.CONTEXT (msg, ctxt) =>
   422   foldl_map activate_elem (ctxt, elems) handle ProofContext.CONTEXT (msg, ctxt) =>
   430     err_in_locale ctxt msg [(name, map fst ps)]);
   423     err_in_locale ctxt msg [(name, map fst ps)]);
   431 
   424 
   442   let val (ctxt', (elemss', factss)) = apsnd split_list (activate_elemss prep_facts ctxt_elemss)
   435   let val (ctxt', (elemss', factss)) = apsnd split_list (activate_elemss prep_facts ctxt_elemss)
   443   in (ctxt', (elemss', flat factss)) end;
   436   in (ctxt', (elemss', flat factss)) end;
   444 
   437 
   445 fun apply_facts name (ctxt, facts) =
   438 fun apply_facts name (ctxt, facts) =
   446   let val (ctxt', (_, facts')) = activate_facts (K I) (ctxt, [((name, []), [Notes facts])])
   439   let val (ctxt', (_, facts')) = activate_facts (K I) (ctxt, [((name, []), [Notes facts])])
   447   in (ctxt', map (#2 o #2) facts') end;
   440   in (ctxt', map #2 facts') end;
   448 
   441 
   449 end;
   442 end;
   450 
   443 
   451 
   444 
   452 
   445 
   711 val gen_context_i = prep_context_statement (K I) cert_elemss get_facts_i;
   704 val gen_context_i = prep_context_statement (K I) cert_elemss get_facts_i;
   712 
   705 
   713 fun gen_facts prep_locale thy name =
   706 fun gen_facts prep_locale thy name =
   714   let val ((((_, (_, facts)), _), _), _) = thy |> ProofContext.init
   707   let val ((((_, (_, facts)), _), _), _) = thy |> ProofContext.init
   715     |> gen_context_i false [] (Locale (prep_locale (Theory.sign_of thy) name)) [] [];
   708     |> gen_context_i false [] (Locale (prep_locale (Theory.sign_of thy) name)) [] [];
   716   in flat (map (#2 o #2) facts) end;
   709   in flat (map #2 facts) end;
   717 
   710 
   718 fun gen_statement prep_locale prep_ctxt raw_locale elems concl ctxt =
   711 fun gen_statement prep_locale prep_ctxt raw_locale elems concl ctxt =
   719   let
   712   let
   720     val thy = ProofContext.theory_of ctxt;
   713     val thy = ProofContext.theory_of ctxt;
   721     val locale = apsome (prep_locale (Theory.sign_of thy)) raw_locale;
   714     val locale = apsome (prep_locale (Theory.sign_of thy)) raw_locale;
   787   end;
   780   end;
   788 
   781 
   789 
   782 
   790 
   783 
   791 (** define locales **)
   784 (** define locales **)
   792 
       
   793 (* add_locale(_i) *)
       
   794 
       
   795 local
       
   796 
       
   797 fun gen_add_locale prep_ctxt prep_expr bname raw_import raw_body thy =
       
   798   let
       
   799     val sign = Theory.sign_of thy;
       
   800     val name = Sign.full_name sign bname;
       
   801     val _ = conditional (is_some (get_locale thy name)) (fn () =>
       
   802       error ("Duplicate definition of locale " ^ quote name));
       
   803 
       
   804     val thy_ctxt = ProofContext.init thy;
       
   805     val (((import_ctxt, (import_elemss, _)), (body_ctxt, (body_elemss, _))),
       
   806         (int_ext_text, ext_text)) = prep_ctxt raw_import raw_body thy_ctxt;
       
   807 
       
   808     val import_parms = params_of import_elemss;
       
   809     val body_parms = params_of body_elemss;
       
   810     val all_parms = import_parms @ body_parms;
       
   811 
       
   812     (* FIXME *)
       
   813     val ((_, spec), defs) = int_ext_text;
       
   814     val ((xs, _), _) = int_ext_text;
       
   815     val xs' = all_parms |> mapfilter (fn (p, _) =>
       
   816       (case assoc_string (xs, p) of None => None | Some T => Some (p, T)));
       
   817 
       
   818     val import = prep_expr sign raw_import;
       
   819     val elems = flat (map snd body_elemss);
       
   820   in
       
   821     thy
       
   822     |> declare_locale name
       
   823     |> put_locale name (make_locale import (map (fn e => (e, stamp ())) elems)
       
   824         ((xs', spec), defs) (all_parms, map fst body_parms))
       
   825   end;
       
   826 
       
   827 in
       
   828 
       
   829 val add_locale = gen_add_locale read_context intern_expr;
       
   830 val add_locale_i = gen_add_locale cert_context (K I);
       
   831 
       
   832 end;
       
   833 
       
   834 
   785 
   835 (* store results *)
   786 (* store results *)
   836 
   787 
   837 local
   788 local
   838 
   789 
   890   in ((ctxt', thy |> put_facts loc args'), facts') end;
   841   in ((ctxt', thy |> put_facts loc args'), facts') end;
   891 
   842 
   892 end;
   843 end;
   893 
   844 
   894 
   845 
       
   846 (* add_locale(_i) *)
       
   847 
       
   848 local
       
   849 
       
   850 fun gen_add_locale prep_ctxt prep_expr bname raw_import raw_body thy =
       
   851   let
       
   852     val sign = Theory.sign_of thy;
       
   853     val name = Sign.full_name sign bname;
       
   854     val _ = conditional (is_some (get_locale thy name)) (fn () =>
       
   855       error ("Duplicate definition of locale " ^ quote name));
       
   856 
       
   857     val thy_ctxt = ProofContext.init thy;
       
   858     val (((import_ctxt, (import_elemss, _)), (body_ctxt, (body_elemss, _))),
       
   859         (int_ext_text, ext_text)) = prep_ctxt raw_import raw_body thy_ctxt;
       
   860 
       
   861     val import_parms = params_of import_elemss;
       
   862     val body_parms = params_of body_elemss;
       
   863     val all_parms = import_parms @ body_parms;
       
   864 
       
   865     (* FIXME *)
       
   866     val ((_, spec), defs) = int_ext_text;
       
   867     val ((xs, _), _) = int_ext_text;
       
   868     val xs' = all_parms |> mapfilter (fn (p, _) =>
       
   869       (case assoc_string (xs, p) of None => None | Some T => Some (p, T)));
       
   870 
       
   871     val import = prep_expr sign raw_import;
       
   872     val elems = flat (map snd body_elemss);
       
   873   in
       
   874     thy
       
   875     |> declare_locale name
       
   876     |> put_locale name (make_locale import (map (fn e => (e, stamp ())) elems)
       
   877         ((xs', spec), defs) (all_parms, map fst body_parms))
       
   878   end;
       
   879 
       
   880 in
       
   881 
       
   882 val add_locale = gen_add_locale read_context intern_expr;
       
   883 val add_locale_i = gen_add_locale cert_context (K I);
       
   884 
       
   885 end;
       
   886 
       
   887 
   895 
   888 
   896 (** locale theory setup **)
   889 (** locale theory setup **)
   897 
   890 
   898 val setup =
   891 val setup =
   899  [LocalesData.init];
   892  [LocalesData.init];