Skip to content

Instantly share code, notes, and snippets.

@emilypi
Created December 17, 2020 13:47
Show Gist options
  • Save emilypi/01d949ef41c44f92d786bb9c2e43353b to your computer and use it in GitHub Desktop.
Save emilypi/01d949ef41c44f92d786bb9c2e43353b to your computer and use it in GitHub Desktop.
{-# 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