src/Pure/Isar/locale.ML
changeset 36096 abc6a2ea4b88
parent 36095 059c3568fdc8
parent 35798 fd1bb29f8170
child 36239 1385c4172d47
equal deleted inserted replaced
36095:059c3568fdc8 36096:abc6a2ea4b88
    31   (* Locale specification *)
    31   (* Locale specification *)
    32   val register_locale: binding ->
    32   val register_locale: binding ->
    33     (string * sort) list * ((string * typ) * mixfix) list ->
    33     (string * sort) list * ((string * typ) * mixfix) list ->
    34     term option * term list ->
    34     term option * term list ->
    35     thm option * thm option -> thm list ->
    35     thm option * thm option -> thm list ->
    36     declaration list * declaration list ->
    36     declaration list ->
    37     (string * (Attrib.binding * (thm list * Attrib.src list) list) list) list ->
    37     (string * (Attrib.binding * (thm list * Attrib.src list) list) list) list ->
    38     (string * morphism) list -> theory -> theory
    38     (string * morphism) list -> theory -> theory
    39   val intern: theory -> xstring -> string
    39   val intern: theory -> xstring -> string
    40   val extern: theory -> string -> xstring
    40   val extern: theory -> string -> xstring
    41   val defined: theory -> string -> bool
    41   val defined: theory -> string -> bool
    42   val params_of: theory -> string -> ((string * typ) * mixfix) list
    42   val params_of: theory -> string -> ((string * typ) * mixfix) list
    43   val intros_of: theory -> string -> thm option * thm option
    43   val intros_of: theory -> string -> thm option * thm option
    44   val axioms_of: theory -> string -> thm list
    44   val axioms_of: theory -> string -> thm list
    45   val instance_of: theory -> string -> morphism -> term list
    45   val instance_of: theory -> string -> morphism -> term list
    46   val specification_of: theory -> string -> term option * term list
    46   val specification_of: theory -> string -> term option * term list
    47   val declarations_of: theory -> string -> declaration list * declaration list
       
    48 
    47 
    49   (* Storing results *)
    48   (* Storing results *)
    50   val add_thmss: string -> string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
    49   val add_thmss: string -> string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
    51     Proof.context -> Proof.context
    50     Proof.context -> Proof.context
    52   val add_type_syntax: string -> declaration -> Proof.context -> Proof.context
       
    53   val add_term_syntax: string -> declaration -> Proof.context -> Proof.context
       
    54   val add_declaration: string -> declaration -> Proof.context -> Proof.context
    51   val add_declaration: string -> declaration -> Proof.context -> Proof.context
       
    52   val add_syntax_declaration: string -> declaration -> Proof.context -> Proof.context
    55 
    53 
    56   (* Activation *)
    54   (* Activation *)
    57   val activate_declarations: string * morphism -> Proof.context -> Proof.context
    55   val activate_declarations: string * morphism -> Proof.context -> Proof.context
    58   val activate_facts: string * morphism -> Context.generic -> Context.generic
    56   val activate_facts: string * morphism -> Context.generic -> Context.generic
    59   val init: string -> theory -> Proof.context
    57   val init: string -> theory -> Proof.context
    95   spec: term option * term list,
    93   spec: term option * term list,
    96     (* assumptions (as a single predicate expression) and defines *)
    94     (* assumptions (as a single predicate expression) and defines *)
    97   intros: thm option * thm option,
    95   intros: thm option * thm option,
    98   axioms: thm list,
    96   axioms: thm list,
    99   (** dynamic part **)
    97   (** dynamic part **)
       
    98 (* <<<<<<< local
   100   decls: (declaration * serial) list * (declaration * serial) list,
    99   decls: (declaration * serial) list * (declaration * serial) list,
   101     (* type and term syntax declarations *)
   100     (* type and term syntax declarations *)
   102   notes: ((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * serial) list,
   101   notes: ((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * serial) list,
       
   102 ======= *)
       
   103   syntax_decls: (declaration * serial) list,
       
   104     (* syntax declarations *)
       
   105   notes: ((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * serial) list,
       
   106 (* >>>>>>> other *)
   103     (* theorem declarations *)
   107     (* theorem declarations *)
   104   dependencies: ((string * morphism) * serial) list
   108   dependencies: ((string * morphism) * serial) list
   105     (* locale dependencies (sublocale relation) *)
   109     (* locale dependencies (sublocale relation) *)
   106 };
   110 };
   107 
   111 
   108 fun mk_locale ((parameters, spec, intros, axioms), ((decls, notes), dependencies)) =
   112 fun mk_locale ((parameters, spec, intros, axioms), ((syntax_decls, notes), dependencies)) =
   109   Loc {parameters = parameters, intros = intros, axioms = axioms, spec = spec,
   113   Loc {parameters = parameters, intros = intros, axioms = axioms, spec = spec,
   110     decls = decls, notes = notes, dependencies = dependencies};
   114     syntax_decls = syntax_decls, notes = notes, dependencies = dependencies};
   111 
   115 
   112 fun map_locale f (Loc {parameters, spec, intros, axioms, decls, notes, dependencies}) =
   116 fun map_locale f (Loc {parameters, spec, intros, axioms, syntax_decls, notes, dependencies}) =
   113   mk_locale (f ((parameters, spec, intros, axioms), ((decls, notes), dependencies)));
   117   mk_locale (f ((parameters, spec, intros, axioms), ((syntax_decls, notes), dependencies)));
   114 
   118 
   115 fun merge_locale (Loc {parameters, spec, intros, axioms, decls = (decls1, decls2),
   119 fun merge_locale
   116   notes, dependencies}, Loc {decls = (decls1', decls2'), notes = notes',
   120  (Loc {parameters, spec, intros, axioms, syntax_decls, notes, dependencies},
   117     dependencies = dependencies', ...}) = mk_locale
   121   Loc {syntax_decls = syntax_decls', notes = notes', dependencies = dependencies', ...}) =
       
   122     mk_locale
   118       ((parameters, spec, intros, axioms),
   123       ((parameters, spec, intros, axioms),
   119         (((merge (eq_snd op =) (decls1, decls1'), merge (eq_snd op =) (decls2, decls2')),
   124         ((merge (eq_snd op =) (syntax_decls, syntax_decls'),
   120           merge (eq_snd op =) (notes, notes')),
   125           merge (eq_snd op =) (notes, notes')),
   121             merge (eq_snd op =) (dependencies, dependencies')));
   126             merge (eq_snd op =) (dependencies, dependencies')));
   122 
   127 
   123 structure Locales = Theory_Data
   128 structure Locales = Theory_Data
   124 (
   129 (
   137 fun the_locale thy name =
   142 fun the_locale thy name =
   138   (case get_locale thy name of
   143   (case get_locale thy name of
   139     SOME (Loc loc) => loc
   144     SOME (Loc loc) => loc
   140   | NONE => error ("Unknown locale " ^ quote name));
   145   | NONE => error ("Unknown locale " ^ quote name));
   141 
   146 
   142 fun register_locale binding parameters spec intros axioms decls notes dependencies thy =
   147 fun register_locale binding parameters spec intros axioms syntax_decls notes dependencies thy =
   143   thy |> Locales.map (Name_Space.define true (Sign.naming_of thy)
   148   thy |> Locales.map (Name_Space.define true (Sign.naming_of thy)
   144     (binding,
   149     (binding,
   145       mk_locale ((parameters, spec, intros, axioms),
   150       mk_locale ((parameters, spec, intros, axioms),
       
   151 (* <<<<<<< local
   146         ((pairself (map (fn decl => (decl, serial ()))) decls, map (fn n => (n, serial ())) notes),
   152         ((pairself (map (fn decl => (decl, serial ()))) decls, map (fn n => (n, serial ())) notes),
   147           map (fn d => (d, serial ())) dependencies))) #> snd);
   153           map (fn d => (d, serial ())) dependencies))) #> snd);
       
   154 ======= *)
       
   155         ((map (fn decl => (decl, serial ())) syntax_decls, map (fn n => (n, serial ())) notes),
       
   156           map (fn d => (d, serial ())) dependencies))) #> snd);
       
   157 (* >>>>>>> other *)
   148 
   158 
   149 fun change_locale name =
   159 fun change_locale name =
   150   Locales.map o apsnd o Symtab.map_entry name o map_locale o apsnd;
   160   Locales.map o apsnd o Symtab.map_entry name o map_locale o apsnd;
   151 
   161 
   152 fun print_locales thy =
   162 fun print_locales thy =
   164 
   174 
   165 fun instance_of thy name morph = params_of thy name |>
   175 fun instance_of thy name morph = params_of thy name |>
   166   map (Morphism.term morph o Free o #1);
   176   map (Morphism.term morph o Free o #1);
   167 
   177 
   168 fun specification_of thy = #spec o the_locale thy;
   178 fun specification_of thy = #spec o the_locale thy;
   169 
       
   170 fun declarations_of thy name = the_locale thy name |>
       
   171   #decls |> pairself (map fst);
       
   172 
   179 
   173 fun dependencies_of thy name = the_locale thy name |>
   180 fun dependencies_of thy name = the_locale thy name |>
   174   #dependencies |> map fst;
   181   #dependencies |> map fst;
   175 
   182 
   176 
   183 
   255 end;
   262 end;
   256 
   263 
   257 
   264 
   258 (* Declarations, facts and entire locale content *)
   265 (* Declarations, facts and entire locale content *)
   259 
   266 
   260 fun activate_decls (name, morph) context =
   267 fun activate_syntax_decls (name, morph) context =
   261   let
   268   let
   262     val thy = Context.theory_of context;
   269     val thy = Context.theory_of context;
   263     val {decls = (typ_decls, term_decls), ...} = the_locale thy name;
   270     val {syntax_decls, ...} = the_locale thy name;
   264   in
   271   in
   265     context
   272     context
   266     |> fold_rev (fn (decl, _) => decl morph) typ_decls
   273     |> fold_rev (fn (decl, _) => decl morph) syntax_decls
   267     |> fold_rev (fn (decl, _) => decl morph) term_decls
       
   268   end;
   274   end;
   269 
   275 
   270 fun activate_notes activ_elem transfer thy (name, morph) input =
   276 fun activate_notes activ_elem transfer thy (name, morph) input =
   271   let
   277   let
   272     val {notes, ...} = the_locale thy name;
   278     val {notes, ...} = the_locale thy name;
   298 (** Public activation functions **)
   304 (** Public activation functions **)
   299 
   305 
   300 fun activate_declarations dep = Context.proof_map (fn context =>
   306 fun activate_declarations dep = Context.proof_map (fn context =>
   301   let
   307   let
   302     val thy = Context.theory_of context;
   308     val thy = Context.theory_of context;
   303   in roundup thy activate_decls Morphism.identity dep (get_idents context, context) |-> put_idents end);
   309   in
       
   310     roundup thy activate_syntax_decls Morphism.identity dep (get_idents context, context)
       
   311     |-> put_idents
       
   312   end);
   304 
   313 
   305 fun activate_facts dep context =
   314 fun activate_facts dep context =
   306   let
   315   let
   307     val thy = Context.theory_of context;
   316     val thy = Context.theory_of context;
   308     val activate = activate_notes Element.init (Element.transfer_morphism o Context.theory_of) thy;
   317     val activate = activate_notes Element.init (Element.transfer_morphism o Context.theory_of) thy;
   516   in ctxt'' end;
   525   in ctxt'' end;
   517 
   526 
   518 
   527 
   519 (* Declarations *)
   528 (* Declarations *)
   520 
   529 
       
   530 (* <<<<<<< local
   521 local
   531 local
   522 
   532 
   523 fun decl_attrib decl phi = Thm.declaration_attribute (K (decl phi));
   533 fun decl_attrib decl phi = Thm.declaration_attribute (K (decl phi));
   524 
   534 
   525 fun add_decls add loc decl =
   535 fun add_decls add loc decl =
   526   ProofContext.theory ((change_locale loc o apfst o apfst) (add (decl, serial ()))) #>
   536   ProofContext.theory ((change_locale loc o apfst o apfst) (add (decl, serial ()))) #>
       
   537 ======= *)
       
   538 fun add_declaration loc decl =
       
   539 (* >>>>>>> other *)
   527   add_thmss loc ""
   540   add_thmss loc ""
   528     [((Binding.conceal Binding.empty, [Attrib.internal (decl_attrib decl)]),
   541     [((Binding.conceal Binding.empty,
       
   542         [Attrib.internal (fn phi => Thm.declaration_attribute (K (decl phi)))]),
   529       [([Drule.dummy_thm], [])])];
   543       [([Drule.dummy_thm], [])])];
   530 
   544 
   531 in
   545 fun add_syntax_declaration loc decl =
   532 
   546   ProofContext.theory ((change_locale loc o apfst o apfst) (cons (decl, serial ())))
   533 val add_type_syntax = add_decls (apfst o cons);
   547   #> add_declaration loc decl;
   534 val add_term_syntax = add_decls (apsnd o cons);
       
   535 val add_declaration = add_decls (K I);
       
   536 
       
   537 end;
       
   538 
   548 
   539 
   549 
   540 (*** Reasoning about locales ***)
   550 (*** Reasoning about locales ***)
   541 
   551 
   542 (* Storage for witnesses, intro and unfold rules *)
   552 (* Storage for witnesses, intro and unfold rules *)