Skip to content

Instantly share code, notes, and snippets.

@purefunctor
Created November 4, 2021 15:02
Show Gist options
  • Save purefunctor/813eba800c62c3aeccc512bdd84c08d3 to your computer and use it in GitHub Desktop.
Save purefunctor/813eba800c62c3aeccc512bdd84c08d3 to your computer and use it in GitHub Desktop.
Collecting instantiated type class methods into an array.
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