Skip to content

Instantly share code, notes, and snippets.

@danidiaz
Last active March 21, 2020 18:37
Show Gist options
  • Select an option

  • Save danidiaz/ca04d1236b91e19df94f75385035caae to your computer and use it in GitHub Desktop.

Select an option

Save danidiaz/ca04d1236b91e19df94f75385035caae to your computer and use it in GitHub Desktop.
Maybeize a product
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wno-partial-type-signatures #-}
module Main where
import qualified GHC.Generics
import Generics.SOP
import Generics.SOP.Constraint
import Generics.SOP.NP
maybeizeProduct :: forall r xs r' ys. (IsProductType r xs, IsProductType r' ys, AllZip Maybeized xs ys) => r -> r'
maybeizeProduct = productTypeTo @r' . trans_NP (Proxy @Maybeized) (mapII maybeize) . productTypeFrom @r
class (b ~ Maybe a) => Maybeized a b where
maybeize :: a -> b
instance Maybeized a (Maybe a) where
maybeize = Just
main :: IO ()
main = do
print $ (maybeizeProduct ('a', 'b') :: (_,_))
print $ (maybeizeProduct ('a', 'b', 'c') :: (_,_,_))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment