src/Pure/theory.ML
changeset 61261 ddb2da7cb2e4
parent 61256 9ce5de06cd3b
child 61262 7bd1eb4b056e
equal deleted inserted replaced
61260:e6f03fae14d5 61261:ddb2da7cb2e4
    23   val begin_theory: string * Position.T -> theory list -> theory
    23   val begin_theory: string * Position.T -> theory list -> theory
    24   val end_theory: theory -> theory
    24   val end_theory: theory -> theory
    25   val add_axiom: Proof.context -> binding * term -> theory -> theory
    25   val add_axiom: Proof.context -> binding * term -> theory -> theory
    26   val const_dep: theory -> string * typ -> Defs.entry
    26   val const_dep: theory -> string * typ -> Defs.entry
    27   val type_dep: string * typ list -> Defs.entry
    27   val type_dep: string * typ list -> Defs.entry
    28   val add_deps: Proof.context -> string -> Defs.entry -> Defs.entry list -> theory -> theory
    28   val add_deps: Defs.context -> string -> Defs.entry -> Defs.entry list -> theory -> theory
    29   val add_deps_global: string -> Defs.entry -> Defs.entry list -> theory -> theory
    29   val add_deps_global: string -> Defs.entry -> Defs.entry list -> theory -> theory
    30   val add_def: Proof.context -> bool -> bool -> binding * term -> theory -> theory
    30   val add_def: Defs.context -> bool -> bool -> binding * term -> theory -> theory
    31   val specify_const: (binding * typ) * mixfix -> theory -> term * theory
    31   val specify_const: (binding * typ) * mixfix -> theory -> term * theory
    32   val check_overloading: Proof.context -> bool -> string * typ -> unit
    32   val check_overloading: Proof.context -> bool -> string * typ -> unit
    33 end
    33 end
    34 
    34 
    35 structure Theory: THEORY =
    35 structure Theory: THEORY =
    73   fun extend (Thy {pos = _, id = _, axioms = _, defs, wrappers}) =
    73   fun extend (Thy {pos = _, id = _, axioms = _, defs, wrappers}) =
    74     make_thy (Position.none, 0, empty_axioms, defs, wrappers);
    74     make_thy (Position.none, 0, empty_axioms, defs, wrappers);
    75 
    75 
    76   fun merge pp (thy1, thy2) =
    76   fun merge pp (thy1, thy2) =
    77     let
    77     let
    78       val ctxt = Syntax.init_pretty pp;
       
    79       val Thy {pos = _, id = _, axioms = _, defs = defs1, wrappers = (bgs1, ens1)} = thy1;
    78       val Thy {pos = _, id = _, axioms = _, defs = defs1, wrappers = (bgs1, ens1)} = thy1;
    80       val Thy {pos = _, id = _, axioms = _, defs = defs2, wrappers = (bgs2, ens2)} = thy2;
    79       val Thy {pos = _, id = _, axioms = _, defs = defs2, wrappers = (bgs2, ens2)} = thy2;
    81 
    80 
    82       val axioms' = empty_axioms;
    81       val axioms' = empty_axioms;
    83       val defs' = Defs.merge ctxt (defs1, defs2);
    82       val defs' = Defs.merge (Syntax.init_pretty pp, NONE) (defs1, defs2);
    84       val bgs' = Library.merge (eq_snd op =) (bgs1, bgs2);
    83       val bgs' = Library.merge (eq_snd op =) (bgs1, bgs2);
    85       val ens' = Library.merge (eq_snd op =) (ens1, ens2);
    84       val ens' = Library.merge (eq_snd op =) (ens1, ens2);
    86     in make_thy (Position.none, 0, axioms', defs', (bgs', ens')) end;
    85     in make_thy (Position.none, 0, axioms', defs', (bgs', ens')) end;
    87 );
    86 );
    88 
    87 
   214 (* dependencies *)
   213 (* dependencies *)
   215 
   214 
   216 fun const_dep thy (c, T) = ((Defs.Const, c), Sign.const_typargs thy (c, T));
   215 fun const_dep thy (c, T) = ((Defs.Const, c), Sign.const_typargs thy (c, T));
   217 fun type_dep (c, args) = ((Defs.Type, c), args);
   216 fun type_dep (c, args) = ((Defs.Type, c), args);
   218 
   217 
   219 fun dependencies ctxt unchecked def description lhs rhs =
   218 fun dependencies (context as (ctxt, _)) unchecked def description lhs rhs =
   220   let
   219   let
   221     val thy = Proof_Context.theory_of ctxt;
   220     val thy = Proof_Context.theory_of ctxt;
   222     val consts = Sign.consts_of thy;
   221     val consts = Sign.consts_of thy;
   223 
   222 
   224     fun prep (item, args) =
   223     fun prep (item, args) =
   233     val _ =
   232     val _ =
   234       if null rhs_extras then ()
   233       if null rhs_extras then ()
   235       else error ("Specification depends on extra type variables: " ^
   234       else error ("Specification depends on extra type variables: " ^
   236         commas_quote (map (Syntax.string_of_typ ctxt o TFree) rhs_extras) ^
   235         commas_quote (map (Syntax.string_of_typ ctxt o TFree) rhs_extras) ^
   237         "\nThe error(s) above occurred in " ^ quote description);
   236         "\nThe error(s) above occurred in " ^ quote description);
   238   in Defs.define ctxt unchecked def description (prep lhs) (map prep rhs) end;
   237   in Defs.define context unchecked def description (prep lhs) (map prep rhs) end;
   239 
   238 
   240 fun cert_entry thy ((Defs.Const, c), args) =
   239 fun cert_entry thy ((Defs.Const, c), args) =
   241       Sign.cert_term thy (Const (c, Sign.const_instance thy (c, args)))
   240       Sign.cert_term thy (Const (c, Sign.const_instance thy (c, args)))
   242       |> dest_Const |> const_dep thy
   241       |> dest_Const |> const_dep thy
   243   | cert_entry thy ((Defs.Type, c), args) =
   242   | cert_entry thy ((Defs.Type, c), args) =
   244       Sign.certify_typ thy (Type (c, args)) |> dest_Type |> type_dep;
   243       Sign.certify_typ thy (Type (c, args)) |> dest_Type |> type_dep;
   245 
   244 
   246 fun add_deps ctxt a raw_lhs raw_rhs thy =
   245 fun add_deps context a raw_lhs raw_rhs thy =
   247   let
   246   let
   248     val (lhs as ((_, lhs_name), _)) :: rhs = map (cert_entry thy) (raw_lhs :: raw_rhs);
   247     val (lhs as ((_, lhs_name), _)) :: rhs = map (cert_entry thy) (raw_lhs :: raw_rhs);
   249     val description = if a = "" then lhs_name ^ " axiom" else a;
   248     val description = if a = "" then lhs_name ^ " axiom" else a;
   250   in thy |> map_defs (dependencies ctxt false NONE description lhs rhs) end;
   249   in thy |> map_defs (dependencies context false NONE description lhs rhs) end;
   251 
   250 
   252 fun add_deps_global a x y thy =
   251 fun add_deps_global a x y thy =
   253   add_deps (Syntax.init_pretty_global thy) a x y thy;
   252   add_deps (Syntax.init_pretty_global thy, NONE) a x y thy;
   254 
   253 
   255 fun specify_const decl thy =
   254 fun specify_const decl thy =
   256   let val (t, thy') = Sign.declare_const_global decl thy;
   255   let val (t, thy') = Sign.declare_const_global decl thy;
   257   in (t, add_deps_global "" (const_dep thy' (dest_Const t)) [] thy') end;
   256   in (t, add_deps_global "" (const_dep thy' (dest_Const t)) [] thy') end;
   258 
   257 
   284 
   283 
   285 (* definitional axioms *)
   284 (* definitional axioms *)
   286 
   285 
   287 local
   286 local
   288 
   287 
   289 fun check_def ctxt thy unchecked overloaded (b, tm) defs =
   288 fun check_def (context as (ctxt, _)) thy unchecked overloaded (b, tm) defs =
   290   let
   289   let
   291     val name = Sign.full_name thy b;
   290     val name = Sign.full_name thy b;
   292     val ((lhs, rhs), _) = Primitive_Defs.dest_def ctxt Term.is_Const (K false) (K false) tm
   291     val ((lhs, rhs), _) = Primitive_Defs.dest_def ctxt Term.is_Const (K false) (K false) tm
   293       handle TERM (msg, _) => error msg;
   292       handle TERM (msg, _) => error msg;
   294     val lhs_const = Term.dest_Const (Term.head_of lhs);
   293     val lhs_const = Term.dest_Const (Term.head_of lhs);
   298     val rhs_types =
   297     val rhs_types =
   299       (fold_types o fold_subtypes) (fn Type t => insert (op =) (type_dep t) | _ => I) rhs [];
   298       (fold_types o fold_subtypes) (fn Type t => insert (op =) (type_dep t) | _ => I) rhs [];
   300     val rhs_deps = rhs_consts @ rhs_types;
   299     val rhs_deps = rhs_consts @ rhs_types;
   301 
   300 
   302     val _ = check_overloading ctxt overloaded lhs_const;
   301     val _ = check_overloading ctxt overloaded lhs_const;
   303   in defs |> dependencies ctxt unchecked (SOME name) name (const_dep thy lhs_const) rhs_deps end
   302   in defs |> dependencies context unchecked (SOME name) name (const_dep thy lhs_const) rhs_deps end
   304   handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
   303   handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
   305    [Pretty.str ("The error(s) above occurred in definition " ^ Binding.print b ^ ":"),
   304    [Pretty.str ("The error(s) above occurred in definition " ^ Binding.print b ^ ":"),
   306     Pretty.fbrk, Pretty.quote (Syntax.pretty_term ctxt tm)]));
   305     Pretty.fbrk, Pretty.quote (Syntax.pretty_term ctxt tm)]));
   307 
   306 
   308 in
   307 in
   309 
   308 
   310 fun add_def ctxt unchecked overloaded raw_axm thy =
   309 fun add_def (context as (ctxt, _)) unchecked overloaded raw_axm thy =
   311   let val axm = cert_axm ctxt raw_axm in
   310   let val axm = cert_axm ctxt raw_axm in
   312     thy
   311     thy
   313     |> map_defs (check_def ctxt thy unchecked overloaded axm)
   312     |> map_defs (check_def context thy unchecked overloaded axm)
   314     |> add_axiom ctxt axm
   313     |> add_axiom ctxt axm
   315   end;
   314   end;
   316 
   315 
   317 end;
   316 end;
   318 
   317