Skip to content

Commit ff072a8

Browse files
committed
Adapt saw-script to changed tuple representation.
1 parent 7a1f95f commit ff072a8

File tree

10 files changed

+37
-51
lines changed

10 files changed

+37
-51
lines changed

src/SAWScript/Crucible/Common/MethodSpec.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ setupToTerm opts sc =
255255

256256
SetupStruct _ _ fs ->
257257
do st <- setupToTerm opts sc base
258-
lift $ scTupleSelector sc st ind (length fs)
258+
lift $ scTupleSelector sc st ind
259259

260260
_ -> MaybeT $ return Nothing
261261

src/SAWScript/Crucible/LLVM/Builtins.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,9 @@ llvm_compositional_extract (Some lm) nm func_name lemmas checkSat setup tactic =
414414
input_terms <- io $ traverse (scExtCns shared_context) input_parameters
415415
applied_extracted_func <- io $ scApplyAll shared_context extracted_func_const input_terms
416416
applied_extracted_func_selectors <-
417-
io $ forM [1 .. (length output_parameters)] $ \i ->
417+
io $ forM [0 .. (length output_parameters - 1)] $ \i ->
418418
mkTypedTerm shared_context
419-
=<< scTupleSelector shared_context applied_extracted_func i (length output_parameters)
419+
=<< scTupleSelector shared_context applied_extracted_func i
420420
let output_parameter_substitution =
421421
Map.fromList $
422422
zip (map ecVarIndex output_parameters) (map ttTerm applied_extracted_func_selectors)

src/SAWScript/Crucible/LLVM/ResolveSetupValue.hs

+2-3
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ resolveSAWTerm cc tp tm =
838838
Cryptol.TVTuple tps ->
839839
do st <- sawCoreState sym
840840
let sc = saw_ctx st
841-
tms <- mapM (\i -> scTupleSelector sc tm i (length tps)) [1 .. length tps]
841+
tms <- mapM (scTupleSelector sc tm) [0 .. length tps - 1]
842842
vals <- zipWithM (resolveSAWTerm cc) tps tms
843843
storTy <-
844844
case toLLVMType dl tp of
@@ -1062,8 +1062,7 @@ memArrayToSawCoreTerm crucible_context endianess typed_term = do
10621062
inner_saw_term <- liftIO $ scTupleSelector
10631063
saw_context
10641064
saw_term
1065-
(field_index + 1)
1066-
(length tuple_element_cryptol_types)
1065+
field_index
10671066
setBytes
10681067
tuple_element_cryptol_type
10691068
inner_saw_term

src/SAWScript/Crucible/LLVM/X86.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,8 @@ setupSimpleLoopFixpointFeature sym sc sawst cfg mvar func =
566566
arguments <- forM fixpoint_substitution_as_list $ \(MapF.Pair _ fixpoint_entry) ->
567567
toSC sym sawst $ Crucible.LLVM.Fixpoint.headerValue fixpoint_entry
568568
applied_func <- scApplyAll sc (ttTerm func) $ implicit_parameters ++ arguments
569-
applied_func_selectors <- forM [1 .. (length fixpoint_substitution_as_list)] $ \i ->
570-
scTupleSelector sc applied_func i (length fixpoint_substitution_as_list)
569+
applied_func_selectors <- forM [0 .. (length fixpoint_substitution_as_list - 1)] $ \i ->
570+
scTupleSelector sc applied_func i
571571
result_substitution <- MapF.fromList <$> zipWithM
572572
(\(MapF.Pair variable _) applied_func_selector ->
573573
MapF.Pair variable <$> bindSAWTerm sym sawst (W4.exprType variable) applied_func_selector)

src/SAWScript/Prover/Exporter.hs

+3-6
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,9 @@ writeVerilogSAT path satq = getSharedContext >>= \sc -> io $
343343
flattenSValue :: IsSymExprBuilder sym => sym -> W4Sim.SValue sym -> IO [Some (W4.SymExpr sym)]
344344
flattenSValue _ (Sim.VBool b) = return [Some b]
345345
flattenSValue _ (Sim.VWord (W4Sim.DBV w)) = return [Some w]
346-
flattenSValue sym (Sim.VPair l r) =
347-
do lv <- Sim.force l
348-
rv <- Sim.force r
349-
ls <- flattenSValue sym lv
350-
rs <- flattenSValue sym rv
351-
return (ls ++ rs)
346+
flattenSValue sym (Sim.VTuple ts) =
347+
do vs <- traverse Sim.force ts
348+
concat <$> traverse (flattenSValue sym) vs
352349
flattenSValue sym (Sim.VVector ts) =
353350
do vs <- mapM Sim.force ts
354351
let getBool (Sim.VBool b) = Just b

src/SAWScript/Prover/MRSolver/Monad.hs

+3-4
Original file line numberDiff line numberDiff line change
@@ -438,15 +438,14 @@ liftSC5 f a b c d e = mrSC >>= \sc -> liftIO (f sc a b c d e)
438438

