Code Monkey home page Code Monkey logo

flitedeprest's Introduction

================================
F-lite: a core subset of Haskell
Matthew N, 26 November 2008
================================

F-lite is a core subset of Haskell.  Unlike GHC Core and Yhc Core,
F-lite has a concrete syntax.  You can write F-lite programs in a
file, and pass them to the F-lite interpreter or compiler.  Another
way to view F-lite is as a minimalist lazy functional language.

F-lite is untyped
-----------------

But as it is a subset of Haskell, you can use a Haskell implementation
to type-check F-lite programs.  

EXAMPLE 0: F-lite definition of 'append'.  Definitions of 'Nil' and
'Cons' are not required - there is no need to define algebraic data
types.

  append Nil ys = ys;
  append (Cons x xs) ys = Cons x (append xs ys);

(The use of semi-colons to seperate equations is mandatory.)

F-lite supports uniform pattern matching
----------------------------------------

Pattern matching is uniform if and only if the order of equations
doesn't matter (Wadler '86).  Uniform pattern matching can be easily
and efficiently compiled to core case expressions.  A core case
expression is one whose patterns all have the form 'constructor
applied to zero or more variables'.  The fact that the order of
equations doesn't matter is also useful when transforming functional
programs, for example by fold/unfold transformations.

EXAMPLE 1: F-lite definition of 'zipWith', illustrating uniform
pattern matching.

  zipWith f Nil ys = Nil;
  zipWith f (Cons x xs) Nil = Nil;
  zipWith f (Cons x xs) (Cons y ys) = Cons (f x y) (zipWith f xs ys);

EXAMPLE 2: F-lite definition of 'init', illustrating nested,
incomplete, uniform pattern matching.

  init (Cons x Nil) = Nil;
  init (Cons x (Cons y ys)) = Cons x (init (Cons y ys));

EXAMPLE 3: F-lite definition of 'init', using a case expression.

  init xs = case xs of {
              Cons x Nil -> Nil;
              Cons x (Cons y ys) -> Cons x (init (Cons y ys));
            };

(The use of semi-colons to seperate case alternatives is mandatory.)

F-lite supports 'let'-expressions
---------------------------------

But they may only bind expressions to variables (not patterns).

EXAMPLE 4: F-lite definition of 'pow', the power-list function,
illustrating a let expression.

  pow Nil = Cons Nil Nil;
  pow (Cons x xs) = let { rest = pow xs } in
                      append rest (map (Cons x) rest);

EXAMPLE 5: F-lite definition of 'repeat', using a let expression to
introduce a cyclic data structure.

  repeat x = let { xs = Cons x xs } in xs;

F-lite supports primitive integers
----------------------------------

Finite precision integers along with the following arithmetic
functions are allowed: (+), (-), (<=), (==), (/=).  The latter three
return 'True' or 'False' accordingly.  These operators must be written
in prefix form and cannot be partially applied.

EXAMPLE 6: F-lite definition of 'negate'.

  negate n = (-) 0 n;

(Negative literals are not supported.)

EXAMPLE 7:  F-lite definition of 'fromTo'.

  fromTo n m = case (<=) n m of {
                 True -> Cons n (fromTo ((+) n 1) m);
                 False -> Nil;
               };

F-lite supports printing
------------------------

Two primitives, 'emit' and 'emitInt', are provided for printing characters
and integers respectively.

EXAMPLE 8: Printing the string "hi!" in F-lite.

  sayHi k = emit 'h' (emit 'i' (emit '!' k))

When evaluated, 'sayHi k' will print "hi!" and return 'k' (the
continuation).

EXAMPLE 9: 'Hello world' in F-lite.

  emitStr Nil k = k;
  emitStr (Cons x xs) k = emit x (emitStr xs k);

  main = emitStr "Hello world!\n" 0;

String literals are internally translated to 'Nil'-'Cons' lists of
characters.  The result of the 'main' function is expected to be an
integer - the displaying of any output must be done explicitly by the
programmer.

EXAMPLE 10: Full F-lite program to display the 10th fibonacci number.

  {

  fib n = case (<=) n 1 of {
            True  -> 1;
            False -> (+) (fib ((-) n 2)) (fib ((-) n 1));
          };

  emitStr Nil k = k;
  emitStr (Cons x xs) k = emit x (emitStr xs k);

  main = emitStr "fib(10) = " (emitInt (fib 10) (emit '\n' 0));

}

The braces enclosing the program are indeed mandatory.  The primitive
'emitInt' function is like 'emit' but prints an integer rather than a
character.  Both 'emit' and 'emitInt' must be applied to at least one
argument.

Our implementation
------------------

Our F-lite implementation includes both an interpreter (written in
Haskell) and a compiler (to C code - see Memo 22).  It works in both
Hugs and GHC.  For example, in the source directory, using Hugs:

  > runhugs fl-pure.hs examples/Fib.hs
  fib(10) = 89

and likewise using GHC:

  > ghc -O2 --make fl-pure -o fl

  > ./fl examples/Fib.hs
  fib(10) = 89

A Cabal script can be used to install the parsec version using GHC:

  > cabal install
  
  > flite examples/Fib.hs
  fib(10) = 89

or even the pure version:

  > cabal install -f "pure"

  > flite-pure examples/Fib.hs
  fib(10) = 89

To compile F-lite programs, use the '-c' command-line option,
and redirect the output to a C file of your choice.

  > flite -c ../examples/Fib.hs > /tmp/Fib.c

The resulting C file can be compiled (with optimisations) using GCC:

  > gcc -O3 /tmp/Fib.c -o Fib

  > ./Fib
  fib(10) = 89

flitedeprest's People

Contributors

jmct avatar

Watchers

Glyn avatar Jason Reich avatar Chris Poskitt avatar  avatar James Cloos avatar  avatar  avatar  avatar

Forkers

jmct

flitedeprest's Issues

Build confusion

cabal configure -fpure looks for an fl-pure.hs file, as does make.sh if there is no fl.hs (which there is). I guess that fl.hs used to be fl-pure.hs.

The result of this is that cabal build doesn't work if configured with the pure flag, which means that regular old cabal configure needs to be used, which imposes a parsec dependency.

However, the library can't be built without parsec anyway, as the type checker pulls it in, so either the pure flag should just be removed (and make.sh updated to reflect that), or the code should be updated to allow a build without parsec.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.