Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
Last active March 17, 2018 21:30
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 sjoerdvisscher/050ceb08b6fcb68cda64d5b5eac7e235 to your computer and use it in GitHub Desktop.
Save sjoerdvisscher/050ceb08b6fcb68cda64d5b5eac7e235 to your computer and use it in GitHub Desktop.
Zeros implemented with one-liner
{-# LANGUAGE DeriveAnyClass, DerivingStrategies, DefaultSignatures, TypeApplications, FlexibleContexts, DeriveGeneric #-}
import GHC.Generics
import Generics.OneLiner (nullaryOp, ADTRecord, Constraints)
class Zeros z where
zero :: z
default zero :: (ADTRecord z, Constraints z Zeros) => z
zero = nullaryOp @Zeros zero
{-# INLINE zero #-}
data B1 = B1 Int
deriving stock (Show, Read, Eq, Ord, Generic)
deriving anyclass (Zeros)
instance Zeros Int where zero = 0
test :: B1
test = zero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment