hasktorch-gradually-typed-0.2.0.0: experimental project for hasktorch
Safe HaskellSafe-Inferred
LanguageHaskell2010

Torch.GraduallyTyped.Shape.Class

Synopsis

Documentation

>>> import Torch.GraduallyTyped.Prelude.List (SList (..))
>>> import Torch.GraduallyTyped

type family AddSizeF (size :: Size Nat) (size' :: Size Nat) :: Size Nat where ... Source #

Equations

AddSizeF ('Size size) ('Size size') = 'Size (size + size') 
AddSizeF size size' = size <+> size' 

type family AddDimF (dim :: Dim (Name Symbol) (Size Nat)) (dim' :: Dim (Name Symbol) (Size Nat)) :: Dim (Name Symbol) (Size Nat) where ... Source #

Equations

AddDimF ('Dim name size) ('Dim name' size') = 'Dim (name <+> name') (AddSizeF size size') 

type family BroadcastSizeF (size :: Size Nat) (size' :: Size Nat) :: Maybe (Size Nat) where ... Source #

Equations

BroadcastSizeF 'UncheckedSize _ = 'Just 'UncheckedSize 
BroadcastSizeF _ 'UncheckedSize = 'Just 'UncheckedSize 
BroadcastSizeF ('Size size) ('Size size) = 'Just ('Size size) 
BroadcastSizeF ('Size size) ('Size 1) = 'Just ('Size size) 
BroadcastSizeF ('Size 1) ('Size size) = 'Just ('Size size) 
BroadcastSizeF ('Size _) ('Size _) = 'Nothing 

type family BroadcastDimF (dim :: Dim (Name Symbol) (Size Nat)) (dim' :: Dim (Name Symbol) (Size Nat)) :: Maybe (Dim (Name Symbol) (Size Nat)) where ... Source #

Equations

BroadcastDimF ('Dim name size) ('Dim name' size') = MapMaybe ('Dim (name <+> name')) (BroadcastSizeF size size') 

type family NumelDimF (dim :: Dim (Name Symbol) (Size Nat)) :: Maybe Nat where ... Source #

Equations

NumelDimF ('Dim _ 'UncheckedSize) = 'Nothing 
NumelDimF ('Dim _ ('Size size)) = 'Just size 

type family BroadcastDimsCheckF (dims :: [Dim (Name Symbol) (Size Nat)]) (dims' :: [Dim (Name Symbol) (Size Nat)]) (result :: Maybe [Dim (Name Symbol) (Size Nat)]) :: [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

BroadcastDimsCheckF dims dims' 'Nothing = TypeError ("Cannot broadcast the dimensions" % ("" % (((((" '" <> dims) <> "' and '") <> dims') <> "'.") % ("" % "You may need to extend, squeeze, or unsqueeze the dimensions manually.")))) 
BroadcastDimsCheckF _ _ ('Just dims) = Reverse dims 

type family BroadcastDimsImplF (reversedDims :: [Dim (Name Symbol) (Size Nat)]) (reversedDims' :: [Dim (Name Symbol) (Size Nat)]) :: Maybe [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

BroadcastDimsImplF '[] reversedDims = 'Just reversedDims 
BroadcastDimsImplF reversedDims '[] = 'Just reversedDims 
BroadcastDimsImplF (dim ': reversedDims) (dim' ': reversedDims') = PrependMaybe (BroadcastDimF dim dim') (BroadcastDimsImplF reversedDims reversedDims') 

type BroadcastDimsF dims dims' = BroadcastDimsCheckF dims dims' (BroadcastDimsImplF (Reverse dims) (Reverse dims')) Source #

type family BroadcastShapesF (shape :: Shape [Dim (Name Symbol) (Size Nat)]) (shape' :: Shape [Dim (Name Symbol) (Size Nat)]) :: Shape [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

BroadcastShapesF shape shape = shape 
BroadcastShapesF ('Shape dims) ('Shape dims') = 'Shape (BroadcastDimsF dims dims') 
BroadcastShapesF shape shape' = shape <+> shape' 

type family NumelDimsF (dims :: [Dim (Name Symbol) (Size Nat)]) :: Maybe Nat where ... Source #

Equations

NumelDimsF '[] = 'Just 1 
NumelDimsF (dim ': dims) = LiftTimesMaybe (NumelDimF dim) (NumelDimsF dims) 

type family NumelF (shape :: Shape [Dim (Name Symbol) (Size Nat)]) :: Maybe Nat where ... Source #

Equations

NumelF 'UncheckedShape = 'Nothing 
NumelF ('Shape dims) = NumelDimsF dims 

type family GetDimAndIndexByNameF (index :: Nat) (result :: (Maybe (Dim (Name Symbol) (Size Nat)), Maybe Nat)) (name :: Symbol) (dims :: [Dim (Name Symbol) (Size Nat)]) :: (Maybe (Dim (Name Symbol) (Size Nat)), Maybe Nat) where ... Source #

Equations

GetDimAndIndexByNameF _ result _ '[] = result 
GetDimAndIndexByNameF index _ name ('Dim 'UncheckedName _ ': dims) = GetDimAndIndexByNameF (index + 1) '('Just ('Dim 'UncheckedName 'UncheckedSize), 'Nothing) name dims 
GetDimAndIndexByNameF index _ name ('Dim ('Name name) size ': _) = '('Just ('Dim ('Name name) size), 'Just index) 
GetDimAndIndexByNameF index result name ('Dim ('Name _) _ ': dims) = GetDimAndIndexByNameF (index + 1) result name dims 

type family GetDimByNameF (name :: Symbol) (dims :: [Dim (Name Symbol) (Size Nat)]) :: Maybe (Dim (Name Symbol) (Size Nat)) where ... Source #

Equations

GetDimByNameF name dims = Fst (GetDimAndIndexByNameF 0 '('Nothing, 'Nothing) name dims) 

type family GetIndexByNameF (name :: Symbol) (dims :: [Dim (Name Symbol) (Size Nat)]) :: Maybe Nat where ... Source #

Equations

GetIndexByNameF name dims = Snd (GetDimAndIndexByNameF 0 '('Nothing, 'Nothing) name dims) 

type family GetDimByIndexF (index :: Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) :: Maybe (Dim (Name Symbol) (Size Nat)) where ... Source #

Equations

GetDimByIndexF 0 (h ': _) = 'Just h 
GetDimByIndexF index (_ ': t) = GetDimByIndexF (index - 1) t 
GetDimByIndexF _ _ = 'Nothing 

type family GetDimImplF (by :: By Symbol Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) :: Maybe (Dim (Name Symbol) (Size Nat)) where ... Source #

Equations

GetDimImplF ('ByName name) dims = GetDimByNameF name dims 
GetDimImplF ('ByIndex index) dims = GetDimByIndexF index dims 

type GetDimErrorMessage (by :: By Symbol Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) = "Cannot return the first dimension matching" % ("" % (((" '" <> by) <> "'") % ("" % ("in the shape" % ("" % (((" '" <> dims) <> "'.") % "")))))) Source #

type family GetDimCheckF (by :: By Symbol Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) (result :: Maybe (Dim (Name Symbol) (Size Nat))) :: Dim (Name Symbol) (Size Nat) where ... Source #

Equations

GetDimCheckF by dims 'Nothing = TypeError (GetDimErrorMessage by dims) 
GetDimCheckF _ _ ('Just dim) = dim 

type family GetDimF (selectDim :: SelectDim (By Symbol Nat)) (shape :: Shape [Dim (Name Symbol) (Size Nat)]) :: Dim (Name Symbol) (Size Nat) where ... Source #

type family (shape :: Shape [Dim (Name Symbol) (Size Nat)]) ! (_k :: k) :: Dim (Name Symbol) (Size Nat) where ... Source #

Equations

shape ! (index :: Nat) = GetDimF ('SelectDim ('ByIndex index)) shape 
shape ! (name :: Symbol) = GetDimF ('SelectDim ('ByName name)) shape 

sGetDimFromShape :: forall selectDim shape dim m. (dim ~ GetDimF selectDim shape, MonadThrow m) => SSelectDim selectDim -> SShape shape -> m (SDim dim) Source #

Get dimension by index or by name from a shape.

>>> shape = SShape $ SName @"batch" :&: SSize @8 :|: SUncheckedName "feature" :&: SSize @2 :|: SNil
>>> dim = sGetDimFromShape (SSelectDim $ SByName @"batch") shape
>>> :type dim
dim :: MonadThrow m => m (SDim ('Dim ('Name "batch") ('Size 8)))
>>> fromSing <$> dim
Dim {dimName = Checked "batch", dimSize = Checked 8}
>>> dim = sGetDimFromShape (SSelectDim $ SByName @"feature") shape
>>> :type dim
dim
  :: MonadThrow m => m (SDim ('Dim 'UncheckedName 'UncheckedSize))
>>> fromSing <$> dim
Dim {dimName = Unchecked "feature", dimSize = Checked 2}
>>> dim = sGetDimFromShape (SSelectDim $ SByName @"sequence") shape
>>> :type dim
dim
  :: MonadThrow m => m (SDim ('Dim 'UncheckedName 'UncheckedSize))
>>> fromSing <$> dim
*** Exception: GetDimError {gdeBy = ByName "sequence"}
>>> dim = sGetDimFromShape (SSelectDim $ SByIndex @0) shape
>>> :type dim
dim :: MonadThrow m => m (SDim ('Dim ('Name "batch") ('Size 8)))
>>> fromSing <$> dim
Dim {dimName = Checked "batch", dimSize = Checked 8}
>>> :type sGetDimFromShape (SSelectDim $ SByIndex @2) shape
sGetDimFromShape (SSelectDim $ SByIndex @2) shape
  :: MonadThrow m => m (SDim (TypeError ...))

type family ReplaceDimByIndexF (index :: Maybe Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) (dim :: Dim (Name Symbol) (Size Nat)) :: Maybe [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

ReplaceDimByIndexF ('Just 0) (_ ': t) dim = 'Just (dim ': t) 
ReplaceDimByIndexF ('Just index) (h ': t) dim = PrependMaybe ('Just h) (ReplaceDimByIndexF ('Just (index - 1)) t dim) 
ReplaceDimByIndexF _ _ _ = 'Nothing 

type family ReplaceDimImplF (by :: By Symbol Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) (dim :: Dim (Name Symbol) (Size Nat)) :: Maybe [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

ReplaceDimImplF ('ByName name) dims dim = ReplaceDimByIndexF (GetIndexByNameF name dims) dims dim 
ReplaceDimImplF ('ByIndex index) dims dim = ReplaceDimByIndexF ('Just index) dims dim 

type family ReplaceDimNameByIndexF (index :: Maybe Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) (name :: Name Symbol) :: Maybe [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

ReplaceDimNameByIndexF ('Just 0) ('Dim _ size ': t) name' = 'Just ('Dim name' size ': t) 
ReplaceDimNameByIndexF ('Just index) (h ': t) name' = PrependMaybe ('Just h) (ReplaceDimNameByIndexF ('Just (index - 1)) t name') 
ReplaceDimNameByIndexF _ _ _ = 'Nothing 

type family ReplaceDimNameImplF (by :: By Symbol Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) (name' :: Name Symbol) :: Maybe [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

ReplaceDimNameImplF ('ByName name) dims name' = ReplaceDimNameByIndexF (GetIndexByNameF name dims) dims name' 
ReplaceDimNameImplF ('ByIndex index) dims name' = ReplaceDimNameByIndexF ('Just index) dims name' 

type family ReplaceDimSizeByIndexF (index :: Maybe Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) (size' :: Size Nat) :: Maybe [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

ReplaceDimSizeByIndexF ('Just 0) ('Dim name _ ': t) size' = 'Just ('Dim name size' ': t) 
ReplaceDimSizeByIndexF ('Just index) (h ': t) size' = PrependMaybe ('Just h) (ReplaceDimSizeByIndexF ('Just (index - 1)) t size') 
ReplaceDimSizeByIndexF _ _ _ = 'Nothing 

type family ReplaceDimSizeImplF (by :: By Symbol Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) (size' :: Size Nat) :: Maybe [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

ReplaceDimSizeImplF ('ByName name) dims size' = ReplaceDimSizeByIndexF (GetIndexByNameF name dims) dims size' 
ReplaceDimSizeImplF ('ByIndex index) dims size' = ReplaceDimSizeByIndexF ('Just index) dims size' 

type ReplaceDimErrorMessage (by :: By Symbol Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) (dim :: Dim (Name Symbol) (Size Nat)) = "Cannot replace the first dimension matching" % ("" % (((" '" <> by) <> "'") % ("" % ("in the shape" % ("" % (((" '" <> dims) <> "'") % ("" % ("with" % ("" % (((" '" <> dim) <> "'.") % "")))))))))) Source #

type family ReplaceDimCheckF (by :: By Symbol Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) (dim :: Dim (Name Symbol) (Size Nat)) (result :: Maybe [Dim (Name Symbol) (Size Nat)]) :: [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

ReplaceDimCheckF by dims dim 'Nothing = TypeError (ReplaceDimErrorMessage by dims dim) 
ReplaceDimCheckF _ _ _ ('Just dims) = dims 

type family ReplaceDimF (selectDim :: SelectDim (By Symbol Nat)) (shape :: Shape [Dim (Name Symbol) (Size Nat)]) (dim :: Dim (Name Symbol) (Size Nat)) :: Shape [Dim (Name Symbol) (Size Nat)] where ... Source #

type family InsertDimByIndexF (index :: Maybe Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) (dim :: Dim (Name Symbol) (Size Nat)) :: Maybe [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

InsertDimByIndexF ('Just 0) dims dim = 'Just (dim ': dims) 
InsertDimByIndexF ('Just index) (h ': t) dim = PrependMaybe ('Just h) (InsertDimByIndexF ('Just (index - 1)) t dim) 
InsertDimByIndexF _ _ _ = 'Nothing 

type family InsertDimImplF (by :: By Symbol Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) (dim :: Dim (Name Symbol) (Size Nat)) :: Maybe [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

InsertDimImplF ('ByName name) dims dim = InsertDimByIndexF (GetIndexByNameF name dims) dims dim 
InsertDimImplF ('ByIndex index) dims dim = InsertDimByIndexF ('Just index) dims dim 

type InsertDimErrorMessage (by :: By Symbol Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) (dim :: Dim (Name Symbol) (Size Nat)) = "Cannot insert the dimension" % ("" % (((" '" <> dim) <> "'") % ("" % ("before the first dimension matching" % ("" % (((" '" <> by) <> "'") % ("" % ("in the shape" % ("" % (((" '" <> dims) <> "'.") % "")))))))))) Source #

type family InsertDimCheckF (by :: By Symbol Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) (dim :: Dim (Name Symbol) (Size Nat)) (result :: Maybe [Dim (Name Symbol) (Size Nat)]) :: [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

InsertDimCheckF by dims dim 'Nothing = TypeError (InsertDimErrorMessage by dims dim) 
InsertDimCheckF _ _ _ ('Just dims) = dims 

type family InsertDimF (selectDim :: SelectDim (By Symbol Nat)) (shape :: Shape [Dim (Name Symbol) (Size Nat)]) (dim :: Dim (Name Symbol) (Size Nat)) :: Shape [Dim (Name Symbol) (Size Nat)] where ... Source #

type family PrependDimF (dim :: Dim (Name Symbol) (Size Nat)) (shape :: Shape [Dim (Name Symbol) (Size Nat)]) :: Shape [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

PrependDimF dim shape = InsertDimF ('SelectDim ('ByIndex 0)) shape dim 

type family RemoveDimByIndexF (index :: Maybe Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) :: Maybe [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

RemoveDimByIndexF ('Just 0) (dim ': dims) = 'Just dims 
RemoveDimByIndexF ('Just index) (h ': t) = PrependMaybe ('Just h) (RemoveDimByIndexF ('Just (index - 1)) t) 
RemoveDimByIndexF _ _ = 'Nothing 

type family RemoveDimImplF (by :: By Symbol Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) :: Maybe [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

RemoveDimImplF ('ByName name) dims = RemoveDimByIndexF (GetIndexByNameF name dims) dims 
RemoveDimImplF ('ByIndex index) dims = RemoveDimByIndexF ('Just index) dims 

type RemoveDimErrorMessage (by :: By Symbol Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) = "Cannot remove the dimension by" % ("" % (((" '" <> by) <> "'") % ("" % ("in the shape" % ("" % (((" '" <> dims) <> "'.") % "")))))) Source #

type family RemoveDimCheckF (by :: By Symbol Nat) (dims :: [Dim (Name Symbol) (Size Nat)]) (result :: Maybe [Dim (Name Symbol) (Size Nat)]) :: [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

RemoveDimCheckF by dims 'Nothing = TypeError (RemoveDimErrorMessage by dims) 
RemoveDimCheckF _ _ ('Just dims) = dims 

type family RemoveDimF (selectDim :: SelectDim (By Symbol Nat)) (shape :: Shape [Dim (Name Symbol) (Size Nat)]) :: Shape [Dim (Name Symbol) (Size Nat)] where ... Source #

sUnifyName :: forall m name name'. MonadThrow m => SName name -> SName name' -> m (SName (name <+> name')) Source #

sUnifySize :: forall m size size'. MonadThrow m => SSize size -> SSize size' -> m (SSize (size <+> size')) Source #

sUnifyDim :: forall m dim dim'. MonadThrow m => SDim dim -> SDim dim' -> m (SDim (dim <+> dim')) Source #

Unify two dimensions.

>>> dimA = SName @"*" :&: SSize @0
>>> dimB = SName @"batch" :&: SSize @0
>>> dim = sUnifyDim dimA dimB
>>> :type dim
dim :: MonadThrow m => m (SDim ('Dim ('Name "batch") ('Size 0)))
>>> fromSing <$> dim
Dim {dimName = Checked "batch", dimSize = Checked 0}
>>> dimC = SName @"feature" :&: SSize @0
>>> :type sUnifyDim dimB dimC
sUnifyDim dimB dimC
  :: MonadThrow m => m (SDim ('Dim (TypeError ...) ('Size 0)))
>>> dimD = SUncheckedName "batch" :&: SSize @0
>>> dim = sUnifyDim dimA dimD
>>> :type dim
dim :: MonadThrow m => m (SDim ('Dim 'UncheckedName ('Size 0)))
>>> fromSing <$> dim
Dim {dimName = Unchecked "batch", dimSize = Checked 0}
>>> dimE = SUncheckedName "feature" :&: SSize @0
>>> dim = sUnifyDim dimB dimE
>>> :type dim
dim :: MonadThrow m => m (SDim ('Dim 'UncheckedName ('Size 0)))
>>> fromSing <$> dim
*** Exception: UnifyNameError {uneExpect = "batch", uneActual = "feature"}