439439
-- | Apply a 'TermProj' to perform a projection on a 'Term'
440440
doTermProj :: Term -> TermProj -> MRM Term
441-
doTermProj t TermProjLeft = liftSC1 scPairLeft t
442-
doTermProj t TermProjRight = liftSC1 scPairRight t
441+
doTermProj t (TermProjTuple i) = liftSC1 (\sc x -> scTupleSelector sc x i) t
443442
doTermProj t (TermProjRecord fld) = liftSC2 scRecordSelect t fld
444443

445444
-- | Apply a 'TermProj' to a type to get the output type of the projection,
446445
-- assuming that the type is already normalized
447446
doTypeProj :: Term -> TermProj -> MRM Term
448-
doTypeProj (asPairType -> Just (tp1, _)) TermProjLeft = return tp1
449-
doTypeProj (asPairType -> Just (_, tp2)) TermProjRight = return tp2
447+
doTypeProj (asTupleType -> Just tps) (TermProjTuple i)
448+
| i < length tps = pure (tps !! i)
450449
doTypeProj (asRecordType -> Just tp_map) (TermProjRecord fld)
451450
| Just tp <- Map.lookup fld tp_map
452451
= return tp

src/SAWScript/Prover/MRSolver/SMT.hs

+16-14
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,9 @@ mrProvable bool_tm =
221221
(closedOpenTerm a)
222222
ec <- instUVar nm ec_tp
223223
liftSC4 genBVVecTerm n len a ec
224-
-- For pairs, recurse on both sides and combine the result as a pair
225-
(asPairType -> Just (tp1, tp2)) -> do
226-
e1 <- instUVar nm tp1
227-
e2 <- instUVar nm tp2
228-
liftSC2 scPairValue e1 e2
224+
-- For tuples, recurse on all components and combine the result as a tuple
225+
(asTupleType -> Just tps) ->
226+
liftSC1 scTuple =<< traverse (instUVar nm) tps
229227
-- Otherwise, create a global variable with the given name and type
230228
tp' -> liftSC2 scFreshEC nm tp' >>= liftSC1 scExtCns
231229

@@ -268,6 +266,12 @@ andTermInCtx (TermInCtx ctx1 t1) (TermInCtx ctx2 t2) =
268266
t2' <- liftTermLike (length ctx2) (length ctx1) t2
269267
TermInCtx (ctx1++ctx2) <$> liftSC2 scAnd t1' t2'
270268

269+
-- | Conjoin a list of 'TermInCtx's, assuming they all have Boolean type.
270+
allTermInCtx :: [TermInCtx] -> MRM TermInCtx
271+
allTermInCtx [] = TermInCtx [] <$> liftSC1 scBool True
272+
allTermInCtx [x] = pure x
273+
allTermInCtx (x : xs) = andTermInCtx x =<< allTermInCtx xs
274+
271275
-- | Extend the context of a 'TermInCtx' with additional universal variables
272276
-- bound "outside" the 'TermInCtx'
273277
extTermInCtx :: [(LocalName,Term)] -> TermInCtx -> TermInCtx
@@ -358,15 +362,13 @@ mrProveEqH _ (asBoolType -> Just _) t1 t2 =
358362
mrProveEqH _ (asIntegerType -> Just _) t1 t2 =
359363
mrProveEqSimple (liftSC2 scIntEq) t1 t2
360364

361-
-- For pair types, prove both the left and right projections are equal
362-
mrProveEqH var_map (asPairType -> Just (tpL, tpR)) t1 t2 =
363-
do t1L <- liftSC1 scPairLeft t1
364-
t2L <- liftSC1 scPairLeft t2
365-
t1R <- liftSC1 scPairRight t1
366-
t2R <- liftSC1 scPairRight t2
367-
condL <- mrProveEqH var_map tpL t1L t2L
368-
condR <- mrProveEqH var_map tpR t1R t2R
369-
andTermInCtx condL condR
365+
-- For tuple types, prove all of the projections are equal
366+
mrProveEqH var_map (asTupleType -> Just tps) t1 t2 =
367+
do let idxs = [0 .. length tps - 1]
368+
ts1 <- liftSC1 (\sc t -> traverse (scTupleSelector sc t) idxs) t1
369+
ts2 <- liftSC1 (\sc t -> traverse (scTupleSelector sc t) idxs) t2
370+
conds <- sequence $ zipWith3 (mrProveEqH var_map) tps ts1 ts2
371+
allTermInCtx conds
370372

371373
-- For non-bitvector vector types, prove all projections are equal by
372374
-- quantifying over a universal index variable and proving equality at that

src/SAWScript/Prover/MRSolver/Solver.hs

+1-8
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,6 @@ asLRTList (asCtor -> Just (primName -> "Prelude.LRT_Cons", [lrt, lrts])) =
145145
(tp_norm_closed :) <$> asLRTList lrts
146146
asLRTList t = throwMRFailure (MalformedLetRecTypes t)
147147

