src/Pure/Isar/specification.ML
changeset 42360 da8817d01e7c
parent 42357 3305f573294e
child 42375 774df7c59508
equal deleted inserted replaced
42359:6ca5407863ed 42360:da8817d01e7c
   115       in fold_rev close bounds A end;
   115       in fold_rev close bounds A end;
   116   in map close_form As end;
   116   in map close_form As end;
   117 
   117 
   118 fun prepare prep_vars parse_prop prep_att do_close raw_vars raw_specss ctxt =
   118 fun prepare prep_vars parse_prop prep_att do_close raw_vars raw_specss ctxt =
   119   let
   119   let
   120     val thy = ProofContext.theory_of ctxt;
   120     val thy = Proof_Context.theory_of ctxt;
   121 
   121 
   122     val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
   122     val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
   123     val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes vars;
   123     val (xs, params_ctxt) = vars_ctxt |> Proof_Context.add_fixes vars;
   124 
   124 
   125     val Asss =
   125     val Asss =
   126       (map o map) snd raw_specss
   126       (map o map) snd raw_specss
   127       |> (burrow o burrow) (Par_List.map_name "Specification.parse_prop" (parse_prop params_ctxt));
   127       |> (burrow o burrow) (Par_List.map_name "Specification.parse_prop" (parse_prop params_ctxt));
   128     val names = Variable.names_of (params_ctxt |> (fold o fold o fold) Variable.declare_term Asss)
   128     val names = Variable.names_of (params_ctxt |> (fold o fold o fold) Variable.declare_term Asss)
   135             (fn Ass => fn i => (burrow (close_forms params_ctxt i []) Ass, i + 1)) Asss' idx)
   135             (fn Ass => fn i => (burrow (close_forms params_ctxt i []) Ass, i + 1)) Asss' idx)
   136       else Asss')
   136       else Asss')
   137       |> flat |> burrow (Syntax.check_props params_ctxt);
   137       |> flat |> burrow (Syntax.check_props params_ctxt);
   138     val specs_ctxt = params_ctxt |> (fold o fold) Variable.declare_term specs;
   138     val specs_ctxt = params_ctxt |> (fold o fold) Variable.declare_term specs;
   139 
   139 
   140     val Ts = specs_ctxt |> fold_map ProofContext.inferred_param xs |> fst;
   140     val Ts = specs_ctxt |> fold_map Proof_Context.inferred_param xs |> fst;
   141     val params = map2 (fn (b, _, mx) => fn T => ((b, T), mx)) vars Ts;
   141     val params = map2 (fn (b, _, mx) => fn T => ((b, T), mx)) vars Ts;
   142     val name_atts = map (fn ((name, atts), _) => (name, map (prep_att thy) atts)) (flat raw_specss);
   142     val name_atts = map (fn ((name, atts), _) => (name, map (prep_att thy) atts)) (flat raw_specss);
   143   in ((params, name_atts ~~ specs), specs_ctxt) end;
   143   in ((params, name_atts ~~ specs), specs_ctxt) end;
   144 
   144 
   145 
   145 
   150   prepare prep_vars parse_prop prep_att do_close
   150   prepare prep_vars parse_prop prep_att do_close
   151     vars (map single_spec specs) #>> apsnd (map the_spec);
   151     vars (map single_spec specs) #>> apsnd (map the_spec);
   152 
   152 
   153 in
   153 in
   154 
   154 
   155 fun check_spec x = prep_spec ProofContext.cert_vars (K I) (K I) true x;
   155 fun check_spec x = prep_spec Proof_Context.cert_vars (K I) (K I) true x;
   156 fun read_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src true x;
   156 fun read_spec x = prep_spec Proof_Context.read_vars Syntax.parse_prop Attrib.intern_src true x;
   157 
   157 
   158 fun check_free_spec x = prep_spec ProofContext.cert_vars (K I) (K I) false x;
   158 fun check_free_spec x = prep_spec Proof_Context.cert_vars (K I) (K I) false x;
   159 fun read_free_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src false x;
   159 fun read_free_spec x = prep_spec Proof_Context.read_vars Syntax.parse_prop Attrib.intern_src false x;
   160 
   160 
   161 fun check_specification vars specs =
   161 fun check_specification vars specs =
   162   prepare ProofContext.cert_vars (K I) (K I) true vars [specs];
   162   prepare Proof_Context.cert_vars (K I) (K I) true vars [specs];
   163 
   163 
   164 fun read_specification vars specs =
   164 fun read_specification vars specs =
   165   prepare ProofContext.read_vars Syntax.parse_prop Attrib.intern_src true vars [specs];
   165   prepare Proof_Context.read_vars Syntax.parse_prop Attrib.intern_src true vars [specs];
   166 
   166 
   167 end;
   167 end;
   168 
   168 
   169 
   169 
   170 (* axiomatization -- within global theory *)
   170 (* axiomatization -- within global theory *)
   171 
   171 
   172 fun gen_axioms do_print prep raw_vars raw_specs thy =
   172 fun gen_axioms do_print prep raw_vars raw_specs thy =
   173   let
   173   let
   174     val ((vars, specs), _) = prep raw_vars raw_specs (ProofContext.init_global thy);
   174     val ((vars, specs), _) = prep raw_vars raw_specs (Proof_Context.init_global thy);
   175     val xs = map (fn ((b, T), _) => (Variable.name b, T)) vars;
   175     val xs = map (fn ((b, T), _) => (Variable.name b, T)) vars;
   176 
   176 
   177     (*consts*)
   177     (*consts*)
   178     val (consts, consts_thy) = thy |> fold_map Theory.specify_const vars;
   178     val (consts, consts_thy) = thy |> fold_map Theory.specify_const vars;
   179     val subst = Term.subst_atomic (map Free xs ~~ consts);
   179     val subst = Term.subst_atomic (map Free xs ~~ consts);
   244 
   244 
   245 fun gen_abbrev do_print prep mode (raw_var, raw_prop) lthy =
   245 fun gen_abbrev do_print prep mode (raw_var, raw_prop) lthy =
   246   let
   246   let
   247     val ((vars, [(_, prop)]), _) =
   247     val ((vars, [(_, prop)]), _) =
   248       prep (the_list raw_var) [(Attrib.empty_binding, raw_prop)]
   248       prep (the_list raw_var) [(Attrib.empty_binding, raw_prop)]
   249         (lthy |> ProofContext.set_mode ProofContext.mode_abbrev);
   249         (lthy |> Proof_Context.set_mode Proof_Context.mode_abbrev);
   250     val ((x, T), rhs) = Local_Defs.abs_def (#2 (Local_Defs.cert_def lthy prop));
   250     val ((x, T), rhs) = Local_Defs.abs_def (#2 (Local_Defs.cert_def lthy prop));
   251     val var =
   251     val var =
   252       (case vars of
   252       (case vars of
   253         [] => (Binding.name x, NoSyn)
   253         [] => (Binding.name x, NoSyn)
   254       | [((b, _), mx)] =>
   254       | [((b, _), mx)] =>
   257             val _ = x = y orelse
   257             val _ = x = y orelse
   258               error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y ^
   258               error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y ^
   259                 Position.str_of (Binding.pos_of b));
   259                 Position.str_of (Binding.pos_of b));
   260           in (b, mx) end);
   260           in (b, mx) end);
   261     val lthy' = lthy
   261     val lthy' = lthy
   262       |> ProofContext.set_syntax_mode mode    (* FIXME ?!? *)
   262       |> Proof_Context.set_syntax_mode mode    (* FIXME ?!? *)
   263       |> Local_Theory.abbrev mode (var, rhs) |> snd
   263       |> Local_Theory.abbrev mode (var, rhs) |> snd
   264       |> ProofContext.restore_syntax_mode lthy;
   264       |> Proof_Context.restore_syntax_mode lthy;
   265 
   265 
   266     val _ = if not do_print then () else print_consts lthy' (K false) [(x, T)];
   266     val _ = if not do_print then () else print_consts lthy' (K false) [(x, T)];
   267   in lthy' end;
   267   in lthy' end;
   268 
   268 
   269 val abbreviation = gen_abbrev false check_free_spec;
   269 val abbreviation = gen_abbrev false check_free_spec;
   281   lthy |> Local_Theory.notation add mode (map (apfst (prep_const lthy)) args);
   281   lthy |> Local_Theory.notation add mode (map (apfst (prep_const lthy)) args);
   282 
   282 
   283 in
   283 in
   284 
   284 
   285 val type_notation = gen_type_notation (K I);
   285 val type_notation = gen_type_notation (K I);
   286 val type_notation_cmd = gen_type_notation (fn ctxt => ProofContext.read_type_name ctxt false);
   286 val type_notation_cmd = gen_type_notation (fn ctxt => Proof_Context.read_type_name ctxt false);
   287 
   287 
   288 val notation = gen_notation (K I);
   288 val notation = gen_notation (K I);
   289 val notation_cmd = gen_notation (fn ctxt => ProofContext.read_const ctxt false dummyT);
   289 val notation_cmd = gen_notation (fn ctxt => Proof_Context.read_const ctxt false dummyT);
   290 
   290 
   291 end;
   291 end;
   292 
   292 
   293 
   293 
   294 (* fact statements *)
   294 (* fact statements *)
   295 
   295 
   296 fun gen_theorems prep_fact prep_att kind raw_facts lthy =
   296 fun gen_theorems prep_fact prep_att kind raw_facts lthy =
   297   let
   297   let
   298     val attrib = prep_att (ProofContext.theory_of lthy);
   298     val attrib = prep_att (Proof_Context.theory_of lthy);
   299     val facts = raw_facts |> map (fn ((name, atts), bs) =>
   299     val facts = raw_facts |> map (fn ((name, atts), bs) =>
   300       ((name, map attrib atts),
   300       ((name, map attrib atts),
   301         bs |> map (fn (b, more_atts) => (prep_fact lthy b, map attrib more_atts))));
   301         bs |> map (fn (b, more_atts) => (prep_fact lthy b, map attrib more_atts))));
   302     val (res, lthy') = lthy |> Local_Theory.notes_kind kind facts;
   302     val (res, lthy') = lthy |> Local_Theory.notes_kind kind facts;
   303     val _ = Proof_Display.print_results true lthy' ((kind, ""), res);
   303     val _ = Proof_Display.print_results true lthy' ((kind, ""), res);
   304   in (res, lthy') end;
   304   in (res, lthy') end;
   305 
   305 
   306 val theorems = gen_theorems (K I) (K I);
   306 val theorems = gen_theorems (K I) (K I);
   307 val theorems_cmd = gen_theorems ProofContext.get_fact Attrib.intern_src;
   307 val theorems_cmd = gen_theorems Proof_Context.get_fact Attrib.intern_src;
   308 
   308 
   309 
   309 
   310 (* complex goal statements *)
   310 (* complex goal statements *)
   311 
   311 
   312 local
   312 local
   329             (vars |> map_filter (fn (x, SOME T) => SOME (Variable.name x, T) | _ => NONE)));
   329             (vars |> map_filter (fn (x, SOME T) => SOME (Variable.name x, T) | _ => NONE)));
   330 
   330 
   331         val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
   331         val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
   332         val (propp, elems_ctxt) = prep_stmt (elems @ constraints) raw_propp ctxt;
   332         val (propp, elems_ctxt) = prep_stmt (elems @ constraints) raw_propp ctxt;
   333 
   333 
   334         val thesis = Object_Logic.fixed_judgment (ProofContext.theory_of ctxt) Auto_Bind.thesisN;
   334         val thesis = Object_Logic.fixed_judgment (Proof_Context.theory_of ctxt) Auto_Bind.thesisN;
   335 
   335 
   336         fun assume_case ((name, (vars, _)), asms) ctxt' =
   336         fun assume_case ((name, (vars, _)), asms) ctxt' =
   337           let
   337           let
   338             val bs = map fst vars;
   338             val bs = map fst vars;
   339             val xs = map Variable.name bs;
   339             val xs = map Variable.name bs;
   340             val props = map fst asms;
   340             val props = map fst asms;
   341             val (Ts, _) = ctxt'
   341             val (Ts, _) = ctxt'
   342               |> fold Variable.declare_term props
   342               |> fold Variable.declare_term props
   343               |> fold_map ProofContext.inferred_param xs;
   343               |> fold_map Proof_Context.inferred_param xs;
   344             val asm = Term.list_all_free (xs ~~ Ts, Logic.list_implies (props, thesis));
   344             val asm = Term.list_all_free (xs ~~ Ts, Logic.list_implies (props, thesis));
   345           in
   345           in
   346             ctxt' |> (snd o ProofContext.add_fixes (map (fn b => (b, NONE, NoSyn)) bs));
   346             ctxt' |> (snd o Proof_Context.add_fixes (map (fn b => (b, NONE, NoSyn)) bs));
   347             ctxt' |> Variable.auto_fixes asm
   347             ctxt' |> Variable.auto_fixes asm
   348             |> ProofContext.add_assms_i Assumption.assume_export
   348             |> Proof_Context.add_assms_i Assumption.assume_export
   349               [((name, [Context_Rules.intro_query NONE]), [(asm, [])])]
   349               [((name, [Context_Rules.intro_query NONE]), [(asm, [])])]
   350             |>> (fn [(_, [th])] => th)
   350             |>> (fn [(_, [th])] => th)
   351           end;
   351           end;
   352 
   352 
   353         val atts = map (Attrib.internal o K)
   353         val atts = map (Attrib.internal o K)
   354           [Rule_Cases.consumes (~ (length obtains)), Rule_Cases.case_names case_names];
   354           [Rule_Cases.consumes (~ (length obtains)), Rule_Cases.case_names case_names];
   355         val prems = Assumption.local_prems_of elems_ctxt ctxt;
   355         val prems = Assumption.local_prems_of elems_ctxt ctxt;
   356         val stmt = [((Binding.empty, atts), [(thesis, [])])];
   356         val stmt = [((Binding.empty, atts), [(thesis, [])])];
   357 
   357 
   358         val (facts, goal_ctxt) = elems_ctxt
   358         val (facts, goal_ctxt) = elems_ctxt
   359           |> (snd o ProofContext.add_fixes [(Binding.name Auto_Bind.thesisN, NONE, NoSyn)])
   359           |> (snd o Proof_Context.add_fixes [(Binding.name Auto_Bind.thesisN, NONE, NoSyn)])
   360           |> fold_map assume_case (obtains ~~ propp)
   360           |> fold_map assume_case (obtains ~~ propp)
   361           |-> (fn ths =>
   361           |-> (fn ths =>
   362             ProofContext.note_thmss "" [((Binding.name Obtain.thatN, []), [(ths, [])])] #>
   362             Proof_Context.note_thmss "" [((Binding.name Obtain.thatN, []), [(ths, [])])] #>
   363             #2 #> pair ths);
   363             #2 #> pair ths);
   364       in ((prems, stmt, SOME facts), goal_ctxt) end);
   364       in ((prems, stmt, SOME facts), goal_ctxt) end);
   365 
   365 
   366 structure Theorem_Hook = Generic_Data
   366 structure Theorem_Hook = Generic_Data
   367 (
   367 (
   373 
   373 
   374 fun gen_theorem schematic prep_att prep_stmt
   374 fun gen_theorem schematic prep_att prep_stmt
   375     kind before_qed after_qed (name, raw_atts) raw_elems raw_concl int lthy =
   375     kind before_qed after_qed (name, raw_atts) raw_elems raw_concl int lthy =
   376   let
   376   let
   377     val _ = Local_Theory.affirm lthy;
   377     val _ = Local_Theory.affirm lthy;
   378     val thy = ProofContext.theory_of lthy;
   378     val thy = Proof_Context.theory_of lthy;
   379 
   379 
   380     val attrib = prep_att thy;
   380     val attrib = prep_att thy;
   381     val atts = map attrib raw_atts;
   381     val atts = map attrib raw_atts;
   382     val elems = raw_elems |> map (Element.map_ctxt_attrib attrib);
   382     val elems = raw_elems |> map (Element.map_ctxt_attrib attrib);
   383     val ((prems, stmt, facts), goal_ctxt) =
   383     val ((prems, stmt, facts), goal_ctxt) =
   384       prep_statement attrib prep_stmt elems raw_concl lthy;
   384       prep_statement attrib prep_stmt elems raw_concl lthy;
   385 
   385 
   386     fun after_qed' results goal_ctxt' =
   386     fun after_qed' results goal_ctxt' =
   387       let val results' =
   387       let val results' =
   388         burrow (map Goal.norm_result o ProofContext.export goal_ctxt' lthy) results
   388         burrow (map Goal.norm_result o Proof_Context.export goal_ctxt' lthy) results
   389       in
   389       in
   390         lthy
   390         lthy
   391         |> Local_Theory.notes_kind kind
   391         |> Local_Theory.notes_kind kind
   392           (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
   392           (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
   393         |> (fn (res, lthy') =>
   393         |> (fn (res, lthy') =>
   401             in lthy'' end)
   401             in lthy'' end)
   402         |> after_qed results'
   402         |> after_qed results'
   403       end;
   403       end;
   404   in
   404   in
   405     goal_ctxt
   405     goal_ctxt
   406     |> ProofContext.note_thmss "" [((Binding.name Auto_Bind.assmsN, []), [(prems, [])])]
   406     |> Proof_Context.note_thmss "" [((Binding.name Auto_Bind.assmsN, []), [(prems, [])])]
   407     |> snd
   407     |> snd
   408     |> Proof.theorem before_qed after_qed' (map snd stmt)
   408     |> Proof.theorem before_qed after_qed' (map snd stmt)
   409     |> (case facts of NONE => I | SOME ths => Proof.refine_insert ths)
   409     |> (case facts of NONE => I | SOME ths => Proof.refine_insert ths)
   410     |> tap (fn state => not schematic andalso Proof.schematic_goal state andalso
   410     |> tap (fn state => not schematic andalso Proof.schematic_goal state andalso
   411         error "Illegal schematic goal statement")
   411         error "Illegal schematic goal statement")