src/Pure/Isar/specification.ML
author wenzelm
Fri Jan 27 19:03:16 2006 +0100 (2006-01-27)
changeset 18810 6dc5416368e9
parent 18786 591a37d48794
child 18828 26b80ed2259b
permissions -rw-r--r--
swapped Toplevel.theory_context;
definition(_i): actually rulify as well, support more of object-logic;
definition(_i): more precise treatment of local fixes;
     1 (*  Title:      Pure/Isar/specification.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Common theory/locale specifications --- with type-inference and
     6 toplevel polymorphism.
     7 *)
     8 
     9 signature SPECIFICATION =
    10 sig
    11   val read_specification: (string * string option * mixfix) list ->
    12     ((string * Attrib.src list) * string list) list -> Proof.context ->
    13     (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
    14     Proof.context
    15   val cert_specification: (string * typ option * mixfix) list ->
    16     ((string * Attrib.src list) * term list) list -> Proof.context ->
    17     (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
    18     Proof.context
    19   val axiomatization: xstring option -> (string * string option * mixfix) list ->
    20     ((bstring * Attrib.src list) * string list) list -> theory ->
    21     (term list * (bstring * thm list) list) * (Proof.context * theory)
    22   val axiomatization_i: string option -> (string * typ option * mixfix) list ->
    23     ((bstring * Attrib.src list) * term list) list -> theory ->
    24     (term list * (bstring * thm list) list) * (Proof.context * theory)
    25   val definition: xstring option ->
    26     ((string * string option * mixfix) option * ((string * Attrib.src list) * string)) list ->
    27     theory -> (term * (bstring * thm)) list * (Proof.context * theory)
    28   val definition_i: string option ->
    29     ((string * typ option * mixfix) option * ((string * Attrib.src list) * term)) list ->
    30     theory -> (term * (bstring * thm)) list * (Proof.context * theory)
    31 end;
    32 
    33 structure Specification: SPECIFICATION =
    34 struct
    35 
    36 (* prepare specification *)
    37 
    38 fun prep_specification prep_vars prep_propp prep_att
    39     raw_vars raw_specs ctxt =
    40   let
    41     val thy = ProofContext.theory_of ctxt;
    42 
    43     val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
    44     val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
    45     val ((specs, vs), specs_ctxt) =
    46       prep_propp (params_ctxt, map (map (rpair ([], [])) o snd) raw_specs)
    47       |> swap |>> map (map fst)
    48       ||>> fold_map ProofContext.inferred_param xs;
    49 
    50     val params = vs ~~ map #3 vars;
    51     val names = map (fst o fst) raw_specs;
    52     val atts = map (map (prep_att thy) o snd o fst) raw_specs;
    53   in ((params, (names ~~ atts) ~~ specs), specs_ctxt) end;
    54 
    55 fun read_specification x =
    56   prep_specification ProofContext.read_vars ProofContext.read_propp Attrib.intern_src x;
    57 fun cert_specification x =
    58   prep_specification ProofContext.cert_vars ProofContext.cert_propp (K I) x;
    59 
    60 
    61 (* axiomatization *)
    62 
    63 fun gen_axiomatization prep init locale raw_vars raw_specs thy =
    64   let
    65     val ctxt = init locale thy;
    66     val (vars, specs) = fst (prep raw_vars raw_specs ctxt);
    67 
    68     val (consts, consts_ctxt) = ctxt |> LocalTheory.consts vars;
    69     val subst = Term.subst_atomic (map (Free o fst) vars ~~ consts);
    70 
    71     val (axioms, axioms_ctxt) =
    72       consts_ctxt
    73       |> LocalTheory.axioms (specs |> map (fn (a, props) => (a, map subst props)))
    74       ||> LocalTheory.theory (Theory.add_finals_i false (map Term.head_of consts));
    75 
    76     val _ =
    77       if null vars then ()
    78       else Pretty.writeln (LocalTheory.pretty_consts ctxt (map fst vars));
    79   in ((consts, axioms), LocalTheory.exit axioms_ctxt) end;
    80 
    81 val axiomatization = gen_axiomatization read_specification LocalTheory.init;
    82 val axiomatization_i = gen_axiomatization cert_specification LocalTheory.init_i;
    83 
    84 
    85 (* definition *)
    86 
    87 fun gen_definition prep init locale args thy =
    88   let
    89     fun define (raw_var, (raw_a, raw_prop)) ctxt =
    90       let
    91         val (vars, [(a, [prop])]) = fst (prep (the_list raw_var) [(raw_a, [raw_prop])] ctxt);
    92         val ((x, T), rhs) = prop
    93           |> ObjectLogic.rulify_term thy
    94           |> ObjectLogic.unatomize_term thy   (*produce meta-level equality*)
    95           |> Logic.strip_imp_concl
    96           |> (snd o ProofContext.cert_def ctxt)
    97           |> ProofContext.abs_def;
    98         val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
    99           if x = x' then mx
   100           else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
   101 
   102         fun prove ctxt' const def =
   103           let
   104             val thy' = ProofContext.theory_of ctxt';
   105             val prop' = Term.subst_atomic [(Free (x, T), const)] prop;
   106             val frees = Term.fold_aterms (fn Free (x, _) =>
   107               if ProofContext.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop' [];
   108           in
   109             Goal.prove thy' frees [] prop' (K (ALLGOALS
   110               (ObjectLogic.rulify_tac THEN'
   111                 ObjectLogic.unatomize_tac THEN'
   112                 Tactic.rewrite_goal_tac [def] THEN'
   113                 Tactic.resolve_tac [Drule.reflexive_thm])))
   114             handle ERROR msg => cat_error msg "Failed to prove definitional specification."
   115           end;
   116       in
   117         ctxt
   118         |> LocalTheory.def_finish prove ((x, mx), (a, rhs))
   119         |>> pair (x, T)
   120       end;
   121 
   122     val ctxt = init locale thy;
   123     val ((decls, defs), defs_ctxt) = ctxt |> fold_map define args |>> split_list;
   124     val _ =
   125       if null decls then ()
   126       else Pretty.writeln (LocalTheory.pretty_consts ctxt decls);
   127   in (defs, LocalTheory.exit defs_ctxt) end;
   128 
   129 val definition = gen_definition read_specification LocalTheory.init;
   130 val definition_i = gen_definition cert_specification LocalTheory.init_i;
   131 
   132 end;