Michael Homer Programming Languages 2 Research focus Making - - PowerPoint PPT Presentation
Michael Homer Programming Languages 2 Research focus Making - - PowerPoint PPT Presentation
Michael Homer Programming Languages 2 Research focus Making programming more accessible to non-programmers. Making (sub-)language creation more available to programmers Packaging 3 Enabling embedded domain-specific languages We
Programming Languages
2
Research focus
- Making programming more
accessible to non-programmers.
- Making (sub-)language creation
more available to programmers
- Packaging
3
Enabling embedded domain-specific languages We want to let a programmer write a library that
- extends the language,
- restricts the language,
- or some combination of the two.
Graceful Dialects. Homer et al. ECOOP’14.
4
Extending the language
aDSL ModuleX
dialect "aDSL" ... ... use {x} on(arg) ...
dslhlpr aDSL
dialect "dslhlpr" ... method use(arg1)
- n(arg2) {
...
}
5
Restricting the language
6
Turtle graphics
import "turtle" as turtle def red = turtle .red ... var lineWidth := 1 var lineColour := black method forward(dist) { turtle .move(dist, lineColour, lineWidth)
}
method turnRight(ang) { turtle .turnRight(ang)
}
... method repeat(n)times(blk) { var counter := 1 while {counter <= n} do { blk.apply counter := counter + 1
} }
method atModuleEnd(mod) { turtle . start
}
7
Helping to write restrictive dialects
method checker(ast) { // Loop over every node and examine it for (ast.nodes) do { n −> for (rules) do { r −> ... } }
}
method rule(block) { rules.push(block)
}
method when(pred : UnaryPredicate) error(msg : String) { rule { node −> def matches = pred.match(node) if (matches.andAlso {matches.result}) then { fail(msg) }
} }
8
Enforcing nominal types
rule { req : QualifiedRequest −> def rType = typeOf(req.receiver) match (rType) case { r : Brand −> ... }
}
rule { decl : Let −> def eType = typeOf(decl.value) if (BrandType.match(eType)) then { eType.name := decl.name.value
} }
209 LOC total
Brand Objects for Nominal Typing. Jones, Homer, and Noble. ECOOP’15.
9
GrAPL
dialect "grapl" N ← [1, 2, 3, 4] print (N) // Prints [1, 2, 3, 4] print (N + 2) // Prints [3, 4, 5, 6] print (+/N) // Prints 10 // Standard Lotto example. print (L[ |
(L ← (n 6 ? 40))])
// Calculate primes up to 20. print ((P ← (n 1 ↓ ι20))/(P∈(P◦·∗P)))
10
Combining tiled and textual programming
Combining Tiled and Textual Views of Code. Homer and Noble. VISSOFT’14. 11
Tiled Grace
http://ecs.vuw.ac.nz/˜mwh/minigrace/tiled/
Combining Tiled and Textual Views of Code. Homer and Noble. VISSOFT’14.
12
Switching views
- Median participant switched 6 times.
- 75% switched at least 4 times.
- Half of participants spent between 24% and 52% of their time in
text view.
2 4 6 5 10 15 20
Total number of switches of view count
13
Error reporting
“The way of showing errors was amazingly helpful.” “It also gave specific detail about the error.” “Error finding and handling looks great, is very easy and straightforward.”
5 10 1 Agree 2 3 4 Neutral 5 6 7 Disagree
Finding errors in the code was easy count
Finding errors in the code was easy 1 Agree 2 3 4 Neutral 5 6 7 Disagree
14
Engagement
0:15 0:30 0:45 1:00 1:30 2:00 2:30 3:00 3:30 4:00 4:30 5:00 6:00 7:00 8:00 9:00 10:00 11:00 12:00 13:00 14:00 10 20 30
count Time spent unprompted on Task 5 (minutes)
More or less time than this Less More
70% used system when they didn’t have to.
15
Engagement
0% 10% 20% 30% 40% 1 Agree 2 3 4 Neutral 5 6 7 Disagree
The system was fun to use Percentage choosing each option
How many technologies used Ten or fewer More than ten
16
Ongoing
- Gradual typing
- Concatenative programming
- Package management
- Object inheritance
17
Outlook
18
References
- Graceful Dialects. European Conference on Object-Oriented Programming
(ECOOP), 2014. Michael Homer, Timothy Jones, James Noble, Kim B. Bruce, and Andrew P . Black.
- Brand Objects for Nominal Typing. European Conference on Object-Oriented
Programming (ECOOP), 2015. Timothy Jones, Michael Homer, and James Noble.
- From APIs to Languages: Generalising Method Names. Dynamic Language
Symposium (DLS), 2015. Michael Homer, Timothy Jones, and James Noble.
- Patterns as Objects in Grace. Dynamic Language Symposium (DLS), 2012.
Michael Homer, James Noble, Kim B. Bruce, Andrew P . Black, and David J. Pearce.
- Combining Tiled and Textual Views of Code. IEEE Working Conference on
Software Visualisation (VISSOFT), 2014. Michael Homer and James Noble.
- A Tile-based Editor for a Textual Programming Language. IEEE Working
Conference on Software Visualisation (VISSOFT), 2013. Michael Homer and James Noble.
19
Michael Homer
Multi-part method names
method check(x) between(low) and(high) { return (x >= low) && (x <= high)
}
if (x < y) then { print "less"
} else {
print "Not less"
}
21
Repeated parts
+ before a part means it can appear more than once. method a(x) +b(y) { ... }
accepts requests
a(1) b(2) a(1) b(2) b(3) a(1) b(2) b(3) b(4) ...
∗ means zero or more times.
22
Parameter binding
Parameters in repeated parts are bound as sequences.
method a(w) +b(x, y) c(z) { // x and y are sequences here for (x) do { xv −> print(xv) } // w and z are not print (w)
}
23
Optional parts
? before a part means it is optional. method a(x) ?b(y) { ... }
accepts requests
a(1) a(1) b(2)
Parameters are also bound as trivial sequences. 24
Grouping parts
Parentheses around parts create a group for other modifiers.
method a(x) ?(b(y) c(z)) { ... }
accepts requests
a(1) a(1) b(2) c(3)
but not
a(1) b(2) a(1) c(3)
25
Alternations
| inside a group creates an alternation.
method a(x) (b(y) | c(z)) { ... }
accepts requests
a(1) b(2) a(1) c(3)
26
Prefixes
Methods are uniquely identified by their prefix of required parts.
Declaration Prefix 1
a(x) b(y) c(z)
a b c 2
a(x) b(y) ?c(z)
a b 3
a(x) ?b(y) c(z)
a 4
a(x) +b(y) c(z)
a b
Shortest prefix used: there is at most one method in an object that could be selected to respond to any given request.
27
Greedy matching
Match request and declaration parts left to right, committing to potential matches eagerly. Greedy matching of parts allows concrete error messages: Error: expected to see part "finally" but saw part "else" instead. 28
Uncallable methods
method a(x) ∗b(y) b(z) {}
There is never a b left for the last part to match. However, the requests presumably intended are accepted by:
method a(x) +b(y) {}
29
match case... method match(target) +case(cases) { for (cases) do { case −> def mr = case.match(target) if (mr) then { return mr.result
} }
fail "Match was not exhaustive"
}
30
Control structures
method if(bool) then(blk) +( elseif (conds) then(blks)) ?else(elseblk) { ... } method try(blk) ∗catch(catchblk) ? finally ( finallyblk ) { ... }
31
Testing DSL
method feature(name) ?background( initialisation ) +(scenario(desc) ?(Given(context) ∗And(contexts)) When(event) Then(expr) ShouldBe(val)
∗(And(exprs) ShouldBe(vals)))
Based on http://cucumber.io/’s testing language 32
Creating a button
Declarative construction of a button:
gtk.button { document.save } label "Save" image(icons.floppy disk) position(gtk. position . right ) relief (gtk. relief .none)
Well-formedness of method name guarantees semantic validity. 33
Embedded DSL: Query language
from(students) Where { s −> s.enrolledIn "COMP231" } Where { s −> s.age > 25 } OrderBy { s −> s.gpa } ThenBy { s −> s.age } ThenBy { s −> s.id } Select { s −> s.name }
from s in students where s.enrolledIn("COMP231") where s.age > 25
- rderby s.gpa, s.age, s.id
select s.name 34
selectProjection .do { p −> return data.select(p)
}
thenBys.do { ifPresent −> for (ifPresent) do { t −> data := data.thenBy(t)
} }
35
Library initialisation
method button(handler : Block) ( ( label( text : String) | mnemonic(mt : String)) ?(image(textImage : Image) ?position(imagePos:PositionType) )
| namedIcon(name : String, size : IconSize) | image(image : Image) | widget(w : Widget)
) ? relief ( style : ReliefStyle) ?alignment(xalign : Number, yalign : Number) { ... }
36
Greedy matching
To match request parts to declaration parts:
- 1. Match the prefix of both.
This is known to match exactly by the method resolution rules.
- 2. If the current request part is accepted by the current declaration
part, commit to it (bind the request arguments into the declaration parameters) and advance the request part. If the declaration part is a group, it matches if the prefix of the group matches the request starting at this point. (a) If the declaration part is not a + or ∗, advance the declaration part. Go to 2 37
- 3. If the current request part is not accepted by the current
declaration part: (a) If the current declaration part is a ∗ or ?, advance the declaration part and go to 2. (b) If the current declaration part is a + that has already consumed at least one request part, advance the declaration part and go to 2. (c) Otherwise, report an error “expected part X, saw part Y”.
- 4. If there are no more declaration parts and there are remaining
request parts, report an error.
- 5. If there are no more request parts, and any remaining declaration
part is not optional, report an error. 38
Enabling embedded domain-specific languages We want to let a programmer write a library that
- extends the language,
- restricts the language,
- or some combination of the two.
Graceful Dialects. Homer et al. ECOOP’14.
39
Extending the language
aDSL ModuleX
dialect "aDSL" ... ... use {x} on(arg) ...
dslhlpr aDSL
dialect "dslhlpr" ... method use(arg1)
- n(arg2) {
...
}
40
Restricting the language
41
Pattern matching
match(node) case { v : VarDeclaration | TypeDeclaration
−> ... }
Destructuring:
case { w : WhileDo(condition, body)
−> if (condition == ...) then {
...
} }
Patterns as Objects in Grace. Homer et al. DLS’12.
42
Building a type checker
rule { req : Request −> match(typeOf(req.in).getMethod(req.name)) case { : NoSuchMethod −> fail "no such method" } case { mt : MethodType −> for (mt.signature) and(req.with) do { s, w −> for (s.params) and(w.args) do { p, a −> if (!typeOf(a).isSubtypeOf(p.decType)) then { fail "argument does not satisfy parameter type" }
} }
return mt.returnType // Type of expression
} }
Brand Objects for Nominal Typing. Jones, Homer, and Noble. ECOOP’15.
43
Contents
Title slide 1 Programming languages 2 Research focus 3 Enabling embedded domain-specific languages 4 Extending the language . . . . . . . . . . . . . . . . . . . 5 Restricting the language . . . . . . . . . . . . . . . . . . . 6 44
Turtle graphics . . . . . . . . . . . . . . . . . . . . . . . . 7 Helping to write restrictive dialects . . . . . . . . . . . . . . 8 Enforcing nominal types . . . . . . . . . . . . . . . . . . . 9 GrAPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Combining tiled and textual programming 11 Tiled Grace . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Switching views . . . . . . . . . . . . . . . . . . . . . . . 13 Error reporting . . . . . . . . . . . . . . . . . . . . . . . . 14 Engagement . . . . . . . . . . . . . . . . . . . . . . . . . 15 Fun by experience . . . . . . . . . . . . . . . . . . . 16 45
Ongoing 17 Outlook 18 References 19 Ending slide 21 Multi-part method names 22 46
Variable Parts 22 Repeated parts . . . . . . . . . . . . . . . . . . . . . . . . 23 Parameter binding . . . . . . . . . . . . . . . . . . . . . . 24 Optional parts . . . . . . . . . . . . . . . . . . . . . . . . 25 Grouping parts . . . . . . . . . . . . . . . . . . . . . . . . 26 Alternations . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Calling methods 27 Prefixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Greedy matching . . . . . . . . . . . . . . . . . . . . . . . 29 Uncallable methods . . . . . . . . . . . . . . . . . . . 30 47
Examples 30 match . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Control structures . . . . . . . . . . . . . . . . . . . . . . 32 Testing DSL . . . . . . . . . . . . . . . . . . . . . . . . . 33 Creating a button . . . . . . . . . . . . . . . . . . . . . . . 34 Embedded DSL: Query language . . . . . . . . . . . . . . 35 Library initialisation . . . . . . . . . . . . . . . . . . . . . . 37 Greedy matching . . . . . . . . . . . . . . . . . . . . . . . 38 48
Enabling embedded domain-specific languages 40 Extending the language . . . . . . . . . . . . . . . . . . . 41 Restricting the language . . . . . . . . . . . . . . . . . . . 42 Pattern matching . . . . . . . . . . . . . . . . . . . . 43 Building a type checker . . . . . . . . . . . . . . . . . . . . 44 Contents 45 49