Skip to content

Instantly share code, notes, and snippets.

@danidiaz
Created July 5, 2022 19:29
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 danidiaz/dba25d721ac43d52ec8183a129dbf903 to your computer and use it in GitHub Desktop.
Save danidiaz/dba25d721ac43d52ec8183a129dbf903 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
-- https://twitter.com/ChShersh/status/1544240982559948801
module Main where
import Data.Kind (Type)
import Data.RBR -- from package "red-black-record"
( Deletable (Delete),
FromList,
I,
Record,
Variant,
injectI,
insert,
unit,
)
import Data.RBR.Subset (projectSubset)
import GHC.TypeLits (Symbol)
type MyTypeTypes = ['("a", Int), '("b", Bool), '("c", Char), '("d", Float)]
type MyTypeTree = FromList MyTypeTypes
type family Expand (x :: [(Symbol, Type)]) :: [(Symbol, Type)] where
Expand '[] = '[]
Expand ('(k, v) ': ts) = '(k, (v, Record Maybe (Delete k v MyTypeTree))) ': Expand ts
type MyTypeTreeExpanded = FromList (Expand MyTypeTypes)
type MyType = Variant I MyTypeTreeExpanded
myVal :: MyType
myVal =
injectI @"b" $
( True,
projectSubset $
insert @"a" @Int (Just 3) $ insert @"c" @Char (Just 'c') $ insert @"d" @Float Nothing $ unit
)
main :: IO ()
main = print myVal
-- fromNS (S (Z (I (True,fromNP (Just 3 :* Just 'c' :* Nothing :* Nil)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment