Created
December 17, 2020 13:47
-
-
Save emilypi/01d949ef41c44f92d786bb9c2e43353b to your computer and use it in GitHub Desktop.
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
{-# language TypeOperators #-} | |
{-# language PatternSynonyms #-} | |
{-# language MultiParamTypeClasses #-} | |
{-# language FlexibleContexts #-} | |
{-# language FlexibleInstances #-} | |
{-# language ViewPatterns #-} | |
module Sub where | |
import Control.Lens | |
class (sub :: k -> *) :<: (sup :: k -> *) where | |
subP :: Prism' (sup a) (sub a) | |
-- | Inject a subtype into a supertype via 'subP' | |
inject :: sub :<: sup => sub a -> sup a | |
inject = review subP | |
-- | Try to project a subtype from a supertype via 'subP' | |
project :: sub :<: sup => sup a -> Maybe (sub a) | |
project = preview subP | |
instance f :<: f where | |
subP = id | |
pattern Inj :: sub :<: sup => sub a -> sup a | |
pattern Inj a <- (project -> Just a) where | |
Inj a = inject a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment