80
Egison Pattern Matching in Ruby - Express Intuition Directly with Essentially New Syntax - Vol.01 Sep/18/2014 Satoshi Egi Rakuten Institute of Technology http://rit.rakuten.co.jp/

Ruby Egison

Embed Size (px)

DESCRIPTION

This slide is presented at RubyKaigi 2014 on Sep 18. We have designed and implemented the library that realizes non-linear pattern matching against unfree data types. We can directly express pattern-matching against lists, multisets, and sets using this library. The library is already released via RubyGems.org as one of gems. The expressive power of this gem derives from the theory behind the Egison programming language and is so strong that we can write poker-hands analyzer in a single pattern matching expression. This is impossible even in any other state-of-the-art programming language. Pattern matching is one of the most important features of programming language for intuitive representation of algorithms. Our library simplifies code by replacing not only complex conditional branches but also nested loops with an intuitive pattern-matching expression.

Citation preview

Page 1: Ruby Egison

Egison Pattern Matching in Ruby - Express Intuition Directly with Essentially New Syntax - Vol.01 Sep/18/2014 Satoshi Egi Rakuten Institute of Technology http://rit.rakuten.co.jp/

Page 2: Ruby Egison

2

Self-Introduction

Name Satoshi Egi (江木 聡志)

Association Rakuten Institute of Technology (楽天技術研究所)

Education Majored Computer Science in the University of Tokyo

Interests Programming Languages, AI (Mathematics)

Website http://www.egison.org/~egi/

Page 3: Ruby Egison

3

Very Quick Introduction of Today’s Contents

I have created the programming language that realized non-linear pattern-matching even against data that have no standard form.

(match-all xs (multiset integer) ! [<cons $x <cons ,x _>> x]) !Egison

Enumerate the elements of the collection ‘xs’ that appear

more than twice

pairs = [] !(1..n).each do |i| ! (i..n).each do |j| ! if xs[i] == xs[j] ! pairs = pairs + ! xs[i] ! end ! end !end !

Ruby

Non-linear patterns allow multiple occurrences of same variables in a pattern

Page 4: Ruby Egison

4

Very Quick Introduction of Today’s Contents

I have implemented the same feature in Ruby! The source code on GitHub!

match_all(xs) do ! with(Multiset.(_x,__x, *_)) { x } !end !Ruby with Egison gem

Enumerate the elements of the collection ‘xs’ that appear

more than twice

pairs = [] !(1..n).each do |i| ! (i..n).each do |j| ! if xs[i] == xs[j] ! pairs = pairs + ! xs[i] ! end ! end !end !

Ruby

https://github.com/egison/egison-ruby

Page 5: Ruby Egison

5

Very Quick Introduction of Today’s Contents

Poker hands analyzer with a single expression in Ruby!

Our pattern-matching expression represents all hands in a single pattern

Page 6: Ruby Egison

6

Very Quick Introduction of Today’s Contents

We can also pattern-match against infinite streams.

… , x, x + 2, …

Page 7: Ruby Egison

7

Source and English Document are on GitHub https://github.com/egison/egison-ruby

Page 8: Ruby Egison

8

Please Check My Blog Article, too

Page 9: Ruby Egison

9

We wrote also Japanese Documents on Qiita

1,0004 views, 185 Hatena Bookmarks!

Page 10: Ruby Egison

10

1. Introduction - What is Pattern Matching? 2. Egison Pattern Matching in Ruby 3. Let’s Try Egison Gem 4. Future Goal – Application of Egison

Table of Contents

Page 11: Ruby Egison

11

1. Introduction - What is Pattern Matching? 2. Egison Pattern Matching in Ruby 3. Let’s Try Egison Gem 4. Future Goal – Application of Egison

Table of Contents

Page 12: Ruby Egison

12

What is Pattern Matching?

A feature of programming languages that simplifies programs.

• Data Decomposition • Pattern-matching enables us to extract and

examine the value of an instance variable very easily

• Conditional Branches • Pattern-matching eliminates nested and

complex conditional branches

Page 13: Ruby Egison

13

Greeting Demonstration

Page 14: Ruby Egison

14

Greeting Demonstration

Page 15: Ruby Egison

15

Greeting Demonstration

Page 16: Ruby Egison

16

Greeting Demonstration

fail

Page 17: Ruby Egison

17

Greeting Demonstration

success

Page 18: Ruby Egison

18

Greeting Demonstration

Page 19: Ruby Egison

19

Flow of Pattern Matching

fail

Page 20: Ruby Egison

20

Flow of Pattern Matching

success

Page 21: Ruby Egison

21

Greeting Demonstration

Page 22: Ruby Egison

22

Flow of Pattern Matching

fail

