Skip to content

Commit 949ab0c

Browse files
authored
Merge branch 'master' into T1884
2 parents 1df6b0a + 33b0d87 commit 949ab0c

File tree

6 files changed

+110
-96
lines changed

6 files changed

+110
-96
lines changed

saw-remote-api/src/SAWServer.hs

+2-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import qualified SAWScript.Crucible.LLVM.MethodSpecIR as CMS (SomeLLVM, LLVMModu
5454
import SAWScript.Options (Options(..), processEnv, defaultOptions)
5555
import SAWScript.Position (Pos(..))
5656
import SAWScript.Prover.Rewrite (basic_ss)
57-
import SAWScript.Proof (newTheoremDB)
57+
import SAWScript.Proof (emptyTheoremDB)
5858
import SAWScript.Value (AIGProxy(..), BuiltinContext(..), JVMSetupM, LLVMCrucibleSetupM, TopLevelRO(..), TopLevelRW(..), defaultPPOpts, SAWSimpset)
5959
import SAWScript.Yosys.State (YosysSequential)
6060
import SAWScript.Yosys.Theorem (YosysImport, YosysTheorem)
@@ -207,7 +207,6 @@ initialState readFileFn =
207207
halloc <- Crucible.newHandleAllocator
208208
jvmTrans <- CJ.mkInitialJVMContext halloc
209209
cwd <- getCurrentDirectory
210-
db <- newTheoremDB
211210
let ro = TopLevelRO
212211
{ roJavaCodebase = jcb
213212
, roOptions = opts
@@ -234,7 +233,7 @@ initialState readFileFn =
234233
, rwMonadify = defaultMonEnv
235234
, rwMRSolverEnv = emptyMREnv
236235
, rwPPOpts = defaultPPOpts
237-
, rwTheoremDB = db
236+
, rwTheoremDB = emptyTheoremDB
238237
, rwSharedContext = sc
239238
, rwJVMTrans = jvmTrans
240239
, rwPrimsAvail = mempty

src/SAWScript/Builtins.hs

+9-6
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,8 @@ core_axiom input =
20372037
t <- parseCore input
20382038
p <- io (termToProp sc t)
20392039
db <- SV.getTheoremDB
2040-
thm <- io (admitTheorem db "core_axiom" p pos "core_axiom")
2040+
(thm, db') <- io (admitTheorem db "core_axiom" p pos "core_axiom")
2041+
SV.putTheoremDB db'
20412042
SV.returnProof thm
20422043

20432044
core_thm :: String -> TopLevel Theorem
@@ -2046,15 +2047,17 @@ core_thm input =
20462047
sc <- getSharedContext
20472048
pos <- SV.getPosition
20482049
db <- SV.getTheoremDB
2049-
thm <- io (proofByTerm sc db t pos "core_thm")
2050+
(thm, db') <- io (proofByTerm sc db t pos "core_thm")
2051+
SV.putTheoremDB db'
20502052
SV.returnProof thm
20512053

20522054
specialize_theorem :: Theorem -> [TypedTerm] -> TopLevel Theorem
20532055
specialize_theorem thm ts =
20542056
do sc <- getSharedContext
20552057
db <- SV.getTheoremDB
20562058
pos <- SV.getPosition
2057-
thm' <- io (specializeTheorem sc db pos "specialize_theorem" thm (map ttTerm ts))
2059+
(thm', db') <- io (specializeTheorem sc db pos "specialize_theorem" thm (map ttTerm ts))
2060+
SV.putTheoremDB db'
20582061
SV.returnProof thm'
20592062

20602063
get_opt :: Int -> TopLevel String
@@ -2427,7 +2430,7 @@ summarize_verification =
24272430
lspecs = [ s | SV.VLLVMCrucibleMethodSpec s <- values ]
24282431
thms = [ t | SV.VTheorem t <- values ]
24292432
db <- SV.getTheoremDB
2430-
summary <- io (computeVerificationSummary db jspecs lspecs thms)
2433+
let summary = computeVerificationSummary db jspecs lspecs thms
24312434
opts <- fmap (SV.sawPPOpts . rwPPOpts) getTopLevelRW
24322435
nenv <- io . scGetNamingEnv =<< getSharedContext
24332436
io $ putStrLn $ prettyVerificationSummary opts nenv summary
@@ -2439,7 +2442,7 @@ summarize_verification_json fpath =
24392442
lspecs = [ s | SV.VLLVMCrucibleMethodSpec s <- values ]
24402443
thms = [ t | SV.VTheorem t <- values ]
24412444
db <- SV.getTheoremDB
2442-
summary <- io (computeVerificationSummary db jspecs lspecs thms)
2445+
let summary = computeVerificationSummary db jspecs lspecs thms
24432446
io (writeFile fpath (jsonVerificationSummary summary))
24442447

24452448
writeVerificationSummary :: TopLevel ()
@@ -2450,7 +2453,7 @@ writeVerificationSummary = do
24502453
let jspecs = [ s | SV.VJVMMethodSpec s <- values ]
24512454
lspecs = [ s | SV.VLLVMCrucibleMethodSpec s <- values ]
24522455
thms = [ t | SV.VTheorem t <- values ]
2453-
summary <- io (computeVerificationSummary db jspecs lspecs thms)
2456+
summary = computeVerificationSummary db jspecs lspecs thms
24542457
opts <- roOptions <$> getTopLevelRO
24552458
dir <- roInitWorkDir <$> getTopLevelRO
24562459
nenv <- io . scGetNamingEnv =<< getSharedContext

src/SAWScript/Interpreter.hs

+2-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import SAWScript.Parser (parseSchema)
6767
import SAWScript.TopLevel
6868
import SAWScript.Utils
6969
import SAWScript.Value
70-
import SAWScript.Proof (newTheoremDB)
70+
import SAWScript.Proof (emptyTheoremDB)
7171
import SAWScript.Prover.Rewrite(basic_ss)
7272
import SAWScript.Prover.Exporter
7373
import SAWScript.Prover.MRSolver (emptyMREnv)
@@ -453,7 +453,6 @@ buildTopLevelEnv proxy opts =
453453
ss <- basic_ss sc
454454
jcb <- JCB.loadCodebase (jarList opts) (classPath opts) (javaBinDirs opts)
455455
currDir <- getCurrentDirectory
456-
thmDB <- newTheoremDB
457456
Crucible.withHandleAllocator $ \halloc -> do
458457
let ro0 = TopLevelRO
459458
{ roJavaCodebase = jcb
@@ -488,7 +487,7 @@ buildTopLevelEnv proxy opts =
488487
, rwProofs = []
489488
, rwPPOpts = SAWScript.Value.defaultPPOpts
490489
, rwSharedContext = sc
491-
, rwTheoremDB = thmDB
490+
, rwTheoremDB = emptyTheoremDB
492491
, rwJVMTrans = jvmTrans
493492
, rwPrimsAvail = primsAvail
494493
, rwSMTArrayMemoryModel = False

src/SAWScript/Proof.hs

+80-76
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ module SAWScript.Proof
6363
, cofinSetMember
6464

6565
, TheoremDB
66-
, newTheoremDB
66+
, emptyTheoremDB
6767
, reachableTheorems
6868

6969
, Theorem
@@ -131,7 +131,6 @@ module SAWScript.Proof
131131

132132
import qualified Control.Monad.Fail as F
133133
import Control.Monad.Except
134-
import Data.IORef
135134
import qualified Data.Foldable as Fold
136135
import Data.List
137136
import Data.Maybe (fromMaybe)
@@ -902,25 +901,21 @@ data Theorem =
902901
data TheoremDB =
903902
TheoremDB
904903
-- TODO, maybe this should be a summary or something simpler?
905-
{ theoremMap :: IORef (Map.Map TheoremNonce Theorem)
904+
{ theoremMap :: Map.Map TheoremNonce Theorem
906905
}
907906

908-
newTheoremDB :: IO TheoremDB
909-
newTheoremDB = TheoremDB <$> newIORef mempty
907+
emptyTheoremDB :: TheoremDB
908+
emptyTheoremDB = TheoremDB mempty
910909

911-
recordTheorem :: TheoremDB -> Theorem -> IO Theorem
912-
recordTheorem db thm@Theorem{ _thmNonce = n } =
913-
do modifyIORef (theoremMap db) (Map.insert n thm)
914-
return thm
910+
recordTheorem :: TheoremDB -> Theorem -> TheoremDB
911+
recordTheorem db thm@Theorem{ _thmNonce = n } = TheoremDB (Map.insert n thm (theoremMap db))
915912

916913
-- | Given a set of root values, find all the theorems in this database
917914
-- that are transitively used in the proofs of those theorems.
918915
-- This function will panic if any of the roots or reachable theorems
919916
-- are not found in the database.
920-
reachableTheorems :: TheoremDB -> Set TheoremNonce -> IO (Map TheoremNonce Theorem)
921-
reachableTheorems db roots =
922-
do m <- readIORef (theoremMap db)
923-
pure $! Set.foldl' (loop m) mempty roots
917+
reachableTheorems :: TheoremDB -> Set TheoremNonce -> Map TheoremNonce Theorem
918+
reachableTheorems db roots = Set.foldl' (loop (theoremMap db)) mempty roots
924919

925920
where
926921
loop m visited curr
@@ -945,7 +940,7 @@ reachableTheorems db roots =
945940
validateTheorem :: SharedContext -> TheoremDB -> Theorem -> IO ()
946941

947942
validateTheorem sc db Theorem{ _thmProp = p, _thmEvidence = e, _thmDepends = thmDep } =
948-
do hyps <- Map.keysSet <$> readIORef (theoremMap db)
943+
do let hyps = Map.keysSet (theoremMap db)
949944
(deps,_) <- checkEvidence sc e p
950945
unless (Set.isSubsetOf deps thmDep && Set.isSubsetOf thmDep hyps)
951946
(fail $ unlines ["Theorem failed to declare its dependencies correctly"
@@ -1148,24 +1143,26 @@ structuralEvidence _sqt (StructuralEvidence sqt' e) = StructuralEvidence sqt' e
11481143
structuralEvidence sqt e = StructuralEvidence sqt e
11491144

11501145
-- | Construct a theorem directly via a proof term.
1151-
proofByTerm :: SharedContext -> TheoremDB -> Term -> Pos -> Text -> IO Theorem
1146+
proofByTerm :: SharedContext -> TheoremDB -> Term -> Pos -> Text -> IO (Theorem, TheoremDB)
11521147
proofByTerm sc db prf loc rsn =
11531148
do ty <- scTypeOf sc prf
11541149
p <- termToProp sc ty
11551150
n <- freshNonce globalNonceGenerator
1156-
recordTheorem db
1157-
Theorem
1158-
{ _thmProp = p
1159-
, _thmStats = mempty
1160-
, _thmEvidence = ProofTerm prf
1161-
, _thmLocation = loc
1162-
, _thmProgramLoc = Nothing
1163-
, _thmReason = rsn
1164-
, _thmNonce = n
1165-
, _thmDepends = mempty
1166-
, _thmElapsedTime = 0
1167-
, _thmSummary = ProvedTheorem mempty
1168-
}
1151+
let thm =
1152+
Theorem
1153+
{ _thmProp = p
1154+
, _thmStats = mempty
1155+
, _thmEvidence = ProofTerm prf
1156+
, _thmLocation = loc
1157+
, _thmProgramLoc = Nothing
1158+
, _thmReason = rsn
1159+
, _thmNonce = n
1160+
, _thmDepends = mempty
1161+
, _thmElapsedTime = 0
1162+
, _thmSummary = ProvedTheorem mempty
1163+
}
1164+
let db' = recordTheorem db thm
1165+
pure (thm, db')
11691166

11701167
-- | Construct a theorem directly from a proposition and evidence
11711168
-- for that proposition. The evidence will be validated to
@@ -1180,31 +1177,33 @@ constructTheorem ::
11801177
Maybe ProgramLoc ->
11811178
Text ->
11821179
NominalDiffTime ->
1183-
IO Theorem
1180+
IO (Theorem, TheoremDB)
11841181
constructTheorem sc db p e loc ploc rsn elapsed =
11851182
do (deps,sy) <- checkEvidence sc e p
11861183
n <- freshNonce globalNonceGenerator
1187-
recordTheorem db
1188-
Theorem
1189-
{ _thmProp = p
1190-
, _thmStats = mempty
1191-
, _thmEvidence = e
1192-
, _thmLocation = loc
1193-
, _thmProgramLoc = ploc
1194-
, _thmReason = rsn
1195-
, _thmNonce = n
1196-
, _thmDepends = deps
1197-
, _thmElapsedTime = elapsed
1198-
, _thmSummary = sy
1199-
}
1184+
let thm =
1185+
Theorem
1186+
{ _thmProp = p
1187+
, _thmStats = mempty
1188+
, _thmEvidence = e
1189+
, _thmLocation = loc
1190+
, _thmProgramLoc = ploc
1191+
, _thmReason = rsn
1192+
, _thmNonce = n
1193+
, _thmDepends = deps
1194+
, _thmElapsedTime = elapsed
1195+
, _thmSummary = sy
1196+
}
1197+
let db' = recordTheorem db thm
1198+
pure (thm, db')
12001199

12011200

12021201
-- | Given a theorem with quantified variables, build a new theorem that
12031202
-- specializes the leading quantifiers with the given terms.
12041203
-- This will fail if the given terms to not match the quantifier structure
12051204
-- of the given theorem.
1206-
specializeTheorem :: SharedContext -> TheoremDB -> Pos -> Text -> Theorem -> [Term] -> IO Theorem
1207-
specializeTheorem _sc _db _loc _rsn thm [] = return thm
1205+
specializeTheorem :: SharedContext -> TheoremDB -> Pos -> Text -> Theorem -> [Term] -> IO (Theorem, TheoremDB)
1206+
specializeTheorem _sc db _loc _rsn thm [] = return (thm, db)
12081207
specializeTheorem sc db loc rsn thm ts =
12091208
do res <- specializeProp sc (_thmProp thm) ts
12101209
case res of
@@ -1231,22 +1230,24 @@ admitTheorem ::
12311230
Prop ->
12321231
Pos ->
12331232
Text ->
1234-
IO Theorem
1233+
IO (Theorem, TheoremDB)
12351234
admitTheorem db msg p loc rsn =
12361235
do n <- freshNonce globalNonceGenerator
1237-
recordTheorem db
1238-
Theorem
1239-
{ _thmProp = p
1240-
, _thmStats = solverStats "ADMITTED" (propSize p)
1241-
, _thmEvidence = Admitted msg loc (propToSequent p)
1242-
, _thmLocation = loc
1243-
, _thmProgramLoc = Nothing
1244-
, _thmReason = rsn
1245-
, _thmNonce = n
1246-
, _thmDepends = mempty
1247-
, _thmElapsedTime = 0
1248-
, _thmSummary = AdmittedTheorem msg
1249-
}
1236+
let thm =
1237+
Theorem
1238+
{ _thmProp = p
1239+
, _thmStats = solverStats "ADMITTED" (propSize p)
1240+
, _thmEvidence = Admitted msg loc (propToSequent p)
1241+
, _thmLocation = loc
1242+
, _thmProgramLoc = Nothing
1243+
, _thmReason = rsn
1244+
, _thmNonce = n
1245+
, _thmDepends = mempty
1246+
, _thmElapsedTime = 0
1247+
, _thmSummary = AdmittedTheorem msg
1248+
}
1249+
let db' = recordTheorem db thm
1250+
pure (thm, db')
12501251

12511252
-- | Construct a theorem that an external solver has proved.
12521253
solverTheorem ::
@@ -1256,22 +1257,24 @@ solverTheorem ::
12561257
Pos ->
12571258
Text ->
12581259
NominalDiffTime ->
1259-
IO Theorem
1260+
IO (Theorem, TheoremDB)
12601261
solverTheorem db p stats loc rsn elapsed =
12611262
do n <- freshNonce globalNonceGenerator
1262-
recordTheorem db
1263-
Theorem
1264-
{ _thmProp = p
1265-
, _thmStats = stats
1266-
, _thmEvidence = SolverEvidence stats (propToSequent p)
1267-
, _thmLocation = loc
1268-
, _thmReason = rsn
1269-
, _thmProgramLoc = Nothing
1270-
, _thmNonce = n
1271-
, _thmDepends = mempty
1272-
, _thmElapsedTime = elapsed
1273-
, _thmSummary = ProvedTheorem stats
1274-
}
1263+
let thm =
1264+
Theorem
1265+
{ _thmProp = p
1266+
, _thmStats = stats
1267+
, _thmEvidence = SolverEvidence stats (propToSequent p)
1268+
, _thmLocation = loc
1269+
, _thmReason = rsn
1270+
, _thmProgramLoc = Nothing
1271+
, _thmNonce = n
1272+
, _thmDepends = mempty
1273+
, _thmElapsedTime = elapsed
1274+
, _thmSummary = ProvedTheorem stats
1275+
}
1276+
let db' = recordTheorem db thm
1277+
pure (thm, db')
12751278

12761279
-- | A @ProofGoal@ contains a proposition to be proved, along with
12771280
-- some metadata.
@@ -1782,7 +1785,7 @@ finishProof ::
17821785
ProofState ->
17831786
Bool {- ^ should we record the theorem in the database? -} ->
17841787
Bool {- ^ do we need to normalize the sequent to match the final goal ? -} ->
1785-
IO ProofResult
1788+
IO (ProofResult, TheoremDB)
17861789
finishProof sc db conclProp
17871790
ps@(ProofState gs (concl,loc,ploc,rsn) stats _ checkEv start)
17881791
recordThm useSequentGoals =
@@ -1795,7 +1798,7 @@ finishProof sc db conclProp
17951798
(deps,sy) <- checkEvidence sc e' conclProp
17961799
n <- freshNonce globalNonceGenerator
17971800
end <- getCurrentTime
1798-
thm <- (if recordThm then recordTheorem db else return)
1801+
let theorem =
17991802
Theorem
18001803
{ _thmProp = conclProp
18011804
, _thmStats = stats
@@ -1808,9 +1811,10 @@ finishProof sc db conclProp
18081811
, _thmElapsedTime = diffUTCTime end start
18091812
, _thmSummary = sy
18101813
}
1811-
pure (ValidProof stats thm)
1814+
let db' = if recordThm then recordTheorem db theorem else db
1815+
pure (ValidProof stats theorem, db')
18121816
_ : _ ->
1813-
pure (UnfinishedProof ps)
1817+
pure (UnfinishedProof ps, db)
18141818

18151819
-- | A type describing counterexamples.
18161820
type CEX = [(ExtCns Term, FirstOrderValue)]

0 commit comments

Comments
 (0)