-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basic.hs
64 lines (50 loc) · 1.3 KB
/
Basic.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE GADTs #-}
module Horth.Basic
( Horth.Basic.add
, Horth.Basic.drop
, Horth.Basic.dup
, Horth.Basic.eq
, Horth.Basic.filter
, Horth.Basic.less
, Horth.Basic.neq
, Horth.Basic.not
, Horth.Basic.println
, Horth.Basic.sub
, Horth.Basic.swap
, Horth.Basic.length
) where
import Horth.ControlFlow
import Horth.Language
import Control.Monad (filterM)
not :: Shape '[Bool] '[Bool]
not = call1 Prelude.not
length :: Shape '[[a]] '[Int]
length = call1 Prelude.length
add :: Num a => Shape '[a, a] '[a]
add = call2 (+)
sub :: Num a => Shape '[a, a] '[a]
sub = call2 (-)
dup :: Shape '[x] '[x, x]
dup = fmap (\(HCons x xs) -> x %: x %: xs)
swap :: Shape '[x, y] '[y, x]
swap = fmap (\(HCons x (HCons y r)) -> y %: x %: r)
drop :: Shape '[x] '[]
drop = fmap (\(HCons x xs) -> xs)
println :: Show x => Shape '[x] '[]
println io = do
(HCons x xs) <- io
print x
return xs
eq :: Eq a => Shape '[a, a] '[Bool]
eq = call2 (==)
neq :: Eq a => Shape '[a, a] '[Bool]
neq = call2 (/=)
less :: Ord a => Shape '[a, a] '[Bool]
less = call2 (<)
filter :: State ((State '[a] -> State (Bool : y)) : [a] : xs) -> State ([a] : xs)
filter args = do
(HCons f (HCons xs rest)) <- args
xs <- filterM (front . f . wrap) xs
return (xs %: rest)