Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Add Generic instance for Maybe #9

Merged
merged 1 commit into from
Jun 3, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Data/Generic/Rep.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module Data.Generic.Rep
, Field(..)
) where

import Data.Maybe (Maybe(..))

-- | A representation for types with no constructors.
data NoConstructors

Expand Down Expand Up @@ -43,3 +45,13 @@ newtype Field (field :: Symbol) a = Field a
class Generic a rep | a -> rep where
to :: rep -> a
from :: a -> rep

instance genericMaybe
:: Generic (Maybe a) (Sum (Constructor "Nothing" NoArguments)
(Constructor "Just" (Argument a))) where
to (Inl _) = Nothing
to (Inr (Constructor (Argument a))) = Just a

from Nothing = Inl (Constructor NoArguments)
from (Just a) = Inr (Constructor (Argument a))