src/Pure/Isar/specification.ML
changeset 44192 a32ca9165928
parent 43329 84472e198515
child 45327 4a027cc86f1a
equal deleted inserted replaced
44191:be78e13a80d6 44192:a32ca9165928
     5 toplevel polymorphism.
     5 toplevel polymorphism.
     6 *)
     6 *)
     7 
     7 
     8 signature SPECIFICATION =
     8 signature SPECIFICATION =
     9 sig
     9 sig
    10   val print_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> unit
       
    11   val check_spec:
    10   val check_spec:
    12     (binding * typ option * mixfix) list -> (Attrib.binding * term) list -> Proof.context ->
    11     (binding * typ option * mixfix) list -> (Attrib.binding * term) list -> Proof.context ->
    13     (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
    12     (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
    14   val read_spec:
    13   val read_spec:
    15     (binding * string option * mixfix) list -> (Attrib.binding * string) list -> Proof.context ->
    14     (binding * string option * mixfix) list -> (Attrib.binding * string) list -> Proof.context ->
    35   val axiom: Attrib.binding * term -> theory -> thm * theory
    34   val axiom: Attrib.binding * term -> theory -> thm * theory
    36   val axiom_cmd: Attrib.binding * string -> theory -> thm * theory
    35   val axiom_cmd: Attrib.binding * string -> theory -> thm * theory
    37   val definition:
    36   val definition:
    38     (binding * typ option * mixfix) option * (Attrib.binding * term) ->
    37     (binding * typ option * mixfix) option * (Attrib.binding * term) ->
    39     local_theory -> (term * (string * thm)) * local_theory
    38     local_theory -> (term * (string * thm)) * local_theory
       
    39   val definition':
       
    40     (binding * typ option * mixfix) option * (Attrib.binding * term) ->
       
    41     bool -> local_theory -> (term * (string * thm)) * local_theory
    40   val definition_cmd:
    42   val definition_cmd:
    41     (binding * string option * mixfix) option * (Attrib.binding * string) ->
    43     (binding * string option * mixfix) option * (Attrib.binding * string) ->
    42     local_theory -> (term * (string * thm)) * local_theory
    44     bool -> local_theory -> (term * (string * thm)) * local_theory
    43   val abbreviation: Syntax.mode -> (binding * typ option * mixfix) option * term ->
    45   val abbreviation: Syntax.mode -> (binding * typ option * mixfix) option * term ->
    44     local_theory -> local_theory
    46     bool -> local_theory -> local_theory
    45   val abbreviation_cmd: Syntax.mode -> (binding * string option * mixfix) option * string ->
    47   val abbreviation_cmd: Syntax.mode -> (binding * string option * mixfix) option * string ->
    46     local_theory -> local_theory
    48     bool -> local_theory -> local_theory
    47   val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> local_theory -> local_theory
    49   val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> local_theory -> local_theory
    48   val type_notation_cmd: bool -> Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
    50   val type_notation_cmd: bool -> Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
    49   val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
    51   val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
    50   val notation_cmd: bool -> Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
    52   val notation_cmd: bool -> Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
    51   val theorems: string ->
    53   val theorems: string ->
    52     (Attrib.binding * (thm list * Attrib.src list) list) list ->
    54     (Attrib.binding * (thm list * Attrib.src list) list) list ->
    53     local_theory -> (string * thm list) list * local_theory
    55     bool -> local_theory -> (string * thm list) list * local_theory
    54   val theorems_cmd: string ->
    56   val theorems_cmd: string ->
    55     (Attrib.binding * (Facts.ref * Attrib.src list) list) list ->
    57     (Attrib.binding * (Facts.ref * Attrib.src list) list) list ->
    56     local_theory -> (string * thm list) list * local_theory
    58     bool -> local_theory -> (string * thm list) list * local_theory
    57   val theorem: string -> Method.text option ->
    59   val theorem: string -> Method.text option ->
    58     (thm list list -> local_theory -> local_theory) -> Attrib.binding ->
    60     (thm list list -> local_theory -> local_theory) -> Attrib.binding ->
    59     Element.context_i list -> Element.statement_i ->
    61     Element.context_i list -> Element.statement_i ->
    60     bool -> local_theory -> Proof.state
    62     bool -> local_theory -> Proof.state
    61   val theorem_cmd: string -> Method.text option ->
    63   val theorem_cmd: string -> Method.text option ->
    74 end;
    76 end;
    75 
    77 
    76 structure Specification: SPECIFICATION =
    78 structure Specification: SPECIFICATION =
    77 struct
    79 struct
    78 
    80 
    79 (* diagnostics *)
       
    80 
       
    81 fun print_consts _ _ [] = ()
       
    82   | print_consts ctxt pred cs = Pretty.writeln (Proof_Display.pretty_consts ctxt pred cs);
       
    83 
       
    84 
       
    85 (* prepare specification *)
    81 (* prepare specification *)
    86 
    82 
    87 local
    83 local
    88 
    84 
    89 fun close_forms ctxt i xs As =
    85 fun close_forms ctxt i xs As =
   164 end;
   160 end;
   165 
   161 
   166 
   162 
   167 (* axiomatization -- within global theory *)
   163 (* axiomatization -- within global theory *)
   168 
   164 
   169 fun gen_axioms do_print prep raw_vars raw_specs thy =
   165 fun gen_axioms prep raw_vars raw_specs thy =
   170   let
   166   let
   171     val ((vars, specs), _) = prep raw_vars raw_specs (Proof_Context.init_global thy);
   167     val ((vars, specs), _) = prep raw_vars raw_specs (Proof_Context.init_global thy);
   172     val xs = map (fn ((b, T), _) => (Variable.check_name b, T)) vars;
   168     val xs = map (fn ((b, T), _) => (Variable.check_name b, T)) vars;
   173 
   169 
   174     (*consts*)
   170     (*consts*)
   186     val (facts, facts_lthy) = axioms_thy
   182     val (facts, facts_lthy) = axioms_thy
   187       |> Named_Target.theory_init
   183       |> Named_Target.theory_init
   188       |> Spec_Rules.add Spec_Rules.Unknown (consts, maps (maps #1 o #2) axioms)
   184       |> Spec_Rules.add Spec_Rules.Unknown (consts, maps (maps #1 o #2) axioms)
   189       |> Local_Theory.notes axioms;
   185       |> Local_Theory.notes axioms;
   190 
   186 
   191     val _ =
       
   192       if not do_print then ()
       
   193       else print_consts facts_lthy (K false) xs;
       
   194   in ((consts, map #2 facts), Local_Theory.exit_global facts_lthy) end;
   187   in ((consts, map #2 facts), Local_Theory.exit_global facts_lthy) end;
   195 
   188 
   196 val axiomatization = gen_axioms false check_specification;
   189 val axiomatization = gen_axioms check_specification;
   197 val axiomatization_cmd = gen_axioms true read_specification;
   190 val axiomatization_cmd = gen_axioms read_specification;
   198 
   191 
   199 fun axiom (b, ax) = axiomatization [] [(b, [ax])] #>> (hd o hd o snd);
   192 fun axiom (b, ax) = axiomatization [] [(b, [ax])] #>> (hd o hd o snd);
   200 fun axiom_cmd (b, ax) = axiomatization_cmd [] [(b, [ax])] #>> (hd o hd o snd);
   193 fun axiom_cmd (b, ax) = axiomatization_cmd [] [(b, [ax])] #>> (hd o hd o snd);
   201 
   194 
   202 
   195 
   203 (* definition *)
   196 (* definition *)
   204 
   197 
   205 fun gen_def do_print prep (raw_var, raw_spec) lthy =
   198 fun gen_def prep (raw_var, raw_spec) int lthy =
   206   let
   199   let
   207     val (vars, [((raw_name, atts), prop)]) = fst (prep (the_list raw_var) [raw_spec] lthy);
   200     val (vars, [((raw_name, atts), prop)]) = fst (prep (the_list raw_var) [raw_spec] lthy);
   208     val (((x, T), rhs), prove) = Local_Defs.derived_def lthy true prop;
   201     val (((x, T), rhs), prove) = Local_Defs.derived_def lthy true prop;
   209     val var as (b, _) =
   202     val var as (b, _) =
   210       (case vars of
   203       (case vars of
   226     val ([(def_name, [th'])], lthy4) = lthy3
   219     val ([(def_name, [th'])], lthy4) = lthy3
   227       |> Local_Theory.notes_kind ""
   220       |> Local_Theory.notes_kind ""
   228         [((name, Code.add_default_eqn_attrib :: atts), [([th], [])])];
   221         [((name, Code.add_default_eqn_attrib :: atts), [([th], [])])];
   229 
   222 
   230     val lhs' = Morphism.term (Local_Theory.target_morphism lthy4) lhs;
   223     val lhs' = Morphism.term (Local_Theory.target_morphism lthy4) lhs;
   231     val _ =
   224 
   232       if not do_print then ()
   225     val _ = Proof_Display.print_consts int lthy4 (member (op =) (Term.add_frees lhs' [])) [(x, T)];
   233       else print_consts lthy4 (member (op =) (Term.add_frees lhs' [])) [(x, T)];
       
   234   in ((lhs, (def_name, th')), lthy4) end;
   226   in ((lhs, (def_name, th')), lthy4) end;
   235 
   227 
   236 val definition = gen_def false check_free_spec;
   228 val definition' = gen_def check_free_spec;
   237 val definition_cmd = gen_def true read_free_spec;
   229 fun definition spec = definition' spec false;
       
   230 val definition_cmd = gen_def read_free_spec;
   238 
   231 
   239 
   232 
   240 (* abbreviation *)
   233 (* abbreviation *)
   241 
   234 
   242 fun gen_abbrev do_print prep mode (raw_var, raw_prop) lthy =
   235 fun gen_abbrev prep mode (raw_var, raw_prop) int lthy =
   243   let
   236   let
   244     val ((vars, [(_, prop)]), _) =
   237     val ((vars, [(_, prop)]), _) =
   245       prep (the_list raw_var) [(Attrib.empty_binding, raw_prop)]
   238       prep (the_list raw_var) [(Attrib.empty_binding, raw_prop)]
   246         (lthy |> Proof_Context.set_mode Proof_Context.mode_abbrev);
   239         (lthy |> Proof_Context.set_mode Proof_Context.mode_abbrev);
   247     val ((x, T), rhs) = Local_Defs.abs_def (#2 (Local_Defs.cert_def lthy prop));
   240     val ((x, T), rhs) = Local_Defs.abs_def (#2 (Local_Defs.cert_def lthy prop));
   258     val lthy' = lthy
   251     val lthy' = lthy
   259       |> Proof_Context.set_syntax_mode mode    (* FIXME ?!? *)
   252       |> Proof_Context.set_syntax_mode mode    (* FIXME ?!? *)
   260       |> Local_Theory.abbrev mode (var, rhs) |> snd
   253       |> Local_Theory.abbrev mode (var, rhs) |> snd
   261       |> Proof_Context.restore_syntax_mode lthy;
   254       |> Proof_Context.restore_syntax_mode lthy;
   262 
   255 
   263     val _ = if not do_print then () else print_consts lthy' (K false) [(x, T)];
   256     val _ = Proof_Display.print_consts int lthy' (K false) [(x, T)];
   264   in lthy' end;
   257   in lthy' end;
   265 
   258 
   266 val abbreviation = gen_abbrev false check_free_spec;
   259 val abbreviation = gen_abbrev check_free_spec;
   267 val abbreviation_cmd = gen_abbrev true read_free_spec;
   260 val abbreviation_cmd = gen_abbrev read_free_spec;
   268 
   261 
   269 
   262 
   270 (* notation *)
   263 (* notation *)
   271 
   264 
   272 local
   265 local
   288 end;
   281 end;
   289 
   282 
   290 
   283 
   291 (* fact statements *)
   284 (* fact statements *)
   292 
   285 
   293 fun gen_theorems prep_fact prep_att kind raw_facts lthy =
   286 fun gen_theorems prep_fact prep_att kind raw_facts int lthy =
   294   let
   287   let
   295     val attrib = prep_att (Proof_Context.theory_of lthy);
   288     val attrib = prep_att (Proof_Context.theory_of lthy);
   296     val facts = raw_facts |> map (fn ((name, atts), bs) =>
   289     val facts = raw_facts |> map (fn ((name, atts), bs) =>
   297       ((name, map attrib atts),
   290       ((name, map attrib atts),
   298         bs |> map (fn (b, more_atts) => (prep_fact lthy b, map attrib more_atts))));
   291         bs |> map (fn (b, more_atts) => (prep_fact lthy b, map attrib more_atts))));
   299     val (res, lthy') = lthy |> Local_Theory.notes_kind kind facts;
   292     val (res, lthy') = lthy |> Local_Theory.notes_kind kind facts;
   300     val _ = Proof_Display.print_results true lthy' ((kind, ""), res);
   293     val _ = Proof_Display.print_results int lthy' ((kind, ""), res);
   301   in (res, lthy') end;
   294   in (res, lthy') end;
   302 
   295 
   303 val theorems = gen_theorems (K I) (K I);
   296 val theorems = gen_theorems (K I) (K I);
   304 val theorems_cmd = gen_theorems Proof_Context.get_fact Attrib.intern_src;
   297 val theorems_cmd = gen_theorems Proof_Context.get_fact Attrib.intern_src;
   305 
   298 
   387         lthy
   380         lthy
   388         |> Local_Theory.notes_kind kind
   381         |> Local_Theory.notes_kind kind
   389           (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
   382           (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
   390         |> (fn (res, lthy') =>
   383         |> (fn (res, lthy') =>
   391           if Binding.is_empty name andalso null atts then
   384           if Binding.is_empty name andalso null atts then
   392             (Proof_Display.print_results true lthy' ((kind, ""), res); lthy')
   385             (Proof_Display.print_results int lthy' ((kind, ""), res); lthy')
   393           else
   386           else
   394             let
   387             let
   395               val ([(res_name, _)], lthy'') = lthy'
   388               val ([(res_name, _)], lthy'') = lthy'
   396                 |> Local_Theory.notes_kind kind [((name, atts), [(maps #2 res, [])])];
   389                 |> Local_Theory.notes_kind kind [((name, atts), [(maps #2 res, [])])];
   397               val _ = Proof_Display.print_results true lthy' ((kind, res_name), res);
   390               val _ = Proof_Display.print_results int lthy' ((kind, res_name), res);
   398             in lthy'' end)
   391             in lthy'' end)
   399         |> after_qed results'
   392         |> after_qed results'
   400       end;
   393       end;
   401   in
   394   in
   402     goal_ctxt
   395     goal_ctxt
   409     |> Library.apply (map (fn (f, _) => f int) (rev (Theorem_Hook.get (Context.Proof goal_ctxt))))
   402     |> Library.apply (map (fn (f, _) => f int) (rev (Theorem_Hook.get (Context.Proof goal_ctxt))))
   410   end;
   403   end;
   411 
   404 
   412 in
   405 in
   413 
   406 
   414 val theorem = gen_theorem false (K I) Expression.cert_statement;
   407 val theorem' = gen_theorem false (K I) Expression.cert_statement;
       
   408 fun theorem a b c d e f = theorem' a b c d e f;
   415 val theorem_cmd = gen_theorem false Attrib.intern_src Expression.read_statement;
   409 val theorem_cmd = gen_theorem false Attrib.intern_src Expression.read_statement;
   416 
   410 
   417 val schematic_theorem = gen_theorem true (K I) Expression.cert_statement;
   411 val schematic_theorem = gen_theorem true (K I) Expression.cert_statement;
   418 val schematic_theorem_cmd = gen_theorem true Attrib.intern_src Expression.read_statement;
   412 val schematic_theorem_cmd = gen_theorem true Attrib.intern_src Expression.read_statement;
   419 
   413