148-
-- | Match a right-nested series of pairs. This is similar to 'asTupleValue'
149-
-- except that it expects a unit value to always be at the end.
150-
asNestedPairs :: Recognizer Term [Term]
151-
asNestedPairs (asPairValue -> Just (x, asNestedPairs -> Just xs)) = Just (x:xs)
152-
asNestedPairs (asFTermF -> Just UnitValue) = Just []
153-
asNestedPairs _ = Nothing
154-
155148
-- | Bind fresh function variables for a @letRecM@ or @multiFixM@ with the given
156149
-- @LetRecTypes@ and definitions for the function bodies as a lambda
157150
mrFreshLetRecVars :: Term -> Term -> MRM [Term]
@@ -169,7 +162,7 @@ mrFreshLetRecVars lrts defs_f =
169162
-- the definitions of the individual letrec-bound functions in terms of the
170163
-- new function constants
171164
defs_tm <- mrApplyAll defs_f fun_tms
172-
defs <- case asNestedPairs defs_tm of
165+
defs <- case asTupleValue defs_tm of
173166
Just defs -> return defs
174167
Nothing -> throwMRFailure (MalformedDefsFun defs_f)
175168

src/SAWScript/Prover/MRSolver/Term.hs

+6-10
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,15 @@ showMRVar :: MRVar -> String
6464
showMRVar = show . ppName . ecName . unMRVar
6565

6666
-- | A tuple or record projection of a 'Term'
67-
data TermProj = TermProjLeft | TermProjRight | TermProjRecord FieldName
67+
data TermProj = TermProjTuple Int | TermProjRecord FieldName
6868
deriving (Eq, Ord, Show)
6969

7070
-- | Recognize a 'Term' as 0 or more projections
7171
asProjAll :: Term -> (Term, [TermProj])
7272
asProjAll (asRecordSelector -> Just ((asProjAll -> (t, projs)), fld)) =
7373
(t, TermProjRecord fld:projs)
74-
asProjAll (asPairSelector -> Just ((asProjAll -> (t, projs)), isRight))
75-
| isRight = (t, TermProjRight:projs)
76-
| not isRight = (t, TermProjLeft:projs)
74+
asProjAll (asTupleSelector -> Just ((asProjAll -> (t, projs)), i)) =
75+
(t, TermProjTuple i : projs)
7776
asProjAll t = (t, [])
7877

7978
-- | Names of functions to be used in computations, which are either names bound
@@ -100,10 +99,8 @@ funNameTerm :: FunName -> Term
10099
funNameTerm (LetRecName var) = Unshared $ FTermF $ ExtCns $ unMRVar var
101100
funNameTerm (EVarFunName var) = Unshared $ FTermF $ ExtCns $ unMRVar var
102101
funNameTerm (GlobalName gdef []) = globalDefTerm gdef
103-
funNameTerm (GlobalName gdef (TermProjLeft:projs)) =
104-
Unshared $ FTermF $ PairLeft $ funNameTerm (GlobalName gdef projs)
105-
funNameTerm (GlobalName gdef (TermProjRight:projs)) =
106-
Unshared $ FTermF $ PairRight $ funNameTerm (GlobalName gdef projs)
102+
funNameTerm (GlobalName gdef (TermProjTuple i : projs)) =
103+
Unshared $ FTermF $ TupleSelector (funNameTerm (GlobalName gdef projs)) i
107104
funNameTerm (GlobalName gdef (TermProjRecord fname:projs)) =
108105
Unshared $ FTermF $ RecordProj (funNameTerm (GlobalName gdef projs)) fname
109106

@@ -381,8 +378,7 @@ instance PrettyInCtx [Term] where
381378
prettyInCtx xs = list <$> mapM prettyInCtx xs
382379

383380
instance PrettyInCtx TermProj where
384-
prettyInCtx TermProjLeft = return (pretty '.' <> "1")
385-
prettyInCtx TermProjRight = return (pretty '.' <> "2")
381+
prettyInCtx (TermProjTuple i) = return (pretty '.' <> pretty i)
386382
prettyInCtx (TermProjRecord fld) = return (pretty '.' <> pretty fld)
387383

388384
instance PrettyInCtx FunName where

src/SAWScript/SBVParser.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ scTyp sc (TRecord fields) =
263263
splitInputs :: SharedContext -> Typ -> Term -> IO [Term]
264264
splitInputs _sc TBool x = return [x]
265265
splitInputs sc (TTuple ts) x =
266-
do xs <- mapM (\i -> scTupleSelector sc x i (length ts)) [1 .. length ts]
266+
do xs <- mapM (scTupleSelector sc x) [0 .. length ts - 1]
267267
yss <- sequence (zipWith (splitInputs sc) ts xs)
268268
return (concat yss)
269269
splitInputs _ (TVec _ TBool) x = return [x]

0 commit comments

Comments
 (0)