The Needle Programming Language

Similar documents
Habanero Extreme Scale Software Research Project

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Analyse et Conception Formelle. Lesson 5. Crash Course on Scala

Formal Engineering for Industrial Software Development

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

CS 111 Classes I 1. Software Organization View to this point:

Chapter 7: Functional Programming Languages

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Functional Programming

Semantic Analysis: Types and Type Checking

Sample CSE8A midterm Multiple Choice (circle one)

Pattern Matching for an Object-oriented and Dynamically Typed Programming Language

TypeScript for C# developers. Making JavaScript manageable

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

Curriculum Map. Discipline: Computer Science Course: C++

Moving from CS 61A Scheme to CS 61B Java

On Understanding Types, Data Abstraction, and Polymorphism

J a v a Quiz (Unit 3, Test 0 Practice)

Java (12 Weeks) Introduction to Java Programming Language

Functional Programming in C++11

Practical Generic Programming with OCaml

Fun with Phantom Types

15-214: Principles of Software Construction 8 th March 2012

Semester Review. CSC 301, Fall 2015

Basic Programming and PC Skills: Basic Programming and PC Skills:

The C Programming Language course syllabus associate level

Checking Access to Protected Members in the Java Virtual Machine

The Smalltalk Programming Language. Beatrice Åkerblom

Lecture 1: Introduction

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Programming and Software Development CTAG Alignments

Timber: A Programming Language for Real-Time Embedded Systems

Chapter 6: Programming Languages

Computing Concepts with Java Essentials

Glossary of Object Oriented Terms

Software quality improvement via pattern matching

Tutorial on Writing Modular Programs in Scala

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

History OOP languages Year Language 1967 Simula Smalltalk

On Understanding Types, Data Abstraction, and Polymorphism

Lecture Data Types and Types of a Language

Computer Programming I & II*

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

El Dorado Union High School District Educational Services

COMPARISON OF OBJECT-ORIENTED AND PROCEDURE-BASED COMPUTER LANGUAGES: CASE STUDY OF C++ PROGRAMMING

Regular Expressions and Automata using Haskell

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Fundamentals of Java Programming

S4 Classes in 15 pages, more or less

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

AP Computer Science Java Subset

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

How Scala Improved Our Java

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Android Application Development Course Program

5. Advanced Object-Oriented Programming Language-Oriented Programming

Class 16: Function Parameters and Polymorphism

16 Collection Classes

Computer Programming I

Compiler Construction

A Scala Tutorial for Java programmers

: provid.ir

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Anatomy of Programming Languages. William R. Cook

Pizza into Java: Translating theory into practice

Java Application Developer Certificate Program Competencies

opalang - Rapid & Secure Web Development

Design by Contract beyond class modelling

Programming Languages CIS 443

Structural Design Patterns Used in Data Structures Implementation

Konzepte objektorientierter Programmierung

Programming Languages

C++FA 5.1 PRACTICE MID-TERM EXAM

#820 Computer Programming 1A

Inheritance, overloading and overriding

Checking Access to Protected Members in the Java Virtual Machine

Stack Allocation. Run-Time Data Structures. Static Structures

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Web Development in Java

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

PL / SQL Basics. Chapter 3

OKLAHOMA SUBJECT AREA TESTS (OSAT )

The V8 JavaScript Engine

CS170 Lab 11 Abstract Data Types & Objects

Programming Language Concepts for Software Developers

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Domains and Competencies

Visual Basic. murach's TRAINING & REFERENCE

Object Oriented Design

Transcription:

The Needle Programming Language The Needle Programming Language 1

What is Needle? Needle is an object-oriented functional programming language with a multimethod-based OO system, and a static type system with parameterized types and substantial ML-style type inference. 2

1.Create extensible ypes datat via sub clasing 2.Use edrrfunctions higher-o to build complex abstractions 3.Use static nipyg t to e mak y"co \slop ding easier What Is Needle For? Needle is designed to support exploratory programming. 3

2.Use edrrfunctions higher-o to build complex abstractions 3.Use static nipyg t to e mak y"co \slop ding easier What Is Needle For? Needle is designed to support exploratory programming. 1. Create extensible datatypes via subclassing 4

