Utilities
WTy2's syntax is designed for allowing defining functions that appear similar to built-in language constructsof other languages. Below are a few examples:
Do
Inspired by Haskell
do(f: () -> t): t = f()
This is primarily useful when defining functions in f(x: t): u = ...
-style where you still want to use a block to define locals or use do
-notation sugar 1. E.g:
foo(x: Int, y: Int): Int = do {
z := x * 3;
w := y - 2;
(z * z + w) / (x + w)
}
If-Elif-Else
Inspired by oh-so-many programming languages, maybe Algol 60 was first?
if[t: Type](c: Bool, e: () -> t): Maybe(t) = with(c) {
| True -> Just(e()),
| False -> Nothing
}
elif[t: Type](x: Maybe(t), c: Bool, e: () -> t): Maybe(t) = with(x, c) {
| (Just(y), _) |-> x,
| (Nothing, True) |-> Just(e()),
| (Nothing, False) |-> Nothing,
}
else[t: Type](x: Maybe(t), e: () -> t): t = with(x) {
| Just(y) |-> y,
| Nothing |-> e()
}
E.g:
_ := if(True) {
"Case one!"
}.elif(False) {
"Case two!"
}.else {
"Case three!"
}
Fun
Inspired by Kotlin
fun[r: Type](t: Type, f: t -> r): t -> r = f
In WTy2, serves to annotate the argument type of a function without having to use arrow-lambda syntax.
Lazy
Inspired by Scala
lazy[t: Type](f: () -> t): () -> t = f
Note this does not perform any memoisation (call-by-need). This would require some form of mutability to implement.
The
Inspired by Idris
the(t: Type, x: t): t = x
Annotates the type of x
.
With and Also
Inspired by Kotlin
with[t: Type, r: Type](x: t, f: t -> r): r = f(x)
also[t: Type, m: Applicative](x: t, f: t -> m()): m(t) = f(x) $> x
with
combines well with lambda-case syntax as a way to pattern match on a variable. E.g:
_ := with(x) {
| 0 |-> "Zero!",
| 1 |-> "One!",
| _ |-> "Other!"
}
Tagged
tagged[a: Type](f: a ~> Any): Type = [x: a] 'f(x)
Returns the set of all values tagged with the constructor.
Impl
impl(t: Type): Type = [u: t] u
The union of all types which implement an open type.
1 Of course in WTy2, do
-notation is not really related to the do
utility defined here; it can be used in any block. "Braces-notation" doesn't quite roll of the tongue though...