Skip to content

Commit 9a543ba

Browse files
committedJul 4, 2017
Revert "Remove Eq SBV instance. Fixes half of #241."
This reverts commit 5958276.
1 parent 7555e9a commit 9a543ba

File tree

11 files changed

+34
-52
lines changed

11 files changed

+34
-52
lines changed
 

‎Data/SBV/Control/Query.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ infix 1 |->
640640
(|->) :: SymWord a => SBV a -> a -> Assignment
641641
SBV a |-> v = case literal v of
642642
SBV (SVal _ (Left cw)) -> Assign a cw
643-
_ -> error "Data.SBV: Impossible happened in |->: Cannot construct a CW with a symbolic value!"
643+
r -> error $ "Data.SBV: Impossible happened in |->: Cannot construct a CW with literal: " ++ show r
644644

645645
-- | Produce the query result from an assignment.
646646
mkSMTResult :: [Assignment] -> Query SMTResult

‎Data/SBV/Core/Data.hs

+5-8
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,9 @@ sRTN = sRoundTowardNegative
208208
sRTZ :: SRoundingMode
209209
sRTZ = sRoundTowardZero
210210

211-
-- | Show a symbolic value. Not particularly "desirable," since we can't
212-
-- really show symbolic values, but will do if needed for test purposes.
213-
-- Note that we don't make this a show instance on purpose to make sure we
214-
-- keep it under control and not let it be visible to the end users
215-
showSBV :: SBV a -> String
216-
showSBV (SBV sv) = show sv
211+
-- Not particularly "desirable", but will do if needed
212+
instance Show (SBV a) where
213+
show (SBV sv) = show sv
217214

218215
-- Equality constraint on SBV values. Not desirable since we can't really compare two
219216
-- symbolic values, but will do.
@@ -458,8 +455,8 @@ declNewSArray mkNm = do
458455
-- | Declare a new functional symbolic array. Note that a read from an uninitialized cell will result in an error.
459456
declNewSFunArray :: forall a b. (HasKind a, HasKind b) => Maybe String -> Symbolic (SFunArray a b)
460457
declNewSFunArray mbNm = return $ SFunArray $ error . msg mbNm
461-
where msg Nothing i = "Reading from an uninitialized array entry, index: " ++ showSBV i
462-
msg (Just nm) i = "Array " ++ show nm ++ ": Reading from an uninitialized array entry, index: " ++ showSBV i
458+
where msg Nothing i = "Reading from an uninitialized array entry, index: " ++ show i
459+
msg (Just nm) i = "Array " ++ show nm ++ ": Reading from an uninitialized array entry, index: " ++ show i
463460

464461
-- | Arrays implemented internally as functions
465462
--

‎Data/SBV/Core/Model.hs

+9-9
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import GHC.Stack
4949
import Data.Array (Array, Ix, listArray, elems, bounds, rangeSize)
5050
import Data.Bits (Bits(..))
5151
import Data.Int (Int8, Int16, Int32, Int64)
52-
import Data.List (genericLength, genericIndex, genericTake, unzip4, unzip5, unzip6, unzip7)
52+
import Data.List (genericLength, genericIndex, genericTake, unzip4, unzip5, unzip6, unzip7, intercalate)
5353
import Data.Maybe (fromMaybe)
5454
import Data.Word (Word8, Word16, Word32, Word64)
5555

@@ -630,7 +630,7 @@ pbToInteger :: String -> Int -> SBool -> Integer
630630
pbToInteger w c b
631631
| c < 0 = error $ "SBV." ++ w ++ ": Non-negative coefficient required, received: " ++ show c
632632
| Just v <- unliteral b = if v then fromIntegral c else 0
633-
| True = error $ "SBV.pbToInteger: Received a symbolic boolean at coefficient " ++ show c
633+
| True = error $ "SBV.pbToInteger: Received a symbolic boolean: " ++ show (c, b)
634634

