src/Pure/theory.ML
author haftmann
Fri Oct 21 14:49:49 2005 +0200 (2005-10-21 ago)
changeset 17952 00eccd84608f
parent 17706 e534e39f3531
child 18139 b15981aedb7b
permissions -rw-r--r--
abandoned rational number functions in favor of General/rat.ML
     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    (*obsolete*)
    13   val rep_theory: theory ->
    14    {axioms: term NameSpace.table,
    15     defs: Defs.T,
    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 begin_theory: string -> theory list -> theory
    33   val end_theory: theory -> theory
    34   val checkpoint: theory -> theory
    35   val copy: theory -> theory
    36   val init_data: theory -> theory
    37   val axiom_space: theory -> NameSpace.T
    38   val oracle_space: theory -> NameSpace.T
    39   val axioms_of: theory -> (string * term) list
    40   val all_axioms_of: theory -> (string * term) list
    41   val defs_of : theory -> Defs.T
    42   val self_ref: theory -> theory_ref
    43   val deref: theory_ref -> theory
    44   val merge: theory * theory -> theory                     (*exception TERM*)
    45   val merge_refs: theory_ref * theory_ref -> theory_ref    (*exception TERM*)
    46   val requires: theory -> string -> string -> unit
    47   val assert_super: theory -> theory -> theory
    48   val dest_def: Pretty.pp -> term -> (string * typ) * term
    49   val add_axioms: (bstring * string) list -> theory -> theory
    50   val add_axioms_i: (bstring * term) list -> theory -> theory
    51   val add_defs: bool -> (bstring * string) list -> theory -> theory
    52   val add_defs_i: bool -> (bstring * term) list -> theory -> theory
    53   val add_finals: bool -> string list -> theory -> theory
    54   val add_finals_i: bool -> term list -> theory -> theory
    55   val add_oracle: bstring * (theory * Object.T -> term) -> theory -> theory
    56 end
    57 
    58 structure Theory: THEORY =
    59 struct
    60 
    61 (** type theory **)
    62 
    63 (* context operations *)
    64 
    65 type theory = Context.theory;
    66 type theory_ref = Context.theory_ref;
    67 
    68 val eq_thy = Context.eq_thy;
    69 val subthy = Context.subthy;
    70 
    71 val parents_of = Context.parents_of;
    72 val ancestors_of = Context.ancestors_of;
    73 
    74 val self_ref = Context.self_ref;
    75 val deref = Context.deref;
    76 val merge = Context.merge;
    77 val merge_refs = Context.merge_refs;
    78 
    79 val begin_theory = Sign.local_path oo Context.begin_thy Sign.pp;
    80 val end_theory = Context.finish_thy;
    81 val checkpoint = Context.checkpoint_thy;
    82 val copy = Context.copy_thy;
    83 
    84 
    85 (* signature operations *)
    86 
    87 val sign_of = I;
    88 
    89 structure SignTheory: SIGN_THEORY = Sign;
    90 open SignTheory;
    91 
    92 
    93 
    94 (** datatype thy **)
    95 
    96 datatype thy = Thy of
    97  {axioms: term NameSpace.table,
    98   defs: Defs.T,
    99   oracles: ((theory * Object.T -> term) * stamp) NameSpace.table};
   100 
   101 fun make_thy (axioms, defs, oracles) =
   102   Thy {axioms = axioms, defs = defs, oracles = oracles};
   103 
   104 fun err_dup_axms dups = error ("Duplicate axiom(s): " ^ commas_quote dups);
   105 fun err_dup_oras dups = error ("Duplicate oracle(s): " ^ commas_quote dups);
   106 
   107 structure ThyData = TheoryDataFun
   108 (struct
   109   val name = "Pure/theory";
   110   type T = thy;
   111   val empty = make_thy (NameSpace.empty_table, Defs.empty, NameSpace.empty_table);
   112   val copy = I;
   113 
   114   fun extend (Thy {axioms, defs, oracles}) = make_thy (NameSpace.empty_table, defs, oracles);
   115 
   116   fun merge pp (thy1, thy2) =
   117     let
   118       val Thy {axioms = _, defs = defs1, oracles = oracles1} = thy1;
   119       val Thy {axioms = _, defs = defs2, oracles = oracles2} = thy2;
   120 
   121       val axioms = NameSpace.empty_table;
   122       val defs = Defs.merge pp (defs1, defs2);
   123       val oracles = NameSpace.merge_tables (eq_snd (op =)) (oracles1, oracles2)
   124         handle Symtab.DUPS dups => err_dup_oras dups;
   125     in make_thy (axioms, defs, oracles) end;
   126 
   127   fun print _ _ = ();
   128 end);
   129 
   130 val init_data = ThyData.init;
   131 
   132 fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
   133 
   134 fun map_thy f = ThyData.map (fn (Thy {axioms, defs, oracles}) =>
   135   make_thy (f (axioms, defs, oracles)));
   136 
   137 fun map_axioms f = map_thy (fn (axioms, defs, oracles) => (f axioms, defs, oracles));
   138 fun map_defs f = map_thy (fn (axioms, defs, oracles) => (axioms, f defs, oracles));
   139 fun map_oracles f = map_thy (fn (axioms, defs, oracles) => (axioms, defs, f oracles));
   140 
   141 
   142 (* basic operations *)
   143 
   144 val axiom_space = #1 o #axioms o rep_theory;
   145 val oracle_space = #1 o #oracles o rep_theory;
   146 
   147 val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
   148 fun all_axioms_of thy = List.concat (map axioms_of (thy :: ancestors_of thy));
   149 
   150 val defs_of = #defs o rep_theory;
   151 
   152 fun requires thy name what =
   153   if Context.exists_name name thy then ()
   154   else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
   155 
   156 fun assert_super thy1 thy2 =
   157   if subthy (thy1, thy2) then thy2
   158   else raise THEORY ("Not a super theory", [thy1, thy2]);
   159 
   160 
   161 
   162 (** add axioms **)
   163 
   164 (* prepare axioms *)
   165 
   166 fun err_in_axm name =
   167   error ("The error(s) above occurred in axiom " ^ quote name);
   168 
   169 fun no_vars pp tm =
   170   (case (Term.term_vars tm, Term.term_tvars tm) of
   171     ([], []) => tm
   172   | (ts, ixns) => error (Pretty.string_of (Pretty.block (Pretty.breaks
   173       (Pretty.str "Illegal schematic variable(s) in term:" ::
   174        map (Pretty.term pp) ts @ map (Pretty.typ pp o TVar) ixns)))));
   175 
   176 fun cert_axm thy (name, raw_tm) =
   177   let
   178     val pp = Sign.pp thy;
   179     val (t, T, _) = Sign.certify_term pp thy raw_tm
   180       handle TYPE (msg, _, _) => error msg
   181         | TERM (msg, _) => error msg;
   182   in
   183     Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
   184     assert (T = propT) "Term not of type prop";
   185     (name, no_vars pp t)
   186   end;
   187 
   188 fun read_def_axm (thy, types, sorts) used (name, str) =
   189   let
   190     val ts = Syntax.read thy (Sign.is_logtype thy) (Sign.syn_of thy) propT str;
   191     val (t, _) = Sign.infer_types (Sign.pp thy) thy types sorts used true (ts, propT);
   192   in cert_axm thy (name, t) end
   193   handle ERROR => err_in_axm name;
   194 
   195 fun read_axm thy name_str = read_def_axm (thy, K NONE, K NONE) [] name_str;
   196 
   197 fun inferT_axm thy (name, pre_tm) =
   198   let
   199     val pp = Sign.pp thy;
   200     val (t, _) = Sign.infer_types pp thy (K NONE) (K NONE) [] true ([pre_tm], propT);
   201   in (name, no_vars pp t) end
   202   handle ERROR => err_in_axm name;
   203 
   204 
   205 (* add_axioms(_i) *)
   206 
   207 local
   208 
   209 fun gen_add_axioms prep_axm raw_axms thy = thy |> map_axioms (fn axioms =>
   210   let
   211     val axms = map (apsnd (Compress.term thy o Logic.varify) o prep_axm thy) raw_axms;
   212     val axioms' = NameSpace.extend_table (Sign.naming_of thy) (axioms, axms)
   213       handle Symtab.DUPS dups => err_dup_axms dups;
   214   in axioms' end);
   215 
   216 in
   217 
   218 val add_axioms = gen_add_axioms read_axm;
   219 val add_axioms_i = gen_add_axioms cert_axm;
   220 
   221 end;
   222 
   223 
   224 
   225 (** add constant definitions **)
   226 
   227 fun prep_const thy (c, T) = (c, Compress.typ thy (Type.varifyT T));
   228 
   229 
   230 (* check_overloading *)
   231 
   232 fun check_overloading thy overloaded (c, T) =
   233   let
   234     val declT =
   235       (case Sign.const_constraint thy c of
   236         NONE => error ("Undeclared constant " ^ quote c)
   237       | SOME declT => declT);
   238     val T' = Type.varifyT T;
   239 
   240     fun message txt =
   241       [Pretty.block [Pretty.str "Specification of constant ",
   242         Pretty.str c, Pretty.str " ::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ thy T)],
   243         Pretty.str txt] |> Pretty.chunks |> Pretty.string_of;
   244   in
   245     if Sign.typ_instance thy (declT, T') then ()
   246     else if Type.raw_instance (declT, T') then
   247       error (Library.setmp show_sorts true
   248         message "imposes additional sort constraints on the constant declaration")
   249     else if overloaded then ()
   250     else warning (message "is strictly less general than the declared type");
   251     (c, T)
   252   end;
   253 
   254 
   255 (* dest_def *)
   256 
   257 fun dest_def pp tm =
   258   let
   259     fun err msg = raise TERM (msg, [tm]);
   260 
   261     val (lhs, rhs) = Logic.dest_equals (Logic.strip_imp_concl tm)
   262       handle TERM _ => err "Not a meta-equality (==)";
   263     val (head, args) = Term.strip_comb lhs;
   264     val (c, T) = Term.dest_Const head
   265       handle TERM _ => err "Head of lhs not a constant";
   266 
   267     fun dest_free (Free (x, _)) = x
   268       | dest_free (Const ("TYPE", Type ("itself", [TFree (x, _)]))) = x
   269       | dest_free _ = raise Match;
   270 
   271     val show_terms = commas_quote o map (Pretty.string_of_term pp);
   272     val show_frees = commas_quote o map dest_free;
   273     val show_tfrees = commas_quote o map fst;
   274 
   275     val lhs_nofrees = filter (not o can dest_free) args;
   276     val lhs_dups = duplicates args;
   277     val rhs_extras = term_frees rhs |> fold (remove op =) args;
   278     val rhs_extrasT = term_tfrees rhs |> fold (remove op =) (typ_tfrees T);
   279   in
   280     if not (null lhs_nofrees) then
   281       err ("Non-variables as arguments on lhs: " ^ show_terms lhs_nofrees)
   282     else if not (null lhs_dups) then
   283       err ("Duplicate variables on lhs: " ^ show_frees lhs_dups)
   284     else if not (null rhs_extras) then
   285       err ("Extra variables on rhs: " ^ show_frees rhs_extras)
   286     else if not (null rhs_extrasT) then
   287       err ("Extra type variables on rhs: " ^ show_tfrees rhs_extrasT)
   288     else if exists_Const (equal (c, T)) rhs then
   289       err ("Constant to be defined occurs on rhs")
   290     else ((c, T), rhs)
   291   end;
   292 
   293 
   294 (* check_def *)
   295 
   296 fun check_def thy overloaded (bname, tm) defs =
   297   let
   298     val pp = Sign.pp thy;
   299     fun prt_const (c, T) =
   300      [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
   301       Pretty.quote (Pretty.typ pp (Type.freeze_type T))];
   302 
   303     val _ = no_vars pp tm;
   304     val name = Sign.full_name thy bname;
   305     val (const, rhs) = dest_def pp tm handle TERM (msg, _) => error msg;
   306     val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
   307     val _ = check_overloading thy overloaded const;
   308   in
   309     defs |> Defs.define (Sign.the_const_type thy)
   310       name (prep_const thy const) (map (prep_const thy) rhs_consts)
   311   end
   312   handle ERROR => error (Pretty.string_of (Pretty.block
   313    [Pretty.str ("The error(s) above occurred in definition " ^ quote bname ^ ":"),
   314     Pretty.fbrk, Pretty.quote (Pretty.term (Sign.pp thy) tm)]));
   315 
   316 
   317 (* add_defs(_i) *)
   318 
   319 local
   320 
   321 fun gen_add_defs prep_axm overloaded raw_axms thy =
   322   let val axms = map (prep_axm thy) raw_axms in
   323     thy
   324     |> map_defs (fold (check_def thy overloaded) axms)
   325     |> add_axioms_i axms
   326   end;
   327 
   328 in
   329 
   330 val add_defs_i = gen_add_defs cert_axm;
   331 val add_defs = gen_add_defs read_axm;
   332 
   333 end;
   334 
   335 
   336 (* add_finals(_i) *)
   337 
   338 local
   339 
   340 fun gen_add_finals prep_term overloaded args thy =
   341   let
   342     fun const_of (Const const) = const
   343       | const_of (Free _) = error "Attempt to finalize variable (or undeclared constant)"
   344       | const_of _ = error "Attempt to finalize non-constant term";
   345     fun specify (c, T) = Defs.define (Sign.the_const_type thy) (c ^ " axiom") (c, T) [];
   346     val finalize = specify o check_overloading thy overloaded o
   347       const_of o no_vars (Sign.pp thy) o prep_term thy;
   348   in thy |> map_defs (fold finalize args) end;
   349 
   350 in
   351 
   352 val add_finals = gen_add_finals Sign.read_term;
   353 val add_finals_i = gen_add_finals Sign.cert_term;
   354 
   355 end;
   356 
   357 
   358 
   359 (** add oracle **)
   360 
   361 fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
   362   NameSpace.extend_table (Sign.naming_of thy) (oracles, [(bname, (oracle, stamp ()))])
   363     handle Symtab.DUPS dups => err_dup_oras dups);
   364 
   365 end;
   366 
   367 structure BasicTheory: BASIC_THEORY = Theory;
   368 open BasicTheory;