introduction to gura programming language
DESCRIPTION
Introduction to Gura Programming Language. This has been translated from a material written in Japanese for LL Diver event on Aug 23, 2014.TRANSCRIPT
Introduction to Gura Programming Language
www.gura-lang.org
Copyright © 2014 [email protected]
LL Diver on Aug 23, 2014
Copyright (C) 2014 ypsitau 2/49
Author
Name Yutaka Saito
Experience Embedded Firmware to GUI
Job History Electric-app maker, chip maker, venture
Current Job Free, unemployed rather
Favorite Lang C++, Gura
devoted to Gura
Copyright (C) 2014 ypsitau 3/49
Extension Module
Iterator Operation
Basic Specification
What's Gura?
Agenda
What's Gura?
Copyright (C) 2014 ypsitau 4/49
What's Gura?
Repeat processes appear in programs so often.
for (i = 0; i < 10; i++) {}
people.each do |person|end
for x in range(10):hoge
Can I deal them without verbose control sequence?
Copyright (C) 2014 ypsitau 5/49
Case Study (1)
Here's a number sequence: -3, -2, -1, 0, 1, 2, 3.Make a list of squared values of them.
Think it with yourfavorite language ...
x
y
Copyright (C) 2014 ypsitau 6/49
Gura's Program (1)
x = [-3, -2, -1, 0, 1, 2, 3]y = x * x
Copyright (C) 2014 ypsitau 7/49
Gura's Program (1)
x = [-3, -2, -1, 0, 1, 2, 3]y = x * x
Multiplying iterator is generated.
Its evaluation creates list.
Iterator Lists generate iterators.1
2
3Evaluation
List List
Iterator
Iterator
List
Copyright (C) 2014 ypsitau 8/49
Case Study (2)
Create a program that reads a text file and printsits content along with line numbers.
Think it with yourfavorite language …
1: #include <std2: int main()3: {4: printf(“H5: }
Copyright (C) 2014 ypsitau 9/49
Gura's Program (2)
printf('%d: %s',1.., readlines('hello.c'))
Copyright (C) 2014 ypsitau 10/49
Gura's Program (2)
printf('%d: %s',1.., readlines('hello.c'))
Iterator that executes printffor each item is generated.
It's destroyed after evaluated.
1
2
Iterator Iterator
Iterator
Evaluation
Copyright (C) 2014 ypsitau 11/49
After All, Gura is ..
A language that can generate iteratorsfrom iterators and evaluate them.
Gura calls this operation “Mapping”.
Iterator
IteratorIterator
Iterator
Iterator Iterator
Iterator Evaluation
Copyright (C) 2014 ypsitau 12/49
Expected Benefits
Simplifies codes of repeat process.
Facilitates parallel computing, maybe.
1
2
Copyright (C) 2014 ypsitau 13/49
Idea for Parallel Computing
Elements in iterator generated last
Iterator generationrequires less load.
Evaluation stageneeds much load.
Distributes elements to processorsfor evaluation. Eval
Iterator
IteratorIterator
Iterator
Iterator Iterator
Iterator Evaluation
Eval Eval
Copyright (C) 2014 ypsitau 14/49
Extension Module
Iterator Operation
Basic Specification
What's Gura?
Agenda
Basic Specification
Copyright (C) 2014 ypsitau 15/49
Basic Specification
Function
Control Sequence
OOP
Collection
Scope Management
Copyright (C) 2014 ypsitau 16/49
Basic Spec (1) Function
x = f(a => 3, b => 4)
f(a:number, b:number) = {a * a + b * b
}
x = f(3, 4)
Definition (1)
Call
Type can be specified
Named arguments
Copyright (C) 2014 ypsitau 17/49
Basic Spec(1) Function
f(3) // a=3, b=[]f(3, 1) // a=3, b=[1]f(3, 1, 4, 1) // a=3, b=[1,4,1]
f(a, b*) = {// any job
}
Call
Definition (2) Variable-length argument that takes more than 0 value.
b+ takes more than 1 value.
Copyright (C) 2014 ypsitau 18/49
Basic Spec(1) Function
my_loop(n) {block} = {while (n > 0) {
block()n -= 1
}}
my_loop(3) {println('hello')
}
Definition (3)
Call
Takes block expression as function object.{block?} means an optional block.
Copyright (C) 2014 ypsitau 19/49
Basic Spec(2) Control Sequence
if (…) {
} elsif (…) {
} elsif (…) {
} else {
}
try {
} catch (…) {
} catch (…) {
}
for (…) {
}
repeat (…) {
}
while (…) {
}
Repeat Branch Exception
cross (…) {
}
Copyright (C) 2014 ypsitau 20/49
Basic Spec(3) OOP
Fruit = class {__init__(name:string, price:number) = {
this.name = namethis.price = price
}Print() = {
printf('%s %d\n', this.name, this.price)}
}
fruit = Fruit('Orange', 90)fruit.Print()
Class Definition
Instantiation and Method Call
Constructor
Copyright (C) 2014 ypsitau 21/49
Basic Spec(3) OOP
A = class {__init__(x, y) = {
// any jobs}
}
B = class(A) {__init__(x, y, z) = {|x, y|
// any jobs}
}
Inheritance
Arguments forbase class constructor
Copyright (C) 2014 ypsitau 22/49
Basic Spec(4) Collection
a = [3, 1, 4, 1, 5, 9]b = ['zero', 'one', 2, 3, 'four', 5]
c = %{ `a => 3, `b => 1, `c => 4 }d = %{
'いぬ' => 'dog', 'ねこ' => 'cat'}
List
Dictionary
Copyright (C) 2014 ypsitau 23/49
Basic Spec(5) Scope Management
create_counter(n:number) = {function {
n -= 1}
}
c = create_counter(4)c() // returns 3c() // returns 2c() // returns 1
Closure
Each function has lexical scope.
Copyright (C) 2014 ypsitau 24/49
Extension Module
Iterator Operation
Basic Specification
What's Gura?
Agenda
Iterator Operation
Copyright (C) 2014 ypsitau 25/49
Iterator Operation
Implicit Mapping
Member Mapping
Function
Repeat Control
Mapping
Generation
Iterator Operation: Mapping and Generation
Iterator
Iterator
Iterator
Iterator
Iterator
Copyright (C) 2014 ypsitau 26/49
Gura's List and Iterator
List
Iterator
['apple', 'orange', 'grape']
('apple', 'orange', 'grape')
All the elements are stored in memory.
Each element would be generated.
Capable of random access
Only evaluation could make next element available
Copyright (C) 2014 ypsitau 27/49
Gura's List and Iterator
Iterator
List Evaluation
Generation
Copyright (C) 2014 ypsitau 28/49
Iterator Operation(1) Implicit Mapping
Generates iterator embedding function or operator
Functionor
Operator
argument values returned value
Implicit Mapping
Iterator
Iterator
Iterator
Iterator
Copyright (C) 2014 ypsitau 29/49
Iterator Operation(1) Implicit Mapping
f(a:number, b:number) = {a * b
}
Usual Function
f(a:number, b:number):map = {a * b
}
Mappable Function Specifies attribute :map
Copyright (C) 2014 ypsitau 30/49
Iterator Operation(1) Implicit Mapping
f(3, 4)Number
f([2, 3, 4], [3, 4, 5])List
f((2, 3, 4), (3, 4, 5))Iterator
f(5, (3, 4, 5))Number andIterator
Answer: 12
Answer: [6, 12, 20]
Answer: (6, 12, 20)
Answer: (15, 20, 25)
Copyright (C) 2014 ypsitau 31/49
Iterator Operation(1) Implicit Mapping
List
Scalar
Iterator
List
Others
Mapping process depends arguments' data type
IteratorCategorizedata types
Three rules to apply mapping
Copyright (C) 2014 ypsitau 32/49
Iterator Operation(1) Implicit Mapping
Generates an iteratorif at least one iterator exists in arguments.
Rule 1
Iterator
Iterator
Iterator
List
Iterator
Scalar
Iterator
Iterator
Iterator
Copyright (C) 2014 ypsitau 33/49
Iterator Operation(1) Implicit Mapping
Generates a list if there's no iterator andat least one list exists in arguments.
Rule 2
List
List
List
Scalar
List
List
Copyright (C) 2014 ypsitau 34/49
Iterator Operation(1) Implicit Mapping
Generates a scalarif only scalars exist in arguments.
Rule 3
Scalar
ScalarScalar
Copyright (C) 2014 ypsitau 35/49
Iterator Operation(2) Member Mapping
Member Mapping
fruits[0] fruits[1] fruits[2]
name name name
Print() Print() Print()
price price price
List of instances
Generates an iterator to access instance members.
Copyright (C) 2014 ypsitau 36/49
Iterator Operation(2) Member Mapping
Member Mapping
fruits[0] fruits[1] fruits[2]
name name name
Print() Print() Print()
price price price
Iterator
Generates an iterator to access instance members.
fruits:*name
fruits:*price
fruits:*Print()
List of instances
Copyright (C) 2014 ypsitau 37/49
Iterator Operation(2) Member Mapping
sum = 0for (fruit in fruits) {
sum += fruit.price}println(sum)
println(fruits:*price.sum())
Prints summation of Fruit instance's member price.Task
Solution1 Using repeat control
Using Member MappingSolution2
Copyright (C) 2014 ypsitau 38/49
Iterator Operation(3) Function
Function A function should return data sequenceby an iterator, not a list.
rtn = readlines('hello.c')
rtn = range(10)
Iterator to generate strings of each line
Iterator to generate numbers from 0 to 9
Design PolicyIterator
Copyright (C) 2014 ypsitau 39/49
Iterator Operation(3) Function
rtn = readlines('hello.c'):list
rtn = range(10):list
List containing strings of each line
List containing numbers from 0 to 9
ListCall with attribute :listin order to get a listFunction Iterator
Copyright (C) 2014 ypsitau 40/49
Iterator Operation(4) Repeat Control
Generate an iteratorfrom repeat control
Repeat Control
values
repeating job
x = for (…):iter {
}
Evaluation result ofrepeating job comes to be
the iterator's element
Specify attribute :iter
for
repeat
while
cross
Iterator
Copyright (C) 2014 ypsitau 41/49
Iterator Operation(4) Repeat Control
n = 0x = for (i in 0..5):iter {
n += i}
Nothing happens at this time
println(x)
Prints result: 0 1 3 6 10 15
Example of repeat control iterator
Copyright (C) 2014 ypsitau 42/49
Iterator Operation(4) Repeat Control
prime() = {p = []for (n in 2..):xiter {
if (!(n % p.each() == 0).or()) {p.add(n)n
}}
}
primes = prime()
Iterator to generate numbers (2, 3, 5, 7..)
Iterator to generate prime numbers
Copyright (C) 2014 ypsitau 43/49
Extension Module
Iterator Operation
Basic Specification
What's Gura?
Agenda
Extension Module
Copyright (C) 2014 ypsitau 44/49
Extension Module
Design Policy
Gura Interpreter itself should have as little dependencyon OS-specific functions and libraries as possible.It should extend capabilities by importing modules.
Gura Interpreter
ModuleModuleModule
ModuleModuleModule
Copyright (C) 2014 ypsitau 45/49
Bundled Modules
GUIwxWidgets Tk SDL
Graphic Drawing
Cairo OpenGL FreeType
Image Data
JPEG PNG GIF BMP
ICO XPM PPM TIFF
Network
cURL Server
Text Processing
Regular Exp
yamlXMLCSV
markdown
Archive and Compression
TAR ZIP GZIP BZIP
Copyright (C) 2014 ypsitau 46/49
Cooperation between Modules
Gura Interpreter
JPEG PNG GIF BMP ICO XPM PPM TIFF
Cairo OpenGL FreeType wxWidgets Tk SDL
image
Graphic DrawingDisplay Output
Read/Write of Image Data
Copyright (C) 2014 ypsitau 47/49
Application Example
[ ID Photo Made at Home - Gura Shot ]
Creates ID photos by extracted faceimage from a file of digital camera.Outputs results in PDF and JPEG.
JPEG image Cairo image JPEG
wxWidgets
Reading File Composing Image Writing File
Output to Display
Copyright (C) 2014 ypsitau 48/49
Thank you
www.gura-lang.org