-
Notifications
You must be signed in to change notification settings - Fork 48
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
Restricting and weakening CoRec #120
Comments
Is |
Ah I missed |
Alright, so I did manage to implement class RIndex t ts ~ i => FMatch1 t ts i where
fmatch1' :: Handler r (f t) -> Rec (Maybe :. f) ts -> Either r (Rec (Maybe :. f) (RDelete t ts))
instance FMatch1 t (t ': ts) 'Z where
fmatch1' _ (Compose Nothing :& xs) = Right xs
fmatch1' (H h) (Compose (Just x) :& _) = Left (h x)
instance (FMatch1 t ts i, RIndex t (s ': ts) ~ 'S i,
RDelete t (s ': ts) ~ (s ': RDelete t ts))
=> FMatch1 t (s ': ts) ('S i) where
fmatch1' h (x :& xs) = (x :&) <$> fmatch1' h xs
-- | Handle a single variant of a 'CoRec': either the function is
-- applied to the variant or the type of the 'CoRec' is refined to
-- reflect the fact that the variant is /not/ compatible with the type
-- of the would-be handler
fmatch1 :: (FMatch1 t ts (RIndex t ts),
RecApplicative ts,
FoldRec (RDelete t ts) (RDelete t ts))
=> Handler r (f t)
-> CoRec f ts
-> Either r (CoRec f (RDelete t ts))
fmatch1 h = fmap (fromJust . firstField)
. fmatch1' h
. coRecToRec
restrictCoRec :: (RecApplicative ts, FoldRec ts ts) => CoRec f (t ': ts) -> Either (f t) (CoRec f ts)
restrictCoRec = fmatch1 (H id) |
That said, my overall goal was to implement newtype Flap a f = Flap (f a)
newtype Union ts a = Union (CoRec (Flap a) ts) I found instances based on |
I'd be interested in discussing how to avoid bouncing between a Here's another take on these functions: restrictCoRec :: forall t ts f. (RecApplicative ts, FoldRec ts ts)
=> CoRec f (t ': ts) -> Either (f t) (CoRec f ts)
restrictCoRec = go . coRecToRec
where go :: Rec (Maybe :. f) (t ': ts) -> Either (f t) (CoRec f ts)
go (Compose Nothing :& xs) = Right (fromJust (firstField xs))
go (Compose (Just x) :& _) = Left x
weakenCoRec :: (RecApplicative ts, FoldRec (t ': ts) (t ': ts))
=> CoRec f ts -> CoRec f (t ': ts)
weakenCoRec = fromJust . firstField . (Compose Nothing :&) . coRecToRec The use of
If these seem okay, we can add them and see where that leads. |
I'm having trouble figuring out how to write the following functions:
They seem like they'd be good additions to this library, but I'm not sure if it's possible to implement them with the current CoRec implementation.
The text was updated successfully, but these errors were encountered: