summaryrefslogtreecommitdiff
path: root/lambdacube-ir.haskell/src/LambdaCube/Linear.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lambdacube-ir.haskell/src/LambdaCube/Linear.hs')
-rw-r--r--lambdacube-ir.haskell/src/LambdaCube/Linear.hs60
1 files changed, 60 insertions, 0 deletions
diff --git a/lambdacube-ir.haskell/src/LambdaCube/Linear.hs b/lambdacube-ir.haskell/src/LambdaCube/Linear.hs
new file mode 100644
index 0000000..286cc93
--- /dev/null
+++ b/lambdacube-ir.haskell/src/LambdaCube/Linear.hs
@@ -0,0 +1,60 @@
1{-# LANGUAGE DeriveFunctor, OverloadedStrings #-}
2module LambdaCube.Linear where
3
4import Data.Int
5import Data.Word
6import Data.Map
7
8import Data.Text
9import Data.Aeson
10import Control.Monad
11
12data V2 a = V2 !a !a deriving (Eq,Ord,Show,Read,Functor)
13data V3 a = V3 !a !a !a deriving (Eq,Ord,Show,Read,Functor)
14data V4 a = V4 !a !a !a !a deriving (Eq,Ord,Show,Read,Functor)
15
16-- matrices are stored in column major order
17type M22F = V2 V2F
18type M23F = V3 V2F
19type M24F = V4 V2F
20type M32F = V2 V3F
21type M33F = V3 V3F
22type M34F = V4 V3F
23type M42F = V2 V4F
24type M43F = V3 V4F
25type M44F = V4 V4F
26
27type V2F = V2 Float
28type V3F = V3 Float
29type V4F = V4 Float
30type V2I = V2 Int32
31type V3I = V3 Int32
32type V4I = V4 Int32
33type V2U = V2 Word32
34type V3U = V3 Word32
35type V4U = V4 Word32
36type V2B = V2 Bool
37type V3B = V3 Bool
38type V4B = V4 Bool
39
40instance ToJSON a => ToJSON (V2 a) where
41 toJSON (V2 x y) = object ["x" .= x, "y" .= y]
42
43instance ToJSON a => ToJSON (V3 a) where
44 toJSON (V3 x y z) = object ["x" .= x, "y" .= y, "z" .= z]
45
46instance ToJSON a => ToJSON (V4 a) where
47 toJSON (V4 x y z w) = object ["x" .= x, "y" .= y, "z" .= z, "w" .= w]
48
49instance FromJSON a => FromJSON (V2 a) where
50 parseJSON (Object obj) = V2 <$> obj .: "x" <*> obj .: "y"
51 parseJSON _ = mzero
52
53instance FromJSON a => FromJSON (V3 a) where
54 parseJSON (Object obj) = V3 <$> obj .: "x" <*> obj .: "y" <*> obj .: "z"
55 parseJSON _ = mzero
56
57instance FromJSON a => FromJSON (V4 a) where
58 parseJSON (Object obj) = V4 <$> obj .: "x" <*> obj .: "y" <*> obj .: "z" <*> obj .: "w"
59 parseJSON _ = mzero
60