src/Pure/theory.ML
changeset 24666 9885a86f14a8
parent 24626 85eceef2edc7
child 24708 d9b00117365e
equal deleted inserted replaced
24665:e5bea50b9b89 24666:9885a86f14a8
    13 
    13 
    14 signature THEORY =
    14 signature THEORY =
    15 sig
    15 sig
    16   include BASIC_THEORY
    16   include BASIC_THEORY
    17   include SIGN_THEORY
    17   include SIGN_THEORY
       
    18   val assert_super: theory -> theory -> theory
    18   val parents_of: theory -> theory list
    19   val parents_of: theory -> theory list
    19   val ancestors_of: theory -> theory list
    20   val ancestors_of: theory -> theory list
    20   val begin_theory: string -> theory list -> theory
    21   val check_thy: theory -> theory_ref
    21   val end_theory: theory -> theory
    22   val deref: theory_ref -> theory
       
    23   val merge: theory * theory -> theory
       
    24   val merge_refs: theory_ref * theory_ref -> theory_ref
       
    25   val merge_list: theory list -> theory
    22   val checkpoint: theory -> theory
    26   val checkpoint: theory -> theory
    23   val copy: theory -> theory
    27   val copy: theory -> theory
    24   val rep_theory: theory ->
    28   val requires: theory -> string -> string -> unit
    25    {axioms: term NameSpace.table,
       
    26     defs: Defs.T,
       
    27     oracles: ((theory * Object.T -> term) * stamp) NameSpace.table}
       
    28   val axiom_space: theory -> NameSpace.T
    29   val axiom_space: theory -> NameSpace.T
    29   val axiom_table: theory -> term Symtab.table
    30   val axiom_table: theory -> term Symtab.table
    30   val oracle_space: theory -> NameSpace.T
    31   val oracle_space: theory -> NameSpace.T
    31   val oracle_table: theory -> ((theory * Object.T -> term) * stamp) Symtab.table
    32   val oracle_table: theory -> ((theory * Object.T -> term) * stamp) Symtab.table
    32   val axioms_of: theory -> (string * term) list
    33   val axioms_of: theory -> (string * term) list
    33   val all_axioms_of: theory -> (string * term) list
    34   val all_axioms_of: theory -> (string * term) list
    34   val defs_of : theory -> Defs.T
    35   val defs_of: theory -> Defs.T
    35   val check_thy: theory -> theory_ref
    36   val at_begin: (theory -> theory option) -> theory -> theory
    36   val deref: theory_ref -> theory
    37   val at_end: (theory -> theory option) -> theory -> theory
    37   val merge: theory * theory -> theory
    38   val begin_theory: string -> theory list -> theory
    38   val merge_refs: theory_ref * theory_ref -> theory_ref
    39   val end_theory: theory -> theory
    39   val merge_list: theory list -> theory
       
    40   val requires: theory -> string -> string -> unit
       
    41   val assert_super: theory -> theory -> theory
       
    42   val cert_axm: theory -> string * term -> string * term
    40   val cert_axm: theory -> string * term -> string * term
    43   val read_axm: theory -> string * string -> string * term
    41   val read_axm: theory -> string * string -> string * term
    44   val add_axioms: (bstring * string) list -> theory -> theory
    42   val add_axioms: (bstring * string) list -> theory -> theory
    45   val add_axioms_i: (bstring * term) list -> theory -> theory
    43   val add_axioms_i: (bstring * term) list -> theory -> theory
    46   val add_deps: string -> string * typ -> (string * typ) list -> theory -> theory
    44   val add_deps: string -> string * typ -> (string * typ) list -> theory -> theory
    49   val add_finals: bool -> string list -> theory -> theory
    47   val add_finals: bool -> string list -> theory -> theory
    50   val add_finals_i: bool -> term list -> theory -> theory
    48   val add_finals_i: bool -> term list -> theory -> theory
    51   val add_oracle: bstring * (theory * Object.T -> term) -> theory -> theory
    49   val add_oracle: bstring * (theory * Object.T -> term) -> theory -> theory
    52 end
    50 end
    53 
    51 
    54 signature THEORY_INTERPRETATOR =
    52 structure Theory: THEORY =
    55 sig
       
    56   type T
       
    57   type interpretator = T list -> theory -> theory
       
    58   val add_those: T list -> theory -> theory
       
    59   val all_those: theory -> T list
       
    60   val add_interpretator: interpretator -> theory -> theory
       
    61   val init: theory -> theory
       
    62 end;
       
    63 
       
    64 signature THEORY_INTERPRETATOR_KEY =
       
    65 sig
       
    66   type T
       
    67   val eq: T * T -> bool
       
    68 end;
       
    69 
       
    70 structure Theory =
       
    71 struct
    53 struct
       
    54 
       
    55 
       
    56 (** theory context operations **)
       
    57 
       
    58 val eq_thy = Context.eq_thy;
       
    59 val subthy = Context.subthy;
       
    60 
       
    61 fun assert_super thy1 thy2 =
       
    62   if subthy (thy1, thy2) then thy2
       
    63   else raise THEORY ("Not a super theory", [thy1, thy2]);
       
    64 
       
    65 val parents_of = Context.parents_of;
       
    66 val ancestors_of = Context.ancestors_of;
       
    67 
       
    68 val check_thy = Context.check_thy;
       
    69 val deref = Context.deref;
       
    70 
       
    71 val merge = Context.merge;
       
    72 val merge_refs = Context.merge_refs;
       
    73 
       
    74 fun merge_list [] = raise THEORY ("Empty merge of theories", [])
       
    75   | merge_list (thy :: thys) = Library.foldl merge (thy, thys);
       
    76 
       
    77 val checkpoint = Context.checkpoint_thy;
       
    78 val copy = Context.copy_thy;
       
    79 
       
    80 fun requires thy name what =
       
    81   if Context.exists_name name thy then ()
       
    82   else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
       
    83 
       
    84 
       
    85 
       
    86 (** theory wrappers **)
       
    87 
       
    88 type wrapper = (theory -> theory option) * stamp;
       
    89 
       
    90 fun apply_wrappers (wrappers: wrapper list) =
       
    91   let
       
    92     fun app [] res = res
       
    93       | app ((f, _) :: fs) (changed, thy) =
       
    94           (case f thy of
       
    95             NONE => app fs (changed, thy)
       
    96           | SOME thy' => app fs (true, thy'));
       
    97     fun app_fixpoint thy =
       
    98       (case app wrappers (false, thy) of
       
    99         (false, _) => thy
       
   100       | (true, thy') => app_fixpoint thy');
       
   101   in app_fixpoint end;
       
   102 
    72 
   103 
    73 
   104 
    74 (** datatype thy **)
   105 (** datatype thy **)
    75 
   106 
    76 datatype thy = Thy of
   107 datatype thy = Thy of
    77  {axioms: term NameSpace.table,
   108  {axioms: term NameSpace.table,
    78   defs: Defs.T,
   109   defs: Defs.T,
    79   oracles: ((theory * Object.T -> term) * stamp) NameSpace.table,
   110   oracles: ((theory * Object.T -> term) * stamp) NameSpace.table,
    80   consolidate: theory -> theory};
   111   wrappers: wrapper list * wrapper list};
    81 
   112 
    82 fun make_thy (axioms, defs, oracles, consolidate) =
   113 fun make_thy (axioms, defs, oracles, wrappers) =
    83   Thy {axioms = axioms, defs = defs, oracles = oracles, consolidate = consolidate};
   114   Thy {axioms = axioms, defs = defs, oracles = oracles, wrappers = wrappers};
    84 
   115 
    85 fun err_dup_axm dup = error ("Duplicate axiom: " ^ quote dup);
   116 fun err_dup_axm dup = error ("Duplicate axiom: " ^ quote dup);
    86 fun err_dup_ora dup = error ("Duplicate oracle: " ^ quote dup);
   117 fun err_dup_ora dup = error ("Duplicate oracle: " ^ quote dup);
    87 
   118 
    88 structure ThyData = TheoryDataFun
   119 structure ThyData = TheoryDataFun
    89 (
   120 (
    90   type T = thy;
   121   type T = thy;
    91   val empty = make_thy (NameSpace.empty_table, Defs.empty, NameSpace.empty_table, I);
   122   val empty = make_thy (NameSpace.empty_table, Defs.empty, NameSpace.empty_table, ([], []));
    92   val copy = I;
   123   val copy = I;
    93 
   124 
    94   fun extend (Thy {axioms, defs, oracles, consolidate}) =
   125   fun extend (Thy {axioms, defs, oracles, wrappers}) =
    95     make_thy (NameSpace.empty_table, defs, oracles, consolidate);
   126     make_thy (NameSpace.empty_table, defs, oracles, wrappers);
    96 
   127 
    97   fun merge pp (thy1, thy2) =
   128   fun merge pp (thy1, thy2) =
    98     let
   129     let
    99       val Thy {axioms = _, defs = defs1, oracles = oracles1,
   130       val Thy {axioms = _, defs = defs1, oracles = oracles1, wrappers = (bgs1, ens1)} = thy1;
   100         consolidate = consolidate1} = thy1;
   131       val Thy {axioms = _, defs = defs2, oracles = oracles2, wrappers = (bgs2, ens2)} = thy2;
   101       val Thy {axioms = _, defs = defs2, oracles = oracles2,
   132 
   102         consolidate = consolidate2} = thy2;
   133       val axioms' = NameSpace.empty_table;
   103 
   134       val defs' = Defs.merge pp (defs1, defs2);
   104       val axioms = NameSpace.empty_table;
   135       val oracles' = NameSpace.merge_tables (eq_snd (op =)) (oracles1, oracles2)
   105       val defs = Defs.merge pp (defs1, defs2);
       
   106       val oracles = NameSpace.merge_tables (eq_snd (op =)) (oracles1, oracles2)
       
   107         handle Symtab.DUP dup => err_dup_ora dup;
   136         handle Symtab.DUP dup => err_dup_ora dup;
   108       val consolidate = consolidate1 #> consolidate2;
   137       val bgs' = Library.merge (eq_snd op =) (bgs1, bgs2);
   109     in make_thy (axioms, defs, oracles, consolidate) end;
   138       val ens' = Library.merge (eq_snd op =) (ens1, ens2);
       
   139     in make_thy (axioms', defs', oracles', (bgs', ens')) end;
   110 );
   140 );
   111 
   141 
   112 fun rep_theory thy = ThyData.get thy |> (fn Thy {axioms, defs, oracles, ...} =>
   142 fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
   113   {axioms = axioms, defs = defs, oracles = oracles});
   143 
   114 
   144 fun map_thy f = ThyData.map (fn (Thy {axioms, defs, oracles, wrappers}) =>
   115 fun map_thy f = ThyData.map (fn (Thy {axioms, defs, oracles, consolidate}) =>
   145   make_thy (f (axioms, defs, oracles, wrappers)));
   116   make_thy (f (axioms, defs, oracles, consolidate)));
   146 
   117 
   147 
   118 fun map_axioms f = map_thy
   148 fun map_axioms f = map_thy
   119   (fn (axioms, defs, oracles, consolidate) => (f axioms, defs, oracles, consolidate));
   149   (fn (axioms, defs, oracles, wrappers) => (f axioms, defs, oracles, wrappers));
       
   150 
   120 fun map_defs f = map_thy
   151 fun map_defs f = map_thy
   121   (fn (axioms, defs, oracles, consolidate) => (axioms, f defs, oracles, consolidate));
   152   (fn (axioms, defs, oracles, wrappers) => (axioms, f defs, oracles, wrappers));
       
   153 
   122 fun map_oracles f = map_thy
   154 fun map_oracles f = map_thy
   123   (fn (axioms, defs, oracles, consolidate) => (axioms, defs, f oracles, consolidate));
   155   (fn (axioms, defs, oracles, wrappers) => (axioms, defs, f oracles, wrappers));
       
   156 
       
   157 fun map_wrappers f = map_thy
       
   158   (fn (axioms, defs, oracles, wrappers) => (axioms, defs, oracles, f wrappers));
   124 
   159 
   125 
   160 
   126 (* basic operations *)
   161 (* basic operations *)
   127 
   162 
   128 val axiom_space = #1 o #axioms o rep_theory;
   163 val axiom_space = #1 o #axioms o rep_theory;
   130 
   165 
   131 val oracle_space = #1 o #oracles o rep_theory;
   166 val oracle_space = #1 o #oracles o rep_theory;
   132 val oracle_table = #2 o #oracles o rep_theory;
   167 val oracle_table = #2 o #oracles o rep_theory;
   133 
   168 
   134 val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
   169 val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
       
   170 fun all_axioms_of thy = maps axioms_of (thy :: ancestors_of thy);
   135 
   171 
   136 val defs_of = #defs o rep_theory;
   172 val defs_of = #defs o rep_theory;
   137 
   173 
   138 fun requires thy name what =
   174 
   139   if Context.exists_name name thy then ()
   175 (* begin/end theory *)
   140   else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
   176 
   141 
   177 val begin_wrappers = rev o #1 o #wrappers o rep_theory;
   142 
   178 val end_wrappers = rev o #2 o #wrappers o rep_theory;
   143 (* interpretators *)
   179 
   144 
   180 fun at_begin f = map_wrappers (apfst (cons (f, stamp ())));
   145 fun add_consolidate f = map_thy
   181 fun at_end f = map_wrappers (apsnd (cons (f, stamp ())));
   146   (fn (axioms, defs, oracles, consolidate) => (axioms, defs, oracles, consolidate #> f));
   182 
   147 
   183 fun begin_theory name imports =
   148 fun consolidate thy =
   184   let
   149   let
   185     val thy = Context.begin_thy Sign.pp name imports;
   150     val Thy {consolidate, ...} = ThyData.get thy;
   186     val wrappers = begin_wrappers thy;
   151   in
   187   in thy |> Sign.local_path |> apply_wrappers wrappers end;
   152     thy |> consolidate
   188 
   153   end;
   189 fun end_theory thy =
   154 
   190   thy |> apply_wrappers (end_wrappers thy) |> Context.finish_thy;
   155 
       
   156 (** type theory **)
       
   157 
       
   158 (* context operations *)
       
   159 
       
   160 val eq_thy = Context.eq_thy;
       
   161 val subthy = Context.subthy;
       
   162 
       
   163 fun assert_super thy1 thy2 =
       
   164   if subthy (thy1, thy2) then thy2
       
   165   else raise THEORY ("Not a super theory", [thy1, thy2]);
       
   166 
       
   167 val parents_of = Context.parents_of;
       
   168 val ancestors_of = Context.ancestors_of;
       
   169 
       
   170 val check_thy = Context.check_thy;
       
   171 val deref = Context.deref;
       
   172 val merge = Context.merge;
       
   173 val merge_refs = Context.merge_refs;
       
   174 
       
   175 fun merge_list [] = raise THEORY ("Empty merge of theories", [])
       
   176   | merge_list (thy :: thys) = Library.foldl merge (thy, thys);
       
   177 
       
   178 val begin_theory = Sign.local_path o consolidate oo Context.begin_thy Sign.pp;
       
   179 val end_theory = Context.finish_thy;
       
   180 val checkpoint = Context.checkpoint_thy;
       
   181 val copy = Context.copy_thy;
       
   182 
   191 
   183 
   192 
   184 (* signature operations *)
   193 (* signature operations *)
   185 
   194 
   186 structure SignTheory: SIGN_THEORY = Sign;
   195 structure SignTheory: SIGN_THEORY = Sign;
   187 open SignTheory;
   196 open SignTheory;
   188 
   197 
   189 
   198 
   190 
   199 
   191 (** axioms **)
   200 (** add axioms **)
   192 
       
   193 fun all_axioms_of thy = maps axioms_of (thy :: ancestors_of thy);
       
   194 
       
   195 
   201 
   196 (* prepare axioms *)
   202 (* prepare axioms *)
   197 
   203 
   198 fun err_in_axm msg name =
   204 fun err_in_axm msg name =
   199   cat_error msg ("The error(s) above occurred in axiom " ^ quote name);
   205   cat_error msg ("The error(s) above occurred in axiom " ^ quote name);
   348   NameSpace.extend_table (Sign.naming_of thy) [(bname, (oracle, stamp ()))] oracles
   354   NameSpace.extend_table (Sign.naming_of thy) [(bname, (oracle, stamp ()))] oracles
   349     handle Symtab.DUP dup => err_dup_ora dup);
   355     handle Symtab.DUP dup => err_dup_ora dup);
   350 
   356 
   351 end;
   357 end;
   352 
   358 
   353 functor TheoryInterpretatorFun(Key: THEORY_INTERPRETATOR_KEY) : THEORY_INTERPRETATOR =
       
   354 struct
       
   355 
       
   356 open Key;
       
   357 
       
   358 type interpretator = T list -> theory -> theory;
       
   359 
       
   360 fun apply ips x = fold_rev (fn (_, f) => f x) ips;
       
   361 
       
   362 structure InterpretatorData = TheoryDataFun (
       
   363   type T = ((serial * interpretator) list * T list) * (theory -> theory);
       
   364   val empty = (([], []), I);
       
   365   val extend = I;
       
   366   val copy = I;
       
   367   fun merge pp (((interpretators1, done1), upd1), ((interpretators2, done2), upd2)) =
       
   368     let
       
   369       fun diff (interpretators1 : (serial * interpretator) list, done1)
       
   370         (interpretators2, done2) = let
       
   371           val interpretators = subtract (eq_fst (op =)) interpretators1 interpretators2;
       
   372           val undone = subtract eq done2 done1;
       
   373         in apply interpretators undone end;
       
   374       val interpretators = AList.merge (op =) (K true) (interpretators1, interpretators2);
       
   375       val done = Library.merge eq (done1, done2);
       
   376       val upd_new = diff (interpretators2, done2) (interpretators1, done1)
       
   377         #> diff (interpretators1, done1) (interpretators2, done2);
       
   378       val upd = upd1 #> upd2 #> upd_new;
       
   379     in ((interpretators, done), upd) end;
       
   380 );
       
   381 
       
   382 fun consolidate thy =
       
   383   let
       
   384     val (_, upd) = InterpretatorData.get thy;
       
   385   in
       
   386     thy |> upd |> (InterpretatorData.map o apsnd) (K I)
       
   387   end;
       
   388 
       
   389 val init = Theory.add_consolidate consolidate;
       
   390 
       
   391 fun add_those xs thy =
       
   392   let
       
   393     val ((interpretators, _), _) = InterpretatorData.get thy;
       
   394   in
       
   395     thy
       
   396     |> apply interpretators xs
       
   397     |> (InterpretatorData.map o apfst o apsnd) (append xs)
       
   398   end;
       
   399 
       
   400 val all_those = snd o fst o InterpretatorData.get;
       
   401 
       
   402 fun add_interpretator interpretator thy =
       
   403   let
       
   404     val ((interpretators, xs), _) = InterpretatorData.get thy;
       
   405   in
       
   406     thy
       
   407     |> interpretator (rev xs)
       
   408     |> (InterpretatorData.map o apfst o apfst) (cons (serial (), interpretator))
       
   409   end;
       
   410 
       
   411 end;
       
   412 
       
   413 structure Theory: THEORY = Theory;
       
   414 structure BasicTheory: BASIC_THEORY = Theory;
   359 structure BasicTheory: BASIC_THEORY = Theory;
   415 open BasicTheory;
   360 open BasicTheory;