Last active
March 29, 2018 22:21
-
-
Save Lysxia/70bfeb67071fdd23f5d68eef7d3664f8 to your computer and use it in GitHub Desktop.
Defunctionalization and generic traversal https://www.reddit.com/r/haskell/comments/884pe0/higherkinded_data_reasonably_polymorphic/
This file contains hidden or 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 DeriveGeneric #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE StandaloneDeriving #-} | |
{-# LANGUAGE TypeApplications #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
import Data.Functor.Identity | |
import GHC.Generics | |
import Generics.OneLiner.Binary | |
-- gvalidate implementation | |
class (a ~ f b) => F f a b | |
instance (a ~ f b) => F f a b | |
gvalidate :: forall t t' f. (ADT t t', Constraints t t' (F f), Applicative f) => t -> f t' | |
gvalidate t = gtraverse @(F f) id t | |
-- HKD | |
type family HKD f a where | |
HKD Identity a = a | |
HKD f a = f a | |
-- Example | |
data Person f = Person | |
{ name :: HKD f String | |
, age :: HKD f Int | |
} deriving Generic | |
deriving instance Show (Person Identity) | |
john :: Person Maybe | |
john = Person (Just "John") (Just 43) | |
main = do | |
print (gvalidate john :: Maybe (Person Identity)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment