Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing "->"s with "→"s, "=>"s with "⇒"s and so on in Haskell

There are a lot of interesting haskell snippets to be found online. This post could be found under this (awesome) Stack Overflow question. The author writes the following:

discount ∷ Floating α ⇒ α → α → α → α
discount τ df x = x * (1 + df) ** (-τ)

Are those fancy arrows and dots just a way to make the online page look nicer, or is there an actual Haskell extension (or whatever, I don't quite know the terminology) which would compile something like that? I should note that the usual -> is used in the code just as well.

I have a strong feeling it's not the first time I see things like that.

like image 487
Zhiltsoff Igor Avatar asked Feb 07 '21 18:02

Zhiltsoff Igor


2 Answers

There's a GHC extension called UnicodeSyntax that allows some Unicode alternatives for certain syntax. However, in general, Haskell source code is written in Unicode, so non-ASCII characters can be used in plain Haskell source code for identifiers and operators, even without any extension.

In the code snippet you include in your question, the author is using both facilities. They are using UnicodeSyntax to allow the Unicode characters , and in place of the built-in ::, => and -> syntax, but they are using the regular Haskell Unicode support to write α and τ for identifiers.

The following program is valid without any extension:

discount :: Floating α => α -> α -> α -> α
discount τ df x = x * (1 + df) ** (-τ)
like image 121
K. A. Buhr Avatar answered Nov 16 '22 00:11

K. A. Buhr


From the GHC docs:

The language extension UnicodeSyntax enables Unicode characters to be used to stand for certain ASCII character sequences.

like image 36
chi Avatar answered Nov 16 '22 00:11

chi