summaryrefslogtreecommitdiff
path: root/src/CHAD/Accum.hs
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2024-11-26 15:25:13 +0100
committerTom Smeding <tom@tomsmeding.com>2024-11-26 15:25:13 +0100
commitae2b1b71a91d60d3bd1dfb21fce98c05c1a4fcbb (patch)
tree1f6afda4b1d6925fe8224ee4f2ca40212fe11aa6 /src/CHAD/Accum.hs
parent7774da51c532006da82617ce307d136897693280 (diff)
WIP accum top-level args
Diffstat (limited to 'src/CHAD/Accum.hs')
-rw-r--r--src/CHAD/Accum.hs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/CHAD/Accum.hs b/src/CHAD/Accum.hs
new file mode 100644
index 0000000..e26f781
--- /dev/null
+++ b/src/CHAD/Accum.hs
@@ -0,0 +1,36 @@
+{-# LANGUAGE DataKinds #-}
+{-# LANGUAGE GADTs #-}
+module CHAD.Accum where
+
+import AST
+import CHAD.Types
+import Data
+
+
+
+hasArrays :: STy t' -> Bool
+hasArrays STNil = False
+hasArrays (STPair a b) = hasArrays a || hasArrays b
+hasArrays (STEither a b) = hasArrays a || hasArrays b
+hasArrays (STMaybe t) = hasArrays t
+hasArrays STArr{} = True
+hasArrays STScal{} = False
+hasArrays STAccum{} = error "Accumulators not allowed in source program"
+
+makeAccumulators :: SList STy envPro -> Ex (Append (D2AcE envPro) env) t -> Ex env (InvTup t (D2E envPro))
+makeAccumulators SNil e = e
+makeAccumulators (t `SCons` envpro) e =
+ makeAccumulators envpro $
+ EWith (EZero t) e
+
+uninvertTup :: SList STy list -> STy core -> Ex env (InvTup core list) -> Ex env (TPair core (Tup list))
+uninvertTup SNil _ e = EPair ext e (ENil ext)
+uninvertTup (t `SCons` list) tcore e =
+ ELet ext (uninvertTup list (STPair tcore t) e) $
+ let recT = STPair (STPair tcore t) (tTup list) -- type of the RHS of that let binding
+ in EPair ext
+ (EFst ext (EFst ext (EVar ext recT IZ)))
+ (EPair ext
+ (ESnd ext (EVar ext recT IZ))
+ (ESnd ext (EFst ext (EVar ext recT IZ))))
+