src/Pure/theory.ML
author wenzelm
Fri Jun 17 18:33:28 2005 +0200 (2005-06-17)
changeset 16443 82a116532e3e
parent 16369 96d73621fabb
child 16495 2e99aca906a7
permissions -rw-r--r--
type theory, theory_ref, exception THEORY and related operations imported from Context;
actual theory content declared as theory data;
removed syn_of;
import theory operations in SIGN_THEORY from Sign;
tuned;
     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   type theory
    11   type theory_ref
    12   val sign_of: theory -> theory    (*dummy*)
    13   val rep_theory: theory ->
    14    {axioms: term NameSpace.table,
    15     defs: Defs.graph,
    16     oracles: ((theory * Object.T -> term) * stamp) NameSpace.table}
    17   val parents_of: theory -> theory list
    18   val ancestors_of: theory -> theory list
    19   val eq_thy: theory * theory -> bool
    20   val subthy: theory * theory -> bool
    21   val cert_axm: theory -> string * term -> string * term
    22   val read_def_axm: theory * (indexname -> typ option) * (indexname -> sort option) ->
    23     string list -> string * string -> string * term
    24   val read_axm: theory -> string * string -> string * term
    25   val inferT_axm: theory -> string * term -> string * term
    26 end
    27 
    28 signature THEORY =
    29 sig
    30   include BASIC_THEORY
    31   include SIGN_THEORY
    32   val init: theory -> theory
    33   val axiom_space: theory -> NameSpace.T
    34   val oracle_space: theory -> NameSpace.T
    35   val axioms_of: theory -> (string * term) list
    36   val all_axioms_of: theory -> (string * term) list
    37   val self_ref: theory -> theory_ref
    38   val deref: theory_ref -> theory
    39   val merge: theory * theory -> theory                     (*exception TERM*)
    40   val merge_refs: theory_ref * theory_ref -> theory_ref    (*exception TERM*)
    41   val requires: theory -> string -> string -> unit
    42   val assert_super: theory -> theory -> theory
    43   val copy: theory -> theory
    44   val checkpoint: theory -> theory
    45   val add_axioms: (bstring * string) list -> theory -> theory
    46   val add_axioms_i: (bstring * term) list -> theory -> theory
    47   val add_defs: bool -> (bstring * string) list -> theory -> theory
    48   val add_defs_i: 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 (** type theory **)
    58 
    59 (* context operations *)
    60 
    61 type theory = Context.theory;
    62 type theory_ref = Context.theory_ref;
    63 
    64 val eq_thy = Context.eq_thy;
    65 val subthy = Context.subthy;
    66 val copy = Context.copy_thy;
    67 val checkpoint = Context.checkpoint_thy;
    68 
    69 val parents_of = Context.parents_of;
    70 val ancestors_of = Context.ancestors_of;
    71 
    72 val self_ref = Context.self_ref;
    73 val deref = Context.deref;
    74 val merge = Context.merge;
    75 val merge_refs = Context.merge_refs;
    76 
    77 
    78 (* signature operations *)
    79 
    80 val sign_of = I;
    81 
    82 structure SignTheory: SIGN_THEORY = Sign;
    83 open SignTheory;
    84 
    85 
    86 
    87 (** diagnostics **)  (* FIXME belongs to defs.ML *)
    88 
    89 fun pretty_const pp (c, T) =
    90  [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
    91   Pretty.quote (Pretty.typ pp (Type.freeze_type T))];    (* FIXME zero indexes!? *)
    92 
    93 fun pretty_path pp path = fold_rev (fn (T, c, def) =>
    94   fn [] => [Pretty.block (pretty_const pp (c, T))]
    95    | prts => Pretty.block (pretty_const pp (c, T) @
    96       [Pretty.brk 1, Pretty.str ("depends via " ^ quote def ^ " on")]) :: prts) path [];
    97 
    98 fun chain_history_msg s =    (* FIXME huh!? *)
    99   if Defs.chain_history () then s ^ ": "
   100   else s ^ " (set DEFS_CHAIN_HISTORY=ON for full history): ";
   101 
   102 fun defs_circular pp path =
   103   Pretty.str (chain_history_msg "Cyclic dependency of definitions") :: pretty_path pp path
   104   |> Pretty.chunks |> Pretty.string_of;
   105 
   106 fun defs_infinite_chain pp path =
   107   Pretty.str (chain_history_msg "Infinite chain of definitions") :: pretty_path pp path
   108   |> Pretty.chunks |> Pretty.string_of;
   109 
   110 fun defs_clash def1 def2 = "Type clash in definitions " ^ quote def1 ^ " and " ^ quote def2;
   111 
   112 fun defs_final pp const =
   113   (Pretty.str "Attempt to define final constant" :: Pretty.brk 1 :: pretty_const pp const)
   114   |> Pretty.block |> Pretty.string_of;
   115 
   116 
   117 
   118 (** datatype thy **)
   119 
   120 datatype thy = Thy of
   121  {axioms: term NameSpace.table,
   122   defs: Defs.graph,
   123   oracles: ((theory * Object.T -> term) * stamp) NameSpace.table};
   124 
   125 fun make_thy (axioms, defs, oracles) =
   126   Thy {axioms = axioms, defs = defs, oracles = oracles};
   127 
   128 fun err_dup_axms dups = error ("Duplicate axiom(s): " ^ commas_quote dups);
   129 fun err_dup_oras dups = error ("Duplicate oracle(s): " ^ commas_quote dups);
   130 
   131 structure ThyData = TheoryDataFun
   132 (struct
   133   val name = "Pure/thy";
   134   type T = thy;
   135   val empty = make_thy (NameSpace.empty_table, Defs.empty, NameSpace.empty_table);
   136   val copy = I;
   137 
   138   fun extend (Thy {axioms, defs, oracles}) = make_thy (NameSpace.empty_table, defs, oracles);
   139 
   140   fun merge pp (thy1, thy2) =
   141     let
   142       val Thy {axioms = _, defs = defs1, oracles = oracles1} = thy1;
   143       val Thy {axioms = _, defs = defs2, oracles = oracles2} = thy2;
   144 
   145       val axioms = NameSpace.empty_table;
   146       val defs = Defs.merge defs1 defs2  (* FIXME produce errors in defs.ML *)
   147         handle Defs.CIRCULAR namess => error (defs_circular pp namess)
   148           | Defs.INFINITE_CHAIN namess => error (defs_infinite_chain pp namess);
   149       val oracles = NameSpace.merge_tables eq_snd (oracles1, oracles2)
   150         handle Symtab.DUPS dups => err_dup_oras dups;
   151     in make_thy (axioms, defs, oracles) end;
   152 
   153   fun print _ _ = ();
   154 end);
   155 
   156 val init = ThyData.init;
   157 
   158 fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
   159 
   160 fun map_thy f = ThyData.map (fn (Thy {axioms, defs, oracles}) =>
   161   make_thy (f (axioms, defs, oracles)));
   162 
   163 fun map_axioms f = map_thy (fn (axioms, defs, oracles) => (f axioms, defs, oracles));
   164 fun map_defs f = map_thy (fn (axioms, defs, oracles) => (axioms, f defs, oracles));
   165 fun map_oracles f = map_thy (fn (axioms, defs, oracles) => (axioms, defs, f oracles));
   166 
   167 
   168 (* basic operations *)
   169 
   170 val axiom_space = #1 o #axioms o rep_theory;
   171 val oracle_space = #1 o #oracles o rep_theory;
   172 
   173 val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
   174 fun all_axioms_of thy = List.concat (map axioms_of (thy :: ancestors_of thy));
   175 
   176 fun requires thy name what =
   177   if Context.exists_name name thy then ()
   178   else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
   179 
   180 fun assert_super thy1 thy2 =
   181   if subthy (thy1, thy2) then thy2
   182   else raise THEORY ("Not a super theory", [thy1, thy2]);
   183 
   184 
   185 
   186 (** add axioms **)
   187 
   188 (* prepare axioms *)
   189 
   190 fun err_in_axm name =
   191   error ("The error(s) above occurred in axiom " ^ quote name);
   192 
   193 fun no_vars pp tm =
   194   (case (Term.term_vars tm, Term.term_tvars tm) of
   195     ([], []) => tm
   196   | (ts, ixns) => error (Pretty.string_of (Pretty.block (Pretty.breaks
   197       (Pretty.str "Illegal schematic variable(s) in term:" ::
   198        map (Pretty.term pp) ts @ map (Pretty.typ pp o TVar) ixns)))));
   199 
   200 fun cert_axm thy (name, raw_tm) =
   201   let
   202     val pp = Sign.pp thy;
   203     val (t, T, _) = Sign.certify_term pp thy raw_tm
   204       handle TYPE (msg, _, _) => error msg
   205         | TERM (msg, _) => error msg;
   206   in
   207     Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
   208     assert (T = propT) "Term not of type prop";
   209     (name, no_vars pp t)
   210   end;
   211 
   212 fun read_def_axm (thy, types, sorts) used (name, str) =
   213   let
   214     val ts = Syntax.read (Sign.is_logtype thy) (Sign.syn_of thy) propT str;
   215     val (t, _) = Sign.infer_types (Sign.pp thy) thy types sorts used true (ts, propT);
   216   in cert_axm thy (name, t) end
   217   handle ERROR => err_in_axm name;
   218 
   219 fun read_axm thy name_str = read_def_axm (thy, K NONE, K NONE) [] name_str;
   220 
   221 fun inferT_axm thy (name, pre_tm) =
   222   let
   223     val pp = Sign.pp thy;
   224     val (t, _) = Sign.infer_types pp thy (K NONE) (K NONE) [] true ([pre_tm], propT);
   225   in (name, no_vars pp t) end
   226   handle ERROR => err_in_axm name;
   227 
   228 
   229 (* add_axioms(_i) *)
   230 
   231 local
   232 
   233 fun gen_add_axioms prep_axm raw_axms thy = thy |> map_axioms (fn axioms =>
   234   let
   235     val axms = map (apsnd (Term.compress_term o Logic.varify) o prep_axm thy) raw_axms;
   236     val axioms' = NameSpace.extend_table (Sign.naming_of thy) (axioms, axms)
   237       handle Symtab.DUPS dups => err_dup_axms dups;
   238   in axioms' end);
   239 
   240 in
   241 
   242 val add_axioms = gen_add_axioms read_axm;
   243 val add_axioms_i = gen_add_axioms cert_axm;
   244 
   245 end;
   246 
   247 
   248 
   249 (** add constant definitions **)
   250 
   251 (* overloading *)
   252 
   253 datatype overloading = Clean | Implicit | Useless;
   254 
   255 fun overloading thy overloaded declT defT =
   256   let
   257     val defT' = Term.incr_tvar (maxidx_of_typ declT + 1) (Type.varifyT defT);
   258   in
   259     if Sign.typ_instance thy (declT, defT') then Clean
   260     else if Sign.typ_instance thy (Type.strip_sorts declT, Type.strip_sorts defT') then Useless
   261     else if overloaded then Clean
   262     else Implicit
   263   end;
   264 
   265 
   266 (* dest_def *)
   267 
   268 fun dest_def pp tm =
   269   let
   270     fun err msg = raise TERM (msg, [tm]);
   271 
   272     val (lhs, rhs) = Logic.dest_equals (Logic.strip_imp_concl tm)
   273       handle TERM _ => err "Not a meta-equality (==)";
   274     val (head, args) = Term.strip_comb lhs;
   275     val (c, T) = Term.dest_Const head
   276       handle TERM _ => err "Head of lhs not a constant";
   277 
   278     fun dest_free (Free (x, _)) = x
   279       | dest_free (Const ("TYPE", Type ("itself", [TFree (x, _)]))) = x
   280       | dest_free _ = raise Match;
   281 
   282     val show_terms = commas_quote o map (Pretty.string_of_term pp);
   283     val show_frees = commas_quote o map dest_free;
   284     val show_tfrees = commas_quote o map fst;
   285 
   286     val lhs_nofrees = filter (not o can dest_free) args;
   287     val lhs_dups = duplicates args;
   288     val rhs_extras = term_frees rhs |> fold (remove op =) args;
   289     val rhs_extrasT = term_tfrees rhs |> fold (remove op =) (typ_tfrees T);
   290   in
   291     if not (null lhs_nofrees) then
   292       err ("Non-variables as arguments on lhs: " ^ show_terms lhs_nofrees)
   293     else if not (null lhs_dups) then
   294       err ("Duplicate variables on lhs: " ^ show_frees lhs_dups)
   295     else if not (null rhs_extras) then
   296       err ("Extra variables on rhs: " ^ show_frees rhs_extras)
   297     else if not (null rhs_extrasT) then
   298       err ("Extra type variables on rhs: " ^ show_tfrees rhs_extrasT)
   299     else if exists_Const (equal (c, T)) rhs then
   300       err ("Constant to be defined occurs on rhs")
   301     else ((c, T), rhs)
   302   end;
   303 
   304 
   305 (* check_def *)
   306 
   307 fun declare thy c defs =      (* FIXME move to defs.ML *)
   308   let val T = Sign.the_const_type thy c
   309   in if_none (try (Defs.declare defs) (c, T)) defs end;
   310 
   311 fun check_def thy overloaded (bname, tm) defs =
   312   let
   313     val pp = Sign.pp thy;
   314 
   315     fun err msg = error (Pretty.string_of (Pretty.chunks
   316      [Pretty.str msg, Pretty.block
   317       [Pretty.str ("The error(s) above occurred in definition " ^ quote bname ^ ":"),
   318         Pretty.fbrk, Pretty.quote (Pretty.term pp tm)]]));
   319 
   320     fun prt_const (c, T) =
   321      [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
   322       Pretty.quote (Pretty.typ pp (Type.freeze_type T))];
   323     fun string_of_def const txt =
   324       [Pretty.block (Pretty.str "Definition of " :: pretty_const pp const), Pretty.str txt]
   325       |> Pretty.chunks |> Pretty.string_of;
   326 
   327     val ((c, defT), rhs) = dest_def pp tm handle TERM (msg, _) => err msg;
   328     val rhs_consts = Term.term_constsT rhs;
   329     val declT = Sign.the_const_type thy c;
   330 
   331     val _ =
   332       (case overloading thy overloaded declT defT of
   333         Clean => ()
   334       | Implicit => warning (string_of_def (c, defT)
   335           ("is strictly less general than the declared type (see " ^ quote bname ^ ")"))
   336       | Useless => err (Library.setmp show_sorts true (string_of_def (c, defT))
   337           "imposes additional sort constraints on the declared type of the constant"));
   338 
   339     val decl_defs = defs |> declare thy c |> fold (declare thy) (map #1 rhs_consts);
   340   in
   341     Defs.define decl_defs (c, defT) (Sign.full_name thy bname) rhs_consts
   342       (* FIXME produce errors in defs.ML *)
   343       handle Defs.DEFS msg => err ("DEFS: " ^ msg)   (* FIXME sys_error!? *)
   344         | Defs.CIRCULAR path => err (defs_circular pp path)
   345         | Defs.INFINITE_CHAIN path => err (defs_infinite_chain pp path)
   346         | Defs.CLASH (_, def1, def2) => err (defs_clash def1 def2)
   347         | Defs.FINAL const => err (defs_final pp const)
   348   end;
   349 
   350 
   351 (* add_defs(_i) *)
   352 
   353 local
   354 
   355 fun gen_add_defs prep_axm overloaded raw_axms thy =
   356   let val axms = map (prep_axm thy) raw_axms in
   357     thy
   358     |> map_defs (fold (check_def thy overloaded) axms)
   359     |> add_axioms_i axms
   360   end;
   361 
   362 in
   363 
   364 val add_defs_i = gen_add_defs cert_axm;
   365 val add_defs = gen_add_defs read_axm;
   366 
   367 end;
   368 
   369 
   370 (* add_finals(_i) *)
   371 
   372 local
   373 
   374 fun gen_add_finals prep_term overloaded raw_terms thy =
   375   let
   376     fun finalize tm finals =
   377       let
   378         fun err msg = raise TERM (msg, [tm]);    (* FIXME error!? *)
   379         val (c, defT) =
   380           (case tm of Const x => x
   381           | Free _ => err "Attempt to finalize variable (or undeclared constant)"
   382           | _ => err "Attempt to finalize non-constant term");
   383         val declT = Sign.the_const_type thy c
   384           handle TYPE (msg, _, _) => err msg;
   385         val _ =    (* FIXME unify messages with defs *)
   386           (case overloading thy overloaded declT defT of
   387             Clean => ()
   388           | Implicit => warning ("Finalizing " ^ quote c ^
   389               " at a strictly less general type than declared")
   390           | Useless => err "Sort constraints stronger than declared");
   391       in Defs.finalize (if_none (try (Defs.declare finals) (c, declT)) finals) (c, defT) end;
   392   in thy |> map_defs (fold finalize (map (prep_term thy) raw_terms)) end;
   393 
   394 fun read_term thy = Sign.simple_read_term thy TypeInfer.logicT;
   395 fun cert_term thy = #1 o Sign.certify_term (Sign.pp thy) thy;
   396 
   397 in
   398 
   399 val add_finals = gen_add_finals read_term;
   400 val add_finals_i = gen_add_finals cert_term;
   401 
   402 end;
   403 
   404 
   405 
   406 (** add oracle **)
   407 
   408 fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
   409   NameSpace.extend_table (Sign.naming_of thy) (oracles, [(bname, (oracle, stamp ()))])
   410     handle Symtab.DUPS dups => err_dup_oras dups);
   411 
   412 end;
   413 
   414 structure BasicTheory: BASIC_THEORY = Theory;
   415 open BasicTheory;