Java/MP Multiparadigm Programming Language by

advertisement
Java/MP
Multiparadigm Programming
Language
by
Roshan Naik
Advisor:
Dr. Timothy Budd
1
Estimated Time: 60 mins
Java/MP Introduction
• What's is Java/MP ?
• Who designed it ?
• Is there an implementation ?
2
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.
3
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.
4
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.
5
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) = 1
factorial(n) = factorial(n-1) * n
int factorial ( int n = 0 ) { return 1; }
int factorial(int n) {
return factorial(n-1) * n;
}
6
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;
};
}
7
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.
8
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.
9
Logic Paradigm (contd.)
• Facts:
Diana
Charles
William
Henry
– mother(Diana, Henry) :- True. // Diana is Henry’s mother
– father(Charles, William) :- True. // Charles is William’s father
– father(Charles, Henry) :- True.
• Rules:
equal
– spouse(x, y) :- father(x, z) & mother(y, z)
|| mother(x, z) & father(y, z).
• Query:
equal
– spouse(Diana, Charles) ? // Is Diana a spouse of Charles ?
– spouse(Charles, a) ? // All spouses for Charles ?
10
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.
11
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.
12
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.
13
New Features
•
Features for:
1. Functional Paradigm
2. Logic Paradigm
14
Java/MP Features
for the functional Paradigm
– Global Functions
– Functions as Values
• Variables of function type
int 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)] Lambda Function
{
Single Statement
return val > 10;
};
15
Features for the functional Paradigm
• Functions as expressions (contd.)
– int foo( [int (int)] func )
{
return func(3);
}
– void bar( )
{
Executed here !!
int k=1;
foo( [ int(int i)]{ return k+i; } ) ;
}
1+3 = 4
16
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
}
Operator Names
+ plus
-
minus
*
multiply
/
divide
<
less
>
greater
17
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 )
equal
equal
{
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 undone
i <- 4;
18
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 );
19
Implementation
Translating Java/MP to Java
20
Java/MP Implementation
• Java/MP is compiled into Java
• Compilation Process:
Java/MP Source Code
Java/MP compiler
Java Source Code
Java compiler
Byte Code
21
Translating to Java
• Types for functions
[boolean(int[ ] , double)] fv;
An interface created for each function type
boolean foo ( int[ ] i, double d ) {
//..
}
Interface Created :
interface boolean_function_int_array_double_
{
boolean apply( int[ ] p1, double p2 );
}
22
Translating to Java
• Function Variables
– Declaration:
•
[boolean(int[] , double)] fv;
•
boolean_function_int_array_double_ fv ;
// Java/MP
// Java
– Assigning functions:
•
fv = foo;
// Java/MP
Anonymous Class
•
fv = new boolean_function_int_array_double_ ( ) // Java
{
public boolean apply(int[] p1, double p2) {
return foo(p1, p2); // forward calls to foo( )
}
};
–Invoking function Variables:
•fv( intArray, 2.5 );
// Java/MP
•fv.apply( intArray, 2.5 );
// Java
23
Lambda functions and saving contexts
• class A { // Java/MP
public [int(int)] foo (int x) {
int y = 4;
return [int (int z)] {
return x + y + z;
};
}
}
Lambda Function
• 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
context32( A receiver, int x, int y ) {
this.receiver = receiver; this.x=x; this.y=y; // store context
• Store context
}
• Invoke
lambda32
with the context
public
int apply(int
z) {
receiver.lambda32(x,y,z) ; // invoke the lambda and pass the stored context
}
}
25
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)
{
int y = 4 ;
return new context32(x, y);
}
// Java
26
Translating to Java
• Global Functions and Operator overloading
– Declaring:
• int plus2( int i ) { // Java/MP
return i+2;
}
• 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);
}
}
– Invoking:
• plus2( 30 );
// Java/MP
• function_plus2.plus2(30); // Java
27
Translating to Java
•
Pass By Name
1. Pass by reference mechanism
swap(int @ x, int @ y) {
// exchange values of x and y
}
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); // evaluate
Thunk3+2+25 here !!
}
writeOnTrue( true, 3+2+25 );
28
Translating to Java
• Relation : An extension of boolean. But in reality, a interface
that defines a function that returns a boolean.
interface relation
{
boolean apply( relation continuation);
}
• Automatic conversion of boolean to relation
relation father (…)
{
return true;
}
relation father (...)
{
return new relation ( ) {
public boolean apply( relation continuation) {
return true;
}
};
}
29
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
30
Translating to Java
Translating relational if Statement:
Context
if( relExpr) {
// Java/MP
System.out.print(..);
}
Convert to a member function
class context29 implements void_function_void {
context29(..context if any..) {
// store context;
}
public void lambda29(..context if any..)
public void apply() {
{ // Java
lambda29(..context if any..) ;
System.out.print(..);
}
}
}
Translated if statement
context29 ctx29 = new context29(..context if any..); // 1. create context
if ( relExpr.apply( new trueRelation( ) ) = = true )
// 2. if( relExpr )
ctx29.apply( );
// 3. execute lambda29
31
Translating to Java
Translating relational while Statement:
while( relExpr) {
// Java/MP
System.out.print(..);
}
Context
class context29 implements void_function_void {
context29(..context if any..) {
// store context;
public void lambda29(..context if any..)
}
{ // Java
public void apply() {
System.out.print(..);
lambda29(..context if any..) ;
}
}
Translated while statement
}
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
}
}
32
);
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.
33
Questions ?
34
Download