Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3...

27
Pattern Synonyms Matthew Pickering 1 Gergő Érdi 2 Simon Peyton Jones 3 Richard A. Eisenberg 4 1 University of Oxford 2 Standard Chartered Bank 3 Microsoft Research 4 Bryn Mawr College Haskell Symposium, September 2016. 1 / 17

Transcript of Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3...

Page 1: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern Synonyms

Matthew Pickering1 Gergő Érdi2 Simon Peyton Jones3

Richard A. Eisenberg4

1University of Oxford

2Standard Chartered Bank

3Microsoft Research

4Bryn Mawr College

Haskell Symposium, September 2016.

1 / 17

Page 2: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Example

-- Datatype definitiondata Type “ TyApp String rType s

-- Functions abstract construction

tyInt :: TypetyInt “ TyApp "Int" r s

mkFunTy :: Type Ñ Type Ñ TypemkFunTy t u “ TyApp "->" rt, u s

plusTy :: TypeplusTy “ tyInt ‘mkFunTy ‘ tyInt ‘mkFunTy ‘ tyInt

2 / 17

Page 3: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Example

-- Datatype definitiondata Type “ TyApp String rType s

-- Pattern synonym abstracts matchingpattern FunTy :: Type Ñ Type Ñ Typepattern FunTy t u “ TyApp "->" rt, u s

funArgTys :: Type Ñ prType s,TypeqfunArgTys pFunTy t uq “ case funArgTys u ofpts, rq Ñ pt : ts, rq

funArgTys t “ pr s, tq

2 / 17

Page 4: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern synonyms

§ Goal: bring function-like abstraction to pattern matching§ Touches all parts of the GHC frontend:

§ Parser, renamer§ Typechecker§ Desugarer§ Interface files

§ No backend changes needed§ Our paper shows the breadth; here we show some depth:

§ Typechecking§ Desugaring

3 / 17

Page 5: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern types

4 / 17

Page 6: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern types

The Typing Principle

It should be possible to determine whether a use of P is well-typedbased only on P ’s type, without reference to P ’s definition.

What is the type of a pattern?

5 / 17

Page 7: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern types

Scrutinee type:

pattern P1 :: Bool

pattern P1 “ True

pattern P2 :: Maybe Bool

pattern P2 “ Just True

pattern P3 :: @a.Maybe a

pattern P3 “ Nothing

Parametric patterns:

pattern P4 :: @a.a Ñ Maybe a

pattern P4 x “ Just x

6 / 17

Page 8: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern types

Scrutinee type:

pattern P1 :: Boolpattern P1 “ True

pattern P2 :: Maybe Boolpattern P2 “ Just True

pattern P3 :: @a.Maybe apattern P3 “ Nothing

Parametric patterns:

pattern P4 :: @a.a Ñ Maybe a

pattern P4 x “ Just x

6 / 17

Page 9: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern types

Scrutinee type:

pattern P1 :: Boolpattern P1 “ True

pattern P2 :: Maybe Boolpattern P2 “ Just True

pattern P3 :: @a.Maybe apattern P3 “ Nothing

Parametric patterns:

pattern P4 :: @a.a Ñ Maybe apattern P4 x “ Just x

6 / 17

Page 10: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern types: required constraints

Required constraints:

pattern P5 :: @a.pNum a,Eq aq ñ a

pattern P5 “ 42

pattern P6 :: @a.pShow aq ñ a

pattern P6 “ pshow Ñ "foo"q

7 / 17

Page 11: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern types: required constraints

Required constraints:

pattern P5 :: @a.pNum a,Eq aq ñ apattern P5 “ 42

pattern P6 :: @a.pShow aq ñ a

pattern P6 “ pshow Ñ "foo"q

7 / 17

Page 12: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern types: required constraints

Required constraints:

pattern P5 :: @a.pNum a,Eq aq ñ apattern P5 “ 42pattern P6 :: @a.pShow aq ñ apattern P6 “ pshow Ñ "foo"q

7 / 17

Page 13: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern types: existentials and provided constraints

data T a whereMkT :: pEq a, Show bq ñ a Ñ a Ñ b Ñ T a

f pMkT x y vq “ if x ” y then Just pshow vq else Nothing

Matching on MkT brings in scope§ the type b§ pEq a, Show bq

allowing p”q and show to be used on the right-hand side

8 / 17

Page 14: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern types: existentials and provided constraints

data T a whereMkT :: pEq a, Show bq ñ a Ñ a Ñ b Ñ T a

f pMkT x y vq “ if x ” y then Just pshow vq else Nothing

