Last active
March 21, 2020 18:37
-
-
Save danidiaz/ca04d1236b91e19df94f75385035caae to your computer and use it in GitHub Desktop.
Maybeize a product
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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