Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement pattern matching in parser #49

Open
sfultong opened this issue Nov 11, 2020 · 10 comments
Open

implement pattern matching in parser #49

sfultong opened this issue Nov 11, 2020 · 10 comments

Comments

@sfultong
Copy link
Collaborator

The first part of this should be pretty easy: all we need to do is be able to match on pairs and zero.

The second part is matching user-defined types and is blocked by #47

@sfultong
Copy link
Collaborator Author

A long term goal is to have incomplete pattern matching sections compile to a refinement check

@hhefesto
Copy link
Contributor

hhefesto commented Dec 23, 2020

As agreed in gitter dm, also pattern matching on all these:

main = \input -> case input of
                  (a, (b, c)) -> foo a b c
                  "string" -> bar
                  [a, b, c] -> foo a b c
                  2 -> bar
                  [(a,b), c, 2] -> foo' a b c

Just leaving this here for record keeping.

@hhefesto
Copy link
Contributor

case patternn of
  (a,a) -> match1Out

matches like the above aren't allowed (i.e. can't have multiple of the same variable in a match)

@hhefesto
Copy link
Contributor

checkout "_" (i.e. ignore) pattern matching

@hhefesto
Copy link
Contributor

hhefesto commented Apr 8, 2021

let myConst = (left MyInt0) 8
in case myConst of
     ((left MyInt0) 8) -> ("Success!", 1)
     x             -> ("default case", 0)

This breaks becuase ((left MyInt0) 8) is an application of a function and this is still disallowed because it's not implemented in generateCondition

@sfultong
Copy link
Collaborator Author

sfultong commented Jul 1, 2021

note that #71 should help with pattern matching on smart constructors and other functions

@sfultong
Copy link
Collaborator Author

Let's take the simplest pattern matching case, with no variables first:

let a = (0,0)
in case a of
  (0,0) -> "pair"
  _       -> "not pair"

this is equivalent to

let a = (0,0)
in if a
    then -- match outer pair
      if not (left a)
      then -- match first 0
        if not (right a)
        then -- match second 0
           "pair"
        else "not pair"
      else "not pair"
    else "not pair"

@sfultong
Copy link
Collaborator Author

sfultong commented Aug 31, 2023

Let's add in a variable:

let a = (0,0)
in case a of
   (x,0) -> (0,x)
   _      -> 2

Then we can do something like

let a = (0,0)
in if a
    then -- match outer pair
      if not (right a)
      then -- match second 0
        (\x -> (0,x)) -- expression after case match
           (left a)     -- apply variable from case match
      else 2
    else 2

@hhefesto
Copy link
Contributor

hhefesto commented Sep 8, 2023

How about these cases?:

let a = 0
in case a of
   (x,y) -> 0
   _     -> 1
let a = (0,0)
in case a of
   (x,y) -> 0
   _     -> 1

knowing that:

telomare> left 0
0
telomare> left (left 0)
0

@sfultong
Copy link
Collaborator Author

sfultong commented Sep 8, 2023

let a = (0,0)
in if a -- notice this if statement, which checks for matching a pair
  then (\x y -> 0) (left a) (right a)
  else 1

This seems like it works for implementing your second case, and you can just do the inverse for case 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants