src/Pure/theory.ML
author wenzelm
Tue Mar 03 15:09:08 2009 +0100 (2009-03-03)
changeset 30218 cdd82ba2b4fd
parent 29606 fedb8be05f24
child 30337 eb189f7e43a1
permissions -rw-r--r--
Binding.str_of;
     1 (*  Title:      Pure/theory.ML
     2     Author:     Lawrence C Paulson and Markus Wenzel
     3 
     4 Logical theory content: axioms, definitions, and begin/end wrappers.
     5 *)
     6 
     7 signature THEORY =
     8 sig
     9   val eq_thy: theory * theory -> bool
    10   val subthy: theory * theory -> bool
    11   val assert_super: theory -> theory -> theory
    12   val parents_of: theory -> theory list
    13   val ancestors_of: theory -> theory list
    14   val check_thy: theory -> theory_ref
    15   val deref: theory_ref -> theory
    16   val merge: theory * theory -> theory
    17   val merge_refs: theory_ref * theory_ref -> theory_ref
    18   val merge_list: theory list -> theory
    19   val checkpoint: theory -> theory
    20   val copy: theory -> theory
    21   val requires: theory -> string -> string -> unit
    22   val axiom_space: theory -> NameSpace.T
    23   val axiom_table: theory -> term Symtab.table
    24   val axioms_of: theory -> (string * term) list
    25   val all_axioms_of: theory -> (string * term) list
    26   val defs_of: theory -> Defs.T
    27   val at_begin: (theory -> theory option) -> theory -> theory
    28   val at_end: (theory -> theory option) -> theory -> theory
    29   val begin_theory: string -> theory list -> theory
    30   val end_theory: theory -> theory
    31   val add_axioms_i: (binding * term) list -> theory -> theory
    32   val add_axioms: (bstring * string) list -> theory -> theory
    33   val add_deps: string -> string * typ -> (string * typ) list -> theory -> theory
    34   val add_defs_i: bool -> bool -> (binding * term) list -> theory -> theory
    35   val add_defs: bool -> bool -> (bstring * string) list -> theory -> theory
    36   val add_finals_i: bool -> term list -> theory -> theory
    37   val add_finals: bool -> string list -> theory -> theory
    38   val specify_const: Properties.T -> (binding * typ) * mixfix -> theory -> term * theory
    39 end
    40 
    41 structure Theory: THEORY =
    42 struct
    43 
    44 
    45 (** theory context operations **)
    46 
    47 val eq_thy = Context.eq_thy;
    48 val subthy = Context.subthy;
    49 
    50 fun assert_super thy1 thy2 =
    51   if subthy (thy1, thy2) then thy2
    52   else raise THEORY ("Not a super theory", [thy1, thy2]);
    53 
    54 val parents_of = Context.parents_of;
    55 val ancestors_of = Context.ancestors_of;
    56 
    57 val check_thy = Context.check_thy;
    58 val deref = Context.deref;
    59 
    60 val merge = Context.merge;
    61 val merge_refs = Context.merge_refs;
    62 
    63 fun merge_list [] = raise THEORY ("Empty merge of theories", [])
    64   | merge_list (thy :: thys) = Library.foldl merge (thy, thys);
    65 
    66 val checkpoint = Context.checkpoint_thy;
    67 val copy = Context.copy_thy;
    68 
    69 fun requires thy name what =
    70   if exists (fn thy' => Context.theory_name thy' = name) (thy :: ancestors_of thy) then ()
    71   else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
    72 
    73 
    74 
    75 (** datatype thy **)
    76 
    77 type wrapper = (theory -> theory option) * stamp;
    78 
    79 fun apply_wrappers (wrappers: wrapper list) =
    80   perhaps (perhaps_loop (perhaps_apply (map fst wrappers)));
    81 
    82 datatype thy = Thy of
    83  {axioms: term NameSpace.table,
    84   defs: Defs.T,
    85   wrappers: wrapper list * wrapper list};
    86 
    87 fun make_thy (axioms, defs, wrappers) = Thy {axioms = axioms, defs = defs, wrappers = wrappers};
    88 
    89 fun err_dup_axm dup = error ("Duplicate axiom: " ^ quote dup);
    90 
    91 structure ThyData = TheoryDataFun
    92 (
    93   type T = thy;
    94   val empty = make_thy (NameSpace.empty_table, Defs.empty, ([], []));
    95   val copy = I;
    96 
    97   fun extend (Thy {axioms, defs, wrappers}) = make_thy (NameSpace.empty_table, defs, wrappers);
    98 
    99   fun merge pp (thy1, thy2) =
   100     let
   101       val Thy {axioms = _, defs = defs1, wrappers = (bgs1, ens1)} = thy1;
   102       val Thy {axioms = _, defs = defs2, wrappers = (bgs2, ens2)} = thy2;
   103 
   104       val axioms' = NameSpace.empty_table;
   105       val defs' = Defs.merge pp (defs1, defs2);
   106       val bgs' = Library.merge (eq_snd op =) (bgs1, bgs2);
   107       val ens' = Library.merge (eq_snd op =) (ens1, ens2);
   108     in make_thy (axioms', defs', (bgs', ens')) end;
   109 );
   110 
   111 fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
   112 
   113 fun map_thy f = ThyData.map (fn (Thy {axioms, defs, wrappers}) =>
   114   make_thy (f (axioms, defs, wrappers)));
   115 
   116 
   117 fun map_axioms f = map_thy (fn (axioms, defs, wrappers) => (f axioms, defs, wrappers));
   118 fun map_defs f = map_thy (fn (axioms, defs, wrappers) => (axioms, f defs, wrappers));
   119 fun map_wrappers f = map_thy (fn (axioms, defs, wrappers) => (axioms, defs, f wrappers));
   120 
   121 
   122 (* basic operations *)
   123 
   124 val axiom_space = #1 o #axioms o rep_theory;
   125 val axiom_table = #2 o #axioms o rep_theory;
   126 
   127 val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
   128 fun all_axioms_of thy = maps axioms_of (thy :: ancestors_of thy);
   129 
   130 val defs_of = #defs o rep_theory;
   131 
   132 
   133 (* begin/end theory *)
   134 
   135 val begin_wrappers = rev o #1 o #wrappers o rep_theory;
   136 val end_wrappers = rev o #2 o #wrappers o rep_theory;
   137 
   138 fun at_begin f = map_wrappers (apfst (cons (f, stamp ())));
   139 fun at_end f = map_wrappers (apsnd (cons (f, stamp ())));
   140 
   141 fun begin_theory name imports =
   142   let
   143     val thy = Context.begin_thy Syntax.pp_global name imports;
   144     val wrappers = begin_wrappers thy;
   145   in thy |> Sign.local_path |> apply_wrappers wrappers end;
   146 
   147 fun end_theory thy =
   148   thy |> apply_wrappers (end_wrappers thy) |> Context.finish_thy;
   149 
   150 
   151 
   152 (** add axioms **)
   153 
   154 (* prepare axioms *)
   155 
   156 fun err_in_axm msg name =
   157   cat_error msg ("The error(s) above occurred in axiom " ^ quote name);
   158 
   159 fun cert_axm thy (b, raw_tm) =
   160   let
   161     val (t, T, _) = Sign.certify_prop thy raw_tm
   162       handle TYPE (msg, _, _) => error msg
   163         | TERM (msg, _) => error msg;
   164   in
   165     Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
   166     (b, Sign.no_vars (Syntax.pp_global thy) t)
   167   end;
   168 
   169 fun read_axm thy (bname, str) =
   170   cert_axm thy (Binding.name bname, Syntax.read_prop_global thy str)
   171     handle ERROR msg => err_in_axm msg bname;
   172 
   173 
   174 (* add_axioms(_i) *)
   175 
   176 local
   177 
   178 fun gen_add_axioms prep_axm raw_axms thy = thy |> map_axioms (fn axioms =>
   179   let
   180     val axms = map (apsnd Logic.varify o prep_axm thy) raw_axms;
   181     val axioms' = fold (snd oo NameSpace.bind (Sign.naming_of thy)) axms axioms
   182       handle Symtab.DUP dup => err_dup_axm dup;
   183   in axioms' end);
   184 
   185 in
   186 
   187 val add_axioms_i = gen_add_axioms cert_axm;
   188 val add_axioms = gen_add_axioms read_axm;
   189 
   190 end;
   191 
   192 
   193 
   194 (** add constant definitions **)
   195 
   196 (* dependencies *)
   197 
   198 fun dependencies thy unchecked is_def name lhs rhs =
   199   let
   200     val pp = Syntax.pp_global thy;
   201     val consts = Sign.consts_of thy;
   202     fun prep const =
   203       let val Const (c, T) = Sign.no_vars pp (Const const)
   204       in (c, Consts.typargs consts (c, Logic.varifyT T)) end;
   205 
   206     val lhs_vars = Term.add_tfreesT (#2 lhs) [];
   207     val rhs_extras = fold (#2 #> Term.fold_atyps (fn TFree v =>
   208       if member (op =) lhs_vars v then I else insert (op =) v | _ => I)) rhs [];
   209     val _ =
   210       if null rhs_extras then ()
   211       else error ("Specification depends on extra type variables: " ^
   212         commas_quote (map (Pretty.string_of_typ pp o TFree) rhs_extras) ^
   213         "\nThe error(s) above occurred in " ^ quote name);
   214   in Defs.define pp unchecked is_def name (prep lhs) (map prep rhs) end;
   215 
   216 fun add_deps a raw_lhs raw_rhs thy =
   217   let
   218     val lhs :: rhs = map (dest_Const o Sign.cert_term thy o Const) (raw_lhs :: raw_rhs);
   219     val name = if a = "" then (#1 lhs ^ " axiom") else a;
   220   in thy |> map_defs (dependencies thy false false name lhs rhs) end;
   221 
   222 fun specify_const tags decl thy =
   223   let val (t as Const const, thy') = Sign.declare_const tags decl thy
   224   in (t, add_deps "" const [] thy') end;
   225 
   226 
   227 (* check_overloading *)
   228 
   229 fun check_overloading thy overloaded (c, T) =
   230   let
   231     val declT = Sign.the_const_constraint thy c
   232       handle TYPE (msg, _, _) => error msg;
   233     val T' = Logic.varifyT T;
   234 
   235     fun message txt =
   236       [Pretty.block [Pretty.str "Specification of constant ",
   237         Pretty.str c, Pretty.str " ::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ_global thy T)],
   238         Pretty.str txt] |> Pretty.chunks |> Pretty.string_of;
   239   in
   240     if Sign.typ_instance thy (declT, T') then ()
   241     else if Type.raw_instance (declT, T') then
   242       error (Library.setmp show_sorts true
   243         message "imposes additional sort constraints on the constant declaration")
   244     else if overloaded then ()
   245     else warning (message "is strictly less general than the declared type");
   246     (c, T)
   247   end;
   248 
   249 
   250 (* check_def *)
   251 
   252 fun check_def thy unchecked overloaded (b, tm) defs =
   253   let
   254     val ctxt = ProofContext.init thy;
   255     val name = Sign.full_name thy b;
   256     val (lhs_const, rhs) = Sign.cert_def ctxt 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 (Binding.str_of b) ^ ":"),
   262     Pretty.fbrk, Pretty.quote (Syntax.pretty_term_global 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
   295       Sign.cert_term thy o prep_term thy;
   296   in thy |> map_defs (fold finalize args) end;
   297 
   298 in
   299 
   300 val add_finals_i = gen_add_finals (K I);
   301 val add_finals = gen_add_finals Syntax.read_term_global;
   302 
   303 end;
   304 
   305 end;