Skip to content

Commit

Permalink
Adding useful type-class instances for NonEmpty
Browse files Browse the repository at this point in the history
  • Loading branch information
recursion-ninja committed Apr 20, 2024
1 parent ddd2c87 commit 3b5517a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 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,15 @@ 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 :| []) .== (y :| []) = x .== y
(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 +2053,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

0 comments on commit 3b5517a

Please sign in to comment.