Download ML Functions - Welcome to Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Currying wikipedia , lookup

Anonymous function wikipedia , lookup

Combinatory logic wikipedia , lookup

Lambda lifting wikipedia , lookup

Lambda calculus definition wikipedia , lookup

Closure (computer programming) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Standard ML wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
Fun max (x,y) = if x > y then x else y;
ML (Meta Language)
a brief introduction
Alex Proctor and Brian Lee
For CSCI 431 at the University of North Carolina,
Asheville
Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002
1
An Overview of ML

Description




ML is a high level functional programming language similar to LISP, Prolog,
and Hope.
ML is further differentiated by a strong and static type system, type inference,
and garbage collection.
ML was the first language to include statically checked polymorphic typing.
History


ML was created as a metalanguage for the Edinburgh LCF proof assistant in
1973.
It has since developed into its own language, hence the acronymn ML no
longer is a relevant descriptor of the language.
Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002
2
ML: An Example Program

ML Computation: Calculation of Expressions




ML is a robust functional language that combines many of the features
of other programming languages we have studied in this course.
The unit of evaluation in ML is the expression. Every expression has a
type, possibly a value, and may or may not cause an effect.
ML also limits defined functions to only take a single parameter,
however, this parameter may be a tuple, or a ‘unit’ (the empty set).
ML is typically run in an interpretive manner.



(ML expression): fun average (x, y) = (x +y) / 2.0;
(User input): - average (5, 7);
(Resulting values): - val it = 6 : int
Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002
3
ML As a Functional Language: ML is more Powerful!

ML is Powerful:

ML has many of the attributes that you would expect in a functional
language like Scheme or LISP, such as:




Computation by expression rather than execution of instruction
Declarative programming abstraction
Lambda calculus function methodology
Yet ML incorporates other features not usually found in functional
languages, such as:




Exception Handling
Garbage Collection
User defined Abstract Data Types
Strong and type handling and type inference
Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002
4
ML As a Functional Language: Further Details

ML exibits referential transparency




ML variables are statically scoped




Variable assignment is a ‘once only’ operation.
Variable values do not change once assigned.
Referential transparency greatly increases the ability of a program to
be quantitatively analyzed, used as a proof, and debugged.
ML’s variables are entirely local to a called function
Variables are bound at run-time. Compiling is not often used.
Global variables don’t exist. Neither do instances of an object as
might be found in other languages supporting object abstractions.
ML can handle many of the more interesting data types

Lists, Real Numbers, User defined ADTs
Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002
5
ML Functions: Extending the Functional Languages

ML functions may be of the highest order



ML uses aggressive type inference methods
ML has few standard data types, but all are extensible
Even Functions may define other Functions!, as in this example:
Fun try (a, x) = a x;
> val try = fn : (‘a -> ‘b) * ‘a -> ‘b
Try (hd, [1,2,3];
>val it = 1 : int
Try (t1, [1,2,3]);
> val it = [2, 3] : int list

Such higher order functions have great utility and are part of the
attraction of a Functional language.
Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002
6
ML Functions: Extending the Functional Languages

ML supports Currying and Mapping:


Higher order functions may even embed arguments into the definition of a
new function.This is called ‘Currying’.
For Example:










Fun incr x y = x + y;
Val incr = fn : int -> int -> int
Incr 5 3;
Vali it = 8 : int
(incr 5) 3;
Val it = 8 : int
Val incr5 = incr 5;
Val incr5 = fn : int -> int
Incr5 3;
Val it = 8 : int
Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002
7
ML Polymorphism:

ML broke programming ground with strong
polymorphism:

ML uses the term polymorphism to refer to the attribute of a function
to handle more than one type of parameter. List is a good example. It
works with any list of any type of elements.



The list functions are similar to Scheme, only cleaner.
The ML conception of polymorphism is different than the idea of
method overloading that the term polymorphism can also signify. We
do not mean merely having two or more functions with the same
name.
ML functions are polymorphic if they contain only polymorphic
functions and operators. For example:

Fun revPair (x, y) = (y, x)
Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002
8
ML Functions: Local Variables are ‘Let’ into a Function

ML allows local variables in a function.

The syntax for local variables follows the pattern :


Let………..in…………end
As we see in the following example:
Fun circleData (radius) =
Let
Val pi = 3.1415926536
Val circumference = two * pi * radius;
Fun area radius = pi * radius * radius
In
(circumference, area (radius))
End;
Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002
9
ML Flow Control: The Function Stack

Flow Control is an Interpretive Process



ML parses each line as an ML expression.
ML can use files and libraries, much like scheme.
ML performs I./O with simple grace, for example :
fun copyFile (inputFileName, outputFileName) =
Let
Val inFile = openIn inputFileName;
Val outFile = openOut outputFileName
In
( output (outFile, input inFile);
flushOut outFile;
closeOut outFile
)
End;

ML has been ported to many graphical windowing environments, such as
the unix/linux x-windowing system.
Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002
10
ML Handles Exceptions

ML supports user-defined error flow control

Such exception handling makes ML an attractive alternative to other
Functional languages where this is not possible
Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002
11
Conclusion: ML is a little known giant

ML should be better known and used:


ML is highly orthogonal. Most functions that work with one type of
parameter will work with any parameter. ML has good internal type
checking mechanisms which are applied to every evaluated expression
consistently.
Because ML allows for recursion, nested constructs, list functions, and
polymorphism, it is considered a highly abstracted language, capable
of implementing the most complex of programming tasks with the
most ease for the programmer.
Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002
12
Conclusion: ML is a popular outcast

ML in their words:


“ML has made significant inroads into the computer science
educational and research communities. The exposure of the type
mechanism at the source language level is a feature not available in
other widely used languages. However, commercial applications of
ML programs are few, and so far it remains mostly a vehicle for
computer science research and educational use.” (That old book).
“…This may be because there are not enough…”

Gentle intro to ML: readers comments
Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002
13