-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
184 lines (123 loc) · 5.18 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
================================
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