Page 23: Ruby Egison

23

Flow of Pattern Matching

fail

Page 24: Ruby Egison

24

Flow of Pattern Matching

fail

Page 25: Ruby Egison

25

Flow of Pattern Matching

fail

Page 26: Ruby Egison

26

Flow of Pattern Matching

success

Page 27: Ruby Egison

27

Greeting Demonstration

Page 28: Ruby Egison

28

1. Introduction - What is Pattern Matching? 2. Egison Pattern Matching in Ruby 3. Let’s Try Egison Gem 4. Future Goal – Application of Egison

Table of Contents

Page 29: Ruby Egison

29

The Features of Egison Pattern-Matching in Ruby

We have features in addition to simplification of data decomposition and conditional branches

• Customizable Way of Pattern-Matching • We can do pattern-matching against not only

fixed data such as lists but also multisets and sets that have multiple ways of decomposition

• Non-Linear Pattern-Matching • We allow multiple occurrences of same

variables in patterns • Pattern-Matching with Backtracking

• We allow multiple results of pattern-matching even infinite results

Page 30: Ruby Egison

30

Customizable Pattern Matching For Various Data

We can define how to pattern-match for each data types

Page 31: Ruby Egison

31

Pattern Matching with Multiple Results

We can handle multiple results of pattern-matching

Page 32: Ruby Egison

32

Element Patterns and Collection Patterns

A literal that starts with ‘_’ is a pattern-variable. We can refer the result of pattern-matching through them.

Page 33: Ruby Egison

33

Element Patterns and Collection Patterns

A subcollection pattern matches the subcollection of the target array. A subcollection pattern starts with ‘*’.

Page 34: Ruby Egison

34

Element Patterns and Collection Patterns

A literal that starts with ‘_’ is a pattern-variable. We can refer the result of pattern-matching through them.

Page 35: Ruby Egison

35

Element Patterns and Collection Patterns

A subcollection pattern matches the subcollection of the target array. A subcollection pattern starts with ‘*’.

Page 36: Ruby Egison

36

Combinations with Pattern Matching

… , x, …, y, …

… , x, …, y, …, z, …

Page 37: Ruby Egison

37

Non-Linear Pattern Matching

A Pattern whose form is ‘__("...")’ is a value pattern

Page 38: Ruby Egison

38

Non-Linear Pattern Matching

A Pattern whose form is ‘__("...")’ is a value pattern

We can omit (“ and “) when enclosed with them is a single variable

Page 39: Ruby Egison

39

Poker Hands Analyzer in Ruby

Page 40: Ruby Egison

40

The Pattern for Straight Flash

Pattern for straight flash

Page 41: Ruby Egison

41

The Pattern for Straight Flash

Pattern for straight flash

Page 42: Ruby Egison

42

Poker Hands Analyzer in Ruby

Same suit with $s

Page 43: Ruby Egison

43

Poker Hands Analyzer in Ruby

Same suit with $s

We can write any expression after ‘__’

Numbers are serial from $n

Page 44: Ruby Egison

44

Poker Hands Analyzer in Ruby

Pattern for two pairs

Page 45: Ruby Egison

45

The Pattern for Two Pairs

Pattern for two pairs

Page 46: Ruby Egison

46

The Pattern for Two Pairs

Pattern for two pairs

Same number with $m

Same number with $n

Page 47: Ruby Egison

47

The Pattern for Two Pairs

Pattern for two pairs

Same number with $m

Same number with $n

Non-linear patterns have very strong power

Page 48: Ruby Egison

48

Poker Hands Analyzer in Ruby

Non-linear patterns enables to represent all hands in a single pattern

Page 49: Ruby Egison

49

We can also pattern-match against infinite streams.

… , x, x + 2, …

Pattern Matching against Streams with Infinite Results

Page 50: Ruby Egison

50

One More Interesting Demonstration

We can also enumerate prime triplets using and-patterns and or-patterns effectively.

Page 51: Ruby Egison

51

1. Introduction - What is Pattern Matching? 2. Egison Pattern Matching in Ruby 3. Let’s Try Egison Gem 4. Future Goal – Application of Egison

Table of Contents

Page 52: Ruby Egison

52

Egison Gem is on GitHub https://github.com/egison/egison-ruby

Page 53: Ruby Egison

53

Try Egison Gem!

$ git clone https://github.com/egison/egison-ruby.git$ cd egison-ruby/$ bundle install$ make$ ls sample/… sample/poker_hands.rb …$ ruby sample/poker_hands.rb…

Egison Gem is written in pure Ruby. Installation is very easy via RubyGems.

We have prepared a lot of samples. Please try all of them.

Page 54: Ruby Egison

54

