pattern synonyms › 98bd › 120ac58b1c1c...pattern types: existentials and provided constraints...
TRANSCRIPT
-
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
-
Example
-- Datatype definitiondata Type “ TyApp String rType s
-- Functions abstract construction
tyInt :: TypetyInt “ TyApp "Int" r smkFunTy :: Type Ñ Type Ñ TypemkFunTy t u “ TyApp "->" rt, u splusTy :: TypeplusTy “ tyInt ‘mkFunTy ‘ tyInt ‘mkFunTy ‘ tyInt
2 / 17
-
Example
-- Datatype definitiondata Type “ TyApp String rType s
-- Pattern synonym abstracts matchingpattern FunTy :: Type Ñ Type Ñ Typepattern FunTy t u “ TyApp "->" rt, u sfunArgTys :: Type Ñ prType s,TypeqfunArgTys pFunTy t uq “ case funArgTys u ofpts, rq Ñ pt : ts, rq
funArgTys t “ pr s, tq
2 / 17
-
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
-
Pattern types
4 / 17
-
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
-
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
-
Pattern types
Scrutinee type:
pattern P1 :: Boolpattern P1 “ Truepattern P2 :: Maybe Boolpattern P2 “ Just Truepattern P3 :: @a.Maybe apattern P3 “ Nothing
Parametric patterns:
pattern P4 :: @a.a Ñ Maybe a
pattern P4 x “ Just x
6 / 17
-
Pattern types
Scrutinee type:
pattern P1 :: Boolpattern P1 “ Truepattern P2 :: Maybe Boolpattern P2 “ Just Truepattern P3 :: @a.Maybe apattern P3 “ Nothing
Parametric patterns:
pattern P4 :: @a.a Ñ Maybe apattern P4 x “ Just x
6 / 17
-
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
-
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
-
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
-
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
-
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
-
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
-
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
-
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
-
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
-
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
-
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
-
Desugaring
11 / 17
-
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
-
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
-
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 “ rasf2 :: rBool s Ñ Boolf2 pSingle Trueq “ Truef2 “ False
f2 rK,Ks “ Falsef2 pFalse : Kq “ K
14 / 17
-
Conclusion
15 / 17
-
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
-
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
http://unsafePerform.IO/patsyn
Pattern typesDesugaringConclusion