src/Pure/theory.ML
author wenzelm
Sun Sep 05 19:47:40 2010 +0200 (2010-09-05)
changeset 39133 70d3915c92f0
parent 36610 bafd82950e24
child 39134 917b4b6ba3d2
permissions -rw-r--r--
pretty printing: prefer regular Proof.context over Pretty.pp, which is mostly for special bootstrap purposes involving theory merge, for example;
     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 -> Name_Space.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_axiom: binding * term -> theory -> theory
    32   val add_deps: string -> string * typ -> (string * typ) list -> theory -> theory
    33   val add_def: bool -> bool -> binding * term -> theory -> theory
    34   val add_finals_i: bool -> term list -> theory -> theory
    35   val add_finals: bool -> string list -> theory -> theory
    36   val specify_const: (binding * typ) * mixfix -> theory -> term * theory
    37 end
    38 
    39 structure Theory: THEORY =
    40 struct
    41 
    42 
    43 (** theory context operations **)
    44 
    45 val eq_thy = Context.eq_thy;
    46 val subthy = Context.subthy;
    47 
    48 fun assert_super thy1 thy2 =
    49   if subthy (thy1, thy2) then thy2
    50   else raise THEORY ("Not a super theory", [thy1, thy2]);
    51 
    52 val parents_of = Context.parents_of;
    53 val ancestors_of = Context.ancestors_of;
    54 
    55 val check_thy = Context.check_thy;
    56 val deref = Context.deref;
    57 
    58 val merge = Context.merge;
    59 val merge_refs = Context.merge_refs;
    60 
    61 fun merge_list [] = raise THEORY ("Empty merge of theories", [])
    62   | merge_list (thy :: thys) = Library.foldl merge (thy, thys);
    63 
    64 val checkpoint = Context.checkpoint_thy;
    65 val copy = Context.copy_thy;
    66 
    67 fun requires thy name what =
    68   if exists (fn thy' => Context.theory_name thy' = name) (thy :: ancestors_of thy) then ()
    69   else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
    70 
    71 
    72 
    73 (** datatype thy **)
    74 
    75 type wrapper = (theory -> theory option) * stamp;
    76 
    77 fun apply_wrappers (wrappers: wrapper list) =
    78   perhaps (perhaps_loop (perhaps_apply (map fst wrappers)));
    79 
    80 datatype thy = Thy of
    81  {axioms: term Name_Space.table,
    82   defs: Defs.T,
    83   wrappers: wrapper list * wrapper list};
    84 
    85 fun make_thy (axioms, defs, wrappers) = Thy {axioms = axioms, defs = defs, wrappers = wrappers};
    86 
    87 structure ThyData = Theory_Data_PP
    88 (
    89   type T = thy;
    90   val empty_axioms = Name_Space.empty_table "axiom" : term Name_Space.table;
    91   val empty = make_thy (empty_axioms, Defs.empty, ([], []));
    92 
    93   fun extend (Thy {axioms = _, defs, wrappers}) = make_thy (empty_axioms, defs, wrappers);
    94 
    95   fun merge pp (thy1, thy2) =
    96     let
    97       val Thy {axioms = _, defs = defs1, wrappers = (bgs1, ens1)} = thy1;
    98       val Thy {axioms = _, defs = defs2, wrappers = (bgs2, ens2)} = thy2;
    99 
   100       val axioms' = empty_axioms;
   101       val defs' = Defs.merge pp (defs1, defs2);
   102       val bgs' = Library.merge (eq_snd op =) (bgs1, bgs2);
   103       val ens' = Library.merge (eq_snd op =) (ens1, ens2);
   104     in make_thy (axioms', defs', (bgs', ens')) end;
   105 );
   106 
   107 fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
   108 
   109 fun map_thy f = ThyData.map (fn (Thy {axioms, defs, wrappers}) =>
   110   make_thy (f (axioms, defs, wrappers)));
   111 
   112 
   113 fun map_axioms f = map_thy (fn (axioms, defs, wrappers) => (f axioms, defs, wrappers));
   114 fun map_defs f = map_thy (fn (axioms, defs, wrappers) => (axioms, f defs, wrappers));
   115 fun map_wrappers f = map_thy (fn (axioms, defs, wrappers) => (axioms, defs, f wrappers));
   116 
   117 
   118 (* basic operations *)
   119 
   120 val axiom_space = #1 o #axioms o rep_theory;
   121 val axiom_table = #2 o #axioms o rep_theory;
   122 
   123 val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
   124 fun all_axioms_of thy = maps axioms_of (thy :: ancestors_of thy);
   125 
   126 val defs_of = #defs o rep_theory;
   127 
   128 
   129 (* begin/end theory *)
   130 
   131 val begin_wrappers = rev o #1 o #wrappers o rep_theory;
   132 val end_wrappers = rev o #2 o #wrappers o rep_theory;
   133 
   134 fun at_begin f = map_wrappers (apfst (cons (f, stamp ())));
   135 fun at_end f = map_wrappers (apsnd (cons (f, stamp ())));
   136 
   137 fun begin_theory name imports =
   138   let
   139     val thy = Context.begin_thy Syntax.pp_global name imports;
   140     val wrappers = begin_wrappers thy;
   141   in
   142     thy
   143     |> Sign.local_path
   144     |> Sign.map_naming (Name_Space.set_theory_name name)
   145     |> apply_wrappers wrappers
   146   end;
   147 
   148 fun end_theory thy =
   149   thy |> apply_wrappers (end_wrappers thy) |> Context.finish_thy;
   150 
   151 
   152 
   153 (** primitive specifications **)
   154 
   155 (* raw axioms *)
   156 
   157 fun cert_axm thy (b, raw_tm) =
   158   let
   159     val t = Sign.cert_prop thy raw_tm
   160       handle TYPE (msg, _, _) => error msg
   161         | TERM (msg, _) => error msg;
   162     val _ = Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
   163 
   164     val bad_sorts =
   165       rev ((fold_types o fold_atyps_sorts) (fn (_, []) => I | (T, _) => insert (op =) T) t []);
   166     val _ = null bad_sorts orelse
   167       error ("Illegal sort constraints in primitive specification: " ^
   168         commas (map (setmp_CRITICAL show_sorts true (Syntax.string_of_typ_global thy)) bad_sorts));
   169   in
   170     (b, Sign.no_vars (Syntax.init_pretty_global thy) t)
   171   end handle ERROR msg =>
   172     cat_error msg ("The error(s) above occurred in axiom " ^ quote (Binding.str_of b));
   173 
   174 fun add_axiom raw_axm thy = thy |> map_axioms (fn axioms =>
   175   let
   176     val axm = apsnd Logic.varify_global (cert_axm thy raw_axm);
   177     val (_, axioms') = Name_Space.define true (Sign.naming_of thy) axm axioms;
   178   in axioms' end);
   179 
   180 
   181 (* dependencies *)
   182 
   183 fun dependencies thy unchecked def description lhs rhs =
   184   let
   185     val ctxt = Syntax.init_pretty_global thy;
   186     val consts = Sign.consts_of thy;
   187     fun prep const =
   188       let val Const (c, T) = Sign.no_vars ctxt (Const const)
   189       in (c, Consts.typargs consts (c, Logic.varifyT_global T)) end;
   190 
   191     val lhs_vars = Term.add_tfreesT (#2 lhs) [];
   192     val rhs_extras = fold (#2 #> Term.fold_atyps (fn TFree v =>
   193       if member (op =) lhs_vars v then I else insert (op =) v | _ => I)) rhs [];
   194     val _ =
   195       if null rhs_extras then ()
   196       else error ("Specification depends on extra type variables: " ^
   197         commas_quote (map (Syntax.string_of_typ ctxt o TFree) rhs_extras) ^
   198         "\nThe error(s) above occurred in " ^ quote description);
   199   in Defs.define (Syntax.pp ctxt) unchecked def description (prep lhs) (map prep rhs) end;
   200 
   201 fun add_deps a raw_lhs raw_rhs thy =
   202   let
   203     val lhs :: rhs = map (dest_Const o Sign.cert_term thy o Const) (raw_lhs :: raw_rhs);
   204     val description = if a = "" then #1 lhs ^ " axiom" else a;
   205   in thy |> map_defs (dependencies thy false NONE description lhs rhs) end;
   206 
   207 fun specify_const decl thy =
   208   let val (t as Const const, thy') = Sign.declare_const decl thy
   209   in (t, add_deps "" const [] thy') end;
   210 
   211 
   212 (* overloading *)
   213 
   214 fun check_overloading thy overloaded (c, T) =
   215   let
   216     val declT = Sign.the_const_constraint thy c
   217       handle TYPE (msg, _, _) => error msg;
   218     val T' = Logic.varifyT_global T;
   219 
   220     fun message txt =
   221       [Pretty.block [Pretty.str "Specification of constant ",
   222         Pretty.str c, Pretty.str " ::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ_global thy T)],
   223         Pretty.str txt] |> Pretty.chunks |> Pretty.string_of;
   224   in
   225     if Sign.typ_instance thy (declT, T') then ()
   226     else if Type.raw_instance (declT, T') then
   227       error (setmp_CRITICAL show_sorts true
   228         message "imposes additional sort constraints on the constant declaration")
   229     else if overloaded then ()
   230     else warning (message "is strictly less general than the declared type");
   231     (c, T)
   232   end;
   233 
   234 
   235 (* definitional axioms *)
   236 
   237 local
   238 
   239 fun check_def thy unchecked overloaded (b, tm) defs =
   240   let
   241     val ctxt = ProofContext.init_global thy;
   242     val name = Sign.full_name thy b;
   243     val ((lhs, rhs), _) = Primitive_Defs.dest_def ctxt Term.is_Const (K false) (K false) tm
   244       handle TERM (msg, _) => error msg;
   245     val lhs_const = Term.dest_Const (Term.head_of lhs);
   246     val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
   247     val _ = check_overloading thy overloaded lhs_const;
   248   in defs |> dependencies thy unchecked (SOME name) name lhs_const rhs_consts end
   249   handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
   250    [Pretty.str ("The error(s) above occurred in definition " ^ quote (Binding.str_of b) ^ ":"),
   251     Pretty.fbrk, Pretty.quote (Syntax.pretty_term_global thy tm)]));
   252 
   253 in
   254 
   255 fun add_def unchecked overloaded raw_axm thy =
   256   let val axm = cert_axm thy raw_axm in
   257     thy
   258     |> map_defs (check_def thy unchecked overloaded axm)
   259     |> add_axiom axm
   260   end;
   261 
   262 end;
   263 
   264 
   265 (* add_finals(_i) *)
   266 
   267 local
   268 
   269 fun gen_add_finals prep_term overloaded args thy =
   270   let
   271     fun const_of (Const const) = const
   272       | const_of (Free _) = error "Attempt to finalize variable (or undeclared constant)"
   273       | const_of _ = error "Attempt to finalize non-constant term";
   274     fun specify (c, T) = dependencies thy false NONE (c ^ " axiom") (c, T) [];
   275     val finalize = specify o check_overloading thy overloaded o const_of o
   276       Sign.cert_term thy o prep_term thy;
   277   in thy |> map_defs (fold finalize args) end;
   278 
   279 in
   280 
   281 val add_finals_i = gen_add_finals (K I);
   282 val add_finals = gen_add_finals Syntax.read_term_global;
   283 
   284 end;
   285 
   286 end;