Skip to content

Latest commit

 

History

History
892 lines (576 loc) · 8.34 KB

match.md

File metadata and controls

892 lines (576 loc) · 8.34 KB

Pattern matching examples

Generated from test cases.

1. Simple matching example

A very basic test that shows how a pattern variable (?likes) gets bound during matching. The pattern

{"likes":"?likes"}

matched against

{"likes":"tacos"}

should return

[{"?likes":"tacos"}]

2. Variable with constant

A map with a variable and a constant. The pattern

{"likes":"?likes","when":"now"}

matched against

{"likes":"tacos","when":"now"}

should return

[{"?likes":"tacos"}]

4. Two constants

A map with two constants. The pattern

{"likes":"queso","when":"now"}

matched against

{"likes":"queso","when":"now"}

should return

[{}]

6. Multiple variables

This simple example shows bindings for two pattern variables. The pattern

{"likes":"?likes","wants":"?wants"}

matched against

{"likes":"tacos","wants":"queso"}

should return

[{"?likes":"tacos","?wants":"queso"}]

7. Deeper variable

Pattern matching is fully structured The pattern

{"needs":{"tacos":{"n":"?n"}}}

matched against

{"needs":{"tacos":{"n":2}}}

should return

[{"?n":2}]

8. Same variable twice (good)

If you use a pattern variable more than once, then the bindings must agree. See the next example. The pattern

{"n":"?n","needs":{"tacos":{"n":"?n"}}}

matched against

{"n":2,"needs":{"tacos":{"n":2}}}

should return

[{"?n":2}]

9. Same variable twice (bad)

If you use a pattern variable more than once, then the bindings must agree. See the previous example. The pattern

{"n":"?n","needs":{"tacos":{"n":"?n"}}}

matched against

{"n":3,"needs":{"tacos":{"n":2}}}

should return

[]

10. Array as a set

An array is treated as a set. The pattern

{"a":["?a"],"is":"?a"}

matched against

{"a":[1,2,3,4],"is":3}

should return

[{"?a":3}]

11. Array with a variable and a constant

An array is treated as a set; multiple bindings possible. The pattern

["a","?x"]

matched against

["a","b","c"]

should return

[{"?x":"b"},{"?x":"c"}]

12. Array with a variable and a map constant

The pattern

[{"likes":"tacos"},"?x"]

matched against

[{"likes":"tacos"},"b","c"]

should return

[{"?x":"b"},{"?x":"c"}]

13. Array with a variable and a constant; message with map elements

The pattern

["a","b","?x"]

matched against

[{"likes":"tacos"},"b","a"]

should return

[{"?x":{"likes":"tacos"}}]

14. Array with a map containing a variable

The pattern

["a","b",{"likes":"?x"}]

matched against

[{"likes":"tacos"},{"likes":"chips"},"b","a"]

should return

[{"?x":"tacos"},{"?x":"chips"}]

15. Array as a set; multiple bss; backtracking

An array is treated as a set. The pattern

{"a":["?a"],"is":["?a"]}

matched against

{"a":[1,2,3,4],"is":[2,3]}

should return

[{"?a":2},{"?a":3}]

16. Bad array vars

Two pattern variables inside an array isn't allowed (because the computational complexity means that some input could be very costly to process). The pattern

{"a":["?x","?y"]}

matched against

{"a":[1]}

should return an error.

17. Property variable vars

You can have at most one pattern variable as a key in a given map. The pattern

{"?x":1}

matched against

{"n":1}

should return

[{"?x":"n"}]

18. Multiple property variable vars

You can have at most one pattern variable as a key in a given map. The pattern

{"?x":1,"?y":2}

matched against

{"m":2,"n":1}

should return an error.

19. A null value

The pattern

{"wants":"?wants"}

matched against

{"needs":null,"wants":"tacos"}

should return

[{"?wants":"tacos"}]

20. Type conflict: int/string

The pattern

{"wants":1}

matched against

{"wants":"one"}

should return

[]

21. Type conflict: int/bool

The pattern

{"wants":1}

matched against

{"wants":true}

should return

[]

22. Anonymous variable used twice

The pattern

{"count":"?","wants":"?"}

matched against

{"count":48,"wants":"tacos"}

should return

[{}]

23. Anonymous variable with normal variable

The pattern

{"count":"?","wants":"?","when":"?when"}

matched against

{"count":48,"wants":"tacos","when":"today"}

should return

[{"?when":"today"}]

24. Anonymous variable as a property variable

The pattern

{"?":"tacos"}

matched against

{"likes":"tacos","needs":"chips"}

should return

[{}]

25. Anonymous variable as a property variable and another variable

The pattern

{"?":{"likes":"?likes"}}

matched against

{"homer":{"likes":"tacos"}}

should return

[{"?likes":"tacos"}]

26. Anonymous variable as a property variable without a match

The pattern

{"?":"tacos"}

matched against

{"needs":"chips"}

should return

[]

34. Inequality: success

The pattern

{"n":"?<n"}

matched against

{"n":3}

with bindings

{"?<n":10}

should return

[{"?<n":10,"?n":3}]

35. Inequality: failure

The pattern

{"n":"?<n"}

matched against

{"n":3}

with bindings

{"?<n":2}

should return

[]

40. Inequality: success (>=)

The pattern

{"n":"?>=n"}

matched against

{"n":11}

with bindings

{"?>=n":11}

should return

[{"?>=n":11,"?n":11}]

41. Inequality: failure (>=)

The pattern

{"n":"?>=n"}

matched against

{"n":11}

with bindings

{"?>=n":12}

should return

[]

44. Inequality: non-numeric

The pattern

{"n":"?<n"}

matched against

{"n":"queso"}

with bindings

{"?<n":2}

should return

[]

45. Inequality: given same

The pattern

{"n":"?<n"}

matched against

{"n":3}

with bindings

{"?<n":10,"?n":3}

should return

[{"?<n":10,"?n":3}]

46. Inequality: given different

The pattern

{"n":"?<n"}

matched against

{"n":3}

with bindings

{"?<n":10,"?n":4}

should return

[]

47. Inequality: used later

The pattern

{"needs":"?n","wants":{"n":"?<n"}}

matched against

{"needs":3,"wants":{"n":3}}

with bindings

{"?<n":10}

should return

[{"?<n":10,"?n":3}]

48. Inequality: used later with conflict

The pattern

{"needs":"?n","wants":{"n":"?<n"}}

matched against

{"needs":4,"wants":{"n":3}}

with bindings

{"?<n":10}

should return

[]

49. Optional pattern variable (absent)

The pattern

{"opt":"??maybe","wants":"?wanted"}

matched against

{"wants":"tacos"}

with bindings

{}

should return

[{"?wanted":"tacos"}]

50. Optional pattern variable (present)

The pattern

{"a":"??maybe","wants":"?wanted"}

matched against

{"a":"queso","wants":"tacos"}

with bindings

{}

should return

[{"??maybe":"queso","?wanted":"tacos"}]

52. Optional pattern variable (array, absent)

The pattern

["??opt"]

matched against

[]

with bindings

{}

should return

[{}]

53. Optional pattern variable (array, present)

The pattern

["??opt","a","b"]

matched against

["a","b"]

with bindings

{}

should return

[{}]

54. Optional pattern variable (array, present)

The pattern

["??opt","a","b"]

matched against

["a","b","c"]

with bindings

{}

should return

[{"??opt":"c"}]

55. Optional pattern variable (array, present, multiple bindings)

The pattern

["??opt","a","b"]

matched against

["a","b","c","d"]

with bindings

{}

should return

[{"??opt":"c"},{"??opt":"d"}]