- PowerPoint PPT Presentation

About This Presentation
Title:

Description:

Extensible Pattern Matching via a Lightweight Language Extension Survey POPL meeting 7/25 Kazuhiro Inaba kinaba_at_is.s.u-tokyo.ac.jp Extensible ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 28
Provided by: kmonosNet
Category:

less

Transcript and Presenter's Notes

Title:


1
Extensible 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??????)
3
Motivation
4
Motivation
  • 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
5
Motivation
  • ??????????????????
  • ?????????????
  • ?????
  • ??Case-Analysis???????????
  • (nil?, head, tail ???????????????)
  • Exhaustiveness ???????????

type complex abstract let my_function c obj
zoom (abs c) (rotate (arg c) obj)
6
Examples
7
Active Patterns by Examples
  • Total Patterns
  • Single Total Patterns
  • Decomposition with Recognizers
  • Partial Patterns

8
Single 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)

9
Decomposition
  • (?????????) ???????????
  • 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

10
Decomposition
  • ?????????????
  • 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

11
Partial 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
12
Partial 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

13
Advanced Topics
  • Parameterized Patterns
  • Recursive Definition of Patterns

14
Parameterized 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

15
Recursive 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))

16
Semantics
17
Semantics
  • Naïve Operational Semantics
  • Okasaki Condition
  • Implementation

18
Naïve Operational Semantics
  • ???????
  • pat1 ? v ?????????
  • ?????????????expr1??????????
  • pat2 ? v ?????????
  • ?????????????expr2??????????

match v with pat1 -gt expr1 pat2 -gt expr2

19
Naïve Operational Semantics
  • pat1 ? v ??????????
  • ??????????????(?????)?????
  • Active Pattern p ?? v ??????????????????? (p)
    v ?????????

20
Naï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
21
Okasaki 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
22
Okasaki 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
23
Okasaki Condition
  • Chris Okasaki, Views for Standard ML, 1998
  • Sensible?Semantic?????????????????1???????????????
    ????????? ????????
  • ???match????????????????????1?????????????????????
    ?2??????????????????

24
Implementation
  • ? ??(??????)???? Okasaki Condition ???????? ?
    Future Work
  • Scott and Ramsey 2000 Algorithm (????Decision
    Tree???Heuristics) ? Active Pattern
    ?????????????????

25
Conclusion
26
Related Work
  • ?????????View??????????????????? (Reference??)
  • Peyton Jones 2007 ?????
  • Parameterized Patterns
  • Partial Patterns
  • Total Patterns
  • Nesting
  • Patterns as first-class values
  • ????????????Active Pattern?????

27
Future Work
  • ???F ???Haskell???????Active Pattern
    ??????????????????????
  • E.g. Monadic Pattern Matching
  • Partial Pattern ??????? match ?? Maybe
    ???????????????????? MonadPlus ??????????/?????
Write a Comment
User Comments (0)
About PowerShow.com