src/Pure/theory.ML
author wenzelm
Wed Jun 07 02:01:28 2006 +0200 (2006-06-07)
changeset 19806 f860b7a98445
parent 19727 f5895f998402
child 20155 da0505518e69
permissions -rw-r--r--
renamed Type.(un)varifyT to Logic.(un)varifyT;
made (un)varify strict wrt. global context -- may use legacy_(un)varify as workaround;
     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 definitions_of: theory -> string ->
    43     {module: string, name: string, lhs: typ, rhs: (string * typ) list} list
    44   val self_ref: theory -> theory_ref
    45   val deref: theory_ref -> theory
    46   val merge: theory * theory -> theory                     (*exception TERM*)
    47   val merge_refs: theory_ref * theory_ref -> theory_ref    (*exception TERM*)
    48   val requires: theory -> string -> string -> unit
    49   val assert_super: theory -> theory -> theory
    50   val add_axioms: (bstring * string) list -> theory -> theory
    51   val add_axioms_i: (bstring * term) list -> theory -> theory
    52   val add_deps: string -> string * typ -> (string * typ) list -> theory -> theory
    53   val add_defs: bool -> bool -> (bstring * string) list -> theory -> theory
    54   val add_defs_i: bool -> bool -> (bstring * term) list -> theory -> theory
    55   val add_finals: bool -> string list -> theory -> theory
    56   val add_finals_i: bool -> term list -> theory -> theory
    57   val add_oracle: bstring * (theory * Object.T -> term) -> theory -> theory
    58 end
    59 
    60 structure Theory: THEORY =
    61 struct
    62 
    63 
    64 (** type theory **)
    65 
    66 (* context operations *)
    67 
    68 type theory = Context.theory;
    69 type theory_ref = Context.theory_ref;
    70 
    71 val eq_thy = Context.eq_thy;
    72 val subthy = Context.subthy;
    73 
    74 val parents_of = Context.parents_of;
    75 val ancestors_of = Context.ancestors_of;
    76 
    77 val self_ref = Context.self_ref;
    78 val deref = Context.deref;
    79 val merge = Context.merge;
    80 val merge_refs = Context.merge_refs;
    81 
    82 val begin_theory = Sign.local_path oo Context.begin_thy Sign.pp;
    83 val end_theory = Context.finish_thy;
    84 val checkpoint = Context.checkpoint_thy;
    85 val copy = Context.copy_thy;
    86 
    87 
    88 (* signature operations *)
    89 
    90 val sign_of = I;
    91 
    92 structure SignTheory: SIGN_THEORY = Sign;
    93 open SignTheory;
    94 
    95 
    96 
    97 (** datatype thy **)
    98 
    99 datatype thy = Thy of
   100  {axioms: term NameSpace.table,
   101   defs: Defs.T,
   102   oracles: ((theory * Object.T -> term) * stamp) NameSpace.table};
   103 
   104 fun make_thy (axioms, defs, oracles) =
   105   Thy {axioms = axioms, defs = defs, oracles = oracles};
   106 
   107 fun err_dup_axms dups = error ("Duplicate axiom(s): " ^ commas_quote dups);
   108 fun err_dup_oras dups = error ("Duplicate oracle(s): " ^ commas_quote dups);
   109 
   110 structure ThyData = TheoryDataFun
   111 (struct
   112   val name = "Pure/theory";
   113   type T = thy;
   114   val empty = make_thy (NameSpace.empty_table, Defs.empty, NameSpace.empty_table);
   115   val copy = I;
   116 
   117   fun extend (Thy {axioms, defs, oracles}) = make_thy (NameSpace.empty_table, defs, oracles);
   118 
   119   fun merge pp (thy1, thy2) =
   120     let
   121       val Thy {axioms = _, defs = defs1, oracles = oracles1} = thy1;
   122       val Thy {axioms = _, defs = defs2, oracles = oracles2} = thy2;
   123 
   124       val axioms = NameSpace.empty_table;
   125       val defs = Defs.merge pp (defs1, defs2);
   126       val oracles = NameSpace.merge_tables (eq_snd (op =)) (oracles1, oracles2)
   127         handle Symtab.DUPS dups => err_dup_oras dups;
   128     in make_thy (axioms, defs, oracles) end;
   129 
   130   fun print _ _ = ();
   131 end);
   132 
   133 val init_data = ThyData.init;
   134 
   135 fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
   136 
   137 fun map_thy f = ThyData.map (fn (Thy {axioms, defs, oracles}) =>
   138   make_thy (f (axioms, defs, oracles)));
   139 
   140 fun map_axioms f = map_thy (fn (axioms, defs, oracles) => (f axioms, defs, oracles));
   141 fun map_defs f = map_thy (fn (axioms, defs, oracles) => (axioms, f defs, oracles));
   142 fun map_oracles f = map_thy (fn (axioms, defs, oracles) => (axioms, defs, f oracles));
   143 
   144 
   145 (* basic operations *)
   146 
   147 val axiom_space = #1 o #axioms o rep_theory;
   148 val oracle_space = #1 o #oracles o rep_theory;
   149 
   150 val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
   151 fun all_axioms_of thy = maps axioms_of (thy :: ancestors_of thy);
   152 
   153 val defs_of = #defs o rep_theory;
   154 
   155 fun definitions_of thy c =
   156   let
   157     val inst = Consts.instance (Sign.consts_of thy);
   158     val defs = defs_of thy;
   159   in
   160     Defs.specifications_of defs c
   161     |> map_filter (fn (_, {is_def, module, name, lhs, rhs}) =>
   162       if is_def then SOME {module = module, name = name, lhs = inst (c, lhs),
   163         rhs = map (fn (d, Us) => (d, inst (d, Us))) rhs}
   164       else NONE)
   165   end;
   166 
   167 fun requires thy name what =
   168   if Context.exists_name name thy then ()
   169   else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
   170 
   171 fun assert_super thy1 thy2 =
   172   if subthy (thy1, thy2) then thy2
   173   else raise THEORY ("Not a super theory", [thy1, thy2]);
   174 
   175 
   176 
   177 (** add axioms **)
   178 
   179 (* prepare axioms *)
   180 
   181 fun err_in_axm msg name =
   182   cat_error msg ("The error(s) above occurred in axiom " ^ quote name);
   183 
   184 fun cert_axm thy (name, raw_tm) =
   185   let
   186     val (t, T, _) = Sign.certify_prop thy raw_tm
   187       handle TYPE (msg, _, _) => error msg
   188         | TERM (msg, _) => error msg;
   189   in
   190     Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
   191     (name, Sign.no_vars (Sign.pp thy) t)
   192   end;
   193 
   194 fun read_def_axm (thy, types, sorts) used (name, str) =
   195   let
   196     val ts = Syntax.read (Context.Theory thy) (Sign.is_logtype thy) (Sign.syn_of thy) propT str;
   197     val (t, _) =
   198       Sign.infer_types (Sign.pp thy) thy (Sign.consts_of thy) types sorts used true (ts, propT);
   199   in cert_axm thy (name, t) end
   200   handle ERROR msg => err_in_axm msg name;
   201 
   202 fun read_axm thy name_str = read_def_axm (thy, K NONE, K NONE) [] name_str;
   203 
   204 fun inferT_axm thy (name, pre_tm) =
   205   let
   206     val pp = Sign.pp thy;
   207     val (t, _) =
   208       Sign.infer_types pp thy (Sign.consts_of thy) (K NONE) (K NONE) [] true ([pre_tm], propT);
   209   in (name, Sign.no_vars pp t) end
   210   handle ERROR msg => err_in_axm msg name;
   211 
   212 
   213 (* add_axioms(_i) *)
   214 
   215 local
   216 
   217 fun gen_add_axioms prep_axm raw_axms thy = thy |> map_axioms (fn axioms =>
   218   let
   219     val axms = map (apsnd (Compress.term thy o Logic.varify) o prep_axm thy) raw_axms;
   220     val axioms' = NameSpace.extend_table (Sign.naming_of thy) (axioms, axms)
   221       handle Symtab.DUPS dups => err_dup_axms dups;
   222   in axioms' end);
   223 
   224 in
   225 
   226 val add_axioms = gen_add_axioms read_axm;
   227 val add_axioms_i = gen_add_axioms cert_axm;
   228 
   229 end;
   230 
   231 
   232 
   233 (** add constant definitions **)
   234 
   235 (* dependencies *)
   236 
   237 fun dependencies thy unchecked is_def name lhs rhs =
   238   let
   239     val pp = Sign.pp thy;
   240     val consts = Sign.consts_of thy;
   241     fun prep const =
   242       let val Const (c, T) = Sign.no_vars pp (Const const)
   243       in (c, Consts.typargs consts (c, Compress.typ thy (Logic.varifyT T))) end;
   244 
   245     val lhs_vars = Term.add_tfreesT (#2 lhs) [];
   246     val rhs_extras = fold (#2 #> Term.fold_atyps (fn TFree v =>
   247       if member (op =) lhs_vars v then I else insert (op =) v | _ => I)) rhs [];
   248     val _ =
   249       if null rhs_extras then ()
   250       else error ("Specification depends on extra type variables: " ^
   251         commas_quote (map (Pretty.string_of_typ pp o TFree) rhs_extras) ^
   252         "\nThe error(s) above occurred in " ^ quote name);
   253   in Defs.define pp unchecked is_def (Context.theory_name thy) name (prep lhs) (map prep rhs) end;
   254 
   255 fun add_deps a raw_lhs raw_rhs thy =
   256   let
   257     val lhs :: rhs = map (dest_Const o Sign.cert_term thy o Const) (raw_lhs :: raw_rhs);
   258     val name = if a = "" then (#1 lhs ^ " axiom") else a;
   259   in thy |> map_defs (dependencies thy false false name lhs rhs) end;
   260 
   261 
   262 (* check_overloading *)
   263 
   264 fun check_overloading thy overloaded (c, T) =
   265   let
   266     val declT =
   267       (case Sign.const_constraint thy c of
   268         NONE => error ("Undeclared constant " ^ quote c)
   269       | SOME declT => declT);
   270     val T' = Logic.varifyT T;
   271 
   272     fun message txt =
   273       [Pretty.block [Pretty.str "Specification of constant ",
   274         Pretty.str c, Pretty.str " ::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ thy T)],
   275         Pretty.str txt] |> Pretty.chunks |> Pretty.string_of;
   276   in
   277     if Sign.typ_instance thy (declT, T') then ()
   278     else if Type.raw_instance (declT, T') then
   279       error (Library.setmp show_sorts true
   280         message "imposes additional sort constraints on the constant declaration")
   281     else if overloaded then ()
   282     else warning (message "is strictly less general than the declared type");
   283     (c, T)
   284   end;
   285 
   286 
   287 (* check_def *)
   288 
   289 fun check_def thy unchecked overloaded (bname, tm) defs =
   290   let
   291     val name = Sign.full_name thy bname;
   292     val (lhs_const, rhs) = Sign.cert_def (Sign.pp thy) tm;
   293     val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
   294     val _ = check_overloading thy overloaded lhs_const;
   295   in defs |> dependencies thy unchecked true name lhs_const rhs_consts end
   296   handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
   297    [Pretty.str ("The error(s) above occurred in definition " ^ quote bname ^ ":"),
   298     Pretty.fbrk, Pretty.quote (Sign.pretty_term thy tm)]));
   299 
   300 
   301 (* add_defs(_i) *)
   302 
   303 local
   304 
   305 fun gen_add_defs prep_axm unchecked overloaded raw_axms thy =
   306   let val axms = map (prep_axm thy) raw_axms in
   307     thy
   308     |> map_defs (fold (check_def thy unchecked overloaded) axms)
   309     |> add_axioms_i axms
   310   end;
   311 
   312 in
   313 
   314 val add_defs_i = gen_add_defs cert_axm;
   315 val add_defs = gen_add_defs read_axm;
   316 
   317 end;
   318 
   319 
   320 (* add_finals(_i) *)
   321 
   322 local
   323 
   324 fun gen_add_finals prep_term overloaded args thy =
   325   let
   326     fun const_of (Const const) = const
   327       | const_of (Free _) = error "Attempt to finalize variable (or undeclared constant)"
   328       | const_of _ = error "Attempt to finalize non-constant term";
   329     fun specify (c, T) = dependencies thy false false (c ^ " axiom") (c, T) [];
   330     val finalize = specify o check_overloading thy overloaded o const_of o prep_term thy;
   331   in thy |> map_defs (fold finalize args) end;
   332 
   333 in
   334 
   335 val add_finals = gen_add_finals Sign.read_term;
   336 val add_finals_i = gen_add_finals Sign.cert_term;
   337 
   338 end;
   339 
   340 
   341 
   342 (** add oracle **)
   343 
   344 fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
   345   NameSpace.extend_table (Sign.naming_of thy) (oracles, [(bname, (oracle, stamp ()))])
   346     handle Symtab.DUPS dups => err_dup_oras dups);
   347 
   348 end;
   349 
   350 structure BasicTheory: BASIC_THEORY = Theory;
   351 open BasicTheory;