Skip to content

Commit

Permalink
Update uses of saw-core API WRT simpset annotations.
Browse files Browse the repository at this point in the history
Currently, we use trivial `()` annotations and ignore annotations
collected by rewrite steps. The plan is to allocate unique nonces
for each theorem and use those to track theorem dependencies.
  • Loading branch information
robdockins committed Apr 26, 2021
1 parent e9b9e84 commit f992bda
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 41 deletions.
38 changes: 18 additions & 20 deletions src/SAWScript/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ replacePrim pat replace t = do
unless c $ fail $ unlines
[ "terms do not have convertible types", show tpat, show ty1, show trepl, show ty2 ]

let ss = emptySimpset
t' <- io $ replaceTerm sc ss (tpat, trepl) (ttTerm t)
let ss = emptySimpset :: SV.SAWSimpset
(_,t') <- io $ replaceTerm sc ss (tpat, trepl) (ttTerm t)

io $ do
ty <- scTypeOf sc (ttTerm t)
Expand Down Expand Up @@ -494,11 +494,11 @@ unfoldGoal unints =
prop' <- io (unfoldProp sc unints' (goalProp goal))
return (prop', UnfoldEvidence unints')

simplifyGoal :: Simpset -> ProofScript ()
simplifyGoal :: SV.SAWSimpset -> ProofScript ()
simplifyGoal ss =
execTactic $ tacticChange $ \goal ->
do sc <- getSharedContext
prop' <- io (simplifyProp sc ss (goalProp goal))
(_,prop') <- io (simplifyProp sc ss (goalProp goal))
return (prop', RewriteEvidence ss)

goal_eval :: [String] -> ProofScript ()
Expand Down Expand Up @@ -882,28 +882,26 @@ quickCheckPrintPrim opts sc numTests tt =
"----------Counterexample----------\n" ++
showList cex' ""

cryptolSimpset :: TopLevel Simpset
cryptolSimpset :: TopLevel SV.SAWSimpset
cryptolSimpset =
do sc <- getSharedContext
io $ Cryptol.mkCryptolSimpset sc

addPreludeEqs :: [String] -> Simpset
-> TopLevel Simpset
addPreludeEqs :: [String] -> SV.SAWSimpset -> TopLevel SV.SAWSimpset
addPreludeEqs names ss = do
sc <- getSharedContext
eqRules <- io $ mapM (scEqRewriteRule sc) (map qualify names)
return (addRules eqRules ss)
where qualify = mkIdent (mkModuleName ["Prelude"])

addCryptolEqs :: [String] -> Simpset
-> TopLevel Simpset
addCryptolEqs :: [String] -> SV.SAWSimpset -> TopLevel SV.SAWSimpset
addCryptolEqs names ss = do
sc <- getSharedContext
eqRules <- io $ mapM (scEqRewriteRule sc) (map qualify names)
return (addRules eqRules ss)
where qualify = mkIdent (mkModuleName ["Cryptol"])

add_core_defs :: String -> [String] -> Simpset -> TopLevel Simpset
add_core_defs :: String -> [String] -> SV.SAWSimpset -> TopLevel SV.SAWSimpset
add_core_defs modname names ss =
do sc <- getSharedContext
defs <- io $ mapM (getDef sc) names -- FIXME: warn if not found
Expand All @@ -917,16 +915,16 @@ add_core_defs modname names ss =
Just d -> return d
Nothing -> fail $ modname ++ " definition " ++ n ++ " not found"

add_prelude_defs :: [String] -> Simpset -> TopLevel Simpset
add_prelude_defs :: [String] -> SV.SAWSimpset -> TopLevel SV.SAWSimpset
add_prelude_defs = add_core_defs "Prelude"

add_cryptol_defs :: [String] -> Simpset -> TopLevel Simpset
add_cryptol_defs :: [String] -> SV.SAWSimpset -> TopLevel SV.SAWSimpset
add_cryptol_defs = add_core_defs "Cryptol"

rewritePrim :: Simpset -> TypedTerm -> TopLevel TypedTerm
rewritePrim :: SV.SAWSimpset -> TypedTerm -> TopLevel TypedTerm
rewritePrim ss (TypedTerm schema t) = do
sc <- getSharedContext
t' <- io $ rewriteSharedTerm sc ss t
(_,t') <- io $ rewriteSharedTerm sc ss t
return (TypedTerm schema t')

unfold_term :: [String] -> TypedTerm -> TopLevel TypedTerm
Expand All @@ -942,23 +940,23 @@ beta_reduce_term (TypedTerm schema t) = do
t' <- io $ betaNormalize sc t
return (TypedTerm schema t')

addsimp :: Theorem -> Simpset -> TopLevel Simpset
addsimp :: Theorem -> SV.SAWSimpset -> TopLevel SV.SAWSimpset
addsimp thm ss =
do sc <- getSharedContext
io (propToRewriteRule sc (thmProp thm)) >>= \case
io (propToRewriteRule sc (thmProp thm) (Just ())) >>= \case
Nothing -> fail "addsimp: theorem not an equation"
Just rule -> pure (addRule rule ss)

addsimp' :: Term -> Simpset -> TopLevel Simpset
addsimp' :: Term -> SV.SAWSimpset -> TopLevel SV.SAWSimpset
addsimp' t ss =
case ruleOfProp t of
case ruleOfProp t Nothing of -- TODO!! track this as an axiom?
Nothing -> fail "addsimp': theorem not an equation"
Just rule -> pure (addRule rule ss)

addsimps :: [Theorem] -> Simpset -> TopLevel Simpset
addsimps :: [Theorem] -> SV.SAWSimpset -> TopLevel SV.SAWSimpset
addsimps thms ss = foldM (flip addsimp) ss thms

addsimps' :: [Term] -> Simpset -> TopLevel Simpset
addsimps' :: [Term] -> SV.SAWSimpset -> TopLevel SV.SAWSimpset
addsimps' ts ss = foldM (flip addsimp') ss ts

print_type :: Term -> TopLevel ()
Expand Down
2 changes: 1 addition & 1 deletion src/SAWScript/Crucible/LLVM/MethodSpecIR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ data LLVMCrucibleContext arch =
, _ccBackend :: Sym
, _ccLLVMSimContext :: Crucible.SimContext (SAWCruciblePersonality Sym) Sym CL.LLVM
, _ccLLVMGlobals :: Crucible.SymGlobalState Sym
, _ccBasicSS :: Simpset
, _ccBasicSS :: Simpset ()
}

makeLenses ''LLVMCrucibleContext
Expand Down
2 changes: 1 addition & 1 deletion src/SAWScript/Crucible/LLVM/ResolveSetupValue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ resolveSAWPred cc tm = do
st <- sawCoreState sym
let sc = saw_ctx st
let ss = cc^.ccBasicSS
tm' <- rewriteSharedTerm sc ss tm
(_,tm') <- rewriteSharedTerm sc ss tm
mx <- case getAllExts tm' of
-- concretely evaluate if it is a closed term
[] -> do modmap <- scGetModuleMap sc
Expand Down
2 changes: 1 addition & 1 deletion src/SAWScript/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,7 @@ primitives = Map.fromList
, "goals 'prop1' and 'prop2'." ]

, prim "empty_ss" "Simpset"
(pureVal emptySimpset)
(pureVal (emptySimpset :: SAWSimpset))
Current
[ "The empty simplification rule set, containing no rules." ]

Expand Down
16 changes: 8 additions & 8 deletions src/SAWScript/Proof.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ propToTerm :: SharedContext -> Prop -> IO Term
propToTerm _sc (Prop tm) = pure tm

-- | Attempt to interpret a proposition as a rewrite rule.
propToRewriteRule :: SharedContext -> Prop -> IO (Maybe (RewriteRule))
propToRewriteRule _sc (Prop tm) =
case ruleOfProp tm of
propToRewriteRule :: SharedContext -> Prop -> Maybe a -> IO (Maybe (RewriteRule a))
propToRewriteRule _sc (Prop tm) ann =
case ruleOfProp tm ann of
Nothing -> pure Nothing
Just r -> pure (Just r)

Expand All @@ -173,10 +173,10 @@ unfoldProp sc unints (Prop tm) =
return (Prop tm')

-- | Rewrite the proposition using the provided Simpset
simplifyProp :: SharedContext -> Simpset -> Prop -> IO Prop
simplifyProp :: Ord a => SharedContext -> Simpset a -> Prop -> IO (Set a, Prop)
simplifyProp sc ss (Prop tm) =
do tm' <- rewriteSharedTerm sc ss tm
return (Prop tm')
do (a, tm') <- rewriteSharedTerm sc ss tm
return (a, Prop tm')

-- | Evaluate the given proposition by round-tripping
-- through the What4 formula representation. This will
Expand Down Expand Up @@ -332,7 +332,7 @@ data Evidence
-- | This type of evidence is used to modify a goal to prove via rewriting.
-- The goal to prove is rewritten by the given simpset; then the provided
-- evidence is used to check the modified goal.
| RewriteEvidence Simpset Evidence
| RewriteEvidence (Simpset ()) Evidence

-- | This type of evidence is used to modify a goal to prove via unfolding
-- constant definitions. The goal to prove is modified by unfolding
Expand Down Expand Up @@ -608,7 +608,7 @@ checkEvidence sc = check mempty
check hyps e' p'

RewriteEvidence ss e' ->
do p' <- simplifyProp sc ss p
do (_,p') <- simplifyProp sc ss p -- TODO, remember the annotations!
check hyps e' p'

EvalEvidence vars e' ->
Expand Down
4 changes: 2 additions & 2 deletions src/SAWScript/Prover/Rewrite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Verifier.SAW.Term.Functor(preludeName, mkIdent, Ident, mkModuleName)
import Verifier.SAW.Conversion
import Verifier.SAW.SharedTerm(SharedContext,scFindDef)

basic_ss :: SharedContext -> IO Simpset
basic_ss :: SharedContext -> IO (Simpset a)
basic_ss sc =
do rs1 <- concat <$> traverse (defRewrites sc) defs
rs2 <- scEqsRewriteRules sc eqs
Expand Down Expand Up @@ -67,7 +67,7 @@ basic_ss sc =



defRewrites :: SharedContext -> Ident -> IO [RewriteRule]
defRewrites :: SharedContext -> Ident -> IO [RewriteRule a]
defRewrites sc ident =
scFindDef sc ident >>= \maybe_def ->
case maybe_def of
Expand Down
16 changes: 9 additions & 7 deletions src/SAWScript/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ data Value
-- operations in these monads can fail at runtime.
| VTopLevel (TopLevel Value)
| VProofScript (ProofScript Value)
| VSimpset Simpset
| VSimpset SAWSimpset
| VTheorem Theorem
-----
| VLLVMCrucibleSetup !(LLVMCrucibleSetupM Value)
Expand Down Expand Up @@ -154,6 +154,8 @@ data Value
| VCFG SAW_CFG
| VGhostVar CMS.GhostGlobal

type SAWSimpset = Simpset ()

data AIGNetwork where
AIGNetwork :: (Typeable l, Typeable g, AIG.IsAIG l g) => AIG.Network l g -> AIGNetwork

Expand All @@ -165,7 +167,7 @@ data SAW_CFG where
JVM_CFG :: Crucible.AnyCFG JVM -> SAW_CFG

data BuiltinContext = BuiltinContext { biSharedContext :: SharedContext
, biBasicSS :: Simpset
, biBasicSS :: SAWSimpset
}
deriving Generic

Expand Down Expand Up @@ -245,7 +247,7 @@ showsSatResult opts r =
showMulti _ [] = showString "]"
showMulti s (eqn : eqns) = showString s . showEqn eqn . showMulti ", " eqns

showSimpset :: PPOpts -> Simpset -> String
showSimpset :: PPOpts -> Simpset a -> String
showSimpset opts ss =
unlines ("Rewrite Rules" : "=============" : map (show . ppRule) (listRules ss))
where
Expand Down Expand Up @@ -382,7 +384,7 @@ data TopLevelRO =
, roPosition :: SS.Pos
, roProxy :: AIGProxy
, roInitWorkDir :: FilePath
, roBasicSS :: Simpset
, roBasicSS :: SAWSimpset
}

data TopLevelRW =
Expand Down Expand Up @@ -459,7 +461,7 @@ getOptions = TopLevel (asks roOptions)
getProxy :: TopLevel AIGProxy
getProxy = TopLevel (asks roProxy)

getBasicSS :: TopLevel Simpset
getBasicSS :: TopLevel SAWSimpset
getBasicSS = TopLevel (asks roBasicSS)

localOptions :: (Options -> Options) -> TopLevel a -> TopLevel a
Expand Down Expand Up @@ -870,10 +872,10 @@ instance FromValue Bool where
fromValue (VBool b) = b
fromValue _ = error "fromValue Bool"

instance IsValue Simpset where
instance IsValue SAWSimpset where
toValue ss = VSimpset ss

instance FromValue Simpset where
instance FromValue SAWSimpset where
fromValue (VSimpset ss) = ss
fromValue _ = error "fromValue Simpset"

Expand Down

0 comments on commit f992bda

Please sign in to comment.