@@ -27,7 +27,6 @@ import Control.Monad.Except (MonadError(..))
2727import Control.Monad.IO.Class (MonadIO (.. ))
2828import Control.Monad.Reader (asks )
2929import Control.Monad.State (MonadState (.. ), gets , modify )
30- import Control.Monad.Trans.Class (MonadTrans (.. ))
3130import qualified Control.Exception as Ex
3231import qualified Data.ByteString as StrictBS
3332import qualified Data.ByteString.Lazy as BS
@@ -64,7 +63,7 @@ import qualified CryptolSAWCore.Simpset as Cryptol
6463import qualified CryptolSAWCore.Monadify as Monadify
6564
6665-- saw-support
67- import qualified SAWSupport.Pretty as PPS (Doc , MemoStyle (.. ), Opts (.. ), defaultOpts , render , pShow )
66+ import qualified SAWSupport.Pretty as PPS (MemoStyle (.. ), Opts (.. ), pShow )
6867
6968-- saw-core
7069import qualified SAWCore.Parser.AST as Un
@@ -75,7 +74,6 @@ import SAWCore.FiniteValue
7574 , FirstOrderValue (.. )
7675 , scFirstOrderValue
7776 )
78- import SAWCore.Module (ModuleMap )
7977import SAWCore.Name (ecShortName )
8078import SAWCore.SATQuery
8179import SAWCore.SCTypeCheck
@@ -152,7 +150,6 @@ import qualified SAWCentral.Prover.RME as Prover
152150import qualified SAWCentral.Prover.ABC as Prover
153151import qualified SAWCentral.Prover.What4 as Prover
154152import qualified SAWCentral.Prover.Exporter as Prover
155- import qualified SAWCentral.Prover.MRSolver as Prover
156153import SAWCentral.VerificationSummary
157154
158155showPrim :: SV. Value -> TopLevel Text
@@ -2246,151 +2243,22 @@ monadifyTypedTerm sc t =
22462243ensureMonadicTerm :: SharedContext -> TypedTerm -> TopLevel TypedTerm
22472244ensureMonadicTerm sc t
22482245 | TypedTermOther tp <- ttType t =
2249- io (Prover. isSpecFunType sc tp) >>= \ case
2246+ io (isSpecFunType sc tp) >>= \ case
22502247 True -> return t
22512248 False -> monadifyTypedTerm sc t
22522249ensureMonadicTerm sc t = monadifyTypedTerm sc t
22532250
2254- -- | Normalizes the given 'TypedTerm's for calling 'Prover.askMRSolver' or
2255- -- 'Prover.refinementTerm' and ensures they are of the expected form.
2256- -- Additionally, if the second argument is @Just str@, prints out @str@
2257- -- followed by an abridged version of the refinement represented by the two
2258- -- terms.
2259- mrSolverNormalizeAndPrintArgs ::
2260- SharedContext -> Maybe PPS. Doc ->
2261- TypedTerm -> TypedTerm -> TopLevel (Term , Term )
2262- mrSolverNormalizeAndPrintArgs sc printStr tt1 tt2 =
2263- do mm <- io $ scGetModuleMap sc
2264- let ? mm = mm
2265- m1 <- ttTerm <$> ensureMonadicTerm sc tt1
2266- m2 <- ttTerm <$> ensureMonadicTerm sc tt2
2267- m1' <- io $ collapseEta <$> betaNormalize sc m1
2268- m2' <- io $ collapseEta <$> betaNormalize sc m2
2269- case printStr of
2270- Nothing -> return ()
2271- Just str -> printOutLnTop Info $ PPS. render PPS. defaultOpts $
2272- " [MRSolver] " <> str <> " : " <> ppTmHead m1' <>
2273- " |= " <> ppTmHead m2'
2274- return (m1', m2')
2275- where -- Turn a term of the form @\x1 ... xn -> f x1 ... xn@ into @f@
2276- collapseEta :: Term -> Term
2277- collapseEta (asLambdaList -> (lamVars,
2278- asApplyAll -> (t@ (smallestLooseVar -> Nothing ),
2279- mapM asLocalVar -> Just argVars)))
2280- | argVars == [(length lamVars - 1 ), (length lamVars - 2 ) .. 0 ] = t
2281- collapseEta t = t
2282- -- Pretty-print the name of the top-level function call, followed by
2283- -- "..." if it is given any arguments, or just "..." if there is no
2284- -- top-level call
2285- ppTmHead :: (? mm :: ModuleMap ) => Term -> PPS. Doc
2286- ppTmHead (asLambdaList -> (_,
2287- asApplyAll -> (t@ (
2288- Prover. asProjAll -> (
2289- Monadify. asTypedGlobalDef -> Just _, _)), args))) =
2290- ppTerm PPS. defaultOpts t <> if length args > 0 then " ..." else " "
2291- ppTmHead _ = " ..."
2292-
2293- -- | The calback to be used by MRSolver for making SMT queries
2294- mrSolverAskSMT :: Set VarIndex -> Sequent -> TopLevel (SolverStats , SolveResult )
2295- mrSolverAskSMT = applyProverToGoal [What4 , Z3 ] [] (Prover. proveWhat4_z3 True )
2296-
2297- -- | Given the result of calling 'Prover.askMRSolver' or
2298- -- 'Prover.refinementTerm', fails and prints out@`err@ followed by the second
2299- -- argument if the given result is @Left err@ for some @err@, or otherwise
2300- -- returns @a@ if the result is@`Right a@ for some @a@. Additionally, if the
2301- -- third argument is @Just str@, prints out @str@ on success (i.e. 'Right').
2302- mrSolverGetResultOrFail ::
2303- Prover. MREnv ->
2304- String {- The string to print out on failure -} ->
2305- Maybe String {- The string to print out on success, if any -} ->
2306- Either Prover. MRFailure a {- The result, printed out on error -} ->
2307- TopLevel a
2308- mrSolverGetResultOrFail env errStr succStr res = case res of
2309- Left err | Prover. mreDebugLevel env == 0 ->
2310- fail (Prover. showMRFailure env err ++ " \n [MRSolver] " ++ errStr)
2311- Left err ->
2312- -- we ignore the MRFailure context here since it will have already
2313- -- been printed by the debug trace
2314- fail (Prover. showMRFailureNoCtx env err ++ " \n [MRSolver] " ++ errStr)
2315- Right a | Just s <- succStr ->
2316- printOutLnTop Info s >> return a
2317- Right a -> return a
2318-
2319- -- | Invokes MRSolver to attempt to solve a focused goal of the form
2320- -- @(a1:A1) -> ... -> (an:An) -> refinesS_eq ...@, assuming the refinements
2321- -- in the given 'Refnset', and printing an error message and exiting if
2322- -- this cannot be done
2323- mrSolver :: SV. SAWRefnset -> ProofScript ()
2324- mrSolver rs = execTactic $ Tactic $ \ goal -> lift $
2325- getSharedContext >>= \ sc ->
2326- case sequentState (goalSequent goal) of
2327- Unfocused -> fail " mrsolver: focus required"
2328- HypFocus _ _ -> fail " mrsolver: cannot apply mrsolver in a hypothesis"
2329- ConclFocus (Prover. asRefinesS . unProp ->
2330- Just (Prover. RefinesS args ev rtp1 rtp2 t1 t2)) _ ->
2331- do tp1 <- liftIO $ scGlobalApply sc " SpecM.SpecM" [ev, rtp1]
2332- tp2 <- liftIO $ scGlobalApply sc " SpecM.SpecM" [ev, rtp2]
2333- let tt1 = TypedTerm (TypedTermOther tp1) t1
2334- tt2 = TypedTerm (TypedTermOther tp2) t2
2335- (m1, m2) <- mrSolverNormalizeAndPrintArgs sc (Just $ " Tactic call" ) tt1 tt2
2336- env <- rwMRSolverEnv <$> get
2337- time1 <- liftIO getCurrentTime
2338- res <- Prover. askMRSolver sc env Nothing mrSolverAskSMT rs args m1 m2
2339- time2 <- liftIO getCurrentTime
2340- let diff = show $ diffUTCTime time2 time1
2341- errStr = printf " Failure in %s" diff
2342- succStr = printf " Success in %s" diff
2343- (stats, mre) <- mrSolverGetResultOrFail env errStr (Just succStr) res
2344- return (() , stats, [] , leafEvidence $ MrSolverEvidence mre)
2345- _ -> error " mrsolver: cannot apply mrsolver to a non-refinement goal"
2346-
2347- -- | Add a proved refinement theorem to a given refinement set
2348- addrefn :: Theorem -> SV. SAWRefnset -> TopLevel SV. SAWRefnset
2349- addrefn thm rs =
2350- getSharedContext >>= \ sc ->
2351- io (scGetModuleMap sc) >>= \ mm ->
2352- let ? mm = mm in
2353- case Prover. asFunAssump (Just (thmNonce thm)) (unProp $ thmProp thm) of
2354- Nothing -> fail " addrefn: theorem is not a refinement"
2355- Just fassump -> pure (Prover. addFunAssump fassump rs)
2356-
2357- -- | Add proved refinement theorems to a given refinement set
2358- addrefns :: [Theorem ] -> SV. SAWRefnset -> TopLevel SV. SAWRefnset
2359- addrefns thms ss = foldM (flip addrefn) ss thms
2360-
2361- -- | Set the debug level of the 'Prover.MREnv'
2362- mrSolverSetDebug :: Int -> TopLevel ()
2363- mrSolverSetDebug dlvl =
2364- modify (\ rw -> rw { rwMRSolverEnv =
2365- Prover. mrEnvSetDebugLevel dlvl (rwMRSolverEnv rw) })
2366-
2367- -- | Modify the 'PPOpts' of the current 'MREnv' to have a maximum printing depth
2368- mrSolverSetDebugDepth :: Int -> TopLevel ()
2369- mrSolverSetDebugDepth depth =
2370- modify (\ rw -> rw { rwMRSolverEnv = (rwMRSolverEnv rw) {
2371- Prover. mrePPOpts = (Prover. mrePPOpts (rwMRSolverEnv rw)) {
2372- PPS. ppMaxDepth = Just depth }}})
2373-
2374- -- | Given a list of names and types representing variables over which to
2375- -- quantify as as well as two terms containing those variables, which may be
2376- -- terms or functions in the SpecM monad, construct the SAWCore term which is
2377- -- the refinement (@SpecM.refinesS@) of the given terms, with the given
2378- -- variables generalized with a Pi type.
2379- refinesTerm :: [TypedTerm ] -> TypedTerm -> TypedTerm -> TopLevel TypedTerm
2380- refinesTerm vars tt1 tt2 =
2381- do sc <- getSharedContext
2382- tt1' <- lambdas vars tt1
2383- tt2' <- lambdas vars tt2
2384- (m1, m2) <- mrSolverNormalizeAndPrintArgs sc Nothing tt1' tt2'
2385- env <- rwMRSolverEnv <$> get
2386- time1 <- liftIO getCurrentTime
2387- res <- Prover. refinementTerm sc env Nothing mrSolverAskSMT
2388- Prover. emptyRefnset [] m1 m2
2389- time2 <- liftIO getCurrentTime
2390- let diff = show $ diffUTCTime time2 time1
2391- errStr = printf " [MRSolver] Failed to build refinement term (%s)" diff
2392- ttRes <- mrSolverGetResultOrFail env errStr Nothing res
2393- io $ mkTypedTerm sc ttRes
2251+ -- | Match a type as being of the form @SpecM E a@ for some @E@ and @a@
2252+ asSpecM :: Term -> Maybe (Term , Term )
2253+ asSpecM (asApplyAll -> (isGlobalDef " SpecM.SpecM" -> Just () , [ev, tp])) =
2254+ return (ev, tp)
2255+ asSpecM _ = fail " not a SpecM type, or event type is not closed!"
2256+
2257+ -- | Test if a type normalizes to a monadic function type of 0 or more arguments
2258+ isSpecFunType :: SharedContext -> Term -> IO Bool
2259+ isSpecFunType sc t = scWhnf sc t >>= \ case
2260+ (asPiList -> (_, asSpecM -> Just _)) -> return True
2261+ _ -> return False
23942262
23952263setMonadification :: SharedContext -> Text -> Text -> Bool -> TopLevel ()
23962264setMonadification sc cry_str saw_str poly_p =
0 commit comments