summaryrefslogtreecommitdiff
path: root/src/Interpreter/Rep.hs
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2024-09-13 23:07:04 +0200
committerTom Smeding <tom@tomsmeding.com>2024-09-13 23:07:04 +0200
commit94938d648e021d2ace0f3b7bf383d256449d619f (patch)
treeef077de27b08027c7117761c3efc7d29b7ad3d56 /src/Interpreter/Rep.hs
parent3d8a6cca424fc5279c43a266900160feb28aa715 (diff)
WIP better zero/plus, fixing Accum (...)
The accumulator implementation was wrong because it forgot (in accumAdd) to take into account that values may be variably-sized. Furthermore, it was also complexity-inefficient because it did not build up a sparse value. Thus let's go for the Haskell-interpreter-equivalent of what a real, fast, compiled implementation would do: just a tree with mutable variables. In practice one can decide to indeed flatten parts of that tree, i.e. using a tree representation for nested pairs is bad, but that should have been done _before_ execution and for _all_ occurrences of that type fragment, not live at runtime by the accumulator implementation.
Diffstat (limited to 'src/Interpreter/Rep.hs')
-rw-r--r--src/Interpreter/Rep.hs20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/Interpreter/Rep.hs b/src/Interpreter/Rep.hs
index 1ded773..7add442 100644
--- a/src/Interpreter/Rep.hs
+++ b/src/Interpreter/Rep.hs
@@ -2,6 +2,8 @@
{-# LANGUAGE TypeFamilies #-}
module Interpreter.Rep where
+import Data.IORef
+import qualified Data.Vector.Mutable as MV
import GHC.TypeError
import Array
@@ -12,6 +14,22 @@ type family Rep t where
Rep TNil = ()
Rep (TPair a b) = (Rep a, Rep b)
Rep (TEither a b) = Either (Rep a) (Rep b)
+ Rep (TMaybe t) = Maybe (Rep t)
Rep (TArr n t) = Array n (Rep t)
Rep (TScal sty) = ScalRep sty
- Rep (TAccum t) = TypeError (Text "Accumulator in Rep")
+ Rep (TAccum t) = IORef (RepAc 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")