author | berghofe |
Fri, 04 Mar 2011 17:39:30 +0100 | |
changeset 41896 | 582cccdda0ed |
parent 41887 | ececcbd08d35 |
child 41948 | 30732d2390c8 |
permissions | -rw-r--r-- |
41561 | 1 |
(* Title: HOL/SPARK/Tools/spark_commands.ML |
2 |
Author: Stefan Berghofer |
|
3 |
Copyright: secunet Security Networks AG |
|
4 |
||
5 |
Isar commands for handling SPARK/Ada verification conditions. |
|
6 |
*) |
|
7 |
||
8 |
signature SPARK_COMMANDS = |
|
9 |
sig |
|
10 |
val setup: theory -> theory |
|
11 |
end |
|
12 |
||
13 |
structure SPARK_Commands: SPARK_COMMANDS = |
|
14 |
struct |
|
15 |
||
16 |
fun read f path = f (Position.file (Path.implode path)) (File.read path); |
|
17 |
||
18 |
fun spark_open vc_name thy = |
|
19 |
let |
|
20 |
val (vc_path, _) = Thy_Load.check_file |
|
41887
ececcbd08d35
simplified Thy_Info.check_file -- discontinued load path;
wenzelm
parents:
41592
diff
changeset
|
21 |
(Thy_Load.master_directory thy) (Path.explode vc_name); |
ececcbd08d35
simplified Thy_Info.check_file -- discontinued load path;
wenzelm
parents:
41592
diff
changeset
|
22 |
val (base, header) = |
ececcbd08d35
simplified Thy_Info.check_file -- discontinued load path;
wenzelm
parents:
41592
diff
changeset
|
23 |
(case Path.split_ext vc_path of |
41561 | 24 |
(base, "vcg") => (base, Fdl_Lexer.vcg_header >> K ()) |
25 |
| (base, "siv") => (base, Fdl_Lexer.siv_header >> K ()) |
|
26 |
| _ => error "File name must end with .vcg or .siv"); |
|
27 |
val fdl_path = Path.ext "fdl" base; |
|
28 |
val rls_path = Path.ext "rls" base; |
|
29 |
in |
|
30 |
SPARK_VCs.set_vcs |
|
31 |
(snd (read Fdl_Parser.parse_declarations fdl_path)) |
|
32 |
(read Fdl_Parser.parse_rules rls_path) |
|
33 |
(snd (snd (read (Fdl_Parser.parse_vcs header) vc_path))) |
|
34 |
base thy |
|
35 |
end; |
|
36 |
||
37 |
fun add_proof_fun_cmd pf thy = |
|
38 |
let val ctxt = ProofContext.init_global thy |
|
39 |
in SPARK_VCs.add_proof_fun |
|
40 |
(fn optT => Syntax.parse_term ctxt #> |
|
41 |
the_default I (Option.map Type.constraint optT) #> |
|
42 |
Syntax.check_term ctxt) pf thy |
|
43 |
end; |
|
44 |
||
45 |
fun get_vc thy vc_name = |
|
46 |
(case SPARK_VCs.lookup_vc thy vc_name of |
|
47 |
SOME (ctxt, (_, proved, ctxt', stmt)) => |
|
41896
582cccdda0ed
spark_end now joins proofs of VCs before writing *.prv file.
berghofe
parents:
41887
diff
changeset
|
48 |
if is_some proved then |
41561 | 49 |
error ("The verification condition " ^ |
50 |
quote vc_name ^ " has already been proved.") |
|
51 |
else (ctxt @ [ctxt'], stmt) |
|
52 |
| NONE => error ("There is no verification condition " ^ |
|
53 |
quote vc_name ^ ".")); |
|
54 |
||
55 |
fun prove_vc vc_name lthy = |
|
56 |
let |
|
57 |
val thy = ProofContext.theory_of lthy; |
|
58 |
val (ctxt, stmt) = get_vc thy vc_name |
|
59 |
in |
|
60 |
Specification.theorem Thm.theoremK NONE |
|
41896
582cccdda0ed
spark_end now joins proofs of VCs before writing *.prv file.
berghofe
parents:
41887
diff
changeset
|
61 |
(fn thmss => (Local_Theory.background_theory |
582cccdda0ed
spark_end now joins proofs of VCs before writing *.prv file.
berghofe
parents:
41887
diff
changeset
|
62 |
(SPARK_VCs.mark_proved vc_name (flat thmss)))) |
41561 | 63 |
(Binding.name vc_name, []) ctxt stmt true lthy |
64 |
end; |
|
65 |
||
41896
582cccdda0ed
spark_end now joins proofs of VCs before writing *.prv file.
berghofe
parents:
41887
diff
changeset
|
66 |
fun string_of_status NONE = "(unproved)" |
582cccdda0ed
spark_end now joins proofs of VCs before writing *.prv file.
berghofe
parents:
41887
diff
changeset
|
67 |
| string_of_status (SOME _) = "(proved)"; |
41561 | 68 |
|
69 |
fun show_status (p, f) = Toplevel.no_timing o Toplevel.keep (fn state => |
|
70 |
let |
|
71 |
val thy = Toplevel.theory_of state; |
|
72 |
||
73 |
val (context, defs, vcs) = SPARK_VCs.get_vcs thy; |
|
74 |
||
75 |
val vcs' = AList.coalesce (op =) (map_filter |
|
76 |
(fn (name, (trace, status, ctxt, stmt)) => |
|
77 |
if p status then |
|
78 |
SOME (trace, (name, status, ctxt, stmt)) |
|
79 |
else NONE) vcs); |
|
80 |
||
81 |
val ctxt = state |> |
|
82 |
Toplevel.theory_of |> |
|
83 |
ProofContext.init_global |> |
|
84 |
Context.proof_map (fold Element.init context) |
|
85 |
in |
|
41592 | 86 |
[Pretty.str "Context:", |
87 |
Pretty.chunks (maps (Element.pretty_ctxt ctxt) context), |
|
41561 | 88 |
|
41592 | 89 |
Pretty.str "Definitions:", |
41561 | 90 |
Pretty.chunks (map (fn (bdg, th) => Pretty.block |
91 |
[Pretty.str (Binding.str_of bdg ^ ":"), |
|
92 |
Pretty.brk 1, |
|
93 |
Display.pretty_thm ctxt th]) |
|
41592 | 94 |
defs), |
41561 | 95 |
|
41592 | 96 |
Pretty.str "Verification conditions:", |
97 |
Pretty.chunks2 (maps (fn (trace, vcs'') => |
|
41561 | 98 |
Pretty.str trace :: |
99 |
map (fn (name, status, context', stmt) => |
|
100 |
Pretty.big_list (name ^ " " ^ f status) |
|
101 |
(Element.pretty_ctxt ctxt context' @ |
|
41592 | 102 |
Element.pretty_stmt ctxt stmt)) vcs'') vcs')] |> |
103 |
Pretty.chunks2 |> Pretty.writeln |
|
41561 | 104 |
end); |
105 |
||
106 |
val _ = |
|
107 |
Outer_Syntax.command "spark_open" |
|
41586 | 108 |
"open a new SPARK environment and load a SPARK-generated .vcg or .siv file" |
41561 | 109 |
Keyword.thy_decl |
110 |
(Parse.name >> (Toplevel.theory o spark_open)); |
|
111 |
||
112 |
val pfun_type = Scan.option |
|
113 |
(Args.parens (Parse.list1 Parse.name) --| Args.colon -- Parse.name); |
|
114 |
||
115 |
val _ = |
|
116 |
Outer_Syntax.command "spark_proof_functions" |
|
41586 | 117 |
"associate SPARK proof functions with terms" |
41561 | 118 |
Keyword.thy_decl |
119 |
(Scan.repeat1 (Parse.name -- (pfun_type --| Args.$$$ "=" -- Parse.term)) >> |
|
120 |
(Toplevel.theory o fold add_proof_fun_cmd)); |
|
121 |
||
122 |
val _ = |
|
123 |
Outer_Syntax.command "spark_vc" |
|
41586 | 124 |
"enter into proof mode for a specific verification condition" |
41561 | 125 |
Keyword.thy_goal |
126 |
(Parse.name >> (fn name => |
|
127 |
(Toplevel.print o Toplevel.local_theory_to_proof NONE (prove_vc name)))); |
|
128 |
||
129 |
val _ = |
|
130 |
Outer_Syntax.improper_command "spark_status" |
|
41586 | 131 |
"show the name and state of all loaded verification conditions" |
41561 | 132 |
Keyword.diag |
133 |
(Scan.optional |
|
134 |
(Args.parens |
|
41896
582cccdda0ed
spark_end now joins proofs of VCs before writing *.prv file.
berghofe
parents:
41887
diff
changeset
|
135 |
( Args.$$$ "proved" >> K (is_some, K "") |
582cccdda0ed
spark_end now joins proofs of VCs before writing *.prv file.
berghofe
parents:
41887
diff
changeset
|
136 |
|| Args.$$$ "unproved" >> K (is_none, K ""))) |
41561 | 137 |
(K true, string_of_status) >> show_status); |
138 |
||
139 |
val _ = |
|
140 |
Outer_Syntax.command "spark_end" |
|
41586 | 141 |
"close the current SPARK environment" |
41561 | 142 |
Keyword.thy_decl |
143 |
(Scan.succeed (Toplevel.theory SPARK_VCs.close)); |
|
144 |
||
145 |
val setup = Theory.at_end (fn thy => |
|
146 |
let |
|
147 |
val _ = SPARK_VCs.is_closed thy |
|
148 |
orelse error ("Found the end of the theory, " ^ |
|
149 |
"but the last SPARK environment is still open.") |
|
150 |
in NONE end); |
|
151 |
||
152 |
end; |