3.Use static nipyg t to e mak y"co \slop ding easier What Is Needle For? Needle is designed to support exploratory programming. 1. Create extensible datatypes via subclassing 2. Use higher-order functions to build complex abstractions 5

What Is Needle For? Needle is designed to support exploratory programming. 1. Create extensible datatypes via subclassing 2. Use higher-order functions to build complex abstractions 3. Use static typing to make sloppy coding easier 6

Literals and Identifiers Usual literals: 3 a "string" C-like identifiers, augmented with? and : frob_foo alphanumeric? x 7

Function Calls Supports most C/Java syntactic conventions as sugar for function calls: f(3); // function call range(from: 0, below: 10); // with named args x[3]; // arrays: elt(x, 3) x[3] = 5; // arrays: set_elt(x, 3, 5) square.area; // sugar for: area(square) square.area = 3; // sugar for: set_area(square, 3) 8

Composite Expressions Blocks: { foo(); bar(); 3 + baz() } If-then-else: if (foo?) { bar } else { frob(); baz } Function expressions: fun(x) { fun(y) { x + y } } fun(vec, pos: x, offset: y) { vec[x] - y } 9

Bindings Binding: { let y = 6; let f = fun(z) { z + y }; f(2) // evals to 8 } // y is captured Local recursive functions: { rec fact(n) { if (n == 0) { 1 } else { n * fact(n - 1) } }; fact(5) } 10

Class Definitions Classes have single inheritance and are multiply-rooted, with no root Object class. class Point { constructor point; } x Integer; y mutable Integer; class ColorPoint (Point) { constructor colorpoint; } color Color; 11

Constructors, Getters, and Setters Making a Point object: point(x: 3, y: 4) colorpoint(x: 3, y: 4, color: red) Accessing a Point: { let pt = point(xpos:3, ypos:4); pt.y = 9; pt.x + y(pt); } 12

Polymorphic Classes Classes also support parametric polymorphism: class List[a] {} class Nil[a] (List) { constructor nil; } class Cons[a] (List) { constructor cons; } head a; tail List[a]; 13

Generic Functions and Methods: The Example Hierarchy First, let s set up a simple hierarchy for the examples: class Thing {} // define a root class class Rock(Thing) {... } class Paper(Thing) {... } class Scissors(Thing) {... } 14

Generic Functions and Methods: Multiple Dispatch Generic functions enable method selection and multiple dispatch: generic beats? (Thing, Thing) -> Boolean; method beats? (x Rock, y Scissors) { true } method beats? (x Paper, y Rock) { true } method beats? (x Scissors, y Paper) { true } method beats? (x Thing, y Thing) { false } beats?(rock, rock) false 15

Generic Functions and OO programming In traditional OO, adding new methods to a class is unmodular even if it s possible. generic inflammable? Thing -> Boolean; method inflammable? (x Thing) { false } method inflammable? (x Paper) { true } 16

Generic Functions and Functional Programming Higher-order functions easily parameterize over behavior, but they don t parameterize over similar data types very well. In Scheme: (map function sequence) ;; for lists (vector-map function sequence) ;; for vectors (string-map function sequence) ;; for strings In Needle: generic map c < Sequence. (a -> b, c[a]) -> c[b]; 17

Type Expressions Simple classes: Integer, Boolean, Char Type variables: a, b, c Parameterized classes: List[a], Table[Integer, Boolean] Function types: Integer -> Boolean (Integer, Integer) -> Integer (String, start:integer, len:integer) -> String 18

Polymorphic Constrained Types Every expression s type consists of a type expression, plus a set of subtype constraints that the type variables have to satisfy: generic map c < Sequence. (a -> b, c[a]) -> c[b]; generic negate a < Number. a -> a; fun(seq) { map(negate, seq) } has type c < Sequence & a < Number. c[a] -> c[a] 19

ML-sub Needle s type system is: Based on Bourdoncle and Merz s ML-sub (1997) Supports type inference (Bonniot 2001) 20

Type Inference Needle has type inference. Eg: { rec error_fact(n) { if (n == 0) { "1" } else { n * error_fact(n - 1) } };... } The compiler will signal an error on this function. 21

The Type Inference Algorithm The basic type inference algorithm has four steps: 1. Generate a polymorphic constrained type at each leaf in the AST. 2. Merge the types together, combining their constraint sets. 3. Check to see if the constraints have a solution. If there is no solution, then the expression has a type error. 4. Simplify the constraint set to report back to the user. 22

A Type Inference Example: Generating Leaf Types generic (+) a < Number. (a, a) -> a; fun(x) { x + x } Let s see how types are assigned to each leaf. 23

A Type Inference Example: Generating Leaf Types generic (+) a < Number. (a, a) -> a; fun(x) { x + x } Let s see how types are assigned to each leaf. 1. {} fun(x) { x + x } 24

A Type Inference Example: Generating Leaf Types generic (+) a < Number. (a, a) -> a; fun(x) { x + x } Let s see how types are assigned to each leaf. 1. {} fun(x) { x + x } 2. {x : t} Abstract(t, x + x ) 25

A Type Inference Example: Generating Leaf Types generic (+) a < Number. (a, a) -> a; fun(x) { x + x } Let s see how types are assigned to each leaf. 1. {} fun(x) { x + x } 2. {x : t} Abstract(t, x + x ) 3. {x : t} Abstract(t, Apply(+, x, x)) 26

A Type Inference Example: Generating Leaf Types generic (+) a < Number. (a, a) -> a; fun(x) { x + x } Let s see how types are assigned to each leaf. 1. {} fun(x) { x + x } 2. {x : t} Abstract(t, x + x ) 3. {x : t} Abstract(t, Apply(+, x, x)) 4. {x : t} Abstract(t, Apply(a < Number. (a,a) -> a, t, t)) 27

2.Abstract( t, a < Number & (a,a) -> a < (t,t) -> b. b ) 3.a < Number & (a,a) -> a < (t,t) -> b. t -> b A Type Inference Example: Merging Leaves Oncae we have the type tree, we can merge the leaf types into a single constrained type: 1. Abstract(t, Apply(a < Number. (a,a) -> a, t, t)) 28

3.a < Number & (a,a) -> a < (t,t) -> b. t -> b A Type Inference Example: Merging Leaves Oncae we have the type tree, we can merge the leaf types into a single constrained type: 1. Abstract(t, Apply(a < Number. (a,a) -> a, t, t)) 2. Abstract(t, a < Number & (a,a) -> a < (t,t) -> b. b) 29

A Type Inference Example: Merging Leaves Oncae we have the type tree, we can merge the leaf types into a single constrained type: 1. Abstract(t, Apply(a < Number. (a,a) -> a, t, t)) 2. Abstract(t, a < Number & (a,a) -> a < (t,t) -> b. b) 3. a < Number & (a,a) -> a < (t,t) -> b. t -> b 30

A Type Inference Example: Constraint Resolution We must verify that there is at least one assignment to the variables that satisfies the constraints: a < Number & (a,a) -> a < (t,t) -> b. t -> b Example: {t a; b a} We check satisfiability using standard techniques: Compute the closure of the constraints. Run a satisfiability algorithm. 31

A Type Inference Example: Constraint Simplification a < Number & (a,a) -> a < (t,t) -> b. t -> b is equivalent to a < Number. a -> a For readability, inferred types must be simplified. 32

Comparison with ML Pros: Datatypes can be extended with subclassing Generic functions give you controlled overloading Cons: No principal types More complex type inference algorithm 33

Comparison with CLOS/Dylan Pros: Integrates well with parametric polymorphism More precise types available for documentation Cons: Stricter lambda-list rules 34

Future Work: Interfaces In current Needle, generic printing might have the interface: generic print a -> String; method print (s String) { s } method print (b Boolean) { if (b) { "true" } else { "false" } } method print (o a) { raise Error(); } Throwing an exception hurts safety. 35

Future Work: Interfaces, cont. What we want is something like this: interface Print(a) { print a -> String; } generic print Print(a). a -> String; String implements Print; // interfaces are added *post-hoc* Boolean implements Print; method print (s String) { s } method print (b Boolean) { if (b) { "true" } else { "false" } } 36

Future Work: Interfaces Lets you add existing types to new protocols Fixes weakness of generic-function style grouping methods. Idea stems from Haskell typeclasses. Implementation in progress. 37

How to get Needle Website at: http://www.nongnu.org/needle Mailing list at: http://mail.nongnu.org/mailman/listinfo/needle-hackers Email me at: neelk@alum.mit.edu 38