I’m using purescript-heterogeneous to show the contents of a homogeneous records of strings.
myRec = { a: "x", b: "y" }
showRec1 myRec == ",x,y"
showRec3 myRec == ",a=x,b=y"
I’m attempting write a showRec2 function that produces the same output as showRec3 (with labels), but written in the simpler style of showRec1 to avoid needing a ShowStringProps instance.
Do I just have a minor typo in showRec2, or are custom instances required to use hfoldlWithIndex?
module Main where
import Prelude
import Effect (Effect)
import Effect.Console (log)
import Heterogeneous.Folding (class FoldingWithIndex, class HFoldl, class HFoldlWithIndex, hfoldl, hfoldlWithIndex)
import Data.Symbol (class IsSymbol, SProxy, reflectSymbol)
myRec :: { a :: String, b :: String }
myRec = { a: "x", b: "y" }
showRec1 :: forall r. HFoldl (String -> String -> String) String r String => r -> String
showRec1 r =
let
f :: String -> String -> String
f acc val = acc <> "," <> val
in
hfoldl f "" r
showRec2 :: forall sym r. IsSymbol sym => HFoldlWithIndex (SProxy sym -> String -> String -> String) String r String => r -> String
showRec2 r =
let
f :: forall sym. IsSymbol sym => SProxy sym -> String -> String -> String
f sym acc val = acc <> "," <> reflectSymbol sym <> "=" <> val
in
hfoldlWithIndex f "" r
data ShowStringProps
= ShowStringProps
instance showStringProps :: (IsSymbol sym) => FoldingWithIndex ShowStringProps (SProxy sym) String String String where
foldingWithIndex ShowStringProps sym acc val = acc <> "," <> reflectSymbol sym <> "=" <> val
showRec3 :: forall r. HFoldlWithIndex ShowStringProps String r String => r -> String
showRec3 r = hfoldlWithIndex ShowStringProps "" r
main :: Effect Unit
main = do
log $ showRec1 myRec
log $ showRec2 myRec
log $ showRec3 myRec
Here’s my compilation error:
Error found:
in module Main
at src/Main.purs:26:5 - 26:22 (line 26, column 5 - line 26, column 22)
No type class instance was found for
Heterogeneous.Folding.HFoldlWithIndex (SProxy t4 -> String -> String -> String)
String
r5
String
The instance head contains unknown type variables. Consider adding a type annotation.
while applying a function hfoldlWithIndex
of type HFoldlWithIndex t0 t1 t2 t3 => t0 -> t1 -> t2 -> t3
to argument f
while inferring the type of hfoldlWithIndex f
in value declaration showRec2
where r5 is a rigid type variable
but if you want to quickly hack on some homogeneous record you can find 