Open julianhyde opened 2 years ago
same
is not just for within from
. Here is its use in a function:
fun f (1, 1) = 0
| f (x, same x) = x
| f (x, y) = x + y;
val f = fn : int * int -> int
f (1, 1);
val it = 0: int;
f (2, 2);
val it = 2: int;
fun (2, 3);
val it = 5: int;
In [Successor ML](), you could achieve a similar effect with a pattern guard (if
):
fun f (1, 1) = 0
| f (x, x2 if x = x2) = x
| f (x, y) = x + y;
(The effect is not identical: the pattern guard (x, x2 if x = x2)
would add x
and x2
to scope, whereas (x, same x)
would only add x
to scope.)
But in Standard ML and current Morel, you would have to expand as follows:
- fun f (1, 1) = 1
= | f (x, y) = if (x = y) then x else x + y;
val f = fn : int * int -> int
- f (1, 1);
val it = 1 : int
- f (2, 2);
val it = 2 : int
- f (2, 3);
val it = 5 : int
It would be useful to add syntactic sugar to re-use a variable in
from
, constraining the new variable. Consider the following expression to compute all paths of length 2:It is inconvenient to have to declare
y2
and then immediately constrain it to equaly
. With the newsame
operator, the following would be equivalent: