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

Add toGuardedList function. #137

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added wrappers for state transformers. ([#132](https://github.com/lsrcz/grisette/pull/132))
- Added `toGuardList` function. ([#137](https://github.com/lsrcz/grisette/pull/137))

### Removed

Expand Down
17 changes: 16 additions & 1 deletion src/Grisette/Core/Data/Class/SimpleMergeable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import Control.Monad.Trans.Cont (ContT (ContT))
import Control.Monad.Trans.Maybe (MaybeT (MaybeT))
import qualified Control.Monad.Writer.Lazy as WriterLazy
import qualified Control.Monad.Writer.Strict as WriterStrict
import Data.Bifunctor (Bifunctor (first))
import Data.Kind (Type)
import GHC.Generics
( Generic (Rep, from, to),
Expand All @@ -71,7 +72,7 @@ import GHC.Generics
)
import GHC.TypeNats (KnownNat, type (<=))
import Generics.Deriving (Default (Default))
import Grisette.Core.Data.Class.Bool (ITEOp (ites))
import Grisette.Core.Data.Class.Bool (ITEOp (ites), LogicalOp (nots, (&&~)))
import Grisette.Core.Data.Class.Function (Function (Arg, Ret, (#)))
import Grisette.Core.Data.Class.Mergeable
( Mergeable (rootStrategy),
Expand All @@ -81,6 +82,7 @@ import Grisette.Core.Data.Class.Mergeable
Mergeable3 (liftRootStrategy3),
MergingStrategy (SimpleStrategy),
)
import Grisette.Core.Data.Class.Solvable (Solvable (con))
import Grisette.IR.SymPrim.Data.Prim.InternedTerm.Term
( LinkedRep,
SupportedPrim,
Expand Down Expand Up @@ -717,6 +719,19 @@ class (UnionLike u) => UnionPrjOp (u :: Type -> Type) where
-- 1
leftMost :: u a -> a

-- | Convert the union to a guarded list.
--
-- >>> toGuardedList (mrgIf "a" (single 1) (mrgIf "b" (single 2) (single 3)) :: UnionM Integer)
-- [(a,1),((&& b (! a)),2),((! (|| b a)),3)]
toGuardedList :: u a -> [(SymBool, a)]
toGuardedList u =
case (singleView u, ifView u) of
(Just x, _) -> [(con True, x)]
(_, Just (c, l, r)) ->
fmap (first (&&~ c)) (toGuardedList l)
++ fmap (first (&&~ nots c)) (toGuardedList r)
_ -> error "Should not happen"

-- | Pattern match to extract single values with 'singleView'.
--
-- >>> case (single 1 :: UnionM Integer) of Single v -> v
Expand Down
17 changes: 15 additions & 2 deletions test/Grisette/Core/Control/Monad/UnionMTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import Grisette.Core.Data.Class.SOrd
import Grisette.Core.Data.Class.SimpleMergeable
( SimpleMergeable (mrgIte),
UnionLike (single, unionIf),
UnionPrjOp (ifView, leftMost, singleView),
UnionPrjOp (ifView, leftMost, singleView, toGuardedList),
merge,
mrgIf,
mrgIte1,
Expand All @@ -61,6 +61,7 @@ import Grisette.IR.SymPrim.Data.Prim.Model
( ModelValuePair ((::=)),
)
import Grisette.IR.SymPrim.Data.SymPrim (SymBool)
import Grisette.TestUtil.SymbolicAssertion ((@?=~))
import Test.Framework (Test, testGroup)
import Test.Framework.Providers.HUnit (testCase)
import Test.HUnit (assertFailure, (@?=))
Expand Down Expand Up @@ -241,7 +242,19 @@ unionMTests =
let r2 :: UnionM (Either SymBool SymBool) =
mrgIf "a" (mrgSingle $ Left "b") (mrgSingle $ Right "c")
leftMost r1 @?= Left "b"
leftMost r2 @?= Left "b"
leftMost r2 @?= Left "b",
testCase "toGuardedList should work" $ do
let actual =
toGuardedList
( mrgIf "a" (single 1) (mrgIf "b" (single 2) (single 3)) ::
UnionM Integer
)
let expected =
[ ("a", 1),
(nots "a" &&~ "b", 2),
(nots "a" &&~ nots "b", 3)
]
actual @?=~ expected
],
testGroup
"MonadUnion"
Expand Down
Loading