summaryrefslogtreecommitdiff
path: root/lc/Prelude.lc
blob: bc89e137e9d487af8bb59bc44298fbc77e67f075 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
{-# LANGUAGE NoImplicitPrelude #-}
module Prelude 
    ( module Prelude
    , module Builtins
    ) where

import Builtins

infixr 9 .
infixl 7 `PrimMulMatVec`, `PrimDot`
infixr 3 ***
infixr 0 $
--infixl 0 &

const x y = x

otherwise = True

x & f = f x

($) = \f x -> f x
(.) = \f g x -> f (g x)

uncurry f (x, y) = f x y

(***) f g (x, y) = (f x, g y)

pi = 3.14

zip :: [a] -> [b] -> [(a,b)]
zip []      xs     = []
zip xs      []     = []
zip (a: as) (b: bs) = (a,b): zip as bs

unzip :: [(a,b)] -> ([a],[b])
unzip [] = ([],[])
unzip ((a,b):xs) = (a:as,b:bs)
  where (as,bs) = unzip xs

filter pred []    = []
filter pred (x:xs) = case pred x of
                       True -> (x : filter pred xs)
                       False -> (filter pred xs)

head :: [a] -> a
head (a: _) = a

tail :: [a] -> [a]
tail (_: xs) = xs

pairs :: [a] -> [(a, a)]
pairs v = zip v (tail v)

foldl' f e [] = e
foldl' f e (x: xs) = foldl' f (f e x) xs

singleton a = [a]

append []     ys = ys
append (x:xs) ys = x : append xs ys

concat = foldl' append []

map _ []     = []
map f (x:xs) = f x : map f xs

concatMap :: (a -> [b]) -> [a] -> [b]
concatMap f x = concat (map f x)

split [] = ([], [])
split (x: xs) = (x: bs, as)  where (as, bs) = split xs

mergeBy f (x:xs) (y:ys) = case f x y of
    LT -> x: mergeBy f xs (y:ys)
    _ -> y: mergeBy f (x:xs) ys
mergeBy f [] xs = xs
mergeBy f xs [] = xs

sortBy f [] = []
sortBy f [x] = [x]
sortBy f xs = uncurry (mergeBy f) ((sortBy f *** sortBy f) (split xs))

(++) = append
infixr 5 ++

iterate :: (a -> a) -> a -> [a]
iterate f x =  x : iterate f (f x)

data Maybe a
    = Nothing
    | Just a
--    deriving (Eq, Ord, Show)


fst (a, b) = a
snd (a, b) = b

tuptype :: [Type] -> Type
tuptype [] = '()
tuptype (x:xs) = '(x, tuptype xs)

data RecordC (xs :: [(String, Type)])
    = RecordCons (tuptype (map snd xs))

foldr1 f [x] = x
foldr1 f (x: xs) = f x (foldr1 f xs)

False ||| x = x
True ||| x = True

infixr 2 |||

True &&& x = x
False &&& x = False

infixr 3 &&&

------------------------------------ Row polymorphism
-- todo: sorted field names (more efficient & easier to use)

{-
isKey _ [] = False
isKey s ((s', _): ss) = s == s' ||| isKey s ss

subList [] _ = []
subList ((s, t): xs) ys = if isKey s ys then subList xs ys else (s, t): subList xs ys

addList [] ys = ys
addList ((s, t): xs) ys = if isKey s ys then addList xs ys else (s, t): addList xs ys

findEq x [] = 'Unit
findEq (s, t) ((s', t'):xs) = if s == s' then 'T2 (t ~ t') (findEq (s, t) xs) else findEq (s, t) xs

sameEq [] _ = 'Unit
sameEq (x: xs) ys = 'T2 (findEq x ys) (sameEq xs ys)

defined [] = True
defined (x: xs) = defined xs

type family Split a b c
type instance Split (RecordC xs) (RecordC ys) z | defined xs &&& defined ys = T2 (sameEq xs ys) (z ~ RecordC (subList xs ys))
type instance Split (RecordC xs) z (RecordC ys) | defined xs &&& defined ys = T2 (sameEq xs ys) (z ~ RecordC (subList xs ys))
type instance Split z (RecordC xs) (RecordC ys) | defined xs &&& defined ys = T2 (sameEq xs ys) (z ~ RecordC (addList xs ys))

-- builtin
-- TODO
record :: [(String, Type)] -> Type
--record xs = RecordCons ({- TODO: sortBy fst-} xs)
-}
-- builtin
unsafeCoerce :: forall a b . a -> b

isKeyC _ _ [] = 'Empty ""
isKeyC s t ((s', t'): ss) = if s == s' then t ~ t' else isKeyC s t ss

-- todo: don't use unsafeCoerce
project :: forall a (xs :: [(String, Type)]) . forall (s :: String) -> 'isKeyC s a xs => RecordC xs -> a
project @a @((s', a'): xs) s @_ (RecordCons ts) | s == s' = fst (unsafeCoerce @_ @(a, tuptype (map snd xs)) ts)
project @a @((s', a'): xs) s @_ (RecordCons ts) = project @a @xs s @(undefined @(isKeyC s a xs)) (RecordCons (snd (unsafeCoerce @_ @(a, tuptype (map snd xs)) ts)))

--------------------------------------- HTML colors

rgb r g b = V4 r g b 1.0

black   = rgb 0.0 0.0 0.0
gray    = rgb 0.5 0.5 0.5
silver  = rgb 0.75 0.75 0.75
white   = rgb 1.0 1.0 1.0
maroon  = rgb 0.5 0.0 0.0
red     = rgb 1.0 0.0 0.0
olive   = rgb 0.5 0.5 0.0
yellow  = rgb 1.0 1.0 0.0
green   = rgb 0.0 0.5 0.0
lime    = rgb 0.0 1.0 0.0
teal    = rgb 0.0 0.5 0.5
aqua    = rgb 0.0 1.0 1.0
navy    = rgb 0.0 0.0 0.5
blue    = rgb 0.0 0.0 1.0
purple  = rgb 0.5 0.0 0.5
fuchsia = rgb 1.0 0.0 1.0

colorImage1 = ColorImage @1
colorImage2 = ColorImage @2

depthImage1 = DepthImage @1

v3FToV4F :: Vec 3 Float -> Vec 4 Float
v3FToV4F v = V4 v%x v%y v%z 1

------------
-- * WebGL 1
------------

-- angle and trigonometric
radians = PrimRadians
degrees = PrimDegrees
sin = PrimSin
cos = PrimCos
tan = PrimTan
sinh = PrimSinH
cosh = PrimCosH
tanh = PrimTanH
asin = PrimASin
asinh = PrimASinH
acos = PrimACos
acosh = PrimACosH
atan = PrimATan
atanh = PrimATanH
atan2 = PrimATan2

-- exponential functions
pow = PrimPow
exp = PrimExp
log = PrimLog
exp2 = PrimExp2
log2 = PrimLog2
sqrt = PrimSqrt
inversesqrt = PrimInvSqrt

-- common functions
abs = PrimAbs
sign = PrimSign
floor = PrimFloor
trunc = PrimTrunc
round = PrimRound
roundEven = PrimRoundEven
ceil = PrimCeil
fract = PrimFract
mod = PrimMod
min = PrimMin
max = PrimMax
modF = PrimModF
clamp = PrimClamp
clampS = PrimClampS
mix = PrimMix
mixS = PrimMixS
mixB = PrimMixB
step = PrimStep
stepS = PrimStepS
smoothstep = PrimSmoothStep
smoothstepS = PrimSmoothStepS
isNan = PrimIsNan
isInf = PrimIsInf

dFdx = PrimDFdx
dFdy = PrimDFdy
fWidth = PrimFWidth

noise1 = PrimNoise1
noise2 = PrimNoise2
noise3 = PrimNoise3
noise4 = PrimNoise4

-- geometric functions
length = PrimLength
distance = PrimDistance
dot = PrimDot
cross = PrimCross
normalize = PrimNormalize
faceforward = PrimFaceForward
reflect = PrimReflect
refract = PrimRefract

transpose = PrimTranspose
det = PrimDeterminant
inv = PrimInverse
outer = PrimOuterProduct

-- operators
infixl 7  *, /, %
infixl 6  +, -
infix  4  /=, <, <=, >=, >

infixr 3  &&
infixr 2  ||

infix 7 `dot`   -- dot
infix 7 `cross` -- cross

infixr 7 *.     -- mulmv
infixl 7 .*     -- mulvm
infixl 7 .*.    -- mulmm

-- arithemtic
a + b = PrimAdd a b
a - b = PrimSub a b
a * b = PrimMul a b
a / b = PrimDiv a b
a % b = PrimMod a b

neg a = PrimNeg a

-- comparison
--a == b = PrimEqual a b
a /= b = PrimNotEqual a b
a < b = PrimLessThan a b
a <= b = PrimLessThanEqual a b
a >= b = PrimGreaterThanEqual a b
a > b = PrimGreaterThan a b

-- logical
a && b = PrimAnd a b
a || b = PrimOr a b
xor = PrimXor
not a = PrimNot a
any a = PrimAny a
all a = PrimAll a

-- matrix functions
a .*. b = PrimMulMatMat a b
a *. b = PrimMulMatVec a b
a .* b = PrimMulVecMat a b

-- temp hack for vector <---> scalar operators
infixl 7  *!, /!, %!
infixl 6  +!, -!

-- arithemtic
a +! b = PrimAddS a b
a -! b = PrimSubS a b
a *! b = PrimMulS a b
a /! b = PrimDivS a b
a %! b = PrimModS a b

------------------
-- common matrices
------------------
{-
-- | Perspective transformation matrix in row major order.
perspective :: Float  -- ^ Near plane clipping distance (always positive).
            -> Float  -- ^ Far plane clipping distance (always positive).
            -> Float  -- ^ Field of view of the y axis, in radians.
            -> Float  -- ^ Aspect ratio, i.e. screen's width\/height.
            -> Mat 4 4 Float
perspective n f fovy aspect = --transpose $
    M44F (V4F (2*n/(r-l))       0       (-(r+l)/(r-l))        0)
         (V4F     0        (2*n/(t-b))  ((t+b)/(t-b))         0)
         (V4F     0             0       (-(f+n)/(f-n))  (-2*f*n/(f-n)))
         (V4F     0             0            (-1)             0)
  where
    t = n*tan(fovy/2)
    b = -t
    r = aspect*t
    l = -r
-}
rotMatrixZ a = M44F (V4 c s 0 0) (V4 (-s) c 0 0) (V4 0 0 1 0) (V4 0 0 0 1)
  where
    c = cos a
    s = sin a

rotMatrixY a = M44F (V4 c 0 (-s) 0) (V4 0 1 0 0) (V4 s 0 c 0) (V4 0 0 0 1)
  where
    c = cos a
    s = sin a

rotMatrixX a = M44F (V4 1 0 0 0) (V4 0 c s 0) (V4 0 (-s) c 0) (V4 0 0 0 1)
  where
    c = cos a
    s = sin a

rotationEuler a b c = rotMatrixY a .*. rotMatrixX b .*. rotMatrixZ c

{-
-- | Camera transformation matrix.
lookat :: Vec 3 Float  -- ^ Camera position.
       -> Vec 3 Float  -- ^ Target position.
       -> Vec 3 Float  -- ^ Upward direction.
       -> M44F
lookat pos target up = translateBefore4 (neg pos) (orthogonal $ toOrthoUnsafe r)
  where
    w = normalize $ pos - target
    u = normalize $ up `cross` w
    v = w `cross` u
    r = transpose $ Mat3 u v w
-}

scale t v = v * V4 t t t 1.0

fromTo :: Float -> Float -> [Float]
fromTo a b = if a > b then [] else a: fromTo (a +! 1.0) b

(!!) :: [a] -> Int -> a
(x : _)  !! 0  =  x
(_ : xs) !! n  =  xs !! (n-1)