Matching on MkT brings in scope§ the type b§ pEq a, Show bq

allowing p”q and show to be used on the right-hand side

f :: T a Ñ Maybe String

8 / 17

Page 15: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern types: existentials and provided constraints

data T a whereMkT :: pEq a, Show bq ñ a Ñ a Ñ b Ñ T a

pattern P x y v “ MkT x y v

Matching on P brings in scope§ the type b§ pEq a, Show bq

8 / 17

Page 16: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern types: existentials and provided constraints

data T a whereMkT :: pEq a, Show bq ñ a Ñ a Ñ b Ñ T a

pattern P x y v “ MkT x y v

Matching on P brings in scope§ the type b§ pEq a, Show bq

pattern P :: @a.pq ñ @b.pEq a, Show bq ñ a Ñ a Ñ b Ñ T a

8 / 17

Page 17: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern types: required & provided constraints

data T a whereMkT :: pEq a, Show bq ñ a Ñ b Ñ T a

pattern P v “ MkT 1 v

§ Matching on P requires pEq a,Num aq§ Matching on P provides pEq a, Show bq

9 / 17

Page 18: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern types: required & provided constraints

data T a whereMkT :: pEq a, Show bq ñ a Ñ b Ñ T a

pattern P v “ MkT 1 v

§ Matching on P requires pEq a,Num aq§ Matching on P provides pEq a, Show bq

pattern P :: @a.pNum aq ñ @b.pEq a, Show bq ñ b Ñ T a

9 / 17

Page 19: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern synonym types

Pattern synonym types are fully specified on six axes:

1. Universially bound type variables univ2. The required context Req (with univ in scope)3. The scrutinee type t (with univ in scope)

4. Existentially bound type variables ex5. The provided context Prov (with univ and ex in scope)6. The types of parameters t1 , t2 , . . . (with univ and ex in scope)

Surface syntax for pattern synonym type signatures:

pattern P :: @univ .Req ñ t

10 / 17

Page 20: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Pattern synonym types

Pattern synonym types are fully specified on six axes:

1. Universially bound type variables univ2. The required context Req (with univ in scope)3. The scrutinee type t (with univ in scope)

4. Existentially bound type variables ex5. The provided context Prov (with univ and ex in scope)6. The types of parameters t1 , t2 , . . . (with univ and ex in scope)

Surface syntax for pattern synonym type signatures:

pattern P :: @univ .Req ñ @ex .Prov ñ t1 Ñ t2 Ñ ...Ñ t

10 / 17

Page 21: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Desugaring

11 / 17

Page 22: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Desugaring

pattern P :: @a.pNum aq ñ @b.pEq a, Show bq ñb Ñ T a

pattern P v “ MkT 3 v

$mP :: @r a.pNum aq ñT a Ñ[email protected] a, Show bq ñ b Ñ rq Ñ r Ñ r

$mP x sk fk “ case x ofMkT 3 v Ñ sk v

Ñ fk

12 / 17

Page 23: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Desugaring

Can be represented in existing GHC Core

§ No changes needed anywhere downstream§ Exported, linked, and potentially inlined just like any otherfunction

§ Synthetic Void# parameter can be used to prevent incorrectstrictness when r is unboxed

13 / 17

Page 24: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Desugaring

Semantics is different from macro-substitution!The full (potentially nested) structure of the pattern synonym ismatched first:

pattern Single a “ ras

f1 :: rBool s Ñ Boolf1 rTrue s “ Truef1 “ False

f1 rK,Ks “ Kf1 pFalse : Kq “ False

pattern Single a “ ras

f2 :: rBool s Ñ Boolf2 pSingle Trueq “ Truef2 “ False

f2 rK,Ks “ Falsef2 pFalse : Kq “ K

14 / 17

Page 25: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Conclusion

15 / 17

Page 26: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

History

§ Proposal added in 2011§ New in GHC 7.8 in 2014§ Incremental improvements in GHC 7.10 and 8.0§ Used in 72 packages on Hackage (as of June 2016)

16 / 17

Page 27: Pattern SynonymsPattern Synonyms MatthewPickering1 GergőÉrdi2 SimonPeytonJones3 RichardA.Eisenberg4 1University of Oxford 2Standard Chartered Bank 3Microsoft Research 4Bryn Mawr

Check our paper for more. . .

§ Lots of examples§ Pattern synonym directionality§ Record syntax support§ Importing/exporting§ Shorthand syntax for pattern synonym signatures§ Formalisation of pattern types (in the extended version)

http://unsafePerform.IO/patsyn

17 / 17