Ambiguous pattern variables Gabriel Scherer , Luc Maranget, Thomas R - - PowerPoint PPT Presentation

ambiguous pattern variables
SMART_READER_LITE
LIVE PREVIEW

Ambiguous pattern variables Gabriel Scherer , Luc Maranget, Thomas R - - PowerPoint PPT Presentation

Ambiguous pattern variables Gabriel Scherer , Luc Maranget, Thomas R efis Northeastern University September 22, 2016 Gabriel Scherer , Luc Maranget, Thomas R efis (Northeastern University) Ambiguous pattern variables September 22, 2016 1


slide-1
SLIDE 1

Ambiguous pattern variables

Gabriel Scherer, Luc Maranget, Thomas R´ efis

Northeastern University

September 22, 2016

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 1 / 17

slide-2
SLIDE 2

The problem

type α exp = | Const of α | Mul of α exp * α exp let is_neutral n = (n = 1) let mul a b = match a, b with | (Const n, v) | (v, Const n) when is_neutral n -> v | a, b -> Mul (a, b)

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 2 / 17

slide-3
SLIDE 3

The problem

type α exp = | Const of α | Mul of α exp * α exp let is_neutral n = (n = 1) let mul a b = match a, b with | (Const n, v) | (v, Const n) when is_neutral n -> v | a, b -> Mul (a, b) mul (Const 2) (Const 1)

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 2 / 17

slide-4
SLIDE 4

The problem

type α exp = | Const of α | Mul of α exp * α exp let is_neutral n = (n = 1) let mul a b = match a, b with | (Const n, v) | (v, Const n) when is_neutral n -> v | a, b -> Mul (a, b) mul (Const 2) (Const 1) = Mul (Const 2, Const 1)

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 2 / 17

slide-5
SLIDE 5

ML patterns (formally)

p ::= pattern | wildcard | p as x variable binding | K(p1, ..., pn) constructor pattern | p | q

  • r-pattern

Variable patterns x are sugar for ( as x). Pair patterns (p, q) are a special case of constructor pattern. A clause of the form | p when g -> e matches p first, then test if g holds, and only then takes the branch to e.

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 3 / 17

slide-6
SLIDE 6

The Clash

(p | q) when g → e readers think the guard g will test both p and q – angelic choice. The specification clearly says otherwise: (p | q) is left-to-right, and only then g is tried.

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 4 / 17

slide-7
SLIDE 7

The Clash

(p | q) when g → e readers think the guard g will test both p and q – angelic choice. The specification clearly says otherwise: (p | q) is left-to-right, and only then g is tried. Note: specifying evaluation order is not always good, after all...

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 4 / 17

slide-8
SLIDE 8

The Clash

(p | q) when g → e readers think the guard g will test both p and q – angelic choice. The specification clearly says otherwise: (p | q) is left-to-right, and only then g is tried. Note: specifying evaluation order is not always good, after all... Note: automatically turning this into (p when g) | (q when g) does not work: changing the semantics of existing code: nope

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 4 / 17

slide-9
SLIDE 9

The Clash

(p | q) when g → e readers think the guard g will test both p and q – angelic choice. The specification clearly says otherwise: (p | q) is left-to-right, and only then g is tried. Note: specifying evaluation order is not always good, after all... Note: automatically turning this into (p when g) | (q when g) does not work: changing the semantics of existing code: nope nested guards don’t exist and would break exhaustivity checking, etc.

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 4 / 17

slide-10
SLIDE 10

The Clash

(p | q) when g → e readers think the guard g will test both p and q – angelic choice. The specification clearly says otherwise: (p | q) is left-to-right, and only then g is tried. Note: specifying evaluation order is not always good, after all... Note: automatically turning this into (p when g) | (q when g) does not work: changing the semantics of existing code: nope nested guards don’t exist and would break exhaustivity checking, etc. side-effects in g would be duplicated

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 4 / 17

slide-11
SLIDE 11

The Clash

(p | q) when g → e readers think the guard g will test both p and q – angelic choice. The specification clearly says otherwise: (p | q) is left-to-right, and only then g is tried. Note: specifying evaluation order is not always good, after all... Note: automatically turning this into (p when g) | (q when g) does not work: changing the semantics of existing code: nope nested guards don’t exist and would break exhaustivity checking, etc. side-effects in g would be duplicated what about nested or-patterns? (p | q) may be deep.

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 4 / 17

slide-12
SLIDE 12

At least complain about it!

Warn when p when g → e and a value may match p in several ways (or-patterns) the test g may depend on which choice is taken: it contains ambiguous pattern variables

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 5 / 17

