lambda-case

37 views
Skip to first unread message

Madis

unread,
Jul 23, 2012, 6:07:16 PM7/23/12
to yeti...@googlegroups.com

Inspired by the GHC lambda-case, I implemented function pattern matching
in Yeti. In essence it allows writing case pattern match nested in the do
function literal in shorter form:

do arg:
case arg of
variants...
esac
done

can be expressed as

\case of
variants...
esac

This is quite similar to OCaml function patterns, for example the
following OCaml code:

function
| `Int n -> n
| `Str s -> int_of_string s

can be written in Yeti now as:

\case of
Int n: n;
Str s: number s;
esac

Although formally introducting a new syntax, it should be easy to
recognize it as a lambda (\) doing a pattern match (case of).
The implementation is available in the current snapshot jar (and git).

http://linux.ee/~mzz/yeti/intro.html#function-pattern-matching
http://hackage.haskell.org/trac/ghc/ticket/7081
https://github.com/mth/yeti/compare/c242dcd895...4132825747
http://linux.ee/~mzz/yeti/yeti.jar

Christian Essl

unread,
Jul 27, 2012, 3:50:26 AM7/27/12
to yeti...@googlegroups.com
Hi Madis,

Thanks for this addition and your ongoing work.

I was wondering whether generally lamba could expose its argument so
that (\...) is syntactic sugar for do x: ... done, where 'x' is the
binding defined by the language specification (could be of course
something else)

ie

\if x then 1 else 0 fi

is same as

do x: if x then 1 else 0 fi

or

\case x of
Some f: f;
esac

is same as

do x:
case x of
Some f: f;
esac
done

Madis Janson

unread,
Jul 30, 2012, 9:41:28 AM7/30/12
to yeti...@googlegroups.com

On Fri, 27 Jul 2012, Christian Essl wrote:

> Hi Madis,
>
> Thanks for this addition and your ongoing work.
>
> I was wondering whether generally lamba could expose its argument so
> that (\...) is syntactic sugar for do x: ... done, where 'x' is the
> binding defined by the language specification (could be of course
> something else)
>
> ie
>
> \if x then 1 else 0 fi
>
> is same as
>
> do x: if x then 1 else 0 fi done

I'm not a big fan of magic bindings popping out of nowhere. Binding to _
would be an option, like \if _ then 1 else 0 fi, but there is already the
(_ x = if x then 1 else 0 fi) variant of lambda. I'm not sure that
extending \ this way would be really needed.

Christian Essl

unread,
Jul 30, 2012, 10:29:27 AM7/30/12
to yeti...@googlegroups.com
Well that's fine for me I just thought that would be a generalization
of the lambda-case, which would also cover the lambda-case with one
more letter.

chrisichris

unread,
Sep 29, 2014, 8:57:55 AM9/29/14
to yeti...@googlegroups.com, ma...@cyber.ee
How about using '?' as predefined binding.

>\if ? == 1 then "one" else "not one" fi 1

It's no must however I just was happy that at least some haking of mine works after the nice work of Mateusz and it works when changing the Sym("_") to Sym("?") YetiAnalyzer.java line 68.

:) Chris
Reply all
Reply to author
Forward
0 new messages