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

Preview:

Citation preview

1

Java/MPMultiparadigm 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 ?

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.

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.

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.

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;}

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; }; }

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.

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.

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

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.

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.

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.

14

New Features

• Features for:1. Functional Paradigm

2. Logic Paradigm

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

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 !!

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

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

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 );

20

Implementation

Translating Java/MP to Java

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

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 );}

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

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

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);

}

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

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

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);

}

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

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

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

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.

34

Questions ?