slide-13
SLIDE 13

At least complain about it!

Warn when p when g → e and a value may match p in several ways (or-patterns) the test g may depend on which choice is taken: it contains ambiguous pattern variables (a, (p | q)) when a < 10 → ...

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 5 / 17

slide-14
SLIDE 14

At least complain about it!

Warn when p when g → e and a value may match p in several ways (or-patterns) the test g may depend on which choice is taken: it contains ambiguous pattern variables (a, (p | q)) when a < 10 → ... (a, p) | (a, q) when a < 10 → ...

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 5 / 17

slide-15
SLIDE 15

At least complain about it!

Warn when p when g → e and a value may match p in several ways (or-patterns) the test g may depend on which choice is taken: it contains ambiguous pattern variables (a, (p | q)) when a < 10 → ... (a, p) | (a, q) when a < 10 → ... (Some v, e) | (e, Some v) when v = 0 → ...

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 5 / 17

slide-16
SLIDE 16

At least complain about it!

Warn when p when g → e and a value may match p in several ways (or-patterns) the test g may depend on which choice is taken: it contains ambiguous pattern variables (a, (p | q)) when a < 10 → ... (a, p) | (a, q) when a < 10 → ... (Some v, e) | (e, Some v) when v = 0 → ... (Some v, None) | (None, Some v) when v = 0 → ...

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 5 / 17

slide-17
SLIDE 17

Our contribution

an algorithm to detect ambiguous pattern variables implemented in OCaml 4.03 (released last April) Demo

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 6 / 17

slide-18
SLIDE 18

How to implement this warning? (Attempts.) As for all pattern matching questions (compilation, exhaustivity, usefulness...): pattern matrices (the take-away of this talk)

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 7 / 17

slide-19
SLIDE 19

Pattern matrix

A matrix: a space of matcheable values that share a common prefix. C1[1, . . . , n] C2[1, . . . , n] . . . Cn[1, . . . , n]      p1,1 p1,2 · · · p1,n p2,1 p2,2 · · · p2,n . . . . . . ... . . . pm,1 pm,2 · · · pm,n      rows: disjunction, alternative columns: sub-patterns matched in parallel contexts: common prefix, possibly different bindings ((S ) as v, ) (S , ( as v)) true N S false

  • represents

| ((S true) as v, N) | (S , S false as v)

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 8 / 17

slide-20
SLIDE 20

Matrix operation: splitting a row (1)

All head constructors. C1[, ] C2[, ] C3[, ]   N p1,2 S p2,1 p2,2 S p3,1 p3,2  

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 9 / 17

slide-21
SLIDE 21

Matrix operation: splitting a row (1)

All head constructors. C1[, ] C2[, ] C3[, ]   N p1,2 S p2,1 p2,2 S p3,1 p3,2   = ⇒ C1[N, ]

  • p1,2
  • C2[S , ]

C3[S , ] p2,1 p2,2 p3,1 p3,2

  • Gabriel Scherer, Luc Maranget, Thomas R´

efis (Northeastern University) Ambiguous pattern variables September 22, 2016 9 / 17

slide-22
SLIDE 22

Matrix operation: splitting a row (2)

Some head constructors. C1[, ] C2[, ] C3[, ]   N p1,2 x p2,2 S p3,1 p3,2   = ⇒ C1[, ] C2[, ] C3[, ]   N p1,2 (N | S ) as x p2,2 S p3,1 p3,2   = ⇒ C1[, ] C2[ as x, ] C2[ as x, ] C3[, ]     N p1,2 N p2,2 S p2,2 S p3,1 p3,2    

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 10 / 17

slide-23
SLIDE 23

Matrix operation: (3)

No head constructors. C1[, ] C2[, ] C3[, ]   p1,2 x p2,2 p3,2   = ⇒

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 11 / 17

slide-24
SLIDE 24

Matrix operation: (3)

No head constructors. C1[, ] C2[, ] C3[, ]   p1,2 x p2,2 p3,2   = ⇒ C1[ , ] C2[x, ] C3[ , ]   p1,2 p2,2 p3,2  

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 11 / 17

slide-25
SLIDE 25

Typical matrix-based algorithm, simplified

Manipulate sets of matrices. Start from a single matrix (single-element set). Split matrices, repeat. Stop when all matrices are empty. Compute your answer from that.

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 12 / 17

slide-26
SLIDE 26

Our algorithm, on an example (1)

(S v, a) | (a, S v) when is_neutral v → ...

  • (S v, a) | (a, S v)
  • Gabriel Scherer, Luc Maranget, Thomas R´

efis (Northeastern University) Ambiguous pattern variables September 22, 2016 13 / 17

