summaryrefslogtreecommitdiff
path: root/src/Interpreter/Rep.hs
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2024-09-15 22:14:11 +0200
committerTom Smeding <tom@tomsmeding.com>2024-09-15 22:14:11 +0200
commit1d748ea62d02e4f66fd0f8be9815b8c3843f8356 (patch)
tree6b7631b191fdd6df137e527844296a93a77d3067 /src/Interpreter/Rep.hs
parent94938d648e021d2ace0f3b7bf383d256449d619f (diff)
WIP Accum stuff
Diffstat (limited to 'src/Interpreter/Rep.hs')
-rw-r--r--src/Interpreter/Rep.hs37
1 files changed, 21 insertions, 16 deletions
diff --git a/src/Interpreter/Rep.hs b/src/Interpreter/Rep.hs
index 7add442..680196c 100644
--- a/src/Interpreter/Rep.hs
+++ b/src/Interpreter/Rep.hs
@@ -1,9 +1,9 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
+{-# LANGUAGE UndecidableInstances #-}
module Interpreter.Rep where
import Data.IORef
-import qualified Data.Vector.Mutable as MV
import GHC.TypeError
import Array
@@ -17,19 +17,24 @@ type family Rep t where
Rep (TMaybe t) = Maybe (Rep t)
Rep (TArr n t) = Array n (Rep t)
Rep (TScal sty) = ScalRep sty
- Rep (TAccum t) = IORef (RepAc t)
+ Rep (TAccum t) = RepAcSparse t
-type family RepAc t where
- RepAc TNil = ()
- RepAc (TPair a b) = (RepAc a, RepAc b)
- -- This is annoying when working with values of type 'RepAc t', because
- -- failing a pattern match does not generate negative type information.
- -- However, it works, saves us from having to defining a LEither type
- -- first-class in the type system with
- -- Rep (LEither a b) = Maybe (Either a b)
- -- and it's not even incorrect, in a way.
- RepAc (TMaybe (TEither a b)) = IORef (Maybe (Either (RepAc a) (RepAc b)))
- RepAc (TMaybe t) = IORef (Maybe (RepAc t))
- RepAc (TArr n t) = (Shape n, MV.IOVector (RepAc t))
- RepAc (TScal sty) = IORef (ScalRep sty)
- RepAc (TAccum t) = TypeError (Text "Nested accumulators")
+-- Mutable, and has an O(1) zero.
+type family RepAcSparse t where
+ RepAcSparse TNil = ()
+ RepAcSparse (TPair a b) = IORef (RepAcDense (TPair a b))
+ RepAcSparse (TEither a b) = TypeError (Text "Non-sparse coproduct is not a monoid")
+ RepAcSparse (TMaybe t) = IORef (Maybe (RepAcDense t)) -- allow the value to be dense, because the Maybe's zero can be used for the contents
+ RepAcSparse (TArr n t) = IORef (RepAcDense (TArr n t)) -- empty array is zero
+ RepAcSparse (TScal sty) = IORef (ScalRep sty)
+ RepAcSparse (TAccum t) = TypeError (Text "RepAcSparse: Nested accumulators")
+
+-- Immutable, and does not necessarily have a zero.
+type family RepAcDense t where
+ RepAcDense TNil = ()
+ RepAcDense (TPair a b) = (RepAcSparse a, RepAcSparse b)
+ RepAcDense (TEither a b) = Either (RepAcSparse a) (RepAcSparse b)
+ RepAcDense (TMaybe t) = Maybe (RepAcSparse t)
+ RepAcDense (TArr n t) = Array n (RepAcSparse t)
+ RepAcDense (TScal sty) = ScalRep sty
+ RepAcDense (TAccum t) = TypeError (Text "RepAcDense: Nested accumulators")