Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.

Slides:



Advertisements
Similar presentations
Transposing F to C Transposing F to C Andrew Kennedy & Don Syme Microsoft Research Cambridge, U.K.
Advertisements

Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Data Structures Lecture 2 Fang Yu Department of Management Information Systems National Chengchi University Fall 2011.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Java Generics.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.
C++ fundamentals.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Java and C++, The Difference An introduction Unit - 00.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
G ENERICS I N J AVA BY: Ankit Goyal Sankalp Singh.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Overview of C++ Polymorphism
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Functional Programming
Modern Programming Tools And Techniques-I
Objects as a programming concept
Types for Programs and Proofs
Road Map Introduction to object oriented programming. Classes
Generics, Lambdas, Reflections
CSE 341 Section 5 Winter 2018.
CSE-321 Programming Languages Introduction to Functional Programming
Overview of C++ Polymorphism
Chapter 14 Abstract Classes and Interfaces
Generics, Lambdas and Reflection
Presentation transcript:

sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL

Contents Overview of SML Overview of Java Motivation behind sml2java A Look at Translations –Key Ideas –Examples Conclusion

What We Like About SML SML has a powerful type system –Strong types prevent errors due to casting –Static typing prevents run-time type errors Pattern matching on data structures produces clean and intuitive code Parametric polymorphism allows generic functions while maintaining type safety

More Reasons We Like SML SML functions are powerful –Higher order functions facilitate writing compact and expressive code –SML compliers unwrap tail recursive functions Garbage collection makes references easy

Whats so Great About Java? Java is widely known and used in both industry and academia Java permits the programmer to write platform independent software Javas first class objects can be built at run- time, and named or left anonymous Garbage collection makes references easy

Why sml2java ?? Concepts underlying the translation could prove educationally valuable in teaching the functional paradigm Using a restricted subset of Java and a proof of correctness of the sml2java translator, the generated code would possess the same well-defined properties as the original SML

Abstraction Function Abstract Input (e.g. {1, 2} and {3}) Program Input (e.g. [1, 2] and [3]) Program Output (e.g. [1, 2, 3]) Abstract Output (e.g. {1, 2, 3}) Abstract Algorithm Program Algorithm Example: union

Translation Diagram Abstract Algorithm (+) SML 3 47 (3,4) : int * int 7: int

Translation Diagram Abstract Algorithm (+)3 47 Java inputJava result

Translation Diagram Abstract Algorithm (+) SML 3 47 (3,4): int * int 7: int Java inputJava result

Primitives SML primitives are translated into Java objects Java primitives (e.g. int, float) cannot be chosen as they would require translated functions to special-case for them An included library provides basic operations on the translated objects (e.g. add)

Tuples and Records SML tuples and records map unique field names to the values they contain Field names are set at compile time Javas HashMap maps unique keys to associated values A HashMap permits keys to be added at runtime Thus a record of length n will require n sequential additions to the HashMap

Datatypes SML datatypes create a new type with one or more constructors A datatype named dt with constructors c1, c2…cn produces a Java class named dt with static methods c1, c2…cn, which return an object of type dt Thus, SML code invoking a datatype constructor becomes a static method call in the translated Java code

Datatype Example datatype qux = FOO of int val myvar = FOO (42) public static class qux extends Datatype { public static qux FOO (Object o) { return new qux (FOO", o); } public static qux myvar = qux.FOO(new Integer (42));

Function Translations SMLs first class functions can be built at run-time, named or left anonymous, and passed to and returned from functions Javas first class objects can be built at run-time, named or left anonymous, and passed to and returned from functions (SML Java)(functionsobjects) Therefore,

Functions val getFirst = fn(x:int, y:int) => x val one = getFirst(1,2) public static Function getFirst = (new Function () { Integer apply(Object arg) { Record rec = (Record) arg; RecordPattern pat = new RecordPattern(); pat.match(rec); Integer x = (Integer) pat.get("1"); Integer y = (Integer) pat.get("2"); return x; } }); public static Integer one = getFirst.apply((( (new Record()).add("1", (new Integer (1)))).add("2", (new Integer (2)))));

Let Expressions SML Let expressions allow for N>1 variable bindings (where binding i can use bindings 1…i), which then can be used in a single expression, which is the result of the whole expression A Java function inside a class allows for N>0 variable bindings (where binding i can use bindings 1…i), which can then be used in a return expression, which is the result of the entire function

Let Expressions val x = let val y = 1 val z = 2 in y+z end public static Integer x = (new Let() { Integer in() { Integer y = new Integer (1); Integer z = new Integer (2); return (Integer.add()).apply((( (new Record()).add("1", y)).add("2", z))); } }).in();

Let Expression Options A Let [Java] interface with one function, in, with no parameters and returning Object A Let [Java] interface with no functions, where every instance would contain an in function that returns an appropriate type Separate the Let clause from the in clause

Module System SML signatures cannot be instantiated They declare variables that structures implementing these signatures must implement Java abstract classes cannot be instantiated They declare functions that non-abstract classes extending an abstract class must implement

Module System Example signature ID = sig val name : string end structure Id :> ID = struct val name = 1337 h4x0r" val secret = CIA supports..." end private static abstract class ID { public static String name = null; } public static class Id extends ID { public static String name = (new String (1337 h4x0r")); private static String secret = (new String(CIA supports...")); }

Conclusion One can successfully translate many core constructs of SML elegantly into Java Some interesting constructs (e.g. parameterized polymorphism) remain While the ideas behind the translation have educational value, the implementation does not Investigating whether a proof of correctness (i.e. to ensure the safeness of translated code) is possible

Acknowledgements We would like to thank Professor David Gries and Ms. Lesley Yorke for securing financing for our presentation We would like to thank Professor Dexter Kozen for his invaluable assistance and guidance