slide-27
SLIDE 27

Our algorithm, on an example (1)

(S v, a) | (a, S v) when is_neutral v → ...

  • (S v, a) | (a, S v)
  • (S v, a)

(a, S v)

  • Gabriel Scherer, Luc Maranget, Thomas R´

efis (Northeastern University) Ambiguous pattern variables September 22, 2016 13 / 17

slide-28
SLIDE 28

Our algorithm, on an example (1)

(S v, a) | (a, S v) when is_neutral v → ...

  • (S v, a) | (a, S v)
  • (S v, a)

(a, S v)

  • (, )

(, ) S v a a S v

  • Gabriel Scherer, Luc Maranget, Thomas R´

efis (Northeastern University) Ambiguous pattern variables September 22, 2016 13 / 17

slide-29
SLIDE 29

Our algorithm, on an example (1)

(S v, a) | (a, S v) when is_neutral v → ...

  • (S v, a) | (a, S v)
  • (S v, a)

(a, S v)

  • (, )

(, ) S v a a S v

  • (, )

( as a, ) ( as a, )   S v a S S v N S v  

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 13 / 17

slide-30
SLIDE 30

Our algorithm, on an example (1)

(S v, a) | (a, S v) when is_neutral v → ...

  • (S v, a) | (a, S v)
  • (S v, a)

(a, S v)

  • (, )

(, ) S v a a S v

  • (, )

( as a, ) ( as a, )   S v a S S v N S v   (S , ) (S as a, ) v a S v

  • (N as a, )
  • S v
  • Gabriel Scherer, Luc Maranget, Thomas R´

efis (Northeastern University) Ambiguous pattern variables September 22, 2016 13 / 17

slide-31
SLIDE 31

Our algorithm, on an example (2)

(S v, a) | (a, S v) when is_neutral v → ... (S v, ) (S as a, ) a S v

  • (N as a, )
  • S v
  • Gabriel Scherer, Luc Maranget, Thomas R´

efis (Northeastern University) Ambiguous pattern variables September 22, 2016 14 / 17

slide-32
SLIDE 32

Our algorithm, on an example (2)

(S v, a) | (a, S v) when is_neutral v → ... (S v, ) (S as a, ) a S v

  • (N as a, )
  • S v
  • (S v, ( as a))

(S v, ( as a)) (S as a, )   S N S v   (N as a, )

  • S v
  • Gabriel Scherer, Luc Maranget, Thomas R´

efis (Northeastern University) Ambiguous pattern variables September 22, 2016 14 / 17

slide-33
SLIDE 33

Our algorithm, on an example (2)

(S v, a) | (a, S v) when is_neutral v → ... (S v, ) (S as a, ) a S v

  • (N as a, )
  • S v
  • (S v, ( as a))

(S v, ( as a)) (S as a, )   S N S v   (N as a, )

  • S v
  • (S v, (S as a))

(S as a, )

  • v
  • (S v, N)
  • ·
  • (N as a, )
  • S v
  • Gabriel Scherer, Luc Maranget, Thomas R´

efis (Northeastern University) Ambiguous pattern variables September 22, 2016 14 / 17

slide-34
SLIDE 34

Our algorithm, on an example (3)

(S v, a) | (a, S v) when is_neutral v → ... (S v , (S as a)) (S as a, v ) · ·

  • (S v, N)
  • ·
  • (N as a, S v)
  • ·
  • Gabriel Scherer, Luc Maranget, Thomas R´

efis (Northeastern University) Ambiguous pattern variables September 22, 2016 15 / 17

slide-35
SLIDE 35

Our algorithm, on an example (3)

(S v, a) | (a, S v) when is_neutral v → ... (S v , (S as a)) (S as a, v ) · ·

  • (S v, N)
  • ·
  • (N as a, S v)
  • ·
  • Gabriel Scherer, Luc Maranget, Thomas R´

efis (Northeastern University) Ambiguous pattern variables September 22, 2016 15 / 17

slide-36
SLIDE 36

Actual implementation

No need for sets of matrices: we recursively traverse the set/tree of splits. Long time, but short space. Most algorithms don’t keep contexts, they retain only what they need. In

  • ur case, variable bindings positions.

See the extended abstract for a more algorithmic presentation.

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 16 / 17

slide-37
SLIDE 37

Conclusion

Arthur Chargu´ eraud, Martin Clochard and Claude March´ e: a problem us: a solution Future work: negative information.

Gabriel Scherer, Luc Maranget, Thomas R´ efis (Northeastern University) Ambiguous pattern variables September 22, 2016 17 / 17