1. Introduction - What is Pattern Matching? 2. Egison Pattern Matching in Ruby 3. Let’s Try Egison Gem 4. Future Goal – Application of Egison

Table of Contents

Page 55: Ruby Egison

55

Introduction of the Egison Programming Language

Page 56: Ruby Egison

56

Profile of Egison

Paradigm Pattern-matching-oriented, Purely functional

Author Satoshi Egi

License MIT

Version 3.3.12 (2014/9/18)

First Released 2011/5/24

Filename Extension .egi

Implemented in Haskell (about 3,500 lines)

I have created Egison to represent human’s intuition directly.

Page 57: Ruby Egison

57

The Additional Features of Pure Egison

We have features in additon to simplification of data decomposition and conditional branches

• Ways of Pattern-Matching is More Customizable • We can customize the way of pattern-

matching against not only against collection such as lists, multisets and sets

• Pattern Modularization with Lexical Scoping • We allow multiple occurrences of same

variables in patterns

Page 58: Ruby Egison

58

More complex example, Mahjong

Page 59: Ruby Egison

59

More complex example, Mahjong

Two same tiles

Three consecutive tiles

Three same tiles

Page 60: Ruby Egison

60

More complex example, Mahjong

Two same tiles

Three consecutive tiles

Three same tiles

Seven twins or one twin + four shuntsu or kohtsu

Page 61: Ruby Egison

61

More complex example, Mahjong

Two same tiles

Three consecutive tiles

Three same tiles

Seven twins or one twin + four shuntsu or kohtsu

Pattern modularization makes programming more simple!

Page 62: Ruby Egison

62

Egison on an online media

Page 63: Ruby Egison

63

Egison on Hacker News

Egison was actively discussed on Hacker News twice!

Page 64: Ruby Egison

64

Total session is 14,109

Access Map of the Website in This Half Year (2014/1/14 – 2014/7/14)

Page 65: Ruby Egison

65

Egison will be taught in the University of Tokyo by Prof. Hagiya

Egison will have big impact on the academic world, too!

Page 66: Ruby Egison

66

Growth of Community in This Year (2013/11/15 – 2014/9/18)

•  Mailing List Members •  Before: 12 – Only my friends •  Current: 23 – Communicating in English

•  Stargazers on GitHub •  Before: 12 – Only my friends •  Current: 214 – People from all over the world!

Page 67: Ruby Egison

67

My Mind Map on Programming Language

Page 68: Ruby Egison

68

My Mind Map on Programming Language

Page 69: Ruby Egison

69

My Mind Map on Programming Language

Egison is pioneering this field!

Page 70: Ruby Egison

70

Application of Egison

Page 71: Ruby Egison

71

There is a Wide Range of Application

•  Daily programming •  Able to express complex tasks simply

•  Data access and analysis •  Work as the most elegant query language •  Able to access the wide range of data types in a

unified way •  Natural language processing

•  Able to handle complex structures intuitively as humans do in their mind

•  AI (Mathematical expression handling) •  Able to handle various mathematical and abstract

notions directly

Page 72: Ruby Egison

72

Query example

•  Query that returns twitter users who are followed by “__Egi” but not follow back “__Egi”.

id integer name string

User:

from_id integer to_id Integer

Follow:

Page 73: Ruby Egison

73

SQL version

•  Complex and difficult to understand •  Complex where clause contains “NOT EXIST” •  Subquery

Page 74: Ruby Egison

74

Egison version

•  Very Simple •  No where clauses •  No subquery

Page 75: Ruby Egison

75

Egison version

•  Very Simple •  No where clauses •  No subquery

Joining 4 tables

1. Get id of “__Egi” 2. Followed by ‘uid’ 3. But not follow back not 4. Get name of ‘fid’ Return the results

Page 76: Ruby Egison

76

My Dream

•  Daily programming •  Able to express complex tasks simply

•  Data access and analysis •  Work as the most elegant query language •  Able to access the wide range of data types in a

unified way •  Natural language processing

•  Able to handle complex structures intuitively as humans do in their mind

•  AI (Mathematical expression handling) •  Able to handle various mathematical and abstract

notions directly

•  Programs that find interesting things •  Programs that build math theories •  Programs that generate programs

Page 77: Ruby Egison

77

[ANN]Articles on Egison will be on CodeIQ MAGAZINE

Page 78: Ruby Egison

78

[ANN] We will post Egison problems on CodeIQ

Page 79: Ruby Egison

79

Thank you!

Please try Egison and give us feedback!

[email protected]

Page 80: Ruby Egison

80

Problem

What is the 40th pair of prime numbers of the form (p, p+8)?

Please try and answer to @Egison_Lang!

e.g. [3, 11] [5, 13] [11, 19]…