src/Pure/theory.ML
author wenzelm
Thu Jul 05 20:01:37 2007 +0200 (2007-07-05)
changeset 23600 5a5332e1351b
parent 23086 12320f6e2523
child 23655 d2d1138e0ddc
permissions -rw-r--r--
removed comments -- no exception TERM;
merge_list: exception THEORY;
     1 (*  Title:      Pure/theory.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson and Markus Wenzel
     4 
     5 Logical theory content: axioms, definitions, oracles.
     6 *)
     7 
     8 signature BASIC_THEORY =
     9 sig
    10   val eq_thy: theory * theory -> bool
    11   val subthy: theory * theory -> bool
    12 end
    13 
    14 signature THEORY =
    15 sig
    16   include BASIC_THEORY
    17   include SIGN_THEORY
    18   val parents_of: theory -> theory list
    19   val ancestors_of: theory -> theory list
    20   val begin_theory: string -> theory list -> theory
    21   val end_theory: theory -> theory
    22   val checkpoint: theory -> theory
    23   val copy: theory -> theory
    24   val rep_theory: theory ->
    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_table: theory -> term Symtab.table
    30   val oracle_space: theory -> NameSpace.T
    31   val oracle_table: theory -> ((theory * Object.T -> term) * stamp) Symtab.table
    32   val axioms_of: theory -> (string * term) list
    33   val all_axioms_of: theory -> (string * term) list
    34   val defs_of : theory -> Defs.T
    35   val self_ref: theory -> theory_ref
    36   val deref: theory_ref -> theory
    37   val merge: theory * theory -> theory
    38   val merge_refs: theory_ref * theory_ref -> theory_ref
    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
    43   val read_axm: theory -> string * string -> string * term
    44   val add_axioms: (bstring * string) list -> theory -> theory
    45   val add_axioms_i: (bstring * term) list -> theory -> theory
    46   val add_deps: string -> string * typ -> (string * typ) list -> theory -> theory
    47   val add_defs: bool -> bool -> (bstring * string) list -> theory -> theory
    48   val add_defs_i: bool -> bool -> (bstring * term) list -> theory -> theory
    49   val add_finals: bool -> string list -> theory -> theory
    50   val add_finals_i: bool -> term list -> theory -> theory
    51   val add_oracle: bstring * (theory * Object.T -> term) -> theory -> theory
    52 end
    53 
    54 structure Theory: THEORY =
    55 struct
    56 
    57 
    58 (** type theory **)
    59 
    60 (* context operations *)
    61 
    62 val eq_thy = Context.eq_thy;
    63 val subthy = Context.subthy;
    64 
    65 val parents_of = Context.parents_of;
    66 val ancestors_of = Context.ancestors_of;
    67 
    68 val self_ref = Context.self_ref;
    69 val deref = Context.deref;
    70 val merge = Context.merge;
    71 val merge_refs = Context.merge_refs;
    72 
    73 fun merge_list [] = raise THEORY ("Empty merge of theories", [])
    74   | merge_list (thy :: thys) = Library.foldl merge (thy, thys);
    75 
    76 val begin_theory = Sign.local_path oo Context.begin_thy Sign.pp;
    77 val end_theory = Context.finish_thy;
    78 val checkpoint = Context.checkpoint_thy;
    79 val copy = Context.copy_thy;
    80 
    81 
    82 (* signature operations *)
    83 
    84 structure SignTheory: SIGN_THEORY = Sign;
    85 open SignTheory;
    86 
    87 
    88 
    89 (** datatype thy **)
    90 
    91 datatype thy = Thy of
    92  {axioms: term NameSpace.table,
    93   defs: Defs.T,
    94   oracles: ((theory * Object.T -> term) * stamp) NameSpace.table};
    95 
    96 fun make_thy (axioms, defs, oracles) =
    97   Thy {axioms = axioms, defs = defs, oracles = oracles};
    98 
    99 fun err_dup_axms dups = error ("Duplicate axiom(s): " ^ commas_quote dups);
   100 fun err_dup_oras dups = error ("Duplicate oracle(s): " ^ commas_quote dups);
   101 
   102 structure ThyData = TheoryDataFun
   103 (
   104   type T = thy;
   105   val empty = make_thy (NameSpace.empty_table, Defs.empty, NameSpace.empty_table);
   106   val copy = I;
   107 
   108   fun extend (Thy {axioms, defs, oracles}) = make_thy (NameSpace.empty_table, defs, oracles);
   109 
   110   fun merge pp (thy1, thy2) =
   111     let
   112       val Thy {axioms = _, defs = defs1, oracles = oracles1} = thy1;
   113       val Thy {axioms = _, defs = defs2, oracles = oracles2} = thy2;
   114 
   115       val axioms = NameSpace.empty_table;
   116       val defs = Defs.merge pp (defs1, defs2);
   117       val oracles = NameSpace.merge_tables (eq_snd (op =)) (oracles1, oracles2)
   118         handle Symtab.DUPS dups => err_dup_oras dups;
   119     in make_thy (axioms, defs, oracles) end;
   120 );
   121 
   122 fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
   123 
   124 fun map_thy f = ThyData.map (fn (Thy {axioms, defs, oracles}) =>
   125   make_thy (f (axioms, defs, oracles)));
   126 
   127 fun map_axioms f = map_thy (fn (axioms, defs, oracles) => (f axioms, defs, oracles));
   128 fun map_defs f = map_thy (fn (axioms, defs, oracles) => (axioms, f defs, oracles));
   129 fun map_oracles f = map_thy (fn (axioms, defs, oracles) => (axioms, defs, f oracles));
   130 
   131 
   132 (* basic operations *)
   133 
   134 val axiom_space = #1 o #axioms o rep_theory;
   135 val axiom_table = #2 o #axioms o rep_theory;
   136 
   137 val oracle_space = #1 o #oracles o rep_theory;
   138 val oracle_table = #2 o #oracles o rep_theory;
   139 
   140 val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
   141 fun all_axioms_of thy = maps axioms_of (thy :: ancestors_of thy);
   142 
   143 val defs_of = #defs o rep_theory;
   144 
   145 fun requires thy name what =
   146   if Context.exists_name name thy then ()
   147   else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
   148 
   149 fun assert_super thy1 thy2 =
   150   if subthy (thy1, thy2) then thy2
   151   else raise THEORY ("Not a super theory", [thy1, thy2]);
   152 
   153 
   154 
   155 (** add axioms **)
   156 
   157 (* prepare axioms *)
   158 
   159 fun err_in_axm msg name =
   160   cat_error msg ("The error(s) above occurred in axiom " ^ quote name);
   161 
   162 fun cert_axm thy (name, raw_tm) =
   163   let
   164     val (t, T, _) = Sign.certify_prop thy raw_tm
   165       handle TYPE (msg, _, _) => error msg
   166         | TERM (msg, _) => error msg;
   167   in
   168     Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
   169     (name, Sign.no_vars (Sign.pp thy) t)
   170   end;
   171 
   172 fun read_axm thy (name, str) =
   173   cert_axm thy (name, Sign.read_prop thy str)
   174     handle ERROR msg => err_in_axm msg name;
   175 
   176 
   177 (* add_axioms(_i) *)
   178 
   179 local
   180 
   181 fun gen_add_axioms prep_axm raw_axms thy = thy |> map_axioms (fn axioms =>
   182   let
   183     val axms = map (apsnd (Compress.term thy o Logic.varify) o prep_axm thy) raw_axms;
   184     val axioms' = NameSpace.extend_table (Sign.naming_of thy) axms axioms
   185       handle Symtab.DUPS dups => err_dup_axms dups;
   186   in axioms' end);
   187 
   188 in
   189 
   190 val add_axioms = gen_add_axioms read_axm;
   191 val add_axioms_i = gen_add_axioms cert_axm;
   192 
   193 end;
   194 
   195 
   196 
   197 (** add constant definitions **)
   198 
   199 (* dependencies *)
   200 
   201 fun dependencies thy unchecked is_def name lhs rhs =
   202   let
   203     val pp = Sign.pp thy;
   204     val consts = Sign.consts_of thy;
   205     fun prep const =
   206       let val Const (c, T) = Sign.no_vars pp (Const const)
   207       in (c, Consts.typargs consts (c, Compress.typ thy (Logic.varifyT T))) end;
   208 
   209     val lhs_vars = Term.add_tfreesT (#2 lhs) [];
   210     val rhs_extras = fold (#2 #> Term.fold_atyps (fn TFree v =>
   211       if member (op =) lhs_vars v then I else insert (op =) v | _ => I)) rhs [];
   212     val _ =
   213       if null rhs_extras then ()
   214       else error ("Specification depends on extra type variables: " ^
   215         commas_quote (map (Pretty.string_of_typ pp o TFree) rhs_extras) ^
   216         "\nThe error(s) above occurred in " ^ quote name);
   217   in Defs.define pp unchecked is_def (Context.theory_name thy) name (prep lhs) (map prep rhs) end;
   218 
   219 fun add_deps a raw_lhs raw_rhs thy =
   220   let
   221     val lhs :: rhs = map (dest_Const o Sign.cert_term thy o Const) (raw_lhs :: raw_rhs);
   222     val name = if a = "" then (#1 lhs ^ " axiom") else a;
   223   in thy |> map_defs (dependencies thy false false name lhs rhs) end;
   224 
   225 
   226 (* check_overloading *)
   227 
   228 fun check_overloading thy overloaded (c, T) =
   229   let
   230     val declT =
   231       (case Sign.const_constraint thy c of
   232         NONE => error ("Undeclared constant " ^ quote c)
   233       | SOME declT => declT);
   234     val T' = Logic.varifyT T;
   235 
   236     fun message txt =
   237       [Pretty.block [Pretty.str "Specification of constant ",
   238         Pretty.str c, Pretty.str " ::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ thy T)],
   239         Pretty.str txt] |> Pretty.chunks |> Pretty.string_of;
   240   in
   241     if Sign.typ_instance thy (declT, T') then ()
   242     else if Type.raw_instance (declT, T') then
   243       error (Library.setmp show_sorts true
   244         message "imposes additional sort constraints on the constant declaration")
   245     else if overloaded then ()
   246     else warning (message "is strictly less general than the declared type");
   247     (c, T)
   248   end;
   249 
   250 
   251 (* check_def *)
   252 
   253 fun check_def thy unchecked overloaded (bname, tm) defs =
   254   let
   255     val name = Sign.full_name thy bname;
   256     val (lhs_const, rhs) = Sign.cert_def (Sign.pp thy) tm;
   257     val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
   258     val _ = check_overloading thy overloaded lhs_const;
   259   in defs |> dependencies thy unchecked true name lhs_const rhs_consts end
   260   handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
   261    [Pretty.str ("The error(s) above occurred in definition " ^ quote bname ^ ":"),
   262     Pretty.fbrk, Pretty.quote (Sign.pretty_term thy tm)]));
   263 
   264 
   265 (* add_defs(_i) *)
   266 
   267 local
   268 
   269 fun gen_add_defs prep_axm unchecked overloaded raw_axms thy =
   270   let val axms = map (prep_axm thy) raw_axms in
   271     thy
   272     |> map_defs (fold (check_def thy unchecked overloaded) axms)
   273     |> add_axioms_i axms
   274   end;
   275 
   276 in
   277 
   278 val add_defs_i = gen_add_defs cert_axm;
   279 val add_defs = gen_add_defs read_axm;
   280 
   281 end;
   282 
   283 
   284 (* add_finals(_i) *)
   285 
   286 local
   287 
   288 fun gen_add_finals prep_term overloaded args thy =
   289   let
   290     fun const_of (Const const) = const
   291       | const_of (Free _) = error "Attempt to finalize variable (or undeclared constant)"
   292       | const_of _ = error "Attempt to finalize non-constant term";
   293     fun specify (c, T) = dependencies thy false false (c ^ " axiom") (c, T) [];
   294     val finalize = specify o check_overloading thy overloaded o const_of o prep_term thy;
   295   in thy |> map_defs (fold finalize args) end;
   296 
   297 in
   298 
   299 val add_finals = gen_add_finals Sign.read_term;
   300 val add_finals_i = gen_add_finals Sign.cert_term;
   301 
   302 end;
   303 
   304 
   305 
   306 (** add oracle **)
   307 
   308 fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
   309   NameSpace.extend_table (Sign.naming_of thy) [(bname, (oracle, stamp ()))] oracles
   310     handle Symtab.DUPS dups => err_dup_oras dups);
   311 
   312 end;
   313 
   314 structure BasicTheory: BASIC_THEORY = Theory;
   315 open BasicTheory;