635635
-- | Predicate for optimizing word operations like (+) and (*).
636636
isConcreteZero :: SBV a -> Bool
@@ -761,13 +761,13 @@ instance (Num a, Bits a, SymWord a) => Bits (SBV a) where
761761
| SBV (SVal _ (Left (CW _ (CWInteger n)))) <- x
762762
= testBit n i
763763
| True
764-
= error "SBV.testBit: Called on symbolic value! Use sTestBit instead."
764+
= error $ "SBV.testBit: Called on symbolic value: " ++ show x ++ ". Use sTestBit instead."
765765
-- NB. popCount is *not* implementable on non-concrete symbolic words
766766
popCount x
767767
| SBV (SVal _ (Left (CW (KBounded _ w) (CWInteger n)))) <- x
768768
= popCount (n .&. (bit w - 1))
769769
| True
770-
= error "SBV.popCount: Called on symbolic value! Use sPopCount instead."
770+
= error $ "SBV.popCount: Called on symbolic value: " ++ show x ++ ". Use sPopCount instead."
771771

772772
-- | Replacement for 'testBit'. Since 'testBit' requires a 'Bool' to be returned,
773773
-- we cannot implement it for symbolic words. Index 0 is the least-significant bit.
@@ -1000,7 +1000,7 @@ instance (Show a, Bounded a, Integral a, Num a, SymWord a) => Enum (SBV a) where
10001000
-- | Helper function for use in enum operations
10011001
enumCvt :: (SymWord a, Integral a, Num b) => String -> SBV a -> b
10021002
enumCvt w x = case unliteral x of
1003-
Nothing -> error $ "Enum." ++ w ++ "{" ++ showType x ++ "}: Called on symbolic value!"
1003+
Nothing -> error $ "Enum." ++ w ++ "{" ++ showType x ++ "}: Called on symbolic value " ++ show x
10041004
Just v -> fromIntegral v
10051005

