<commit_before>{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE PolyKinds #-}

-- | Higher-order (and poly-kinded) implementation of Data Types à la Carte [1]
--
-- \[1\] <NAME>. Data Types à la Carte.
--       /Journal of Functional Programming/, 18(4):423-436, 2008,
--       <http://dx.doi.org/10.1017/S0956796808006758>.

module Data.ALaCarte where



-- | Coproducts
data (f :+: g) a b
    = Inl (f a b)
    | Inr (g a b)
  deriving (Functor)

infixr :+:

-- | A constraint @f `:<:` g@ expresses that the signature @f@ is subsumed by @g@, i.e. @f@ can be
-- used to construct elements in @g@.
class f :<: g
  where
    inj :: f a b -> g a b

instance (f :<: f)
  where
    inj = id

instance (f :<: (f :+: g))
  where
    inj = Inl

instance (f :<: h) => (f :<: (g :+: h))
  where
    inj = Inr . inj
<commit_msg>Added prj method to :<:<commit_after>{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE PolyKinds #-}

-- | Higher-order (and poly-kinded) implementation of Data Types à la Carte [1]
--
-- \[1\] <NAME>. Data Types à la Carte.
--       /Journal of Functional Programming/, 18(4):423-436, 2008,
--       <http://dx.doi.org/10.1017/S0956796808006758>.

module Data.ALaCarte where



-- | Coproducts
data (f :+: g) a b
    = Inl (f a b)
    | Inr (g a b)
  deriving (Functor)

infixr :+:

-- | A constraint @f `:<:` g@ expresses that the signature @f@ is subsumed by @g@, i.e. @f@ can be
-- used to construct elements in @g@.
class f :<: g
  where
    inj :: f a b -> g a b
    prj :: g a b -> Maybe (f a b)

instance (f :<: f)
  where
    inj = id
    prj = Just

instance (f :<: (f :+: g))
  where
    inj = Inl
    prj (Inl f) = Just f
    prj _       = Nothing

instance (f :<: h) => (f :<: (g :+: h))
  where
    inj = Inr . inj
    prj (Inr h) = prj h
    prj _       = Nothing

{'fraction_non_alphanumeric': 0.17333333333333334, 'fraction_numerical': 0.04111111111111111, 'mean_word_length': 2.2567811934900543, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1508613', 'n_tokens_mistral': 798, 'n_tokens_neox': 737, 'n_words': 242}