Search code examples
haskelltwitterghc

Using Emoji in Haskell


I've recently come across a bot on Twitter named EmojiHaskell, that claims to tweet 'interpretable Haskell code with emoji variable names'. A particular Tweet caught my attention, as it looked like malformed syntax to me, so I decided to take a closer look. So far I've produced the following code:

module Main where

šŸ™ :: [šŸ³] -> Maybe šŸ³
šŸ™ [] = Nothing
šŸ™ (šŸ‘½:as) = Just šŸ‘½

main = print $ šŸ™ "ā™„"

Since I've used Ī» on occasion in my Haskell code, I expected this code to work, but it appears that GHC doesn't like the emoji at all.

With $ runhaskell Main.hs I get:

Main.hs:4:1: parse error on input ā€˜šŸ™ā€™

I've already had a look at the UnicodeSyntax extension, and tried to only use some or single emoji instead of all of them to see if a certain one provokes the problem.

Now my question is this: Is there currently a Haskell compiler that would accept the code? Can I get GHC to work with this code somehow?


Solution

  • That code is not valid haskell. The reason is that šŸ™ (like, probably, all Emojis) is a symbol character:

    Prelude> import Data.Char
    Prelude Data.Char> generalCategory 'šŸ™'
    OtherSymbol
    

    But you can still use them like any other symbol, namely as an operator:

    Prelude Data.Char> let (šŸ™) = (+)
    Prelude Data.Char> 32 šŸ™ 42
    74
    

    Furthermore, as user3237465 pointed out, if you use the prefix syntax for operators, i.e. put it in parentheses, you can even use it like any other symbol:

    (šŸ™) :: [a] -> Maybe a
    (šŸ™) [] = Nothing
    (šŸ™) ((šŸ‘½):as) = Just (šŸ‘½)
    
    main = print $ (šŸ™) "ā™„"
    

    This is almost the example in the original post. Unfortunately, this trick does not work for the type variable. The the documentation is worded a bit unfortunately, but in fact symbols are never type variables and always type constructors