The Singleton Type Fake Keyword Trick
blog.plover.com/prog/haskell/fake-keyword.htmlPosted Aug 8, 2018, 6:35 PM UTC
Posted Aug 8, 2018, 6:35 PM UTC
do can be used to wrap any expression
Prelude> 1 * do 2 + 3
5
With BlockArguments you will be able to do the
while cond do
whatever
example right out of the box!
You still need to use this trick for the rest, though. ;)
Hooray for BlockArguments
In Agda, you can define a mixfix operator, e.g.
if_then_else_ : {A : Set} -> Bool -> A -> A -> A
if true then t else f = t
if false then t else f = f
or while_do_ likewise.
There are also syntax declarations which allow to define custom binding structures.
I've definitely done this before, but only in hobby projects a long time ago. It's not unique to haskell though, I picked it up from lisp where it's been around probably longer than I have.
It's even in R5RS Scheme's minimalist syntax-rules macro system.
I'd rather use newtypes around the arguments I want to label.
esqueleto does a simpler version of this to emulate the sytnax of case expressions:
case_
[ when_ <condition> then_ <value>
, ...
]
(else_ <alternative>)
when_ has three arguments. The second argument is of type unit, and then_ is an alias for ()
Along with the idiom bracket example, you can look at the HList package, which provides similar "syntax" for heterogeneous lists.
Speaking of fake keywords, I wrote this a while ago and it surprises me as each of the following three functions makes sense on its own
But when combined gives a neat approximation of
if-then-elsewhere every sub-expression means something:What precedence makes this work right with chained conditions? Like:
else_is also?:fromerrors.