This repository has been archived by the owner on Jul 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.hs
113 lines (89 loc) · 2.72 KB
/
game.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
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
{-# LANGUAGE GADTs, RankNTypes #-}
--
data Term x where
K :: Term (a -> b -> a)
S' :: Term ((a -> b -> c) -> (a -> b) -> a -> c)
Const :: a -> Term a
(:@) :: Term (a -> b) -> (Term a) -> Term b
infixl 6 :@
eval'::Term a -> Term a
eval' (K :@ x :@ y) = x
eval' (S' :@ x :@ y :@ z) = x :@ z :@ (y :@ z)
eval' x = x
--
data Term' x where
E :: (Integral a => a -> Bool) -> Term' (Integral a => a -> Bool)
N :: Int -> Term' Int
Q :: Int -> (Integral a => a -> Bool) -> Term' ((Integral a => a -> Bool), Int)
eval :: Term' x -> x
eval (N x) = x
--eval (E x) = x
-- eval (Q (x,y)) = eval x $ eval y
foo :: Bool
foo = eval (Q odd 1)
-- data S x where
-- Ns :: S [Int]
-- Eval :: S (Int -> Bool)
-- Map :: S ([Int] -> S a -> Bool)
-- Even :: S (Int -> Bool)
-- IsPrime :: S (Int -> Bool)
-- Expr :: S (a, b)
-- eval :: S x -> S x
-- eval (Expr (Even, Ns) = map isEven (
-- eval x = x
--
-- data Singleton a where
-- Even :: Singleton (Int -> Bool)
-- Odd :: Singleton (Int -> Bool)
-- Prime :: Singleton (Int -> Bool)
-- -- Const' :: [Int] -> Singleton [Int]
-- eval'
-- data Span a where
-- Map :: Span (Singleton a -> [Int] -> Bool)
-- data Boolean a where
-- Not :: Boolean (Bool -> Bool)
-- Xor :: Boolean (Bool -> Bool -> Bool)
--
-- -- BTW, type signatures are mandatory if you're using GADTs
-- translate :: Singleton a -> a
-- translate a = case a of
-- Even -> not . odd
-- Odd -> odd
-- Prime -> isPrime
-- functions :: [Function a] -> [a]
-- functions = map translate
--
-- eval :: [a] -> [Function (a -> Bool)] -> Bool
-- eval nums exprs = and $ zipWith (\x y -> y x) nums $ functions exprs
isPrime :: Int -> Bool
isPrime n = aux primes
where
aux ps | head ps < n = aux $ tail ps
| head ps == n = True
| otherwise = False
primes :: [Int]
primes = sieve [2..]
sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0]
sieve [] = undefined
{- -- apparently need GADTs for this... http://haskell.org/haskellwiki/GADT
data Function = Fold |
Prime | Composite |
Subtract | Add | Multiply | Divide | Exponent |
GreaterThan | LessThan
foo xs y = zipWith ($) (map translateUnadic' xs) y
-- translate :: Function ->[Int] -> Bool
translateUnadic Not = not
translateUnadic' Even = not . odd
translateUnadic' Odd = odd
translateUnadic'' Prime = isPrime
translateUnadic'' Composite = not . isPrime
translate And = (&&)
translate Or = (||)
-- translate Xor = xor
translate' Fold = \x y -> foldr x 0 y
translate'' Subtract = (-)
translate'' Add = (+)
-- translate'' =
xor :: Bool -> Bool -> Bool
xor x y = (x &¬ y) || (y ||not x)
-}