-
Notifications
You must be signed in to change notification settings - Fork 32
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 special And
associativity helpers e.g. _ :: Refined (pl And pr) a -> Refined pr (Refined pl a)
#104
Comments
raehik
changed the title
Add special
Add special Apr 13, 2024
And
associativity helpers e.g. _ :: Refined (pl
And pr) a -> Refined pr (Refined pl a)
And
associativity helpers e.g. _ :: Refined (pl \
And\ pr) a -> Refined pr (Refined pl a)
raehik
changed the title
Add special
Add special Apr 13, 2024
And
associativity helpers e.g. _ :: Refined (pl \
And\ pr) a -> Refined pr (Refined pl a)
And
associativity helpers e.g. _ :: Refined (pl And pr) a -> Refined pr (Refined pl a)
This makes total sense. Frankly I thought this already existed |
This is how I've defined them in -- | Take just the left predicate from an 'And'.
rerefineAndL :: Refined (And l r) a -> Refined l a
rerefineAndL = unsafeRerefine
-- | Take just the right predicate from an 'And'.
rerefineAndR :: Refined (And l r) a -> Refined r a
rerefineAndR = unsafeRerefine
-- | Eliminate an 'And' by applying the left predicate, then the right.
eliminateAndLR :: Refined (And l r) a -> Refined r (Refined l a)
eliminateAndLR = unsafeRefine . unsafeRefine . unrefine
-- | Eliminate an 'And' by applying the right predicate, then the left.
eliminateAndRL :: Refined (And l r) a -> Refined l (Refined r a)
eliminateAndRL = unsafeRefine . unsafeRefine . unrefine
-- | Introduce an 'And' given a double-'Refined'. Inner is left.
introduceAndLR :: Refined r (Refined l a) -> Refined (And l r) a
introduceAndLR = unsafeRefine . unrefine . unrefine
-- | Introduce an 'And' given a double-'Refined'. Inner is right.
introduceAndRL :: Refined l (Refined r a) -> Refined (And l r) a
introduceAndRL = unsafeRefine . unrefine . unrefine Is that about appropriate to be put into |
This makes total sense to me. A PR would be great. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some background. I use refined all over binrep to define binary representation building blocks. One such building block is
data NullPad (n :: Natural)
/type NullPadded n = Refined (NullPad n)
, for null-padded data. If you want null-padded bytestrings though, you also need them to beNullTerminated
. So I end up withRefined (NullPad n) (Refined NullTerminate a)
.The two
Refined
layers make the rest of my tools harder to use. I could write a custom newtype to work around it. But that goes against refined's design philosophy. And it turns out I can useAnd
to solve it. The instances look something like this:The refines are safe, since I'm just re-associating the predicates. It would be great to have some
And
helpers in the base library to hide these unsafe primitives!The text was updated successfully, but these errors were encountered: