GH-196 pattern matching lhs #701

Merged
zxq9 merged 6 commits from GH-196-pattern-matching-lhs into lima 2020-01-15 17:41:04 +09:00
zxq9 commented 2020-01-14 20:36:07 +09:00 (Migrated from gitlab.com)

Created by: UlfNorell

Fixes GH-196

Some examples:

  function from_some(Some(x)) = x

  function
    length : list('a) => int
    length([])      = 0
    length(_ :: xs) = 1 + length(xs)

  function
    append([], ys)      = ys
    append(x :: xs, ys) = x :: append(xs, ys)
*Created by: UlfNorell* Fixes GH-196 Some examples: ```sophia function from_some(Some(x)) = x function length : list('a) => int length([]) = 0 length(_ :: xs) = 1 + length(xs) function append([], ys) = ys append(x :: xs, ys) = x :: append(xs, ys) ```
zxq9 commented 2020-01-14 22:31:25 +09:00 (Migrated from gitlab.com)

Created by: tolbrino

Review: Approved

This is awesome. Does it have the same efficiency footprint as the switch statement?

*Created by: tolbrino* **Review:** Approved This is awesome. Does it have the same efficiency footprint as the `switch` statement?
zxq9 commented 2020-01-14 22:35:12 +09:00 (Migrated from gitlab.com)

Created by: UlfNorell

After ac9e9716cb it compiles to exactly the same code. The type checker desugars it to a switch on the tuple of all the arguments, so there's a little work to be done by the backend to get rid of the extra matching-a-tuple-against-a-tuple.

For append the desugaring is

function append(arg1, arg2) =
  switch((arg1, arg2))
    ([], ys) => ys
    (x :: xs, ys) => x :: append(xs, ys)

and the backend simplifies it to

function append(arg1, arg2) =
  switch(arg1)
    [] => arg2
    x :: xs => x :: append(xs, arg2)
*Created by: UlfNorell* After ac9e9716cb43370eba106762ac3fc3442d3cd60a it compiles to exactly the same code. The type checker desugars it to a switch on the tuple of all the arguments, so there's a little work to be done by the backend to get rid of the extra matching-a-tuple-against-a-tuple. For `append` the desugaring is ```sophia function append(arg1, arg2) = switch((arg1, arg2)) ([], ys) => ys (x :: xs, ys) => x :: append(xs, ys) ``` and the backend simplifies it to ```sophia function append(arg1, arg2) = switch(arg1) [] => arg2 x :: xs => x :: append(xs, arg2) ```
zxq9 commented 2020-01-15 17:13:18 +09:00 (Migrated from gitlab.com)

Created by: hanssv

Review: Approved

*Created by: hanssv* **Review:** Approved
zxq9 commented 2020-01-15 17:41:04 +09:00 (Migrated from gitlab.com)

Merged by: UlfNorell at 2020-01-15 08:41:04 UTC

*Merged by: UlfNorell at 2020-01-15 08:41:04 UTC*
Sign in to join this conversation.
No description provided.