
EBG A Lazy Functional Programming Language Implemented on the Java Virtual Machine Tony Clark February Abstract The Java programming language oers a number of features including p ortability graphics networking Java implements the ob jectoriented execution mo del in terms of classes ob jects with state message pass ing and inclusion p olymorphism This work aims to provide a mixed paradigm environment which oers the advantages of b oth ob jectoriented and functional programming The functional paradigm is supp orted by a new language called EBG which compiles to the Java VM The resulting environment can supp ort applications which use b oth ob jectoriented and functional programming as appropriate Introduction The programming language Java has b ecome very p opular by combining a num b er of features including p ortability ob jectoriented programming WWW com patibility networking graphics and a growing collection of libraries The lan guage itself is reasonably small and oers a particular mo del of programming language execution based on classes ob jects message passing and inclusion p olymorphism Cardelli Wegner Although the b enets of using the language are large most notably its p orta bility and ease of library construction programmers are forced to use a particu lar style of programming even when it do es not suit all parts of the application For example op erations over p olymorphic lists are not readily supp orted by the ob jectoriented mo del since inclusion p olymorphism is often incompatible with parametric p olymorphism Java uses type casts to recover the type of a list element Another example o ccurs when programming in terms of lists whose elements are data items of lo osely related data types Java requires the use of type tests to determine the actual type of a data item Fortunately the p ortability of Java arises from its use of a Virtual Machine VM This is a standard interface for executable co de dened in terms of a col lection of machine instructions In principle to take advantage of Java features it is not necessary to program in Java So long as a program can b e translated into Java VM instructions it can oer Javalike advantages This pap er describ es research which aims to pro duce a mixed programming environment oering Javalike advantages The environment provides a new language called EBG in addition to Java EBG is a lazy higher order functional programming language with a HindleyMilner type system mo dules separate compilation algebraic types pattern matching and an interface to Java based on the ob jectoriented mo del of program execution The resulting environment allows applications to b e implemented as a mix ture of functional and ob jectoriented programming with the aim b eing to allow control and data to pass semi freely b etween the languages The essential feature of the implementation is to translate a functional pro gram into an equivalent Java program using a onetoone corresp ondence b e tween functions and classes Each execution of a function denition pro duces a new closure corresp ondingly the Java program instantiates the appropriate class pro ducing an object Since the Java VM do es not directly supp ort lexi cal scoping and nested classes class closures a pro cess termed class lifting is p erformed on the Java program A new binary format is used to contain the result of transforming and com piling an EBG program The default Java class loader is extended to recognise b oth the extended and basic formats allowing EBG and Java binary les to b e loaded into the same machine Finally the Java reective language features are exploited to allow EBG and Java programs to interact This pap er is structured as follows Section provides example EBG pro gram co de and shows how the interface to Java programs is used Section describ es how EBG co de is translated to Java by dening interpreters for sub sets of b oth languages and sketching a pro of of consistency for the translation The languages are called and Java resp ectively Section describ es how class lifting is p erformed which transforms a Java program containing nested classes into one in which classes o ccur only at the toplevel Section describ es how the EBG co de is translated to Java VM co de via an intermediate EBG VM language the extensions to the class loader and the interlanguage communication mechanisms Finally section analyses the work compares it with related work and outlines future plans A basic knowledge of Java ob jectoriented programming and functional pro gramming are assumed The reader is directed to Garside Mariani Venners Meyer Bird Wadler and Field Harrison for introductory material Example EBG Programs Sieve of Eratosthenes Figures and show a simple example of a mixed language application Figure is an EBG package called Sieve which implements a lazily generated list of prime numbers using a pro cess called the Sieve of Eratosthenes see Henderson for more details The packages list and command provide denitions import list command integersFrom n nintegersFromn sievenns nsieveremIfx divisible x n ns primes sieveintegersFrom main new TestSieve produces Obj o send o printPrimes Figure Example EBG Co de for Sieve of Eratosthenes for list and Java interface op erators resp ectively The package contains a collection of denitions integersFrom is a function which generates an innite list of numbers in sequence starting with n sieve is a function which is applied to a list of numbers and removes those numbers which are multiples of numbers o ccurring earlier in the list primes is a list of all prime numbers starting from The function main is an example of how imp erative features are enco ded in EBG The command new takes a Java class name as an argument and instan tiates the class The inx op erator produces evaluates its left hand op erand and supplies the value to its right hand op erand The command send is applied to an ob ject a metho d name and a list of arguments The result is equivalent to the following Java statement oprintPrimes An EBG package roughly corresp onds to a Java class where all of the toplevel denitions are declared static Any of the toplevel symbols in an EBG package can b e referenced by a Java program using the EBG package name as though it were a Java class name for example Sieveprimes Figure shows the source co de for a Java class TestSieve which uses the EBG package Sieve In addition TestSieve uses a collection of static meth o ds provided by JavaInterface which allow EBG values to b e manipulated isList isCons head and tail Both EBG and Java source co de compile using the EBG compiler ebgc and the Java compiler javac resp ectively to pro duce Java VM ob ject co de Using a simple extension of the default Java class loader in addition to the package javalangreflect b oth EBG and Java ob ject co de can b e mixed into a running Java machine Execution of the system starts by loading the EBG Sieve package and start ing to execute the commands in main The rst command creates an instance of the class TestSieve by dynamically loading the appropriate class le and instantiating the resulting class The Java reective interface is used to p erform public class TestSieve extends JavaInterface public void printPrimes printNumsSieveprimes public static void printNumsValue nums ifisListnums ifisConsnums Systemoutprintlnheadnums printNumstailnums Figure Example Java Co de Calling EBG Co de metalevel op erations such as send which invokes a named metho d of an ob ject In this case when printPrimes is invoked control passes from EBG co de to Java co de The metho d printPrimes uses the EBG package as a class with a static at tribute primes and calls printNums passing a lazily generated innite sequence of prime numbers The metho d printNums uses the metho ds isCons head and tail to print out all of the elements of the list The control ow of the program is shown in gure Environments The evaluation of and Javaexpression use environments to asso ciate keys with values In particular free variables in an expression are b ound in the current environment and Java uses an environment to mo del the heap Figure shows the denition of an EBG package Env which implements environments env is a parametric type with three data constructors Type variables in EBG are sequences of characters env is parameterised with resp ect to the type of the keys and the type of the values An environment is either Empty an asso ciation Bind k v b etween a key k and a value v or the comp osition of two environments Pair e e Environ ment lo okup is p erformed by lookup key env default which returns the rightmost value asso ciated with key in env or default if the environment do es not contain the key Environments may contain more than one entry for a key and shadowing o ccurs on the right The function mapEnv is used to apply a function to all values in an environment EBG Java main is invoked new TestSieve ! instantiate TestSieve initialise instance send o printPrimes ! reference Sieveprimes call printNums print remove if divisible by tailnums ! print remove if divisible by tailnums ! print remove if divisible by tailnums Figure The control ow in Sieve of Eratosthenes Compiling EBG EBG is implemented by a translation to Java The key issues of the translation are function representation and function application This section describ es these issues by dening two toy languages and analysing the translation b etween them The rst language called is a sublanguage of EBG providing
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages30 Page
-
File Size-