1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated...

33
1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins

Transcript of 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated...

Page 1: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

1

Java/MPMultiparadigm Programming

Language

by

Roshan Naik

Advisor:

Dr. Timothy Budd

Estimated Time: 60 mins

Page 2: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

2

Java/MP Introduction

• What's is Java/MP ?

• Who designed it ?

• Is there an implementation ?

Page 3: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

3

Paradigms

• Paradigm: A set of assumptions, concepts, values and practices that constitutes a way of viewing reality.

• Common Programming Paradigms:– Imperative - C,Pascal,Basic,Fortran

– Functional - Lisp, Haskell

– Object Oriented - C++, Java

– Logic - Prolog

– Generic, Constraint, Visual, Access-Oriented, Spreadsheet, Rule-Based etc.

Page 4: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

4

Imperative Paradigm

• Step by step instructions are given to the computer.

• Instruction sequences continually make incremental changes to areas in memory.

• Characterized by functions, variables, loops and branch instructions.

• Examples: C, Pascal, Basic, Fortran.

Page 5: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

5

Functional Paradigm• Based on Lambda Calculus• Only functions and values. No variables. No

loops. No side effects. • Functions can be treated as values. • A list or a sequence is often a built in data

type.• Mathematical techniques can be used to test

the correctness of the program.• Examples: Lisp, Scheme, Haskell.

Page 6: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

6

Functional Paradigm (contd.)• Simple Functions:

square(n) = n * n // mathematical version int square(int n) { return n*n; } // in a programming language

• Composition: cube(n) = square(n) * n int cube(int) = { return square(n) * n; }

• Recursion:factorial(0) = 1factorial(n) = factorial(n-1) * n

int factorial ( int n = 0 ) { return 1; }int factorial(int n) { return factorial(n-1) * n;}

Page 7: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

7

Functional Paradigm (contd.)• Treating functions as values:

– Functions as arguments:• f( g(x) , h(x) , n ) = g( h(n) ) // mathematical version• int f( int(int) g, int(int) h, int n ) { // in a programming language return g(h(n)) ; }

– Functions as results:• int(int) foo (int x) { return int bar(int y) { return x + y; }; }

Page 8: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

8

Object Oriented Paradigm• Originated from attempts to simulate real world

world objects and their interactions.• Objects contain behavior and data.• Objects interact with each other by exchanging messages.

• A group of similar objects is called a Class.• Classes can be organized into a hierarchy such that

properties and behavior that is common among related classes can be factored into a parent class.

• Examples: C++, Small Talk, Java.

Page 9: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

9

Logic Paradigm• Based on Predicate Calculus.• Problem solving is done by inferencing new

results using well known facts and results from earlier inferences.

• Facts and Rules of inference are the two key abstractions in this paradigm.

• The problem instance to be solved is formulated as a Query.

• Backtracking is used to recover from inferences that do not lead to a solution.

• Examples: Prolog, Gödel.

Page 10: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

10

• Facts:– mother(Diana, Henry) :- True. // Diana is Henry’s mother– father(Charles, William) :- True. // Charles is William’s father– father(Charles, Henry) :- True.

Logic Paradigm (contd.)Diana

William Henry

Charles

•Rules:–spouse(x, y) :- father(x, z) & mother(y, z) || mother(x, z) & father(y, z).

•Query:–spouse(Diana, Charles) ? // Is Diana a spouse of Charles ?–spouse(Charles, a) ? // All spouses for Charles ?

equal

equal

Page 11: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

11

Java/MP• Multiparadigm language

– Imperative, Object Oriented, Functional and Logic paradigms.

• Really the next generation of Leda.

• Designed as an extension to Java.

• Upward compatible superset of Java.

Page 12: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

12

Java/MP Motivation• A multiparadigm language can provide far greater

expressive power than languages confined to a single paradigm.

• Java is a popular programming language and is easy to learn but it strictly confines programmers to the object oriented paradigm.

• By building Java/MP on top of Java we reduce the learning curve and bring it to a wider bigger audience.

