-
Notifications
You must be signed in to change notification settings - Fork 0
/
Golforth.hs
57 lines (44 loc) · 1.18 KB
/
Golforth.hs
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
{-# LANGUAGE FlexibleInstances, TupleSections #-}
module Golforth where
import Control.Arrow
f0 = flip (,)
f1 :: (a -> b) -> ((z,a) -> (z,b))
f1 = second
f2 k ~((z,a),b) = (z, k a b)
f3 k ~(((z,a),b),c) = (z, k a b c)
f4 k ~((((z,a),b),c),d) = (z, k a b c d)
f5 k ~(((((z,a),b),c),d),e) = (z, k a b c d e)
f6 k ~((((((z,a),b),c),d),e),f) = (z, k a b c d e f)
f7 k ~(((((((z,a),b),c),d),e),f),g) = (z, k a b c d e f g)
c0 w = snd . w $ ()
c1 w a = snd . w . (,a) $ ()
c2 w a b = snd . w . (,b) . (,a) $ ()
c3 w a b c = snd . w . (,c) . (,b) . (,a) $ ()
qu = f0
r0 w = fst . w
r1 w = w
r2 w s = case w s of ~(z,(a,b)) -> ((z,a),b)
p0 = (>>> qu ())
p1 = id
p2 = (>>> f2 (,))
plus = f2 (+)
minus = f2 (-)
times = f2 (*)
divide = f2 div
modulus = f2 mod
eudiv = r2 (f2 divMod)
pair = f2 (,)
cons = f2 (flip (:))
uncons (z,(x:xs)) = ((z,xs),x)
empty ~s@(z,xs) = (s,null xs)
fi (((z,b),t),e) = if b then t z else e z
pop = fst
-- this should really be f1'd but ... it's easier to use in haskell this way
dip :: (a -> b) -> ((a,c) -> (b,c))
dip = first
swap ~((z,a),b) = ((z,b),a)
dup ~(z,a) = ((z,a),a)
apply ~(z,f) = f z
eumul = dip swap >>> times >>> plus
-- same comment as dip
red f = dup >>> dip f