Skip to content

Instantly share code, notes, and snippets.

@Profpatsch
Last active July 8, 2018 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Profpatsch/da32febab011fde0a99c3a2fe34e9c45 to your computer and use it in GitHub Desktop.
Save Profpatsch/da32febab011fde0a99c3a2fe34e9c45 to your computer and use it in GitHub Desktop.
Going from a type implementing Generic1 to a Holey (which can be used to get a Zipper for free)
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, MultiParamTypeClasses, TypeOperators, FlexibleContexts, TypeFamilies #-}
module FunctorCombo.Generic (
encode
) where
import GHC.Generics
import qualified FunctorCombo.Functor as F
class Encode gen where
type DerG gen :: * -> *
encode :: gen a -> DerG gen a
instance Encode (Rec0 c) where
type DerG (Rec0 c) = F.Const c
encode (K1 c) = F.Const c
instance Encode Par1 where
type DerG Par1 = F.Id
encode (Par1 r) = F.Id r
instance Functor f => Encode (Rec1 f) where
type DerG (Rec1 f) = (f F.:. F.Id)
encode (Rec1 fp) = F.O $ fmap F.Id fp
instance Encode (f :+: g) where
type DerG (f :+: g) = (f F.:+: g)
encode (L1 fp) = F.InL fp
encode (R1 fp) = F.InR fp
instance Encode (f :*: g) where
type DerG (f :*: g) = (f F.:*: g)
encode (fp :*: gp) = fp F.:*: gp
instance Encode (f :.: g) where
type DerG (f :.: g) = (f F.:. g)
encode (Comp1 fgp) = F.O fgp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment