@@ -63,7 +63,7 @@ module SAWScript.Proof
63
63
, cofinSetMember
64
64
65
65
, TheoremDB
66
- , newTheoremDB
66
+ , emptyTheoremDB
67
67
, reachableTheorems
68
68
69
69
, Theorem
@@ -131,7 +131,6 @@ module SAWScript.Proof
131
131
132
132
import qualified Control.Monad.Fail as F
133
133
import Control.Monad.Except
134
- import Data.IORef
135
134
import qualified Data.Foldable as Fold
136
135
import Data.List
137
136
import Data.Maybe (fromMaybe )
@@ -902,25 +901,21 @@ data Theorem =
902
901
data TheoremDB =
903
902
TheoremDB
904
903
-- TODO, maybe this should be a summary or something simpler?
905
- { theoremMap :: IORef ( Map. Map TheoremNonce Theorem )
904
+ { theoremMap :: Map. Map TheoremNonce Theorem
906
905
}
907
906
908
- newTheoremDB :: IO TheoremDB
909
- newTheoremDB = TheoremDB <$> newIORef mempty
907
+ emptyTheoremDB :: TheoremDB
908
+ emptyTheoremDB = TheoremDB mempty
910
909
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))
915
912
916
913
-- | Given a set of root values, find all the theorems in this database
917
914
-- that are transitively used in the proofs of those theorems.
918
915
-- This function will panic if any of the roots or reachable theorems
919
916
-- 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
924
919
925
920
where
926
921
loop m visited curr
@@ -945,7 +940,7 @@ reachableTheorems db roots =
945
940
validateTheorem :: SharedContext -> TheoremDB -> Theorem -> IO ()
946
941
947
942
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)
949
944
(deps,_) <- checkEvidence sc e p
950
945
unless (Set. isSubsetOf deps thmDep && Set. isSubsetOf thmDep hyps)
951
946
(fail $ unlines [" Theorem failed to declare its dependencies correctly"
@@ -1148,24 +1143,26 @@ structuralEvidence _sqt (StructuralEvidence sqt' e) = StructuralEvidence sqt' e
1148
1143
structuralEvidence sqt e = StructuralEvidence sqt e
1149
1144
1150
1145
-- | 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 )
1152
1147
proofByTerm sc db prf loc rsn =
1153
1148
do ty <- scTypeOf sc prf
1154
1149
p <- termToProp sc ty
1155
1150
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')
1169
1166
1170
1167
-- | Construct a theorem directly from a proposition and evidence
1171
1168
-- for that proposition. The evidence will be validated to
@@ -1180,31 +1177,33 @@ constructTheorem ::
1180
1177
Maybe ProgramLoc ->
1181
1178
Text ->
1182
1179
NominalDiffTime ->
1183
- IO Theorem
1180
+ IO ( Theorem , TheoremDB )
1184
1181
constructTheorem sc db p e loc ploc rsn elapsed =
1185
1182
do (deps,sy) <- checkEvidence sc e p
1186
1183
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')
1200
1199
1201
1200
1202
1201
-- | Given a theorem with quantified variables, build a new theorem that
1203
1202
-- specializes the leading quantifiers with the given terms.
1204
1203
-- This will fail if the given terms to not match the quantifier structure
1205
1204
-- 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)
1208
1207
specializeTheorem sc db loc rsn thm ts =
1209
1208
do res <- specializeProp sc (_thmProp thm) ts
1210
1209
case res of
@@ -1231,22 +1230,24 @@ admitTheorem ::
1231
1230
Prop ->
1232
1231
Pos ->
1233
1232
Text ->
1234
- IO Theorem
1233
+ IO ( Theorem , TheoremDB )
1235
1234
admitTheorem db msg p loc rsn =
1236
1235
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')
1250
1251
1251
1252
-- | Construct a theorem that an external solver has proved.
1252
1253
solverTheorem ::
@@ -1256,22 +1257,24 @@ solverTheorem ::
1256
1257
Pos ->
1257
1258
Text ->
1258
1259
NominalDiffTime ->
1259
- IO Theorem
1260
+ IO ( Theorem , TheoremDB )
1260
1261
solverTheorem db p stats loc rsn elapsed =
1261
1262
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')
1275
1278
1276
1279
-- | A @ProofGoal@ contains a proposition to be proved, along with
1277
1280
-- some metadata.
@@ -1782,7 +1785,7 @@ finishProof ::
1782
1785
ProofState ->
1783
1786
Bool {- ^ should we record the theorem in the database? -} ->
1784
1787
Bool {- ^ do we need to normalize the sequent to match the final goal ? -} ->
1785
- IO ProofResult
1788
+ IO ( ProofResult , TheoremDB )
1786
1789
finishProof sc db conclProp
1787
1790
ps@ (ProofState gs (concl,loc,ploc,rsn) stats _ checkEv start)
1788
1791
recordThm useSequentGoals =
@@ -1795,7 +1798,7 @@ finishProof sc db conclProp
1795
1798
(deps,sy) <- checkEvidence sc e' conclProp
1796
1799
n <- freshNonce globalNonceGenerator
1797
1800
end <- getCurrentTime
1798
- thm <- ( if recordThm then recordTheorem db else return )
1801
+ let theorem =
1799
1802
Theorem
1800
1803
{ _thmProp = conclProp
1801
1804
, _thmStats = stats
@@ -1808,9 +1811,10 @@ finishProof sc db conclProp
1808
1811
, _thmElapsedTime = diffUTCTime end start
1809
1812
, _thmSummary = sy
1810
1813
}
1811
- pure (ValidProof stats thm)
1814
+ let db' = if recordThm then recordTheorem db theorem else db
1815
+ pure (ValidProof stats theorem, db')
1812
1816
_ : _ ->
1813
- pure (UnfinishedProof ps)
1817
+ pure (UnfinishedProof ps, db )
1814
1818
1815
1819
-- | A type describing counterexamples.
1816
1820
type CEX = [(ExtCns Term , FirstOrderValue )]
0 commit comments