cheddar-lang / Cheddar

🧀 Cheddar, the language that works for you
cheddar.vihan.org
Apache License 2.0
28 stars 9 forks source link

Proposal: Pattern Matching #77

Open vihanb opened 8 years ago

vihanb commented 8 years ago

(draft) This proposal details the workings of the planned let f :: pattern matching statement

Overview

The pattern matching definition allows behavior to be assigned independently for given instance depending upon the operands.

To avoid ambiguation with let ... =, the let f :: syntax is taken as an alternative requiring the function to provide a specific type signature outlining at minimum it's I/O.

Due to Cheddar's design philosophy to provide freedom, a specific type signature is not enforced and any -> any as a generic signature fulfills the I/O requirements for type enforcement.

The functional type signatures are targeted to be able to be provided within any type annotation.

Examples

let f :: Number -> Number
f(n < 2) = 1
f(n) = f(n - 1) + f(n - 2)

Illustrating the where clause when multiple variables are used requiring a statement for disambiguation:

let x: number = 2
let f :: Number -> Number
f(n where n < x) -> 1
f(n) -> f(n - 1) + f(n - 2)

Illustrating multiple arguments:

let f :: Number -> nil
f(a, b where true) -> nil

Functional type annotation examples (separate proposal but inlining for sake of completeness):

Number -> Number
(any -> Number) -> Number
Number -> (Number -> Number)
Number -> [Number, Number]

Illustrating un-proposed generics:

Number -> MyClass<Number>

Definition

TODO

Formal Grammar

TODO

ConorOBrien-Foxx commented 8 years ago

When will where ever be used?

ConorOBrien-Foxx commented 8 years ago

Also, how would I define pattern matching funcs with more than one arg? Ones that aren't strictly typed? One where the input is strictly typed, but the output not? Vice versa?

somebody1234 commented 8 years ago

@ConorOBrien-Foxx where will be needed when both sides are a variable, in which case it is unclear which one is the argument name.

ConorOBrien-Foxx commented 8 years ago

@somebody1234 ah, of course.