Improved Type-Level FizzBuzz


This is a slight improvment of the code in my previous post A Preliminary Attempt at Type-Level FizzBuzz. The credit goes to my friend Shulhi Sapli. He took a look at my code and came up with a cleaner solution. This part should be straightforward if you were able to folow the code from the previous post.

ConcatSymbols is a type-level function to concat each Symbol with a line break in between each value. That way we can transform a list of Nat with NatToSym and ConcatSymbols into a single Symbol, turn it into a String and print it.

The nicest part is he found a solution to type-level function mapping. He got the idea from (Thinking with Types Type-Level Programming in Haskell)[] in chapter 10. This type-level technique is called defunctionalization, which allows for higher order type-level functions in Haskell.

Now we just need a list of Nats and we can map FizzBuzz and ConcatSymbols to get a Symbol. Much cleaner than the previous version.

One thing I have not been able to do is create a working Range type-level function that takes two Nat and returns all the numbers between them in a list. The following compiles.

I want to compile and run the following, but it seems to get caught an infinite loop. The compiler suggest using the GHC flag -freduction-depth=0, but it does not seem to help. Even for small values it gets stuck.