Let's say I have a convention in Haskell where I define a series of functions like this:
data Node = MkNode
s0 :: Node -> s -> Node
s0 a _ = a
s1 :: (s -> a) -> (a -> Node) -> s -> Node
s1 a b c = b (a c)
s2 :: (s -> a) -> (s -> b) -> (a -> b -> Node) -> s -> Node
s2 a b c d = c (a d) (b d)
s3 :: (s -> a) -> (s -> b) -> (s -> c) -> (a -> b -> c -> Node) -> s -> Node
s3 a b c d e = d (a e) (b e) (c e)
If possible, I would love to define a function sn that takes a variable number of arguments, always with this pattern. I've seen this sort of thing done before using typeclasses, but I can't quite figure out how to do it in this case. For example, I can imagine:
class NAble elt where
sn :: elt -> state -> Node
instance NAble Node where
sn elt _ = elt
But then I'm stuck. I'm not sure what the recursive definition would be. Perhaps something like:
instance (NAble b) => NAble (a -> b) where
sn eltMaker state = ss (eltMaker state) state
But that's obviously not quite right. Not sure if this is possible, but it would be cool if it were. Of course the order of the arguments can change if that helps get it right, but it'd be really nice to get this to work. Any help would be appreciated!
ashould be an arbitrary argument thateltMakercan consume in order to create anNAble b.HList '[s -> a, s -> b, s -> c, …] -> (HList '[a, b, c, …] -> Node) -> s -> Node. Again, not sure if this will make your task any easier, but I think it’s worth a try.