    -- | The value representation used in the interpreter.
    --
    -- Kept simple and free of unnecessary operational details (in
    -- particular, no references to the interpreter monad).
    module Language.Futhark.Interpreter.Values
      ( -- * Shapes
        Shape (..),
        ValueShape,
        typeShape,
        structTypeShape,

        -- * Values
        Value (..),
        valueShape,
        arrayValueShape,
        prettyValue,
        valueText,
        valueAccum,
        valueAccumLM,
        fromTuple,
        arrayLength,
        isEmptyArray,
        prettyEmptyArray,
        toArray,
        toArray',
        toTuple,

        -- * Conversion
        fromDataValue,
      )
    where

    import Control.Monad.Identity
    import Data.Array
    import Data.List (genericLength)
    import Data.Map qualified as M
    import Data.Maybe
    import Data.Monoid hiding (Sum)
    import Data.Text qualified as T
    import Data.Vector.Storable qualified as SVec
    import Futhark.Data qualified as V
    import Futhark.Util (chunk, mapAccumLM)
    import Futhark.Util.Pretty
    import Language.Futhark hiding (Shape, matchDims)
    import Language.Futhark.Interpreter.AD qualified as AD
    import Language.Futhark.Primitive qualified as P
    import Prelude hiding (break, mod)

    prettyRecord :: (a -> Doc ann) -> M.Map Name a -> Doc ann
    prettyRecord :: forall a ann. (a -> Doc ann) -> Map Name a -> Doc ann
    prettyRecord a -> Doc ann
    p Map Name a
    m
      | Just [a]
    vs <- Map Name a -> Maybe [a]
    forall a. Map Name a -> Maybe [a]
    areTupleFields Map Name a
    m =
          Doc ann -> Doc ann
    forall ann. Doc ann -> Doc ann
    parens (Doc ann -> Doc ann) -> Doc ann -> Doc ann
    forall a b. (a -> b) -> a -> b
    $ Doc ann -> Doc ann
    forall ann. Doc ann -> Doc ann
    align (Doc ann -> Doc ann) -> Doc ann -> Doc ann
    forall a b. (a -> b) -> a -> b
    $ [Doc ann] -> Doc ann
    forall ann. [Doc ann] -> Doc ann
    vsep ([Doc ann] -> Doc ann) -> [Doc ann] -> Doc ann
    forall a b. (a -> b) -> a -> b
    $ Doc ann -> [Doc ann] -> [Doc ann]
    forall ann. Doc ann -> [Doc ann] -> [Doc ann]
    punctuate Doc ann
    forall ann. Doc ann
    comma ([Doc ann] -> [Doc ann]) -> [Doc ann] -> [Doc ann]
    forall a b. (a -> b) -> a -> b
    $ (a -> Doc ann) -> [a] -> [Doc ann]
    forall a b. (a -> b) -> [a] -> [b]
    map a -> Doc ann
    p [a]
    vs
      | Bool
    otherwise =
          Doc ann -> Doc ann
    forall ann. Doc ann -> Doc ann
    braces (Doc ann -> Doc ann) -> Doc ann -> Doc ann
    forall a b. (a -> b) -> a -> b
    $ Doc ann -> Doc ann
    forall ann. Doc ann -> Doc ann
    align (Doc ann -> Doc ann) -> Doc ann -> Doc ann
    forall a b. (a -> b) -> a -> b
    $ [Doc ann] -> Doc ann
    forall ann. [Doc ann] -> Doc ann
    vsep ([Doc ann] -> Doc ann) -> [Doc ann] -> Doc ann
    forall a b. (a -> b) -> a -> b
    $ Doc ann -> [Doc ann] -> [Doc ann]
    forall ann. Doc ann -> [Doc ann] -> [Doc ann]
    punctuate Doc ann
    forall ann. Doc ann
    comma ([Doc ann] -> [Doc ann]) -> [Doc ann] -> [Doc ann]
    forall a b. (a -> b) -> a -> b
    $ ((Name, a) -> Doc ann) -> [(Name, a)] -> [Doc ann]
    forall a b. (a -> b) -> [a] -> [b]
    map (Name, a) -> Doc ann
    forall {a}. Pretty a => (a, a) -> Doc ann
    field ([(Name, a)] -> [Doc ann]) -> [(Name, a)] -> [Doc ann]
    forall a b. (a -> b) -> a -> b
    $ Map Name a -> [(Name, a)]
    forall k a. Map k a -> [(k, a)]
    M.toList Map Name a
    m
      where
        field :: (a, a) -> Doc ann
    field (a
    k, a
    v) = a -> Doc ann
    forall ann. a -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    pretty a
    k Doc ann -> Doc ann -> Doc ann
    forall ann. Doc ann -> Doc ann -> Doc ann
    <+> Doc ann
    forall ann. Doc ann
    equals Doc ann -> Doc ann -> Doc ann
    forall ann. Doc ann -> Doc ann -> Doc ann
    <+> a -> Doc ann
    p a
    v

    -- | A shape is a tree to accomodate the case of records.  It is
    -- parameterised over the representation of dimensions.
    data Shape d
      = ShapeDim d (Shape d)
      | ShapeLeaf
      | ShapeRecord (M.Map Name (Shape d))
      | ShapeSum (M.Map Name [Shape d])
      deriving (Shape d -> Shape d -> Bool
    (Shape d -> Shape d -> Bool)
    -> (Shape d -> Shape d -> Bool) -> Eq (Shape d)
    forall d. Eq d => Shape d -> Shape d -> Bool
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: forall d. Eq d => Shape d -> Shape d -> Bool
    == :: Shape d -> Shape d -> Bool
    $c/= :: forall d. Eq d => Shape d -> Shape d -> Bool
    /= :: Shape d -> Shape d -> Bool
    Eq, Int -> Shape d -> ShowS
    [Shape d] -> ShowS
    Shape d -> String
    (Int -> Shape d -> ShowS)
    -> (Shape d -> String) -> ([Shape d] -> ShowS) -> Show (Shape d)
    forall d. Show d => Int -> Shape d -> ShowS
    forall d. Show d => [Shape d] -> ShowS
    forall d. Show d => Shape d -> String
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: forall d. Show d => Int -> Shape d -> ShowS
    showsPrec :: Int -> Shape d -> ShowS
    $cshow :: forall d. Show d => Shape d -> String
    show :: Shape d -> String
    $cshowList :: forall d. Show d => [Shape d] -> ShowS
    showList :: [Shape d] -> ShowS
    Show, (forall a b. (a -> b) -> Shape a -> Shape b)
    -> (forall a b. a -> Shape b -> Shape a) -> Functor Shape
    forall a b. a -> Shape b -> Shape a
    forall a b. (a -> b) -> Shape a -> Shape b
    forall (f :: * -> *).
    (forall a b. (a -> b) -> f a -> f b)
    -> (forall a b. a -> f b -> f a) -> Functor f
    $cfmap :: forall a b. (a -> b) -> Shape a -> Shape b
    fmap :: forall a b. (a -> b) -> Shape a -> Shape b
    $c<$ :: forall a b. a -> Shape b -> Shape a
    <$ :: forall a b. a -> Shape b -> Shape a
    Functor, (forall m. Monoid m => Shape m -> m)
    -> (forall m a. Monoid m => (a -> m) -> Shape a -> m)
    -> (forall m a. Monoid m => (a -> m) -> Shape a -> m)
    -> (forall a b. (a -> b -> b) -> b -> Shape a -> b)
    -> (forall a b. (a -> b -> b) -> b -> Shape a -> b)
    -> (forall b a. (b -> a -> b) -> b -> Shape a -> b)
    -> (forall b a. (b -> a -> b) -> b -> Shape a -> b)
    -> (forall a. (a -> a -> a) -> Shape a -> a)
    -> (forall a. (a -> a -> a) -> Shape a -> a)
    -> (forall a. Shape a -> [a])
    -> (forall a. Shape a -> Bool)
    -> (forall a. Shape a -> Int)
    -> (forall a. Eq a => a -> Shape a -> Bool)
    -> (forall a. Ord a => Shape a -> a)
    -> (forall a. Ord a => Shape a -> a)
    -> (forall a. Num a => Shape a -> a)
    -> (forall a. Num a => Shape a -> a)
    -> Foldable Shape
    forall a. Eq a => a -> Shape a -> Bool
    forall a. Num a => Shape a -> a
    forall a. Ord a => Shape a -> a
    forall m. Monoid m => Shape m -> m
    forall a. Shape a -> Bool
    forall a. Shape a -> Int
    forall a. Shape a -> [a]
    forall a. (a -> a -> a) -> Shape a -> a
    forall m a. Monoid m => (a -> m) -> Shape a -> m
    forall b a. (b -> a -> b) -> b -> Shape a -> b
    forall a b. (a -> b -> b) -> b -> Shape a -> b
    forall (t :: * -> *).
    (forall m. Monoid m => t m -> m)
    -> (forall m a. Monoid m => (a -> m) -> t a -> m)
    -> (forall m a. Monoid m => (a -> m) -> t a -> m)
    -> (forall a b. (a -> b -> b) -> b -> t a -> b)
    -> (forall a b. (a -> b -> b) -> b -> t a -> b)
    -> (forall b a. (b -> a -> b) -> b -> t a -> b)
    -> (forall b a. (b -> a -> b) -> b -> t a -> b)
    -> (forall a. (a -> a -> a) -> t a -> a)
    -> (forall a. (a -> a -> a) -> t a -> a)
    -> (forall a. t a -> [a])
    -> (forall a. t a -> Bool)
    -> (forall a. t a -> Int)
    -> (forall a. Eq a => a -> t a -> Bool)
    -> (forall a. Ord a => t a -> a)
    -> (forall a. Ord a => t a -> a)
    -> (forall a. Num a => t a -> a)
    -> (forall a. Num a => t a -> a)
    -> Foldable t
    $cfold :: forall m. Monoid m => Shape m -> m
    fold :: forall m. Monoid m => Shape m -> m
    $cfoldMap :: forall m a. Monoid m => (a -> m) -> Shape a -> m
    foldMap :: forall m a. Monoid m => (a -> m) -> Shape a -> m
    $cfoldMap' :: forall m a. Monoid m => (a -> m) -> Shape a -> m
    foldMap' :: forall m a. Monoid m => (a -> m) -> Shape a -> m
    $cfoldr :: forall a b. (a -> b -> b) -> b -> Shape a -> b
    foldr :: forall a b. (a -> b -> b) -> b -> Shape a -> b
    $cfoldr' :: forall a b. (a -> b -> b) -> b -> Shape a -> b
    foldr' :: forall a b. (a -> b -> b) -> b -> Shape a -> b
    $cfoldl :: forall b a. (b -> a -> b) -> b -> Shape a -> b
    foldl :: forall b a. (b -> a -> b) -> b -> Shape a -> b
    $cfoldl' :: forall b a. (b -> a -> b) -> b -> Shape a -> b
    foldl' :: forall b a. (b -> a -> b) -> b -> Shape a -> b
    $cfoldr1 :: forall a. (a -> a -> a) -> Shape a -> a
    foldr1 :: forall a. (a -> a -> a) -> Shape a -> a
    $cfoldl1 :: forall a. (a -> a -> a) -> Shape a -> a
    foldl1 :: forall a. (a -> a -> a) -> Shape a -> a
    $ctoList :: forall a. Shape a -> [a]
    toList :: forall a. Shape a -> [a]
    $cnull :: forall a. Shape a -> Bool
    null :: forall a. Shape a -> Bool
    $clength :: forall a. Shape a -> Int
    length :: forall a. Shape a -> Int
    $celem :: forall a. Eq a => a -> Shape a -> Bool
    elem :: forall a. Eq a => a -> Shape a -> Bool
    $cmaximum :: forall a. Ord a => Shape a -> a
    maximum :: forall a. Ord a => Shape a -> a
    $cminimum :: forall a. Ord a => Shape a -> a
    minimum :: forall a. Ord a => Shape a -> a
    $csum :: forall a. Num a => Shape a -> a
    sum :: forall a. Num a => Shape a -> a
    $cproduct :: forall a. Num a => Shape a -> a
    product :: forall a. Num a => Shape a -> a
    Foldable, Functor Shape
    Foldable Shape
    (Functor Shape, Foldable Shape) =>
    (forall (f :: * -> *) a b.
     Applicative f =>
     (a -> f b) -> Shape a -> f (Shape b))
    -> (forall (f :: * -> *) a.
        Applicative f =>
        Shape (f a) -> f (Shape a))
    -> (forall (m :: * -> *) a b.
        Monad m =>
        (a -> m b) -> Shape a -> m (Shape b))
    -> (forall (m :: * -> *) a. Monad m => Shape (m a) -> m (Shape a))
    -> Traversable Shape
    forall (t :: * -> *).
    (Functor t, Foldable t) =>
    (forall (f :: * -> *) a b.
     Applicative f =>
     (a -> f b) -> t a -> f (t b))
    -> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
    -> (forall (m :: * -> *) a b.
        Monad m =>
        (a -> m b) -> t a -> m (t b))
    -> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
    -> Traversable t
    forall (m :: * -> *) a. Monad m => Shape (m a) -> m (Shape a)
    forall (f :: * -> *) a. Applicative f => Shape (f a) -> f (Shape a)
    forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Shape a -> m (Shape b)
    forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Shape a -> f (Shape b)
    $ctraverse :: forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Shape a -> f (Shape b)
    traverse :: forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Shape a -> f (Shape b)
    $csequenceA :: forall (f :: * -> *) a. Applicative f => Shape (f a) -> f (Shape a)
    sequenceA :: forall (f :: * -> *) a. Applicative f => Shape (f a) -> f (Shape a)
    $cmapM :: forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Shape a -> m (Shape b)
    mapM :: forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Shape a -> m (Shape b)
    $csequence :: forall (m :: * -> *) a. Monad m => Shape (m a) -> m (Shape a)
    sequence :: forall (m :: * -> *) a. Monad m => Shape (m a) -> m (Shape a)
    Traversable)

    -- | The shape of an array.
    type ValueShape = Shape Int64

    instance (Pretty d) => Pretty (Shape d) where
      pretty :: forall ann. Shape d -> Doc ann
    pretty Shape d
    ShapeLeaf = Doc ann
    forall a. Monoid a => a
    mempty
      pretty (ShapeDim d
    d Shape d
    s) = Doc ann -> Doc ann
    forall ann. Doc ann -> Doc ann
    brackets (d -> Doc ann
    forall ann. d -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    pretty d
    d) Doc ann -> Doc ann -> Doc ann
    forall a. Semigroup a => a -> a -> a
    <> Shape d -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    forall ann. Shape d -> Doc ann
    pretty Shape d
    s
      pretty (ShapeRecord Map Name (Shape d)
    m) = (Shape d -> Doc ann) -> Map Name (Shape d) -> Doc ann
    forall a ann. (a -> Doc ann) -> Map Name a -> Doc ann
    prettyRecord Shape d -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    forall ann. Shape d -> Doc ann
    pretty Map Name (Shape d)
    m
      pretty (ShapeSum Map Name [Shape d]
    cs) =
        [Doc ann] -> Doc ann
    forall a. Monoid a => [a] -> a
    mconcat (Doc ann -> [Doc ann] -> [Doc ann]
    forall ann. Doc ann -> [Doc ann] -> [Doc ann]
    punctuate Doc ann
    " | " [Doc ann]
    forall {ann}. [Doc ann]
    cs')
        where
          ppConstr :: (a, [a]) -> Doc ann
    ppConstr (a
    name, [a]
    fs) = [Doc ann] -> Doc ann
    forall ann. [Doc ann] -> Doc ann
    sep ([Doc ann] -> Doc ann) -> [Doc ann] -> Doc ann
    forall a b. (a -> b) -> a -> b
    $ (Doc ann
    "#" Doc ann -> Doc ann -> Doc ann
    forall a. Semigroup a => a -> a -> a
    <> a -> Doc ann
    forall ann. a -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    pretty a
    name) Doc ann -> [Doc ann] -> [Doc ann]
    forall a. a -> [a] -> [a]
    : (a -> Doc ann) -> [a] -> [Doc ann]
    forall a b. (a -> b) -> [a] -> [b]
    map a -> Doc ann
    forall ann. a -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    pretty [a]
    fs
          cs' :: [Doc ann]
    cs' = ((Name, [Shape d]) -> Doc ann) -> [(Name, [Shape d])] -> [Doc ann]
    forall a b. (a -> b) -> [a] -> [b]
    map (Name, [Shape d]) -> Doc ann
    forall {a} {a} {ann}. (Pretty a, Pretty a) => (a, [a]) -> Doc ann
    ppConstr ([(Name, [Shape d])] -> [Doc ann])
    -> [(Name, [Shape d])] -> [Doc ann]
    forall a b. (a -> b) -> a -> b
    $ Map Name [Shape d] -> [(Name, [Shape d])]
    forall k a. Map k a -> [(k, a)]
    M.toList Map Name [Shape d]
    cs

    emptyShape :: ValueShape -> Bool
    emptyShape :: ValueShape -> Bool
    emptyShape (ShapeDim Int64
    d ValueShape
    s) = Int64
    d Int64 -> Int64 -> Bool
    forall a. Eq a => a -> a -> Bool
    == Int64
    0 Bool -> Bool -> Bool
    || ValueShape -> Bool
    emptyShape ValueShape
    s
    emptyShape ValueShape
    _ = Bool
    False

    typeShape :: TypeBase d u -> Shape d
    typeShape :: forall d u. TypeBase d u -> Shape d
    typeShape (Array u
    _ Shape d
    shape ScalarTypeBase d NoUniqueness
    et) =
      (d -> Shape d -> Shape d) -> Shape d -> [d] -> Shape d
    forall a b. (a -> b -> b) -> b -> [a] -> b
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr d -> Shape d -> Shape d
    forall d. d -> Shape d -> Shape d
    ShapeDim (TypeBase d NoUniqueness -> Shape d
    forall d u. TypeBase d u -> Shape d
    typeShape (ScalarTypeBase d NoUniqueness -> TypeBase d NoUniqueness
    forall dim u. ScalarTypeBase dim u -> TypeBase dim u
    Scalar ScalarTypeBase d NoUniqueness
    et)) ([d] -> Shape d) -> [d] -> Shape d
    forall a b. (a -> b) -> a -> b
    $ Shape d -> [d]
    forall dim. Shape dim -> [dim]
    shapeDims Shape d
    shape
    typeShape (Scalar (Record Map Name (TypeBase d u)
    fs)) =
      Map Name (Shape d) -> Shape d
    forall d. Map Name (Shape d) -> Shape d
    ShapeRecord (Map Name (Shape d) -> Shape d) -> Map Name (Shape d) -> Shape d
    forall a b. (a -> b) -> a -> b
    $ (TypeBase d u -> Shape d)
    -> Map Name (TypeBase d u) -> Map Name (Shape d)
    forall a b k. (a -> b) -> Map k a -> Map k b
    M.map TypeBase d u -> Shape d
    forall d u. TypeBase d u -> Shape d
    typeShape Map Name (TypeBase d u)
    fs
    typeShape (Scalar (Sum Map Name [TypeBase d u]
    cs)) =
      Map Name [Shape d] -> Shape d
    forall d. Map Name [Shape d] -> Shape d
    ShapeSum (Map Name [Shape d] -> Shape d) -> Map Name [Shape d] -> Shape d
    forall a b. (a -> b) -> a -> b
    $ ([TypeBase d u] -> [Shape d])
    -> Map Name [TypeBase d u] -> Map Name [Shape d]
    forall a b k. (a -> b) -> Map k a -> Map k b
    M.map ((TypeBase d u -> Shape d) -> [TypeBase d u] -> [Shape d]
    forall a b. (a -> b) -> [a] -> [b]
    map TypeBase d u -> Shape d
    forall d u. TypeBase d u -> Shape d
    typeShape) Map Name [TypeBase d u]
    cs
    typeShape TypeBase d u
    t
      | Just TypeBase d NoUniqueness
    t' <- TypeBase d u -> Maybe (TypeBase d NoUniqueness)
    forall d u. TypeBase d u -> Maybe (TypeBase d NoUniqueness)
    isAccType TypeBase d u
    t =
          TypeBase d NoUniqueness -> Shape d
    forall d u. TypeBase d u -> Shape d
    typeShape TypeBase d NoUniqueness
    t'
      | Bool
    otherwise =
          Shape d
    forall d. Shape d
    ShapeLeaf

    structTypeShape :: StructType -> Shape (Maybe Int64)
    structTypeShape :: StructType -> Shape (Maybe Int64)
    structTypeShape = (ExpBase Info VName -> Maybe Int64)
    -> Shape (ExpBase Info VName) -> Shape (Maybe Int64)
    forall a b. (a -> b) -> Shape a -> Shape b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap ExpBase Info VName -> Maybe Int64
    forall {a} {f :: * -> *} {vn}. Num a => ExpBase f vn -> Maybe a
    dim (Shape (ExpBase Info VName) -> Shape (Maybe Int64))
    -> (StructType -> Shape (ExpBase Info VName))
    -> StructType
    -> Shape (Maybe Int64)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . StructType -> Shape (ExpBase Info VName)
    forall d u. TypeBase d u -> Shape d
    typeShape
      where
        dim :: ExpBase f vn -> Maybe a
    dim (IntLit Integer
    x f StructType
    _ SrcLoc
    _) = a -> Maybe a
    forall a. a -> Maybe a
    Just (a -> Maybe a) -> a -> Maybe a
    forall a b. (a -> b) -> a -> b
    $ Integer -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    x
        dim ExpBase f vn
    _ = Maybe a
    forall a. Maybe a
    Nothing

    -- | A fully evaluated Futhark value.
    data Value m
      = ValuePrim !PrimValue
      | ValueArray ValueShape !(Array Int (Value m))
      | -- Stores the full shape.
        ValueRecord (M.Map Name (Value m))
      | ValueFun (Value m -> m (Value m))
      | -- Stores the full shape.
        ValueSum ValueShape Name [Value m]
      | -- The shape, the update function, and the array.
        ValueAcc ValueShape (Value m -> Value m -> m (Value m)) !(Array Int (Value m))
      | -- A primitive value with added information used in automatic differentiation
        ValueAD AD.Depth AD.ADVariable

    instance Show (Value m) where
      show :: Value m -> String
    show (ValuePrim PrimValue
    v) = String
    "ValuePrim " String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> PrimValue -> String
    forall a. Show a => a -> String
    show PrimValue
    v String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    ""
      show (ValueArray ValueShape
    shape Array Int (Value m)
    vs) = [String] -> String
    unwords [String
    "ValueArray", String
    "(" String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> ValueShape -> String
    forall a. Show a => a -> String
    show ValueShape
    shape String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    ")", String
    "(" String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> Array Int (Value m) -> String
    forall a. Show a => a -> String
    show Array Int (Value m)
    vs String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    ")"]
      show (ValueRecord Map Name (Value m)
    fs) = String
    "ValueRecord " String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    "(" String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> Map Name (Value m) -> String
    forall a. Show a => a -> String
    show Map Name (Value m)
    fs String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    ")"
      show (ValueSum ValueShape
    shape Name
    c [Value m]
    vs) = [String] -> String
    unwords [String
    "ValueSum", String
    "(" String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> ValueShape -> String
    forall a. Show a => a -> String
    show ValueShape
    shape String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    ")", Name -> String
    forall a. Show a => a -> String
    show Name
    c, String
    "(" String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> [Value m] -> String
    forall a. Show a => a -> String
    show [Value m]
    vs String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    ")"]
      show ValueFun {} = String
    "ValueFun _"
      show ValueAcc {} = String
    "ValueAcc _"
      show (ValueAD Depth
    d ADVariable
    v) = [String] -> String
    unwords [String
    "ValueAD", Depth -> String
    forall a. Show a => a -> String
    show Depth
    d, ADVariable -> String
    forall a. Show a => a -> String
    show ADVariable
    v]

    instance Eq (Value m) where
      ValuePrim (SignedValue IntValue
    x) == :: Value m -> Value m -> Bool
    == ValuePrim (SignedValue IntValue
    y) =
        PrimValue -> PrimValue -> Bool
    P.doCmpEq (IntValue -> PrimValue
    P.IntValue IntValue
    x) (IntValue -> PrimValue
    P.IntValue IntValue
    y)
      ValuePrim (UnsignedValue IntValue
    x) == ValuePrim (UnsignedValue IntValue
    y) =
        PrimValue -> PrimValue -> Bool
    P.doCmpEq (IntValue -> PrimValue
    P.IntValue IntValue
    x) (IntValue -> PrimValue
    P.IntValue IntValue
    y)
      ValuePrim (FloatValue FloatValue
    x) == ValuePrim (FloatValue FloatValue
    y) =
        PrimValue -> PrimValue -> Bool
    P.doCmpEq (FloatValue -> PrimValue
    P.FloatValue FloatValue
    x) (FloatValue -> PrimValue
    P.FloatValue FloatValue
    y)
      ValuePrim (BoolValue Bool
    x) == ValuePrim (BoolValue Bool
    y) =
        PrimValue -> PrimValue -> Bool
    P.doCmpEq (Bool -> PrimValue
    P.BoolValue Bool
    x) (Bool -> PrimValue
    P.BoolValue Bool
    y)
      ValueArray ValueShape
    _ Array Int (Value m)
    x == ValueArray ValueShape
    _ Array Int (Value m)
    y = Array Int (Value m)
    x Array Int (Value m) -> Array Int (Value m) -> Bool
    forall a. Eq a => a -> a -> Bool
    == Array Int (Value m)
    y
      ValueRecord Map Name (Value m)
    x == ValueRecord Map Name (Value m)
    y = Map Name (Value m)
    x Map Name (Value m) -> Map Name (Value m) -> Bool
    forall a. Eq a => a -> a -> Bool
    == Map Name (Value m)
    y
      ValueSum ValueShape
    _ Name
    n1 [Value m]
    vs1 == ValueSum ValueShape
    _ Name
    n2 [Value m]
    vs2 = Name
    n1 Name -> Name -> Bool
    forall a. Eq a => a -> a -> Bool
    == Name
    n2 Bool -> Bool -> Bool
    && [Value m]
    vs1 [Value m] -> [Value m] -> Bool
    forall a. Eq a => a -> a -> Bool
    == [Value m]
    vs2
      ValueAcc ValueShape
    _ Value m -> Value m -> m (Value m)
    _ Array Int (Value m)
    x == ValueAcc ValueShape
    _ Value m -> Value m -> m (Value m)
    _ Array Int (Value m)
    y = Array Int (Value m)
    x Array Int (Value m) -> Array Int (Value m) -> Bool
    forall a. Eq a => a -> a -> Bool
    == Array Int (Value m)
    y
      Value m
    _ == Value m
    _ = Bool
    False

    prettyValueWith :: (PrimValue -> Doc a) -> Value m -> Doc a
    prettyValueWith :: forall a (m :: * -> *). (PrimValue -> Doc a) -> Value m -> Doc a
    prettyValueWith PrimValue -> Doc a
    pprPrim = Int -> Value m -> Doc a
    forall {m :: * -> *}. Int -> Value m -> Doc a
    pprPrec Int
    0
      where
        pprPrec :: Int -> Value m -> Doc a
    pprPrec Int
    _ (ValuePrim PrimValue
    v) = PrimValue -> Doc a
    pprPrim PrimValue
    v
        pprPrec Int
    _ (ValueArray ValueShape
    _ Array Int (Value m)
    a) =
          let elements :: [Value m]
    elements = Array Int (Value m) -> [Value m]
    forall i e. Array i e -> [e]
    elems Array Int (Value m)
    a -- [Value]
              separator :: [Doc ann] -> Doc ann
    separator = case [Value m]
    elements of
                ValueArray ValueShape
    _ Array Int (Value m)
    _ : [Value m]
    _ -> [Doc ann] -> Doc ann
    forall ann. [Doc ann] -> Doc ann
    vsep
                [Value m]
    _ -> [Doc ann] -> Doc ann
    forall ann. [Doc ann] -> Doc ann
    hsep
           in Doc a -> Doc a
    forall ann. Doc ann -> Doc ann
    brackets (Doc a -> Doc a) -> Doc a -> Doc a
    forall a b. (a -> b) -> a -> b
    $ Doc a -> Doc a
    forall ann. Doc ann -> Doc ann
    align (Doc a -> Doc a) -> Doc a -> Doc a
    forall a b. (a -> b) -> a -> b
    $ [Doc a] -> Doc a
    forall ann. [Doc ann] -> Doc ann
    separator ([Doc a] -> Doc a) -> [Doc a] -> Doc a
    forall a b. (a -> b) -> a -> b
    $ Doc a -> [Doc a] -> [Doc a]
    forall ann. Doc ann -> [Doc ann] -> [Doc ann]
    punctuate Doc a
    forall ann. Doc ann
    comma ([Doc a] -> [Doc a]) -> [Doc a] -> [Doc a]
    forall a b. (a -> b) -> a -> b
    $ (Value m -> Doc a) -> [Value m] -> [Doc a]
    forall a b. (a -> b) -> [a] -> [b]
    map Value m -> Doc a
    pprElem [Value m]
    elements
        pprPrec Int
    _ (ValueRecord Map Name (Value m)
    m) = (Value m -> Doc a) -> Map Name (Value m) -> Doc a
    forall a ann. (a -> Doc ann) -> Map Name a -> Doc ann
    prettyRecord (Int -> Value m -> Doc a
    pprPrec Int
    0) Map Name (Value m)
    m
        pprPrec Int
    _ ValueFun {} = Doc a
    "#<fun>"
        pprPrec Int
    _ ValueAcc {} = Doc a
    "#<acc>"
        pprPrec Int
    p (ValueSum ValueShape
    _ Name
    n [Value m]
    vs) =
          Bool -> Doc a -> Doc a
    forall a. Bool -> Doc a -> Doc a
    parensIf (Int
    p Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > (Int
    0 :: Int)) (Doc a -> Doc a) -> Doc a -> Doc a
    forall a b. (a -> b) -> a -> b
    $ Doc a
    "#" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> [Doc a] -> Doc a
    forall ann. [Doc ann] -> Doc ann
    sep (Name -> Doc a
    forall a ann. Pretty a => a -> Doc ann
    forall ann. Name -> Doc ann
    pretty Name
    n Doc a -> [Doc a] -> [Doc a]
    forall a. a -> [a] -> [a]
    : (Value m -> Doc a) -> [Value m] -> [Doc a]
    forall a b. (a -> b) -> [a] -> [b]
    map (Int -> Value m -> Doc a
    pprPrec Int
    1) [Value m]
    vs)
        pprPrec Int
    _ (ValueAD Depth
    _ ADVariable
    v) = PrimValue -> Doc a
    pprPrim (PrimValue -> Doc a) -> PrimValue -> Doc a
    forall a b. (a -> b) -> a -> b
    $ PrimValue -> PrimValue
    putV (PrimValue -> PrimValue) -> PrimValue -> PrimValue
    forall a b. (a -> b) -> a -> b
    $ ADVariable -> PrimValue
    AD.varPrimal ADVariable
    v
        pprElem :: Value m -> Doc a
    pprElem v :: Value m
    v@ValueArray {} = Int -> Value m -> Doc a
    pprPrec Int
    0 Value m
    v
        pprElem Value m
    v = Doc a -> Doc a
    forall ann. Doc ann -> Doc ann
    group (Doc a -> Doc a) -> Doc a -> Doc a
    forall a b. (a -> b) -> a -> b
    $ Int -> Value m -> Doc a
    pprPrec Int
    0 Value m
    v

        putV :: PrimValue -> PrimValue
    putV (P.IntValue IntValue
    x) = IntValue -> PrimValue
    SignedValue IntValue
    x
        putV (P.FloatValue FloatValue
    x) = FloatValue -> PrimValue
    FloatValue FloatValue
    x
        putV (P.BoolValue Bool
    x) = Bool -> PrimValue
    BoolValue Bool
    x
        putV PrimValue
    P.UnitValue = Bool -> PrimValue
    BoolValue Bool
    True

    -- | Prettyprint value.
    prettyValue :: Value m -> Doc a
    prettyValue :: forall (m :: * -> *) a. Value m -> Doc a
    prettyValue = (PrimValue -> Doc a) -> Value m -> Doc a
    forall a (m :: * -> *). (PrimValue -> Doc a) -> Value m -> Doc a
    prettyValueWith PrimValue -> Doc a
    forall {ann}. PrimValue -> Doc ann
    pprPrim
      where
        pprPrim :: PrimValue -> Doc ann
    pprPrim (UnsignedValue (Int8Value Int8
    v)) = Word8 -> Doc ann
    forall ann. Word8 -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    pretty (Int8 -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int8
    v :: Word8)
        pprPrim (UnsignedValue (Int16Value Int16
    v)) = Word16 -> Doc ann
    forall ann. Word16 -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    pretty (Int16 -> Word16
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int16
    v :: Word16)
        pprPrim (UnsignedValue (Int32Value Int32
    v)) = Word32 -> Doc ann
    forall ann. Word32 -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    pretty (Int32 -> Word32
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int32
    v :: Word32)
        pprPrim (UnsignedValue (Int64Value Int64
    v)) = Word64 -> Doc ann
    forall ann. Word64 -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    pretty (Int64 -> Word64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int64
    v :: Word64)
        pprPrim (SignedValue (Int8Value Int8
    v)) = Int8 -> Doc ann
    forall ann. Int8 -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    pretty Int8
    v
        pprPrim (SignedValue (Int16Value Int16
    v)) = Int16 -> Doc ann
    forall ann. Int16 -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    pretty Int16
    v
        pprPrim (SignedValue (Int32Value Int32
    v)) = Int32 -> Doc ann
    forall ann. Int32 -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    pretty Int32
    v
        pprPrim (SignedValue (Int64Value Int64
    v)) = Int64 -> Doc ann
    forall ann. Int64 -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    pretty Int64
    v
        pprPrim (BoolValue Bool
    True) = Doc ann
    "true"
        pprPrim (BoolValue Bool
    False) = Doc ann
    "false"
        pprPrim (FloatValue (Float16Value Half
    v)) = Doc ann -> Half -> Doc ann
    forall {a} {ann}. (RealFloat a, Show a) => Doc ann -> a -> Doc ann
    pprFloat Doc ann
    "f16." Half
    v
        pprPrim (FloatValue (Float32Value Float
    v)) = Doc ann -> Float -> Doc ann
    forall {a} {ann}. (RealFloat a, Show a) => Doc ann -> a -> Doc ann
    pprFloat Doc ann
    "f32." Float
    v
        pprPrim (FloatValue (Float64Value Double
    v)) = Doc ann -> Double -> Doc ann
    forall {a} {ann}. (RealFloat a, Show a) => Doc ann -> a -> Doc ann
    pprFloat Doc ann
    "f64." Double
    v
        pprFloat :: Doc ann -> a -> Doc ann
    pprFloat Doc ann
    t a
    v
          | a -> Bool
    forall a. RealFloat a => a -> Bool
    isInfinite a
    v, a
    v a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    >= a
    0 = Doc ann
    t Doc ann -> Doc ann -> Doc ann
    forall a. Semigroup a => a -> a -> a
    <> Doc ann
    "inf"
          | a -> Bool
    forall a. RealFloat a => a -> Bool
    isInfinite a
    v, a
    v a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    < a
    0 = Doc ann
    "-" Doc ann -> Doc ann -> Doc ann
    forall a. Semigroup a => a -> a -> a
    <> Doc ann
    t Doc ann -> Doc ann -> Doc ann
    forall a. Semigroup a => a -> a -> a
    <> Doc ann
    "inf"
          | a -> Bool
    forall a. RealFloat a => a -> Bool
    isNaN a
    v = Doc ann
    t Doc ann -> Doc ann -> Doc ann
    forall a. Semigroup a => a -> a -> a
    <> Doc ann
    "nan"
          | Bool
    otherwise = String -> Doc ann
    forall ann. String -> Doc ann
    forall a ann. Pretty a => a -> Doc ann
    pretty (String -> Doc ann) -> String -> Doc ann
    forall a b. (a -> b) -> a -> b
    $ a -> String
    forall a. Show a => a -> String
    show a
    v

    -- | The value in the textual format.
    valueText :: Value m -> T.Text
    valueText :: forall (m :: * -> *). Value m -> Text
    valueText = Doc (ZonkAny 0) -> Text
    forall a. Doc a -> Text
    docText (Doc (ZonkAny 0) -> Text)
    -> (Value m -> Doc (ZonkAny 0)) -> Value m -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (PrimValue -> Doc (ZonkAny 0)) -> Value m -> Doc (ZonkAny 0)
    forall a (m :: * -> *). (PrimValue -> Doc a) -> Value m -> Doc a
    prettyValueWith PrimValue -> Doc (ZonkAny 0)
    forall a ann. Pretty a => a -> Doc ann
    forall {ann}. PrimValue -> Doc ann
    pretty

    valueShape :: Value m -> ValueShape
    valueShape :: forall (m :: * -> *). Value m -> ValueShape
    valueShape (ValueArray ValueShape
    shape Array Int (Value m)
    _) = ValueShape
    shape
    valueShape (ValueAcc ValueShape
    shape Value m -> Value m -> m (Value m)
    _ Array Int (Value m)
    _) = ValueShape
    shape
    valueShape (ValueRecord Map Name (Value m)
    fs) = Map Name ValueShape -> ValueShape
    forall d. Map Name (Shape d) -> Shape d
    ShapeRecord (Map Name ValueShape -> ValueShape)
    -> Map Name ValueShape -> ValueShape
    forall a b. (a -> b) -> a -> b
    $ (Value m -> ValueShape)
    -> Map Name (Value m) -> Map Name ValueShape
    forall a b k. (a -> b) -> Map k a -> Map k b
    M.map Value m -> ValueShape
    forall (m :: * -> *). Value m -> ValueShape
    valueShape Map Name (Value m)
    fs
    valueShape (ValueSum ValueShape
    shape Name
    _ [Value m]
    _) = ValueShape
    shape
    valueShape Value m
    _ = ValueShape
    forall d. Shape d
    ShapeLeaf

    -- | Retrieve the part of the value shape that corresponds to outer array
    -- dimensions. This is used for reporting shapes in those cases where the full
    -- shape is not important, namely in indexing errors.
    arrayValueShape :: Value m -> ValueShape
    arrayValueShape :: forall (m :: * -> *). Value m -> ValueShape
    arrayValueShape = ValueShape -> ValueShape
    forall {d}. Shape d -> Shape d
    outer (ValueShape -> ValueShape)
    -> (Value m -> ValueShape) -> Value m -> ValueShape
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value m -> ValueShape
    forall (m :: * -> *). Value m -> ValueShape
    valueShape
      where
        outer :: Shape d -> Shape d
    outer (ShapeDim d
    d Shape d
    s) = d -> Shape d -> Shape d
    forall d. d -> Shape d -> Shape d
    ShapeDim d
    d (Shape d -> Shape d) -> Shape d -> Shape d
    forall a b. (a -> b) -> a -> b
    $ Shape d -> Shape d
    outer Shape d
    s
        outer Shape d
    _ = Shape d
    forall d. Shape d
    ShapeLeaf

    -- TODO: Perhaps there is some clever way to reuse the code between
    -- valueAccum and valueAccumLM
    valueAccum :: (a -> Value m -> (a, Value m)) -> a -> Value m -> (a, Value m)
    valueAccum :: forall a (m :: * -> *).
    (a -> Value m -> (a, Value m)) -> a -> Value m -> (a, Value m)
    valueAccum a -> Value m -> (a, Value m)
    f a
    i = Identity (a, Value m) -> (a, Value m)
    forall a. Identity a -> a
    runIdentity (Identity (a, Value m) -> (a, Value m))
    -> (Value m -> Identity (a, Value m)) -> Value m -> (a, Value m)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (a -> Value m -> Identity (a, Value m))
    -> a -> Value m -> Identity (a, Value m)
    forall (f :: * -> *) a (m :: * -> *).
    Monad f =>
    (a -> Value m -> f (a, Value m)) -> a -> Value m -> f (a, Value m)
    valueAccumLM a -> Value m -> Identity (a, Value m)
    forall {f :: * -> *}.
    Applicative f =>
    a -> Value m -> f (a, Value m)
    f' a
    i
      where
        f' :: a -> Value m -> f (a, Value m)
    f' a
    acc Value m
    v = (a, Value m) -> f (a, Value m)
    forall a. a -> f a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ((a, Value m) -> f (a, Value m)) -> (a, Value m) -> f (a, Value m)
    forall a b. (a -> b) -> a -> b
    $ a -> Value m -> (a, Value m)
    f a
    acc Value m
    v

    valueAccumLM :: (Monad f) => (a -> Value m -> f (a, Value m)) -> a -> Value m -> f (a, Value m)
    valueAccumLM :: forall (f :: * -> *) a (m :: * -> *).
    Monad f =>
    (a -> Value m -> f (a, Value m)) -> a -> Value m -> f (a, Value m)
    valueAccumLM a -> Value m -> f (a, Value m)
    f a
    i v :: Value m
    v@(ValuePrim {}) = a -> Value m -> f (a, Value m)
    f a
    i Value m
    v
    valueAccumLM a -> Value m -> f (a, Value m)
    f a
    i v :: Value m
    v@(ValueAD {}) = a -> Value m -> f (a, Value m)
    f a
    i Value m
    v
    valueAccumLM a -> Value m -> f (a, Value m)
    f a
    i (ValueRecord Map Name (Value m)
    m) = do
      (a, b) <- (a -> Value m -> f (a, Value m))
    -> a -> Map Name (Value m) -> f (a, Map Name (Value m))
    forall (m :: * -> *) (t :: * -> *) acc x y.
    (Monad m, Traversable t) =>
    (acc -> x -> m (acc, y)) -> acc -> t x -> m (acc, t y)
    mapAccumLM ((a -> Value m -> f (a, Value m)) -> a -> Value m -> f (a, Value m)
    forall (f :: * -> *) a (m :: * -> *).
    Monad f =>
    (a -> Value m -> f (a, Value m)) -> a -> Value m -> f (a, Value m)
    valueAccumLM a -> Value m -> f (a, Value m)
    f) a
    i Map Name (Value m)
    m
      pure (a, ValueRecord b)
    valueAccumLM a -> Value m -> f (a, Value m)
    f a
    i (ValueArray ValueShape
    s Array Int (Value m)
    a) = do
      -- TODO: This could probably be better
      -- Transform into a map
      let m :: Map Int (Value m)
    m = [(Int, Value m)] -> Map Int (Value m)
    forall k a. Ord k => [(k, a)] -> Map k a
    M.fromList ([(Int, Value m)] -> Map Int (Value m))
    -> [(Int, Value m)] -> Map Int (Value m)
    forall a b. (a -> b) -> a -> b
    $ Array Int (Value m) -> [(Int, Value m)]
    forall i e. Ix i => Array i e -> [(i, e)]
    assocs Array Int (Value m)
    a
      -- Accumulate over the map
      (i', m') <- (a -> Value m -> f (a, Value m))
    -> a -> Map Int (Value m) -> f (a, Map Int (Value m))
    forall (m :: * -> *) (t :: * -> *) acc x y.
    (Monad m, Traversable t) =>
    (acc -> x -> m (acc, y)) -> acc -> t x -> m (acc, t y)
    mapAccumLM ((a -> Value m -> f (a, Value m)) -> a -> Value m -> f (a, Value m)
    forall (f :: * -> *) a (m :: * -> *).
    Monad f =>
    (a -> Value m -> f (a, Value m)) -> a -> Value m -> f (a, Value m)
    valueAccumLM a -> Value m -> f (a, Value m)
    f) a
    i Map Int (Value m)
    m
      -- Transform back into an array and return
      let a' = (Int, Int) -> [(Int, Value m)] -> Array Int (Value m)
    forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
    array (Array Int (Value m) -> (Int, Int)
    forall i e. Array i e -> (i, i)
    bounds Array Int (Value m)
    a) (Map Int (Value m) -> [(Int, Value m)]
    forall k a. Map k a -> [(k, a)]
    M.toList Map Int (Value m)
    m')
      pure (i', ValueArray s a')
    valueAccumLM a -> Value m -> f (a, Value m)
    f a
    i (ValueSum ValueShape
    shape Name
    c [Value m]
    vs) = do
      (a, vs') <- (a -> Value m -> f (a, Value m))
    -> a -> [Value m] -> f (a, [Value m])
    forall (m :: * -> *) (t :: * -> *) acc x y.
    (Monad m, Traversable t) =>
    (acc -> x -> m (acc, y)) -> acc -> t x -> m (acc, t y)
    mapAccumLM ((a -> Value m -> f (a, Value m)) -> a -> Value m -> f (a, Value m)
    forall (f :: * -> *) a (m :: * -> *).
    Monad f =>
    (a -> Value m -> f (a, Value m)) -> a -> Value m -> f (a, Value m)
    valueAccumLM a -> Value m -> f (a, Value m)
    f) a
    i [Value m]
    vs
      pure (a, ValueSum shape c vs')
    valueAccumLM a -> Value m -> f (a, Value m)
    _ a
    _ Value m
    v = String -> f (a, Value m)
    forall a. HasCallStack => String -> a
    error (String -> f (a, Value m)) -> String -> f (a, Value m)
    forall a b. (a -> b) -> a -> b
    $ String
    "valueAccum not implemented for " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Value m -> String
    forall a. Show a => a -> String
    show Value m
    v

    -- | Does the value correspond to an empty array?
    isEmptyArray :: Value m -> Bool
    isEmptyArray :: forall (m :: * -> *). Value m -> Bool
    isEmptyArray = ValueShape -> Bool
    emptyShape (ValueShape -> Bool) -> (Value m -> ValueShape) -> Value m -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value m -> ValueShape
    forall (m :: * -> *). Value m -> ValueShape
    valueShape

    -- | String representation of an empty array with the provided element
    -- type.  This is pretty ad-hoc - don't expect good results unless the
    -- element type is a primitive.
    prettyEmptyArray :: TypeBase () () -> Value m -> T.Text
    prettyEmptyArray :: forall (m :: * -> *). TypeBase () () -> Value m -> Text
    prettyEmptyArray TypeBase () ()
    t Value m
    v =
      Text
    "empty(" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> ValueShape -> Text
    forall {a}. Pretty a => Shape a -> Text
    dims (Value m -> ValueShape
    forall (m :: * -> *). Value m -> ValueShape
    valueShape Value m
    v) Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> TypeBase () () -> Text
    forall a. Pretty a => a -> Text
    prettyText TypeBase () ()
    t' Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    ")"
      where
        t' :: TypeBase () ()
    t' = Int -> TypeBase () () -> TypeBase () ()
    forall dim as. Int -> TypeBase dim as -> TypeBase dim as
    stripArray (TypeBase () () -> Int
    forall d u. TypeBase d u -> Int
    arrayRank TypeBase () ()
    t) TypeBase () ()
    t
        dims :: Shape a -> Text
    dims (ShapeDim a
    n Shape a
    rowshape) =
          Text
    "[" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> a -> Text
    forall a. Pretty a => a -> Text
    prettyText a
    n Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    "]" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Shape a -> Text
    dims Shape a
    rowshape
        dims Shape a
    _ = Text
    ""

    toArray :: ValueShape -> [Value m] -> Value m
    toArray :: forall (m :: * -> *). ValueShape -> [Value m] -> Value m
    toArray ValueShape
    shape [Value m]
    vs = ValueShape -> Array Int (Value m) -> Value m
    forall (m :: * -> *). ValueShape -> Array Int (Value m) -> Value m
    ValueArray ValueShape
    shape ((Int, Int) -> [Value m] -> Array Int (Value m)
    forall i e. Ix i => (i, i) -> [e] -> Array i e
    listArray (Int
    0, [Value m] -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [Value m]
    vs Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1) [Value m]
    vs)

    toArray' :: ValueShape -> [Value m] -> Value m
    toArray' :: forall (m :: * -> *). ValueShape -> [Value m] -> Value m
    toArray' ValueShape
    rowshape [Value m]
    vs = ValueShape -> Array Int (Value m) -> Value m
    forall (m :: * -> *). ValueShape -> Array Int (Value m) -> Value m
    ValueArray ValueShape
    shape ((Int, Int) -> [Value m] -> Array Int (Value m)
    forall i e. Ix i => (i, i) -> [e] -> Array i e
    listArray (Int
    0, [Value m] -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [Value m]
    vs Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1) [Value m]
    vs)
      where
        shape :: ValueShape
    shape = Int64 -> ValueShape -> ValueShape
    forall d. d -> Shape d -> Shape d
    ShapeDim ([Value m] -> Int64
    forall i a. Num i => [a] -> i
    genericLength [Value m]
    vs) ValueShape
    rowshape

    arrayLength :: (Integral int) => Array Int (Value m) -> int
    arrayLength :: forall int (m :: * -> *).
    Integral int =>
    Array Int (Value m) -> int
    arrayLength = Int -> int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int -> int)
    -> (Array Int (Value m) -> Int) -> Array Int (Value m) -> int
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Int
    1) (Int -> Int)
    -> (Array Int (Value m) -> Int) -> Array Int (Value m) -> Int
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Int, Int) -> Int
    forall a b. (a, b) -> b
    snd ((Int, Int) -> Int)
    -> (Array Int (Value m) -> (Int, Int))
    -> Array Int (Value m)
    -> Int
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Array Int (Value m) -> (Int, Int)
    forall i e. Array i e -> (i, i)
    bounds

    toTuple :: [Value m] -> Value m
    toTuple :: forall (m :: * -> *). [Value m] -> Value m
    toTuple = Map Name (Value m) -> Value m
    forall (m :: * -> *). Map Name (Value m) -> Value m
    ValueRecord (Map Name (Value m) -> Value m)
    -> ([Value m] -> Map Name (Value m)) -> [Value m] -> Value m
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [(Name, Value m)] -> Map Name (Value m)
    forall k a. Ord k => [(k, a)] -> Map k a
    M.fromList ([(Name, Value m)] -> Map Name (Value m))
    -> ([Value m] -> [(Name, Value m)])
    -> [Value m]
    -> Map Name (Value m)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Name] -> [Value m] -> [(Name, Value m)]
    forall a b. [a] -> [b] -> [(a, b)]
    zip [Name]
    tupleFieldNames

    fromTuple :: Value m -> Maybe [Value m]
    fromTuple :: forall (m :: * -> *). Value m -> Maybe [Value m]
    fromTuple (ValueRecord Map Name (Value m)
    m) = Map Name (Value m) -> Maybe [Value m]
    forall a. Map Name a -> Maybe [a]
    areTupleFields Map Name (Value m)
    m
    fromTuple Value m
    _ = Maybe [Value m]
    forall a. Maybe a
    Nothing

    fromDataShape :: V.Vector Int -> ValueShape
    fromDataShape :: Vector Int -> ValueShape
    fromDataShape = (Int -> ValueShape -> ValueShape)
    -> ValueShape -> [Int] -> ValueShape
    forall a b. (a -> b -> b) -> b -> [a] -> b
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr (Int64 -> ValueShape -> ValueShape
    forall d. d -> Shape d -> Shape d
    ShapeDim (Int64 -> ValueShape -> ValueShape)
    -> (Int -> Int64) -> Int -> ValueShape -> ValueShape
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int -> Int64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral) ValueShape
    forall d. Shape d
    ShapeLeaf ([Int] -> ValueShape)
    -> (Vector Int -> [Int]) -> Vector Int -> ValueShape
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Vector Int -> [Int]
    forall a. Storable a => Vector a -> [a]
    SVec.toList

    fromDataValueWith ::
      (SVec.Storable a) =>
      (a -> PrimValue) ->
      SVec.Vector Int ->
      SVec.Vector a ->
      Value m
    fromDataValueWith :: forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith a -> PrimValue
    f Vector Int
    shape Vector a
    vector
      | Vector Int -> Bool
    forall a. Storable a => Vector a -> Bool
    SVec.null Vector Int
    shape = PrimValue -> Value m
    forall (m :: * -> *). PrimValue -> Value m
    ValuePrim (PrimValue -> Value m) -> PrimValue -> Value m
    forall a b. (a -> b) -> a -> b
    $ a -> PrimValue
    f (a -> PrimValue) -> a -> PrimValue
    forall a b. (a -> b) -> a -> b
    $ Vector a -> a
    forall a. Storable a => Vector a -> a
    SVec.head Vector a
    vector
      | Vector a -> Bool
    forall a. Storable a => Vector a -> Bool
    SVec.null Vector a
    vector =
          ValueShape -> [Value m] -> Value m
    forall (m :: * -> *). ValueShape -> [Value m] -> Value m
    toArray (Vector Int -> ValueShape
    fromDataShape Vector Int
    shape) ([Value m] -> Value m) -> [Value m] -> Value m
    forall a b. (a -> b) -> a -> b
    $
            Int -> Value m -> [Value m]
    forall a. Int -> a -> [a]
    replicate (Vector Int -> Int
    forall a. Storable a => Vector a -> a
    SVec.head Vector Int
    shape) ((a -> PrimValue) -> Vector Int -> Vector a -> Value m
    forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith a -> PrimValue
    f Vector Int
    shape' Vector a
    vector)
      | Bool
    otherwise =
          ValueShape -> [Value m] -> Value m
    forall (m :: * -> *). ValueShape -> [Value m] -> Value m
    toArray (Vector Int -> ValueShape
    fromDataShape Vector Int
    shape)
            ([Value m] -> Value m) -> ([[a]] -> [Value m]) -> [[a]] -> Value m
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ([a] -> Value m) -> [[a]] -> [Value m]
    forall a b. (a -> b) -> [a] -> [b]
    map ((a -> PrimValue) -> Vector Int -> Vector a -> Value m
    forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith a -> PrimValue
    f Vector Int
    shape' (Vector a -> Value m) -> ([a] -> Vector a) -> [a] -> Value m
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [a] -> Vector a
    forall a. Storable a => [a] -> Vector a
    SVec.fromList)
            ([[a]] -> Value m) -> [[a]] -> Value m
    forall a b. (a -> b) -> a -> b
    $ Int -> [a] -> [[a]]
    forall a. Int -> [a] -> [[a]]
    chunk (Vector Int -> Int
    forall a. (Storable a, Num a) => Vector a -> a
    SVec.product Vector Int
    shape') (Vector a -> [a]
    forall a. Storable a => Vector a -> [a]
    SVec.toList Vector a
    vector)
      where
        shape' :: Vector Int
    shape' = Vector Int -> Vector Int
    forall a. Storable a => Vector a -> Vector a
    SVec.tail Vector Int
    shape

    -- | Convert a Futhark value in the externally observable data format
    -- to an interpreter value.
    fromDataValue :: V.Value -> Value m
    fromDataValue :: forall (m :: * -> *). Value -> Value m
    fromDataValue (V.I8Value Vector Int
    shape Vector Int8
    vector) =
      (Int8 -> PrimValue) -> Vector Int -> Vector Int8 -> Value m
    forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith (IntValue -> PrimValue
    SignedValue (IntValue -> PrimValue) -> (Int8 -> IntValue) -> Int8 -> PrimValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int8 -> IntValue
    Int8Value) Vector Int
    shape Vector Int8
    vector
    fromDataValue (V.I16Value Vector Int
    shape Vector Int16
    vector) =
      (Int16 -> PrimValue) -> Vector Int -> Vector Int16 -> Value m
    forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith (IntValue -> PrimValue
    SignedValue (IntValue -> PrimValue)
    -> (Int16 -> IntValue) -> Int16 -> PrimValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int16 -> IntValue
    Int16Value) Vector Int
    shape Vector Int16
    vector
    fromDataValue (V.I32Value Vector Int
    shape Vector Int32
    vector) =
      (Int32 -> PrimValue) -> Vector Int -> Vector Int32 -> Value m
    forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith (IntValue -> PrimValue
    SignedValue (IntValue -> PrimValue)
    -> (Int32 -> IntValue) -> Int32 -> PrimValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int32 -> IntValue
    Int32Value) Vector Int
    shape Vector Int32
    vector
    fromDataValue (V.I64Value Vector Int
    shape Vector Int64
    vector) =
      (Int64 -> PrimValue) -> Vector Int -> Vector Int64 -> Value m
    forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith (IntValue -> PrimValue
    SignedValue (IntValue -> PrimValue)
    -> (Int64 -> IntValue) -> Int64 -> PrimValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int64 -> IntValue
    Int64Value) Vector Int
    shape Vector Int64
    vector
    fromDataValue (V.U8Value Vector Int
    shape Vector Word8
    vector) =
      (Word8 -> PrimValue) -> Vector Int -> Vector Word8 -> Value m
    forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith (IntValue -> PrimValue
    UnsignedValue (IntValue -> PrimValue)
    -> (Word8 -> IntValue) -> Word8 -> PrimValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int8 -> IntValue
    Int8Value (Int8 -> IntValue) -> (Word8 -> Int8) -> Word8 -> IntValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Word8 -> Int8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral) Vector Int
    shape Vector Word8
    vector
    fromDataValue (V.U16Value Vector Int
    shape Vector Word16
    vector) =
      (Word16 -> PrimValue) -> Vector Int -> Vector Word16 -> Value m
    forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith (IntValue -> PrimValue
    UnsignedValue (IntValue -> PrimValue)
    -> (Word16 -> IntValue) -> Word16 -> PrimValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int16 -> IntValue
    Int16Value (Int16 -> IntValue) -> (Word16 -> Int16) -> Word16 -> IntValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Word16 -> Int16
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral) Vector Int
    shape Vector Word16
    vector
    fromDataValue (V.U32Value Vector Int
    shape Vector Word32
    vector) =
      (Word32 -> PrimValue) -> Vector Int -> Vector Word32 -> Value m
    forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith (IntValue -> PrimValue
    UnsignedValue (IntValue -> PrimValue)
    -> (Word32 -> IntValue) -> Word32 -> PrimValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int32 -> IntValue
    Int32Value (Int32 -> IntValue) -> (Word32 -> Int32) -> Word32 -> IntValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Word32 -> Int32
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral) Vector Int
    shape Vector Word32
    vector
    fromDataValue (V.U64Value Vector Int
    shape Vector Word64
    vector) =
      (Word64 -> PrimValue) -> Vector Int -> Vector Word64 -> Value m
    forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith (IntValue -> PrimValue
    UnsignedValue (IntValue -> PrimValue)
    -> (Word64 -> IntValue) -> Word64 -> PrimValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int64 -> IntValue
    Int64Value (Int64 -> IntValue) -> (Word64 -> Int64) -> Word64 -> IntValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Word64 -> Int64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral) Vector Int
    shape Vector Word64
    vector
    fromDataValue (V.F16Value Vector Int
    shape Vector Half
    vector) =
      (Half -> PrimValue) -> Vector Int -> Vector Half -> Value m
    forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith (FloatValue -> PrimValue
    FloatValue (FloatValue -> PrimValue)
    -> (Half -> FloatValue) -> Half -> PrimValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Half -> FloatValue
    Float16Value) Vector Int
    shape Vector Half
    vector
    fromDataValue (V.F32Value Vector Int
    shape Vector Float
    vector) =
      (Float -> PrimValue) -> Vector Int -> Vector Float -> Value m
    forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith (FloatValue -> PrimValue
    FloatValue (FloatValue -> PrimValue)
    -> (Float -> FloatValue) -> Float -> PrimValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Float -> FloatValue
    Float32Value) Vector Int
    shape Vector Float
    vector
    fromDataValue (V.F64Value Vector Int
    shape Vector Double
    vector) =
      (Double -> PrimValue) -> Vector Int -> Vector Double -> Value m
    forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith (FloatValue -> PrimValue
    FloatValue (FloatValue -> PrimValue)
    -> (Double -> FloatValue) -> Double -> PrimValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Double -> FloatValue
    Float64Value) Vector Int
    shape Vector Double
    vector
    fromDataValue (V.BoolValue Vector Int
    shape Vector Bool
    vector) =
      (Bool -> PrimValue) -> Vector Int -> Vector Bool -> Value m
    forall a (m :: * -> *).
    Storable a =>
    (a -> PrimValue) -> Vector Int -> Vector a -> Value m
    fromDataValueWith Bool -> PrimValue
    BoolValue Vector Int
    shape Vector Bool
    vector