-
Notifications
You must be signed in to change notification settings - Fork 73
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
Membership proof rewrite, membership testing, compose effect #282
Merged
KingoftheHomeless
merged 11 commits into
polysemy-research:master
from
KingoftheHomeless:unionRewrite
Dec 8, 2019
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a339f77
trash
KingoftheHomeless c36f912
trash
KingoftheHomeless 3e0cdcf
Union rewrite
KingoftheHomeless a17f525
Exports, tests, and renamed KnownEffectRow
KingoftheHomeless bde0345
Got rid of artifacts accidently introduced
KingoftheHomeless a6f9100
More documentation. tryMembership seperate from KnownRow
KingoftheHomeless 69251eb
'expose' combinator
KingoftheHomeless a96cab0
Applied review suggestions, add Membership module
KingoftheHomeless a95e58a
Fixed a replace-all goof
KingoftheHomeless ce43b91
Scrap expose/Using in favor of interceptUsing/H
KingoftheHomeless 7f5dca4
Fixed Haddock failure
KingoftheHomeless File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{-# LANGUAGE AllowAmbiguousTypes #-} | ||
module Polysemy.Bundle | ||
( -- * Effect | ||
Bundle (..) | ||
-- * Actions | ||
, sendBundle | ||
, injBundle | ||
-- * Interpretations | ||
, runBundle | ||
, subsumeBundle | ||
-- * Miscellaneous | ||
, KnownList | ||
) where | ||
|
||
import Polysemy | ||
import Polysemy.Internal | ||
import Polysemy.Internal.Bundle | ||
import Polysemy.Internal.Union | ||
|
||
------------------------------------------------------------------------------ | ||
-- | An effect for collecting multiple effects into one effect. | ||
-- | ||
-- Useful for effect newtypes. | ||
data Bundle r m a where | ||
Bundle :: ElemOf e r -> e m a -> Bundle r m a | ||
|
||
------------------------------------------------------------------------------ | ||
-- | Injects an effect into a 'Bundle'. Useful together with 'transform'. | ||
injBundle :: forall e r m a. Member e r => e m a -> Bundle r m a | ||
injBundle = Bundle membership | ||
{-# INLINE injBundle #-} | ||
|
||
------------------------------------------------------------------------------ | ||
-- | Send uses of an effect to a 'Bundle' containing that effect. | ||
sendBundle | ||
:: forall e r' r a | ||
. (Member e r', Member (Bundle r') r) | ||
=> Sem (e ': r) a | ||
-> Sem r a | ||
sendBundle = hoistSem $ \u -> case decomp u of | ||
Right (Weaving e s wv ex ins) -> | ||
injWeaving $ | ||
Weaving (Bundle (membership @e @r') e) s (sendBundle @e @r' . wv) ex ins | ||
Left g -> hoist (sendBundle @e @r') g | ||
{-# INLINE sendBundle #-} | ||
|
||
------------------------------------------------------------------------------ | ||
-- | Run a @'Bundle' r@ by prepending @r@ to the effect stack. | ||
runBundle | ||
:: forall r' r a | ||
. KnownList r' | ||
=> Sem (Bundle r' ': r) a | ||
-> Sem (Append r' r) a | ||
runBundle = hoistSem $ \u -> hoist runBundle $ case decomp u of | ||
Right (Weaving (Bundle pr e) s wv ex ins) -> | ||
Union (extendMembership @_ @r pr) $ Weaving e s wv ex ins | ||
Left g -> weakenList @r' @r g | ||
{-# INLINE runBundle #-} | ||
|
||
------------------------------------------------------------------------------ | ||
-- | Run a @'Bundle' r@ if the effect stack contains all effects of @r@. | ||
subsumeBundle | ||
:: forall r' r a | ||
. Members r' r | ||
=> Sem (Bundle r' ': r) a | ||
-> Sem r a | ||
subsumeBundle = hoistSem $ \u -> hoist subsumeBundle $ case decomp u of | ||
Right (Weaving (Bundle pr e) s wv ex ins) -> | ||
Union (subsumeMembership pr) (Weaving e s wv ex ins) | ||
Left g -> g | ||
{-# INLINE subsumeBundle #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{-# LANGUAGE AllowAmbiguousTypes #-} | ||
module Polysemy.Internal.Bundle where | ||
|
||
import Data.Proxy | ||
import Polysemy | ||
import Polysemy.Internal.Union | ||
|
||
type family Append l r where | ||
Append (a ': l) r = a ': (Append l r) | ||
Append '[] r = r | ||
|
||
extendMembership :: forall r r' e. ElemOf e r -> ElemOf e (Append r r') | ||
extendMembership Here = Here | ||
extendMembership (There e) = There (extendMembership @_ @r' e) | ||
{-# INLINE extendMembership #-} | ||
|
||
subsumeMembership :: forall r r' e. Members r r' => ElemOf e r -> ElemOf e r' | ||
subsumeMembership Here = membership @e @r' | ||
subsumeMembership (There (pr :: ElemOf e r'')) = subsumeMembership @r'' @r' pr | ||
{-# INLINE subsumeMembership #-} | ||
|
||
weakenList :: forall r' r m a | ||
. KnownList r' | ||
=> Union r m a | ||
-> Union (Append r' r) m a | ||
weakenList u = unconsKnownList @_ @r' u (\_ (_ :: Proxy r'') -> weaken (weakenList @r'' u)) | ||
{-# INLINE weakenList #-} | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | A class for type-level lists with a known spine. | ||
-- | ||
-- This constraint is eventually satisfied as @r@ is instantied to a | ||
-- concrete list. | ||
class KnownList (l :: [k]) where | ||
unconsKnownList :: (l ~ '[] => a) | ||
-> ( forall x r | ||
. (l ~ (x ': r), KnownList r) | ||
=> Proxy x | ||
-> Proxy r | ||
-> a | ||
) | ||
-> a | ||
|
||
instance KnownList '[] where | ||
unconsKnownList b _ = b | ||
{-# INLINE unconsKnownList #-} | ||
|
||
instance KnownList r => KnownList (x ': r) where | ||
unconsKnownList _ b = b Proxy Proxy | ||
{-# INLINE unconsKnownList #-} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a way to generalize this with the new machinery? I've always hated having to write a few blessed
raise
functions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, there's nothing that the new membership proof machinery can help on this point.
This should be implementable, though: