src/Pure/theory.ML
changeset 16339 b02b6da609c3
parent 16313 79b37d5e50b1
child 16369 96d73621fabb
equal deleted inserted replaced
16338:3d1851acb4d0 16339:b02b6da609c3
    10   type theory
    10   type theory
    11   exception THEORY of string * theory list
    11   exception THEORY of string * theory list
    12   val rep_theory: theory ->
    12   val rep_theory: theory ->
    13     {sign: Sign.sg,
    13     {sign: Sign.sg,
    14       defs: Defs.graph,
    14       defs: Defs.graph,
    15       axioms: term Symtab.table,
    15       axioms: term NameSpace.table,
    16       oracles: ((Sign.sg * Object.T -> term) * stamp) Symtab.table,
    16       oracles: ((Sign.sg * Object.T -> term) * stamp) NameSpace.table,
    17       parents: theory list,
    17       parents: theory list,
    18       ancestors: theory list}
    18       ancestors: theory list}
    19   val sign_of: theory -> Sign.sg
    19   val sign_of: theory -> Sign.sg
    20   val is_draft: theory -> bool
    20   val is_draft: theory -> bool
    21   val syn_of: theory -> Syntax.syntax
    21   val syn_of: theory -> Syntax.syntax
    31 end
    31 end
    32 
    32 
    33 signature THEORY =
    33 signature THEORY =
    34 sig
    34 sig
    35   include BASIC_THEORY
    35   include BASIC_THEORY
    36   val axiomK: string
    36   val axioms_of: theory -> (string * term) list
    37   val oracleK: string
    37   val all_axioms_of: theory -> (string * term) list
    38   (*theory extension primitives*)
    38   val add_classes: (bstring * xstring list) list -> theory -> theory
    39   val add_classes: (bclass * xclass list) list -> theory -> theory
    39   val add_classes_i: (bstring * class list) list -> theory -> theory
    40   val add_classes_i: (bclass * class list) list -> theory -> theory
    40   val add_classrel: (xstring * xstring) list -> theory -> theory
    41   val add_classrel: (xclass * xclass) list -> theory -> theory
       
    42   val add_classrel_i: (class * class) list -> theory -> theory
    41   val add_classrel_i: (class * class) list -> theory -> theory
    43   val add_defsort: string -> theory -> theory
    42   val add_defsort: string -> theory -> theory
    44   val add_defsort_i: sort -> theory -> theory
    43   val add_defsort_i: sort -> theory -> theory
    45   val add_types: (bstring * int * mixfix) list -> theory -> theory
    44   val add_types: (bstring * int * mixfix) list -> theory -> theory
    46   val add_nonterminals: bstring list -> theory -> theory
    45   val add_nonterminals: bstring list -> theory -> theory
   127 
   126 
   128 datatype theory =
   127 datatype theory =
   129   Theory of {
   128   Theory of {
   130     sign: Sign.sg,
   129     sign: Sign.sg,
   131     defs: Defs.graph,
   130     defs: Defs.graph,
   132     axioms: term Symtab.table,
   131     axioms: term NameSpace.table,
   133     oracles: ((Sign.sg * Object.T -> term) * stamp) Symtab.table,
   132     oracles: ((Sign.sg * Object.T -> term) * stamp) NameSpace.table,
   134     parents: theory list,
   133     parents: theory list,
   135     ancestors: theory list};
   134     ancestors: theory list};
   136 
   135 
   137 fun make_theory sign defs axioms oracles parents ancestors =
   136 fun make_theory sign defs axioms oracles parents ancestors =
   138   Theory {sign = sign, defs = defs, axioms = axioms,
   137   Theory {sign = sign, defs = defs, axioms = axioms,
   144 val is_draft = Sign.is_draft o sign_of;
   143 val is_draft = Sign.is_draft o sign_of;
   145 val syn_of = Sign.syn_of o sign_of;
   144 val syn_of = Sign.syn_of o sign_of;
   146 val parents_of = #parents o rep_theory;
   145 val parents_of = #parents o rep_theory;
   147 val ancestors_of = #ancestors o rep_theory;
   146 val ancestors_of = #ancestors o rep_theory;
   148 
   147 
       
   148 val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
       
   149 fun all_axioms_of thy = List.concat (map axioms_of (thy :: ancestors_of thy));
       
   150 
   149 (*errors involving theories*)
   151 (*errors involving theories*)
   150 exception THEORY of string * theory list;
   152 exception THEORY of string * theory list;
   151 
   153 
   152 (*compare theories*)
   154 (*compare theories*)
   153 val subthy = Sign.subsig o pairself sign_of;
   155 val subthy = Sign.subsig o pairself sign_of;
   162   if subthy (thy1, thy2) then thy2
   164   if subthy (thy1, thy2) then thy2
   163   else raise THEORY ("Not a super theory", [thy1, thy2]);
   165   else raise THEORY ("Not a super theory", [thy1, thy2]);
   164 
   166 
   165 (*partial Pure theory*)
   167 (*partial Pure theory*)
   166 val pre_pure =
   168 val pre_pure =
   167   make_theory Sign.pre_pure Defs.empty Symtab.empty Symtab.empty [] [];
   169   make_theory Sign.pre_pure Defs.empty NameSpace.empty_table NameSpace.empty_table [] [];
   168 
   170 
   169 
   171 
   170 
   172 
   171 (** extend theory **)
   173 (** extend theory **)
   172 
   174 
   173 (*name spaces*)
       
   174 val axiomK = "axiom";
       
   175 val oracleK = "oracle";
       
   176 
       
   177 
       
   178 (* extend logical part of a theory *)
   175 (* extend logical part of a theory *)
   179 
   176 
   180 fun err_dup_axms names =
   177 fun err_dup_axms dups = error ("Duplicate axiom(s): " ^ commas_quote dups);
   181   error ("Duplicate axiom name(s): " ^ commas_quote names);
   178 fun err_dup_oras dups = error ("Duplicate oracle(s): " ^ commas_quote dups);
   182 
   179 
   183 fun err_dup_oras names =
   180 fun ext_theory thy ext_sg axms oras =
   184   error ("Duplicate oracles: " ^ commas_quote names);
       
   185 
       
   186 fun ext_theory thy ext_sg new_axms new_oras =
       
   187   let
   181   let
   188     val Theory {sign, defs, axioms, oracles, parents, ancestors} = thy;
   182     val Theory {sign, defs, axioms, oracles, parents, ancestors} = thy;
   189     val draft = Sign.is_draft sign;
   183     val draft = Sign.is_draft sign;
   190     val axioms' = Symtab.extend (if draft then axioms else Symtab.empty, new_axms)
   184     val naming = Sign.naming_of sign;
   191       handle Symtab.DUPS names => err_dup_axms names;
   185 
   192     val oracles' = Symtab.extend (oracles, new_oras)
   186     val axioms' = NameSpace.extend_table naming
   193       handle Symtab.DUPS names => err_dup_oras names;
   187         (if draft then axioms else NameSpace.empty_table, axms)
       
   188       handle Symtab.DUPS dups => err_dup_axms dups;
       
   189     val oracles' = NameSpace.extend_table naming (oracles, oras)
       
   190       handle Symtab.DUPS dups => err_dup_oras dups;
       
   191 
   194     val (parents', ancestors') =
   192     val (parents', ancestors') =
   195       if draft then (parents, ancestors) else ([thy], thy :: ancestors);
   193       if draft then (parents, ancestors) else ([thy], thy :: ancestors);
   196   in make_theory (ext_sg sign) defs axioms' oracles' parents' ancestors' end;
   194   in make_theory (ext_sg sign) defs axioms' oracles' parents' ancestors' end;
   197 
   195 
   198 
   196 
   235 val qualified_names        = ext_sign (K Sign.qualified_names) ();
   233 val qualified_names        = ext_sign (K Sign.qualified_names) ();
   236 val no_base_names          = ext_sign (K Sign.no_base_names) ();
   234 val no_base_names          = ext_sign (K Sign.no_base_names) ();
   237 val custom_accesses        = ext_sign Sign.custom_accesses;
   235 val custom_accesses        = ext_sign Sign.custom_accesses;
   238 val set_policy             = ext_sign Sign.set_policy;
   236 val set_policy             = ext_sign Sign.set_policy;
   239 val restore_naming         = ext_sign Sign.restore_naming o sign_of;
   237 val restore_naming         = ext_sign Sign.restore_naming o sign_of;
   240 val add_space              = ext_sign Sign.add_space;
       
   241 val hide_space             = ext_sign o Sign.hide_space;
   238 val hide_space             = ext_sign o Sign.hide_space;
   242 val hide_space_i           = ext_sign o Sign.hide_space_i;
   239 val hide_space_i           = ext_sign o Sign.hide_space_i;
   243 fun hide_classes b         = curry (hide_space_i b) Sign.classK;
   240 fun hide_classes b         = curry (hide_space_i b) Sign.classK;
   244 fun hide_types b           = curry (hide_space_i b) Sign.typeK;
   241 fun hide_types b           = curry (hide_space_i b) Sign.typeK;
   245 fun hide_consts b          = curry (hide_space_i b) Sign.constK;
   242 fun hide_consts b          = curry (hide_space_i b) Sign.constK;
   298 local
   295 local
   299 
   296 
   300 fun gen_add_axioms prep_axm raw_axms thy =
   297 fun gen_add_axioms prep_axm raw_axms thy =
   301   let
   298   let
   302     val sg = sign_of thy;
   299     val sg = sign_of thy;
   303     val raw_axms' = map (apfst (Sign.full_name sg)) raw_axms;
   300     val axms = map (apsnd (Term.compress_term o Logic.varify) o prep_axm sg) raw_axms;
   304     val axioms =
   301   in ext_theory thy I axms [] end;
   305       map (apsnd (Term.compress_term o Logic.varify) o prep_axm sg) raw_axms';
       
   306     val ext_sg = Sign.add_space (axiomK, map fst axioms);
       
   307   in ext_theory thy ext_sg axioms [] end;
       
   308 
   302 
   309 in
   303 in
   310 
   304 
   311 val add_axioms = gen_add_axioms read_axm;
   305 val add_axioms = gen_add_axioms read_axm;
   312 val add_axioms_i = gen_add_axioms cert_axm;
   306 val add_axioms_i = gen_add_axioms cert_axm;
   314 end;
   308 end;
   315 
   309 
   316 
   310 
   317 (* add oracle **)
   311 (* add oracle **)
   318 
   312 
   319 fun add_oracle (raw_name, oracle) (thy as Theory {sign, ...}) =
   313 fun add_oracle (bname, oracle) thy =
   320   let
   314   ext_theory thy I [] [(bname, (oracle, stamp ()))];
   321     val name = Sign.full_name sign raw_name;
       
   322     val ext_sg = Sign.add_space (oracleK, [name]);
       
   323   in ext_theory thy ext_sg [] [(name, (oracle, stamp ()))] end;
       
   324 
   315 
   325 
   316 
   326 
   317 
   327 (** add constant definitions **)
   318 (** add constant definitions **)
   328 
   319 
   535     val defss = map (#defs o rep_theory) thys;
   526     val defss = map (#defs o rep_theory) thys;
   536     val defs' = foldl (uncurry Defs.merge) (hd defss) (tl defss)
   527     val defs' = foldl (uncurry Defs.merge) (hd defss) (tl defss)
   537       handle Defs.CIRCULAR namess => error (defs_circular (Sign.pp sign') namess)
   528       handle Defs.CIRCULAR namess => error (defs_circular (Sign.pp sign') namess)
   538         | Defs.INFINITE_CHAIN namess => error (defs_infinite_chain (Sign.pp sign') namess);
   529         | Defs.INFINITE_CHAIN namess => error (defs_infinite_chain (Sign.pp sign') namess);
   539 
   530 
   540     val axioms' = Symtab.empty;
   531     val axioms' = NameSpace.empty_table;
   541 
   532 
       
   533     val oras_spaces = map (#1 o #oracles o rep_theory) thys;
       
   534     val oras_space' = Library.foldl NameSpace.merge (hd oras_spaces, tl oras_spaces);
   542     fun eq_ora ((_, (_, s1: stamp)), (_, (_, s2))) = s1 = s2;
   535     fun eq_ora ((_, (_, s1: stamp)), (_, (_, s2))) = s1 = s2;
   543     val oracles' =
   536     val oras' =
   544       Symtab.make (gen_distinct eq_ora
   537       Symtab.make (gen_distinct eq_ora
   545         (List.concat (map (Symtab.dest o #oracles o rep_theory) thys)))
   538         (List.concat (map (Symtab.dest o #2 o #oracles o rep_theory) thys)))
   546       handle Symtab.DUPS names => err_dup_oras names;
   539       handle Symtab.DUPS names => err_dup_oras names;
       
   540     val oracles' = (oras_space', oras');
   547 
   541 
   548     val parents' = gen_distinct eq_thy thys;
   542     val parents' = gen_distinct eq_thy thys;
   549     val ancestors' =
   543     val ancestors' =
   550       gen_distinct eq_thy (parents' @ List.concat (map ancestors_of thys));
   544       gen_distinct eq_thy (parents' @ List.concat (map ancestors_of thys));
   551   in make_theory sign' defs' axioms' oracles' parents' ancestors' end;
   545   in make_theory sign' defs' axioms' oracles' parents' ancestors' end;