forked from blynn/compiler
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goldbach.hs
52 lines (44 loc) · 1.23 KB
/
goldbach.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
infixr 9 .;
infixr 0 $;
($) f x = f x;
(.) f g x = f (g x);
data Maybe a = Nothing | Just a;
maybe n j m = case m of { Nothing -> n; Just x -> j x };
data Bool = True | False;
ife a b c = case a of { True -> b ; False -> c };
(&&) f g = ife f g False;
foldr c n l = case l of { [] -> n; (:) h t -> c h $ foldr c n t };
any f xs = foldr (\x t -> ife (f x) True t) False xs;
not a = case a of { True -> False; False -> True };
data Nat = Zero | Succ Nat;
foldn f n m = case m of
{ Zero -> n
; Succ m' -> f $ foldn f n m'
};
add = foldn Succ;
mul a = foldn (add a) Zero;
pre n = case n of { Zero -> Nothing ; Succ n' -> Just n' };
sub a b = foldn (maybe Nothing pre) (Just a) b;
subdiv n q = case n of
{ Zero -> True
; Succ n' -> case sub n q of
{ Nothing -> False
; Just m -> subdiv m q
}
};
twoUp f q = case q of
{ Zero -> False
; Succ q' -> case q' of
{ Zero -> False
; Succ _ -> f q
}
};
downer n = case n of
{ Zero -> []
; Succ n' -> n' : downer n'
};
isPrime n = twoUp (not . any (twoUp (subdiv n)) . downer) n;
psum n p = isPrime p && maybe False isPrime (sub n p);
pp n = any (psum n) $ downer n;
goldbach' n = pp n && goldbach' (Succ $ Succ n);
goldbach = goldbach' $ Succ $ Succ $ Succ $ Succ Zero;