Page 13: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

13

Language Design Goals

• Extensions should be kept simple.• Extensions should look evolutionary.• Add as few extensions as possible.• Compatible with the Java Virtual Machine.• Functional and Logic paradigms are incorporated as

extensions to the Object Oriented paradigm.• Allow to freely mix and match paradigms and the

integration should be seamless.

Page 14: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

14

New Features

• Features for:1. Functional Paradigm

2. Logic Paradigm

Page 15: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

15

Java/MP Featuresfor the functional Paradigm

– Global Functions– Functions as Values

• Variables of function typeint i = 34;[boolean (int)] func = foo; // foo can be a global/member function

• Functions as expressions ( lambda function )– i = 3+4;– func = [boolean (int val)] {

return val > 10; };

Lambda Function

Single Statement

Page 16: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

16

Features for the functional Paradigm

• Functions as expressions (contd.)– int foo( [int (int)] func )

{

return func(3);

}

– void bar( )

{ int k=1;

foo( [ int(int i)]{ return k+i; } ) ;

} 1+3 = 4

Executed here !!

Page 17: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

17

Features for the Logic Paradigm

• Operator Overloading– String plus( String x, String y ) { // addition operator +

return x.concat(y); }– String z = str1 + str2; // same as: z = plus(str1, str2 );

