src/Pure/theory.ML
author haftmann
Tue Sep 06 08:30:43 2005 +0200 (2005-09-06)
changeset 17271 2756a73f63a5
parent 17038 6dbd7c63a5a6
child 17496 26535df536ae
permissions -rw-r--r--
introduced some new-style AList operations
     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.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 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.graph
    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.graph,
    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 (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 (* check_overloading *)
   228 
   229 fun check_overloading thy overloaded (c, T) =
   230   let
   231     val declT =
   232       (case Sign.const_constraint thy c of
   233         NONE => error ("Undeclared constant " ^ quote c)
   234       | SOME declT => declT);
   235     val T' = Type.varifyT T;
   236 
   237     fun message txt =
   238       [Pretty.block [Pretty.str "Specification of constant ",
   239         Pretty.str c, Pretty.str " ::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ thy T)],
   240         Pretty.str txt] |> Pretty.chunks |> Pretty.string_of;
   241   in
   242     if Sign.typ_instance thy (declT, T') then ()
   243     else if Type.raw_instance (declT, T') then
   244       error (Library.setmp show_sorts true
   245         message "imposes additional sort constraints on the constant declaration")
   246     else if overloaded then ()
   247     else warning (message "is strictly less general than the declared type");
   248     (c, T)
   249   end;
   250 
   251 
   252 (* dest_def *)
   253 
   254 fun dest_def pp tm =
   255   let
   256     fun err msg = raise TERM (msg, [tm]);
   257 
   258     val (lhs, rhs) = Logic.dest_equals (Logic.strip_imp_concl tm)
   259       handle TERM _ => err "Not a meta-equality (==)";
   260     val (head, args) = Term.strip_comb lhs;
   261     val (c, T) = Term.dest_Const head
   262       handle TERM _ => err "Head of lhs not a constant";
   263 
   264     fun dest_free (Free (x, _)) = x
   265       | dest_free (Const ("TYPE", Type ("itself", [TFree (x, _)]))) = x
   266       | dest_free _ = raise Match;
   267 
   268     val show_terms = commas_quote o map (Pretty.string_of_term pp);
   269     val show_frees = commas_quote o map dest_free;
   270     val show_tfrees = commas_quote o map fst;
   271 
   272     val lhs_nofrees = filter (not o can dest_free) args;
   273     val lhs_dups = duplicates args;
   274     val rhs_extras = term_frees rhs |> fold (remove op =) args;
   275     val rhs_extrasT = term_tfrees rhs |> fold (remove op =) (typ_tfrees T);
   276   in
   277     if not (null lhs_nofrees) then
   278       err ("Non-variables as arguments on lhs: " ^ show_terms lhs_nofrees)
   279     else if not (null lhs_dups) then
   280       err ("Duplicate variables on lhs: " ^ show_frees lhs_dups)
   281     else if not (null rhs_extras) then
   282       err ("Extra variables on rhs: " ^ show_frees rhs_extras)
   283     else if not (null rhs_extrasT) then
   284       err ("Extra type variables on rhs: " ^ show_tfrees rhs_extrasT)
   285     else if exists_Const (equal (c, T)) rhs then
   286       err ("Constant to be defined occurs on rhs")
   287     else ((c, T), rhs)
   288   end;
   289 
   290 
   291 (* check_def *)
   292 
   293 fun check_def thy overloaded (bname, tm) defs =
   294   let
   295     val pp = Sign.pp thy;
   296     fun prt_const (c, T) =
   297      [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
   298       Pretty.quote (Pretty.typ pp (Type.freeze_type T))];
   299     fun declared (c, _) = (c, Sign.the_const_type thy c);
   300 
   301     val _ = no_vars pp tm;
   302     val (const, rhs) = dest_def pp tm handle TERM (msg, _) => error msg;
   303     val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
   304     val _ = check_overloading thy overloaded const;
   305   in
   306     defs
   307     |> Defs.declare thy (declared const)
   308     |> fold (Defs.declare thy o declared) rhs_consts
   309     |> Defs.define thy const (Sign.full_name thy bname) rhs_consts
   310   end
   311   handle ERROR => error (Pretty.string_of (Pretty.block
   312    [Pretty.str ("The error(s) above occurred in definition " ^ quote bname ^ ":"),
   313     Pretty.fbrk, Pretty.quote (Pretty.term (Sign.pp thy) tm)]));
   314 
   315 
   316 (* add_defs(_i) *)
   317 
   318 local
   319 
   320 fun gen_add_defs prep_axm overloaded raw_axms thy =
   321   let val axms = map (prep_axm thy) raw_axms in
   322     thy
   323     |> map_defs (fold (check_def thy overloaded) axms)
   324     |> add_axioms_i axms
   325   end;
   326 
   327 in
   328 
   329 val add_defs_i = gen_add_defs cert_axm;
   330 val add_defs = gen_add_defs read_axm;
   331 
   332 end;
   333 
   334 
   335 (* add_finals(_i) *)
   336 
   337 local
   338 
   339 fun gen_add_finals prep_term overloaded raw_terms thy =
   340   let
   341     val pp = Sign.pp thy;
   342     fun finalize tm =
   343       let
   344         val _ = no_vars pp tm;
   345         val const as (c, _) =
   346           (case tm of Const x => x
   347           | Free _ => error "Attempt to finalize variable (or undeclared constant)"
   348           | _ => error "Attempt to finalize non-constant term")
   349           |> check_overloading thy overloaded;
   350       in Defs.declare thy (c, Sign.the_const_type thy c) #> Defs.finalize thy const end;
   351   in thy |> map_defs (fold (finalize o prep_term thy) raw_terms) end;
   352 
   353 fun read_term thy = Sign.simple_read_term thy TypeInfer.logicT;
   354 fun cert_term thy = #1 o Sign.certify_term (Sign.pp thy) thy;
   355 
   356 in
   357 
   358 val add_finals = gen_add_finals read_term;
   359 val add_finals_i = gen_add_finals cert_term;
   360 
   361 end;
   362 
   363 
   364 
   365 (** add oracle **)
   366 
   367 fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
   368   NameSpace.extend_table (Sign.naming_of thy) (oracles, [(bname, (oracle, stamp ()))])
   369     handle Symtab.DUPS dups => err_dup_oras dups);
   370 
   371 end;
   372 
   373 structure BasicTheory: BASIC_THEORY = Theory;
   374 open BasicTheory;