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