Defining an “mempty”-like function with GHC Generics?

爱⌒轻易说出口 提交于 2021-01-28 04:44:45

问题


I am writing a client library for the Zoho REST API and have a bunch of different record types that have all Maybe a fields, i.e:

data Approval = Approval
  { apDelegate :: Maybe Bool
  , apApprove :: Maybe Bool
  , apReject :: Maybe Bool
  , apResubmit :: Maybe Bool
  } deriving (Eq, Show, Generic)

data ContactSpecialFields = ContactSpecialFields
  { csfCurrencySymbol :: Maybe Text -- $currency_symbol
  , csfState :: Maybe Text -- $state
  , csfProcessFlow :: Maybe Bool -- $process_flow
  , csfApproved :: Maybe Bool -- $approved
  , csfApproval :: Approval  -- $approval
  , csfEditable :: Maybe Bool -- $editable
  } deriving (Eq, Show)

-- and so on

I need an way to define "empty" records of such types, eg:

emptyApproval :: Approval
emptyApproval = Approval
  { apDelegate = Nothing
  , apApprove = Nothing
  , apReject = Nothing
  , apResubmit = Nothing
  }

Therefore, I reached out for GHC.Generics and got something working (which is buggy!):

-- These parts seem logically correct to me...

class GEmptyZohoStructure f where
  gEmptyZohoStructure :: f p

instance (GEmptyZohoStructure f, GEmptyZohoStructure g) => GEmptyZohoStructure (f :*: g) where
  gEmptyZohoStructure = (gEmptyZohoStructure :: f p) :*: (gEmptyZohoStructure :: g p)

instance GEmptyZohoStructure Maybe where
  gEmptyZohoStructure = Nothing

class EmptyZohoStructure a where
  emptyZohoStructure :: a

  default emptyZohoStructure :: (Generic a, (GEmptyZohoStructure (Rep a))) => a
  emptyZohoStructure = GHC.Generics.to gEmptyZohoStructure

-- Whereas these parts are random type-class instances that I've written, just 
-- to get the code to compile.

instance (GEmptyZohoStructure f) => GEmptyZohoStructure (M1 i t f) where
  gEmptyZohoStructure = (gEmptyZohoStructure :: f p)

instance (GEmptyZohoStructure f) => GEmptyZohoStructure (K1 i (f p)) where
  gEmptyZohoStructure = gEmptyZohoStructure

instance EmptyZohoStructure Approval

while the code compiles, the following (understandably) results in a stack overflow during runtime:

ghci> emptyZohoStructure  :: Approval
*** Exception: stack overflow

I was following the encode tutorial given at https://www.stackage.org/haddock/lts-12.1/base-4.11.1.0/GHC-Generics.html#g:12 , where due to the argument being passed to the encode function, it allows one the opportunity to unwrap the M1 / K1 constructor and build some meaningful recursion hierarchy. How do I write generic for M1 and K1 for my use-case (where the generic function doesn't really have any arguments)?


回答1:


There is no point defining GEmptyZohoStructure Maybe in a typeclass for generics.

class G f where 
  gempty' :: f p
instance (G f, G g) => G ( f :*: g) where 
  gempty' = gempty' :*: gempty'

instance G c => G (D1 x c) where
  gempty' = M1 gempty'
instance G s => G (C1 x s) where
  gempty' = M1 gempty'    
instance E t => G (S1 m (Rec0 t)) where -- the key instance
  gempty' = M1 (K1 gempty)

class E a where
  gempty :: a
  default gempty :: (Generic a, G (Rep a)) => a
  gempty = to gempty'    
instance E (Maybe a) where 
  gempty = Nothing

After that you can define any product type consisting of Maybe values.



来源:https://stackoverflow.com/questions/59518275/defining-an-mempty-like-function-with-ghc-generics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!