10061006
-- | The 'SDivisible' class captures the essence of division.
@@ -1757,7 +1757,7 @@ instance Metric SReal where minimize nm o = addSValOptGoal (unSBV `fmap` Mini
17571757
-- Quickcheck interface on symbolic-booleans..
17581758
instance Testable SBool where
17591759
property (SBV (SVal _ (Left b))) = property (cwToBool b)
1760-
property _ = error "Cannot quick-check in the presence of uninterpreted constants!"
1760+
property s = error $ "Cannot quick-check in the presence of uninterpreted constants! (" ++ show s ++ ")"
17611761

17621762
instance Testable (Symbolic SBool) where
17631763
property prop = QC.monadicIO $ do (cond, r, tvals) <- QC.run test
@@ -1771,13 +1771,13 @@ instance Testable (Symbolic SBool) where
17711771

17721772
case map fst unints of
17731773
[] -> case unliteral r of
1774-
Nothing -> noQC
1774+
Nothing -> noQC [show r]
17751775
Just b -> return (cond, b, tvals)
1776-
_ -> noQC
1776+
us -> noQC us
17771777

17781778
complain qcInfo = showModel defaultSMTCfg (SMTModel [] qcInfo)
17791779

1780-
noQC = error "Cannot quick-check in the presence of uninterpreted constants!"
1780+
noQC us = error $ "Cannot quick-check in the presence of uninterpreted constants: " ++ intercalate ", " us
17811781

17821782
-- | Quick check an SBV property. Note that a regular 'quickCheck' call will work just as
17831783
-- well. Use this variant if you want to receive the boolean result.

‎Data/SBV/Core/Symbolic.hs

+1-2
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,7 @@ instance Show SVal where
596596
show (SVal k (Right _)) = "<symbolic> :: " ++ show k
597597

598598
-- | Equality constraint on SBV values. Not desirable since we can't really compare two
599-
-- symbolic values, but will do. The only reason we have this is because we want
600-
-- Bits instance for SBV, which has an Eq super-class.
599+
-- symbolic values, but will do.
601600
instance Eq SVal where
602601
SVal _ (Left a) == SVal _ (Left b) = a == b
603602
a == b = error $ "Comparing symbolic bit-vectors; Use (.==) instead. Received: " ++ show (a, b)

‎Data/SBV/Examples/Crypto/AES.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ toBytes x = [x1, x2, x3, x4]
112112
-- Q.E.D.
113113
fromBytes :: [GF28] -> SWord32
114114
fromBytes [x1, x2, x3, x4] = (x1 # x2) # (x3 # x4)
115-
fromBytes xs = error $ "fromBytes: Unexpected (/= 4) input length: " ++ show (length xs)
115+
fromBytes xs = error $ "fromBytes: Unexpected input: " ++ show xs
116116

117117
-- | Rotating a state row by a fixed amount to the right.
118118
rotR :: [GF28] -> Int -> [GF28]
119119
rotR [a, b, c, d] 1 = [d, a, b, c]
120120
rotR [a, b, c, d] 2 = [c, d, a, b]
121121
rotR [a, b, c, d] 3 = [b, c, d, a]
122-
rotR xs i = error $ "rotR: Unexpected input: " ++ show (length xs, i)
122+
rotR xs i = error $ "rotR: Unexpected input: " ++ show (xs, i)
123123

124124
-----------------------------------------------------------------------------
125125
-- ** The key schedule

‎Data/SBV/Examples/Puzzles/MagicSquare.hs

+1-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ magic n
6666
| True
6767
= do putStrLn $ "Solution #" ++ show i
6868
mapM_ printRow board
69-
let good = case unliteral (isMagic sboard) of
70-
Just False -> "False"
71-
Just True -> "True"
72-
Nothing -> "Symbolic!"
73-
putStrLn $ "Valid Check: " ++ good
69+
putStrLn $ "Valid Check: " ++ show (isMagic sboard)
7470
putStrLn "Done."
7571
where lmod = length model
7672
board = chunk n model

‎Data/SBV/Examples/Puzzles/NQueens.hs

+1-7
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,5 @@ nQueens n
4242
dispSolution model
4343
| lmod /= n = error $ "Impossible! Backend solver returned " ++ show lmod ++ " values, was expecting: " ++ show n
4444
| True = do putStr $ show model
45-
let v = isValid n (map literal model)
46-
case unliteral v of
47-
Just True -> putStrLn " (Valid: True)"
48-
Just False -> do putStrLn " (Valid: False)"
49-
error $ "Failed to solve!"
50-
Nothing -> do putStrLn " (Valid: Symbolic!)"
51-
error $ "Failed to solve!"
45+
putStrLn $ " (Valid: " ++ show (isValid n (map literal model)) ++ ")"
5246
where lmod = length model

‎Data/SBV/Examples/Puzzles/Sudoku.hs

+1-5
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ dispSolution (i, f) (_, fs)
6464
| lmod /= i = error $ "Impossible! Backend solver returned " ++ show lmod ++ " values, was expecting: " ++ show i
6565
| True = do putStrLn "Final board:"
6666
mapM_ printRow final
67-
let res = case unliteral (valid final) of
68-
Nothing -> "Symbolic!"
69-
Just False -> "Failed!"
70-
Just True -> "True"
71-
putStrLn $ "Valid Check: " ++ res
67+
putStrLn $ "Valid Check: " ++ show (valid final)
7268
putStrLn "Done."
7369
where lmod = length fs
7470
final = f (map literal fs)

‎Data/SBV/Tools/Polynomial.hs

+7-7
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ liftC f x y = let (a, b) = f (literal x) (literal y) in (fromJust (unliteral a),
9595
liftS :: SymWord a => (a -> String) -> SBV a -> String
9696
liftS f s
9797
| Just x <- unliteral s = f x
98-
| True = "<symbolic>"
98+
| True = show s
9999

100100
-- | Pretty print as a polynomial
101101
sp :: Bits a => Bool -> a -> String
@@ -137,9 +137,9 @@ ites s xs ys
137137
polyMult :: (Num a, Bits a, SymWord a, FromBits (SBV a)) => (SBV a, SBV a, [Int]) -> SBV a
138138
polyMult (x, y, red)
139139
| isReal x
140-
= error $ "SBV.polyMult: Received a real value: " ++ show (unSBV x)
140+
= error $ "SBV.polyMult: Received a real value: " ++ show x
141141
| not (isBounded x)
142-
= error $ "SBV.polyMult: Received infinite precision value: " ++ show (unSBV x)
142+
= error $ "SBV.polyMult: Received infinite precision value: " ++ show x
143143
| True
144144
= fromBitsLE $ genericTake sz $ r ++ repeat false
145145
where (_, r) = mdp ms rs
@@ -152,9 +152,9 @@ polyMult (x, y, red)
152152
polyDivMod :: (Num a, Bits a, SymWord a, FromBits (SBV a)) => SBV a -> SBV a -> (SBV a, SBV a)
153153
polyDivMod x y
154154
| isReal x
155-
= error $ "SBV.polyDivMod: Received a real value: " ++ show (unSBV x)
155+
= error $ "SBV.polyDivMod: Received a real value: " ++ show x
156156
| not (isBounded x)
157-
= error $ "SBV.polyDivMod: Received infinite precision value: " ++ show (unSBV x)
157+
= error $ "SBV.polyDivMod: Received infinite precision value: " ++ show x
158158
| True
159159
= ite (y .== 0) (0, x) (adjust d, adjust r)
160160
where adjust xs = fromBitsLE $ genericTake sz $ xs ++ repeat false
@@ -243,9 +243,9 @@ crcBV n m p = take n $ go (replicate n false) (m ++ replicate n false)
243243
crc :: (FromBits (SBV a), FromBits (SBV b), Num a, Num b, Bits a, Bits b, SymWord a, SymWord b) => Int -> SBV a -> SBV b -> SBV b
244244
crc n m p
245245
| isReal m || isReal p
246-
= error $ "SBV.crc: Received a real value: " ++ show (unSBV m, unSBV p)
246+
= error $ "SBV.crc: Received a real value: " ++ show (m, p)
247247
| not (isBounded m) || not (isBounded p)
248-
= error $ "SBV.crc: Received an infinite precision value: " ++ show (unSBV m, unSBV p)
248+
= error $ "SBV.crc: Received an infinite precision value: " ++ show (m, p)
249249
| True
250250
= fromBitsBE $ replicate (sz - n) false ++ crcBV n (blastBE m) (blastBE p)
251251
where sz = intSizeOf p

‎Data/SBV/Tools/STree.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ readSTree :: (Num i, Bits i, SymWord i, SymWord e) => STree i e -> SBV i -> SBV
4747
readSTree s i = walk (blastBE i) s
4848
where walk [] (SLeaf v) = v
4949
walk (b:bs) (SBin l r) = ite b (walk bs r) (walk bs l)
50-
walk bs _ = error $ "SBV.STree.readSTree: Impossible happened! Length: " ++ show (length bs)
50+
walk _ _ = error $ "SBV.STree.readSTree: Impossible happened while reading: " ++ show i
5151

5252
-- | Writing a value, similar to how reads are done. The important thing is that the tree
5353
-- representation keeps updates to a minimum.
5454
writeSTree :: (Mergeable (SBV e), Num i, Bits i, SymWord i, SymWord e) => STree i e -> SBV i -> SBV e -> STree i e
5555
writeSTree s i j = walk (blastBE i) s
5656
where walk [] _ = SLeaf j
5757
walk (b:bs) (SBin l r) = SBin (ite b l (walk bs l)) (ite b (walk bs r) r)
58-
walk bs _ = error $ "SBV.STree.writeSTree: Impossible happened! Length: " ++ show (length bs)
58+
walk _ _ = error $ "SBV.STree.writeSTree: Impossible happened while reading: " ++ show i
5959

6060
-- | Construct the fully balanced initial tree using the given values.
6161
mkSTree :: forall i e. HasKind i => [SBV e] -> STree i e

‎Data/SBV/Utils/PrettyNum.hs

+4-4
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ instance PrettyNum CW where
100100
| True = let CWInteger w = cwVal cw in sbin False False (hasSign cw, intSizeOf cw) w
101101

102102
instance (SymWord a, PrettyNum a) => PrettyNum (SBV a) where
103-
hexS s = maybe "<symbolic>" (hexS :: a -> String) $ unliteral s
104-
binS s = maybe "<symbolic>" (binS :: a -> String) $ unliteral s
105-
hex s = maybe "<symbolic>" (hex :: a -> String) $ unliteral s
106-
bin s = maybe "<symbolic>" (bin :: a -> String) $ unliteral s
103+
hexS s = maybe (show s) (hexS :: a -> String) $ unliteral s
104+
binS s = maybe (show s) (binS :: a -> String) $ unliteral s
105+
hex s = maybe (show s) (hex :: a -> String) $ unliteral s
106+
bin s = maybe (show s) (bin :: a -> String) $ unliteral s
107107

108108
-- | Show as a hexadecimal value. First bool controls whether type info is printed
109109
-- while the second boolean controls wether 0x prefix is printed. The tuple is

0 commit comments

Comments
 (0)
Please sign in to comment.