• Pass by Name– void assign4( int @ i ) {

i = 4; // change in value is reflected in the calling function }

– void bar( ) { int j = 3; assign4( j ); System.out.print( j ); // prints 4 }

+ plus

- minus

* multiply

/ divide

< less

> greater

Operator Names

Page 18: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

18

Features for the Logic Paradigm

• Relation: Can be considered as an extension of boolean.

• Facts and Rules are represented using functions that return a relation.– relation father ( String @ x, String @ y )

{ return eq( x, “Charles” ) && eq( y, “William” ) // father(Charles,William)

|| eq( x, “Charles” ) && eq( y, “Henry” ) ; // father(Charles, Henry) }

– if( father( “Charles”, “Henry” ) )

•Arrow Operator: Assignment that can be undonei <- 4;

equal equal

Page 19: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

19

Features for the Logic Paradigm

• Relational if and while Statements– String son = null; if ( father(“Charles”, son) )

System.out.println( son ); // print any one son

–String son = null;

while ( father(“Charles”, son) )

System.out.println( son ); // print all sons

–int a = 3;

if ( ( a<- 2 || a<-10 ) && (a>5) ) System.out.println( a );

Page 20: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

20

Implementation

Translating Java/MP to Java

Page 21: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

21

Java/MP Implementation

• Java/MP is compiled into Java

• Compilation Process:

Java/MP Source Code

Java Source Code

Byte Code

Java/MP compiler

Java compiler

Page 22: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

22

Translating to Java• Types for functions

[boolean(int[ ] , double)] fv;

An interface created for each function typeboolean foo ( int[ ] i, double d ) { //..}

Interface Created :interface boolean_function_int_array_double_{

boolean apply( int[ ] p1, double p2 );}

Page 23: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

23

Translating to Java• Function Variables

– Declaration:• [boolean(int[] , double)] fv; // Java/MP

• boolean_function_int_array_double_ fv ; // Java

–Invoking function Variables:•fv( intArray, 2.5 ); // Java/MP

•fv.apply( intArray, 2.5 ); // Java

– Assigning functions:•fv = foo; // Java/MP

•fv = new boolean_function_int_array_double_ ( ) // Java {

public boolean apply(int[] p1, double p2) { return foo(p1, p2); // forward calls to foo( ) } };

Anonymous Class

Page 24: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

25

• class A { // Java/MP

public [int(int)] foo (int x) {

int y = 4;

return [int (int z)] {

return x + y + z;

};

}

}

• int lambda32( int x, int y, int z ) { // member function of A

return x + y + z ;

} • class context32 implements int_function_int_ { // inner class of A

}

Lambda functions and saving contexts

Lambda Function

context32( A receiver, int x, int y ) {

this.receiver = receiver; this.x=x; this.y=y; // store context

}

public int apply(int z) {

receiver.lambda32(x,y,z) ; // invoke the lambda and pass the stored context

}

• Store context • Invoke lambda32 with the context

Page 25: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

26

Translating to Java

• [int(int)] foo (int x) { // Java/MP

int y = 4;

return [int (int z)] {

return x + y + z;

};

}

• int_function_int_ foo(int x) // Java

{

int y = 4 ;

return new context32(x, y);

}

Page 26: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

27

• Global Functions and Operator overloading– Declaring:

• int plus2( int i ) { // Java/MP return i+2;

}

– Invoking:• plus2( 30 ); // Java/MP

Translating to Java

• class function_plus2 implements int_function_int_ // Java {

}

public static int plus2(int i) { return i+2; }

public int apply ( int p1 ) { // implementing interface return plus2(p1); }

• function_plus2.plus2(30); // Java

Page 27: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

28

Translating to Java• Pass By Name

1. Pass by reference mechanism

int x=3, y=4; swap(x,y); System.out.print(x); // x is 4, if x and y are passed by name to swap

2. Delay evaluation of expressions until needed

• void writeOnTrue(boolean b ,int @ x) // evaluate x if b is true

{

if(b = = true)

System.out.print(x);

}

• writeOnTrue( true, 3+2+25 );

swap(int @ x, int @ y) {

// exchange values of x and y

}

// evaluate 3+2+25 here !!Thunk

Page 28: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

29

Translating to Java

• Automatic conversion of boolean to relationrelation father (…) {

return true; }

relation father (...) { return new relation ( ) {

public boolean apply( relation continuation) { return true; } } ;

}

•Relation : An extension of boolean. But in reality, a interface that defines a function that returns a boolean.

interface relation

{

boolean apply( relation continuation);

}

Page 29: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

30

Translating to Java• Relational if and while statements

• if ( ( a <- 5 | | a <- 20 | | a <- 13 ) && a>10 )

System.out.print( a ); // prints 20

• while( ( a <- 5 | | a <- 20 | | a <- 13 ) && a>10 )

System.out.print( a ); // prints 20 and 13

• String son = null;

while( father( “Charles”, son ) )

System.out.print( son ); // prints Henry and William

Page 30: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

31

Translating to JavaTranslating relational if Statement:

if( relExpr) { // Java/MP

System.out.print(..);

}

public void lambda29(..context if any..)

{ // Java

System.out.print(..);

}

Convert to a member function

class context29 implements void_function_void {

context29(..context if any..) {

// store context;

}

public void apply() {

lambda29(..context if any..) ;

}

}

Context

context29 ctx29 = new context29(..context if any..); // 1. create context

if ( relExpr.apply( new trueRelation( ) ) = = true ) // 2. if( relExpr )

ctx29.apply( ); // 3. execute lambda29

Translated if statement

Page 31: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

32

Translating to JavaTranslating relational while Statement:while( relExpr) { // Java/MP

System.out.print(..);

}

class context29 implements void_function_void {

context29(..context if any..) {

// store context;

}

public void apply() {

lambda29(..context if any..) ;

}

}

Context

public void lambda29(..context if any..)

{ // Java

System.out.print(..);

}

final context29 ctx29 = new context29(..context if any..); // 1. create context

relExpr.apply( new relation( ) { // 2. while( relExpr )

public boolean apply ( relation continuation ) {

ctx29.apply( ); // 3. execute lambda29 when relExpr=true

return false; // 4. return false to force backtracking

}

}

);

Translated while statement

Page 32: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

33

Current Status• Working prototype compiler on Unix and

Windows.• Not yet industrial strength. Needs more testing.• No byte-code parser to import existing classes.

• Future Work– Generate byte code directly.– Performance issues.– Multiparadigm-enabled debugger.

Page 33: 1 Java/MP Multiparadigm Programming Language by Roshan Naik Advisor: Dr. Timothy Budd Estimated Time: 60 mins.

34

Questions ?