Created
November 4, 2021 15:02
-
-
Save purefunctor/813eba800c62c3aeccc512bdd84c08d3 to your computer and use it in GitHub Desktop.
Collecting instantiated type class methods into an array.
This file contains 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
module Main where | |
import Prelude | |
import Data.Array | |
import Debug | |
import Type.Prelude | |
import Type.Row | |
import Type.RowList | |
import Unsafe.Coerce | |
import Partial.Unsafe | |
class Refl a where | |
refl :: a -> a | |
instance Refl Int where | |
refl n = n + 1 | |
instance Refl Number where | |
refl n = n + 1.0 | |
instance Refl String where | |
refl n = n <> "no!" | |
class ReflRowList r where | |
refls :: Proxy r -> Array (forall a. a -> a) | |
instance ReflRowList Nil where | |
refls _ = [] | |
else instance (ReflRowList r, Refl a) => ReflRowList (Cons n a r) where | |
refls _ = unsafeCoerce [refl :: a -> a] <> refls (Proxy :: _ r) | |
type R = Cons "a" Int (Cons "b" Number (Cons "c" String Nil)) | |
z = spy "help" (refls (Proxy :: _ R)) | |
i = spy "int" $ (unsafePartial $ unsafeIndex z 0) 68 | |
n = spy "number" $ (unsafePartial $ unsafeIndex z 1) 419.0 | |
s = spy "string" $ (unsafePartial $ unsafeIndex z 2) "oh " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment