Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding useful type-class instances for NonEmpty #689

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Data/SBV/Core/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ import Data.Char (toLower, isDigit)
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Kind (Type)
import Data.List (genericLength, genericIndex, genericTake, unzip4, unzip5, unzip6, unzip7, intercalate, isPrefixOf)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NE
import Data.Maybe (fromMaybe, mapMaybe)
import Data.String (IsString(..))
import Data.Word (Word8, Word16, Word32, Word64)
Expand Down Expand Up @@ -959,6 +961,13 @@ instance OrdSymbolic a => OrdSymbolic [a] where
_ .< [] = sFalse
(x:xs) .< (y:ys) = x .< y .|| (x .== y .&& xs .< ys)

-- NonEmpty
instance (EqSymbolic a) => EqSymbolic (NonEmpty a) where
(x :| xs) .== (y :| ys) = x .== y .&& xs .== ys

instance (OrdSymbolic a) => OrdSymbolic (NonEmpty a) where
(x :| xs) .< (y :| ys) = x .< y .|| xs .< ys

-- Maybe
instance EqSymbolic a => EqSymbolic (Maybe a) where
Nothing .== Nothing = sTrue
Expand Down Expand Up @@ -2042,6 +2051,15 @@ instance Mergeable a => Mergeable [a] where
"Use the 'SList' type (and Data.SBV.List routines) to model fully symbolic lists."
where (lxs, lys) = (length xs, length ys)

-- NonEmpty
instance Mergeable a => Mergeable (NonEmpty a) where
symbolicMerge f t xs ys
| lxs == lys = NE.zipWith (symbolicMerge f t) xs ys
| True = cannotMerge "lists"
("Branches produce different sizes: " ++ show lxs ++ " vs " ++ show lys ++ ". Must have the same length.")
"Use the 'SList' type (and Data.SBV.List routines) to model fully symbolic lists."
where (lxs, lys) = (length xs, length ys)

-- ZipList
instance Mergeable a => Mergeable (ZipList a) where
symbolicMerge force test (ZipList xs) (ZipList ys)
Expand Down
Loading