Skip to content

Instantly share code, notes, and snippets.

@mstksg
Last active April 26, 2017 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstksg/629fda869802463f9c718fddd7d5c0f1 to your computer and use it in GitHub Desktop.
Save mstksg/629fda869802463f9c718fddd7d5c0f1 to your computer and use it in GitHub Desktop.
data ProdMap :: (a -> b -> Type) -> [a] -> [b] -> Type where
PMZ :: ProdMap f '[] '[]
PMS :: f a b -> ProdMap f as bs -> ProdMap f (a ': as) (b ': bs)
data Slice :: Nat -> Nat -> Type where
Slice :: Sing l -> Sing c -> Sing r -> Slice (l + c + r) c
slice
:: (SingI ns, SingI ms)
=> ProdMap Slice ns ms
-> Tensor ns
-> Tensor ms
-- given a type-level list `ns` of the number of items from each dimension to take,
-- returns the `ProdMap Slice ms ns` that encodes that.
sliceHeads :: Sing ns -> ProdMap Slice ms ns
sliceHeads = \case
SNil -> PMZ
s@SNat `SCons` ss -> Slice (SNat @0) s meh `PMS` sliceHeads ss
-- meh has to be :: Sing (m - n), and positive, so in real life, we'd
-- have to also iterate over ms as well.
headsFromList
:: SingI ms
=> [Integer]
-> Tensor ms
-> (forall ns. SingI ns => Tensor ns -> r)
-> r
headsFromList ns t f = withSomeSing ns $ \nsSing ->
withSingI nsSing $
f (slice (sliceHeads nsSing) t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment