Title:
1Extensible Pattern Matching via a Lightweight
Language Extension ? Survey
- POPL meeting 7/25
- Kazuhiro Inaba
- kinaba_at_is.s.u-tokyo.ac.jp
2??
- Extensible Pattern Matching via a Lightweight
Language ExtensionDon Syme, Gregory Neverov,
James MargetsonAccepted for ICFP 2007 - F ? Active Pattern ???????????
- ????????????????????????
(Submitted Draft?Web??????)
3Motivation
4Motivation
- Abstract Data Type ????????????
- ???View???????????
type complex abstract let my_function c obj
match c with Polar(r, t) -gt zoom r
(rotate t obj) let another_function c match
c with Rect(x, y) -gt x y
5Motivation
- ??????????????????
- ?????????????
- ?????
- ??Case-Analysis???????????
- (nil?, head, tail ???????????????)
- Exhaustiveness ???????????
type complex abstract let my_function c obj
zoom (abs c) (rotate (arg c) obj)
6Examples
7Active Patterns by Examples
- Total Patterns
- Single Total Patterns
- Decomposition with Recognizers
- Partial Patterns
8Single Total Patterns
- type complex Rect of float float
- type complex Rect of float float
- let (Polar) c
- match c with
- Rect(x,y) -gt (atan2 y x, sqrt(xxyy))
- val (Polar) complex -gt float float
- ????????? (let (r,t) (Polar) c in ???)
- let my_function c obj
- match c with
- Polar(r, t) -gt zoom r (rotate t obj)
9Decomposition
- let (SnocNil) lst
- if lst
- then Nil
- else Snoc(rev(tl(rev lst)), hd(rev lst))
- val (SnocNil)
- a list -gt Choicelt(a lista),unitgt
- match 123 with
- Nil -gt
- Snoc(hh, t) -gt t _at_ hh
- Val it int list 3 1 2
10Decomposition
- match 123 with Snoc(hh,t) -gt t _at_ hh
- match 123 with Snoc(hh,t) -gt t _at_ hh
-
- stdin(59,0) warning FS0025 Incomplete pattern
match. - val it int list 3 1 2
11Partial Patterns
- ??????????????????????
- ??complete???
- ?case?distinct????
- ???case???????????????
type value IntVal of int FloatVal
of float ColorVal of int int
int match str with ParseInt i -gt IntVal i
ParseFloat f -gt FloatVal f ParseColor c -gt
ColorVal c _ -gt failwith invalid input
12Partial Patterns
- let (ParseInt_) s
- let i ref 0 in
- if Int32.TryParse (s,i) then Some !i
- else None
- val (ParseInt_) string -gt int option
13Advanced Topics
- Parameterized Patterns
- Recursive Definition of Patterns
14Parameterized Patterns
- let (ParseRegex_) re s
- if match re s then Some s else None
- val (ParseRegex_)
- regex -gt string -gt string option
- match str with
- ParseRegex \\d s -gt dec s
- ParseRegex 0x\\dA-Fa-f s -gt hex s
15Recursive Definition
- type a joinlist Empty
- Single of a
- Join of a joinlist a joinlist
- let rec (NilCons) function
- Empty -gt Nil
- Single(x) -gt Cons(x, Empty)
- Join(Nil, Nil) -gt Nil
- Join(Nil,Cons(x,xs))-gt Cons(x,xs)
- Join(Cons(x,xs),ys) -gt Cons(x,Join(xs,ys))
16Semantics
17Semantics
- Naïve Operational Semantics
- Okasaki Condition
- Implementation
18Naïve Operational Semantics
- ???????
- pat1 ? v ?????????
- ?????????????expr1??????????
- pat2 ? v ?????????
- ?????????????expr2??????????
match v with pat1 -gt expr1 pat2 -gt expr2
19Naïve Operational Semantics
- pat1 ? v ??????????
- ??????????????(?????)?????
- Active Pattern p ?? v ??????????????????? (p)
v ?????????
20Naïve Semantics (?)
- let (SnocNil)
- match 123 with
- Nil -gt
- Snoc(hh, t) -gt t _at_ hh
( Nil -gt ) match (SnocNil) 123
with Choice_2_1 () -gt _ -gt (
Snoc(hh,t) -gt t _at_ hh ) match (SnocNil)
123 with Choice_2_2 (hh,t) -gt t _at_ hh
21Okasaki Condition
- Naïve Semantics ????????
- ???
( Nil -gt ) match (SnocNil) 123
with Choice_2_1 () -gt _ -gt (
Snoc(hh,t) -gt t _at_ hh ) match (SnocNil)
123 with Choice_2_2 (hh,t) -gt t _at_ hh
22Okasaki Condition
- Active Pattern?????????????????????? (SnocNil)
??????????? - ?????????Exhaustive?match???????semantics??fail??
??????
( Nil -gt ) match (SnocNil) 123
with Choice_2_1 () -gt _ -gt (
Snoc(hh,t) -gt t _at_ hh ) match (SnocNil)
123 with Choice_2_2 (hh,t) -gt t _at_ hh
23Okasaki Condition
- Chris Okasaki, Views for Standard ML, 1998
- Sensible?Semantic?????????????????1???????????????
????????? ???????? - ???match????????????????????1?????????????????????
?2??????????????????
24Implementation
- ? ??(??????)???? Okasaki Condition ???????? ?
Future Work - Scott and Ramsey 2000 Algorithm (????Decision
Tree???Heuristics) ? Active Pattern
?????????????????
25Conclusion
26Related Work
- ?????????View??????????????????? (Reference??)
- Peyton Jones 2007 ?????
- Parameterized Patterns
- Partial Patterns
- Total Patterns
- Nesting
- Patterns as first-class values
- ????????????Active Pattern?????
27Future Work
- ???F ???Haskell???????Active Pattern
?????????????????????? - E.g. Monadic Pattern Matching
- Partial Pattern ??????? match ?? Maybe
???????????????????? MonadPlus ??????????/?????