Building A Graceful Language Design by Instructor Timothy Jones - - PowerPoint PPT Presentation

building a graceful language
SMART_READER_LITE
LIVE PREVIEW

Building A Graceful Language Design by Instructor Timothy Jones - - PowerPoint PPT Presentation

Building A Graceful Language Design by Instructor Timothy Jones Victoria University of Wellington tim@ecs.vuw.ac.nz December 16, 2013 The Language The Language Frustration with languages used for teaching Pascal is old, Java is bloated


slide-1
SLIDE 1

Building A Graceful Language

Design by Instructor

Timothy Jones Victoria University of Wellington

tim@ecs.vuw.ac.nz December 16, 2013

slide-2
SLIDE 2

The Language

The Language

Frustration with languages used for teaching Pascal is old, Java is bloated Grace is the absence of everything that indicates pain or difficulty, hesitation or incongruity. — William Hazlitt

1

slide-3
SLIDE 3

The Language

Goals

◮ Support multiple paradigms

◮ Objects ◮ Scripting/Procedural ◮ Functional

◮ Minimise conceptual burden ◮ Diverse applications within syntactically consistent language 2

slide-4
SLIDE 4

The Language

First Taste

In Java: public class Main { public static void main(String[] args) { System.out.println("Hello world"); } } In Grace: print "Hello world"

3

slide-5
SLIDE 5

The Language

Objects

var john := object { def name is public = "John" method say(phrase) { print "John says {phrase}" } print "John has been born!" }

4

slide-6
SLIDE 6

The Language

Gradually Typed

type Person = { name → String say(phrase : String) → Done } var kate : Person := object { def name : String is public = "Kate" method say(phrase : String) → Done { print "Kate says {phrase}" } }

5

slide-7
SLIDE 7

The Language

Classes

class aPerson.named(name′) → Person { def name is public = name′ method say(phrase) { print "{name} says {phrase}" } } Translates to: def aPerson = object { method named(name′) → Person {

  • bject {

def name is public = name′ method say(phrase) { print "{name} says {phrase}" } } } }

6

slide-8
SLIDE 8

The Language

Blocks

First class functions: def double = { x → x + x } double.apply 10 Like numbers and strings, no need for parens in method requests method shifUp(list : ListNumber) { list.map { x → x + 1 } }

7

slide-9
SLIDE 9

The Language

Control Structures

Methods may be written ‘mixfix’ method substringFrom(start) to(end) { ... } str.substringFrom 3 to 5 Combined with blocks, we can define our own control structures: method while(cond) do(block) { if(cond.apply) then { block.apply while(cond) do(block) } } while { x < y } do { x := x ∗ 2 }

8

slide-10
SLIDE 10

The Language

Dialects

Change the local definitions (but not the syntax) of a module Check that the module conforms to certain rules (eg. must use types, no mutable variables) The entire static type system is just a dialect!

9

slide-11
SLIDE 11

The Designers

The Designers

◮ Andrew Black ◮ Kim Bruce ◮ James Noble 10

slide-12
SLIDE 12

The Designers

The Designers

◮ Object Constructors and Dynamic Typing ◮ Kim Bruce ◮ James Noble 10

slide-13
SLIDE 13

The Designers

The Designers

◮ Object Constructors and Dynamic Typing ◮ Classes and Static Typing ◮ James Noble 10

slide-14
SLIDE 14

The Designers

The Designers

◮ Object Constructors and Dynamic Typing ◮ Classes and Static Typing ◮ The Mediator 10

slide-15
SLIDE 15

The Designers

The Audience

Designed by Instructors, for Instructors Differences of opinion in the design represent real-world differences

  • f opinion in how OO should be taught

Compromise leads to interesting design decisions!

11

slide-16
SLIDE 16

Object Inheritance

Implementing Inheritance

Objects-only? Delegation! def snake = object { def noise = "hiss" method makeNoise { print(self.noise) } } def ratleSnake = object { inherits snake def noise = "rattle" }

12

slide-17
SLIDE 17

Object Inheritance

Implementing Inheritance

Objects-only? Delegation! def snake = object { def noise = "hiss" method makeNoise { print(self.noise) // self is bound to the receiver of makeNoise } } def ratleSnake = object { inherits snake def noise = "rattle" }

12

slide-18
SLIDE 18

Object Inheritance

The Identity Problem

The two objects have separate identities! def snake = object { def this = self def noise = "hiss" method makeNoise { print(this.noise) } } def ratleSnake = object { inherits snake def noise = "rattle" }

13

slide-19
SLIDE 19

Object Inheritance

‘Becomes’ Inheritance

Solution: an inheriting object merges identities with its super object class aSnake.new { def this = self def noise = "hiss" method makeNoise { print(this.noise) // identity of self is rewritten to be rattleSnake } } def ratleSnake = object { inherits aSnake.new // can only inherit from a fresh object def noise = "rattle" }

14

slide-20
SLIDE 20

Object Inheritance

Initialisation Problem

Objects have different structure at each part of the constructor chain class aSnake.new { def noise = "hiss" self.makeNoise // self is not yet rattleSnake } def ratleSnake = object { inherits aSnake.new def noise = "rattle" method makeNoise { print(self.noise) } }

15

slide-21
SLIDE 21

Object Inheritance

Constructor Specialisation

Every method with a tail-call object has two variants method new {

  • bject {}

} method new_inherits(self) { ... } Essentially JavaScript’s new Snake vs. Snake.call(this)

16

slide-22
SLIDE 22

Object Inheritance

Abstract methods

class aBird.new { method fly { if(self.canFly) then { print "take off!" } else { print "crashed!" } } } def kiwi = object { inherits aBird.new def canFly = false }

17

slide-23
SLIDE 23

Object Inheritance

Abstract methods

class aBird.new { // is this class well-typed? method fly { if(self.canFly) then { print "take off!" } else { print "crashed!" } } } def kiwi = object { inherits aBird.new def canFly = false }

17

slide-24
SLIDE 24

Typing Self

Typing Self

What is the type of self? The value is never explicitly given a type, so how do you supply it?

18

slide-25
SLIDE 25

Typing Self

Typing Self

What is the type of self? The value is never explicitly given a type, so how do you supply it? Default is dynamic, add extra information in a dialect

18

slide-26
SLIDE 26

Typing Self

Typing Self

What is the type of self? The value is never explicitly given a type, so how do you supply it? Default is dynamic, add extra information in a dialect Add annotations to prevent all-or-nothing scenario

18

slide-27
SLIDE 27

Status Report

How far away are we?

◮ Trial courses starting next year ◮ Tooling and development environments the next major goal ◮ Ready for general consumption by 2015? 19

slide-28
SLIDE 28

Links

Links

gracelang.org ecs.vuw.ac.nz/~mwh/minigrace/js github.com/mwh/minigrace grace-core@cecs.pdx.edu

20