16

I would like to remove the if ... then ... else ... keywords, because I am embedding a language/DSL in Haskell. if, then and else convey a lot of meaning in many domains, and it would be great if I could redefine (or leave them undefined) them to reflect the nature of the language/domain.

I've searched on Google and stackoverflow, but found nothing. (I did find an old thread on why if ... then ... else ... was included as keywords in Haskell.)

My IDE is in Leksah, and, if the keywords can be removed, it would also be nice to have a setting to change the if ... then ... else ... keywords back to their normal font/color/unbold.


I've already tried a naming convention of if' for if and so on. It doesn't feel as good, especially when I want to define if and if', and have to define if' and if'' instead, or if1 and if2. The presence of both if' and if might also be confusing. (The confusion is not that serious an issue in my situation as the users of the DSL are Haskell programmers, but I suppose it can help in other situations).


Summarizing the responses to date:

  • Use the RebindableSyntax extension to GHC. Not as general as removing the keywords: the syntax of Haskell's if-then-else is retained. (Frerich Raabe)
  • Workaround: Use very similar words/names, by using data Conditional b a = If b (Then a) (Else a) (only applicable in some contexts). (C. A. McCann)

If RebindableSyntax is a relatively new feature, then it's unlikely to find a more general way, at least not till the next version of GHC.

Dingfeng Quek
  • 898
  • 5
  • 14
  • Do you actually want to _remove_ keywords of a language so that the compiler can treat them as variables? – eternalmatt Aug 09 '11 at 12:55
  • 3
    If your DSL is sufficiently distinct from the surrounding Haskell, and works by building some sort of data structure representing expressions in the DSL, you could also just use a data type. e.g., something like `data Conditional b a = If b (Then a) (Else a)`. – C. A. McCann Aug 09 '11 at 13:13
  • You should remember that you shouldn't design your DSL's to make code look like English, you should design them to make it easy to see if the code doesn't work. – Anonymous Aug 09 '11 at 13:34
  • 1
    Antal: Thanks for the edit. ; camccann: Good suggestion. (And it seems like you have refactored your SO name.) ; eternalmatt: I agree that there are disadvantages when removing keywords, which must be properly considered. However, Haskell's if-then-else is quite unnecessary even as sugar, yet they use up prime short names. – Dingfeng Quek Aug 10 '11 at 05:31

3 Answers3

31

The RebindableSyntax extension to GHC lets you overload if ... then ... else expressions with your own version. In particular, the ifThenElse function is used to define alternative meanings. if e1 then e2 else e3" means ifThenElse e1 e2 e3.

See the blog article Rebindable if..then..else expressions for a nice discussion of this feature, including some examples.

Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
11

You can't remove existing keywords. As was pointed out you can use RebindableSyntax, but that might not do what you want.

The only thing getting close to removing keywords is to turn on the CPP option and doing something like

#define if if_
#define then then_
#define else else_

The preprocessor will then expand if/then/else to if_/then_/else_.

augustss
  • 22,884
  • 5
  • 56
  • 93
0

How about:

cond True  t _ = t
cond False _ f = f
user2023370
  • 10,488
  • 6
  • 50
  • 83
  • 1
    This is missing the point, really. Of course we can implement the same behavior as "if ... then ... else ..." with a different name, but what we really want is a different behavior with the same name. – Daniel Wagner Aug 10 '11 at 01:10