Skip to content

Commit c921d18

Browse files
committed
Add examples for G-Machine Mark 6
1 parent e1480a8 commit c921d18

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,18 @@ $ flc -eti examples/nats.core examples/map.core examples/pe1.core
5959
```
6060

6161
##### G-Machine
62-
Compile and export compiled opcodes for the G-Machine.
62+
Compile and export compiled opcodes for the G-Machine. (Compiled opcodes are currently only for Mark 1 of the G-Machine.)
6363
```bash
6464
$ flc -c -e gm examples/skk3.core
6565
$ # alternatively:
6666
$ flc -cegm examples/skk3.core
6767
```
6868

69+
Also Project Euler 1, but using the G-Machine.
70+
```bash
71+
$ flc examples/gmprelude.core examples/pe1.core
72+
```
73+
6974
---
7075

7176
### Journal

examples/gmprelude.core

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
-- Useful functions for G-Machine programs
2+
-- The CorePrelude already defines the basic SKI combinators
3+
-- and `if`, `True`, `False`, `Cons`, and `Nil`
4+
5+
-- Logical operators
6+
and x y = if x y False ;
7+
or x y = if x True y ;
8+
xor x y = if x (not y) y ;
9+
not x = if x False True ;
10+
11+
-- Useful simple combinators
12+
id x = x ;
13+
double x = x + x ;
14+
15+
-- Useful simple arithmetic operators and predicates
16+
inc x = x + 1 ;
17+
zero x = x == 0 ;
18+
even x = x == ((x / 2) * 2) ;
19+
odd = compose not even ;
20+
mod m n = m - (m / n) * n ;
21+
22+
add acc val = acc + val ;
23+
mul acc val = acc * val ;
24+
25+
-- Sample math functions
26+
fac n = if (zero n) 1 (n * fac (n - 1)) ;
27+
fib n = if (n < 2) n (fib (n - 1) + fib (n - 2)) ;
28+
29+
-- List operations, LISP-style
30+
car l = case l of
31+
<4> x y -> x ;
32+
cdr l = case l of
33+
<4> x y -> y ;
34+
length l = case l of
35+
<3> -> 0 ;
36+
<4> x y -> 1 + length y ;
37+
nth n l = case l of
38+
<3> -> 0 ;
39+
<4> x xs -> if (zero n) x (nth (n - 1) xs) ;
40+
41+
caar = compose car car ;
42+
cadr = compose car cdr ;
43+
cdar = compose cdr car ;
44+
cddr = compose cdr cdr ;
45+
caadr = compose car cadr ;
46+
caaar = compose car caar ;
47+
cdadr = compose cdr cadr ;
48+
cdaar = compose cdr caar ;
49+
caddr = compose car cddr ;
50+
cadar = compose car cdar ;
51+
cdddr = compose cdr cddr ;
52+
cddar = compose cdr cdar ;
53+
54+
-- Sequences/streams
55+
numsFrom x = Cons x (numsFrom (x + 1)) ;
56+
nats = numsFrom 0 ;
57+
ones = Cons 1 ones ;
58+
59+
-- List operations
60+
map f lst = case lst of
61+
<3> -> Nil ;
62+
<4> x xs -> Cons (f x) (map f xs) ;
63+
filter p lst =
64+
case lst of
65+
<3> -> Nil ;
66+
<4> x xs ->
67+
let rest = filter p xs in
68+
if (p x) (Cons x rest) rest ;
69+
foldl f acc lst = case lst of
70+
<3> -> acc ;
71+
<4> x xs -> foldl f (f acc x) xs ;
72+
take n lst = if (n <= 0) Nil (take2 n lst) ;
73+
take2 n lst = case lst of
74+
<3> -> Nil ;
75+
<4> x xs -> Cons x (take (n - 1) xs) ;
76+
takeWhile p lst = case lst of
77+
<3> -> Nil ;
78+
<4> x xs -> if (p x) (Cons x (takeWhile p xs)) Nil ;
79+
drop n lst = if (n <= 0) lst (drop2 n lst) ;
80+
drop2 n lst = case lst of
81+
<3> -> Nil ;
82+
<4> x xs -> drop (n - 1) xs ;
83+
84+
-- Aggregate operations
85+
sum = foldl add 0 ;
86+
prod = foldl mul 1 ;
87+
88+
-- Useful function to generate a range
89+
range a b = map (add a) (take ((b - a) + 1) nats)

examples/length.core

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Requires GM Mark 6
22
length xs =
33
case xs of
4-
<1> -> 0 ;
5-
<2> y ys -> 1 + length ys ;
4+
<3> -> 0 ;
5+
<4> y ys -> 1 + length ys ;
66

77
main = length (Pack{2,2} 3 (Pack{2,2} 5 Pack{1,0}))

0 commit comments

Comments
 (0)