Cheddar icon indicating copy to clipboard operation
Cheddar copied to clipboard

Proposal: Pattern Matching

Open vihanb opened this issue 9 years ago • 4 comments

(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

vihanb avatar Sep 22 '16 04:09 vihanb

When will where ever be used?

ConorOBrien-Foxx avatar Sep 23 '16 23:09 ConorOBrien-Foxx

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?

ConorOBrien-Foxx avatar Sep 24 '16 18:09 ConorOBrien-Foxx

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

somebody1234 avatar Sep 25 '16 05:09 somebody1234

@somebody1234 ah, of course.

ConorOBrien-Foxx avatar Sep 28 '16 21:09 ConorOBrien-Foxx