-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SingI Instance for data-types #303
Comments
What I want to write is an instance for this class: class InitRandom a where
init :: MonadRandom m => Sing a -> m a |
If you're looking for something that only generates If you're looking for something that will generate That being said, the code that |
I thought I was doing something wrong because, as you said, it didn't typecheck 😉 ok, can I write the instance by hand or is it a limitation of GHC/singletons that it doesn't typecheck?? |
You can certainly write data Option a = None | Some a
data instance Sing (z :: Option a) where
SNone :: Sing None
SSome :: Sing a -> Sing (Some a)
instance SingI None where
sing = SNone
instance SingI a => SingI (Some a) where
sing = SSome sing The real question is: how are you planning to give |
I think I confused myself with all these types 😀 |
I'll close it because the code above doesn't make sense, or at least is not what I want to do. |
What may interest you is the idea of parameterizing {-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeInType #-}
module Foo where
import Data.Kind
import Data.Singletons.Prelude
import qualified Data.Vector as V
import GHC.TypeNats
newtype Vec' (p :: Type -> Type) (n :: Nat) a = MkVec (p a)
deriving Show
-- Value-level
type Vec = Vec' V.Vector
-- Type-level
type PVec = Vec' []
data instance Sing (z :: PVec n a) where
SMkVec :: Sing x -> Sing (MkVec x :: PVec n a)
instance SingKind a => SingKind (PVec n a) where
type Demote (PVec n a) = Vec n (Demote a)
fromSing (SMkVec sx) = MkVec $ V.fromList $ fromSing sx
toSing (MkVec x) = withSomeSing (V.toList x) $ SomeSing . SMkVec
instance SingI x => SingI (MkVec x :: PVec n a) where
sing = SMkVec sing Of course, you'd still have to write all of this out by hand, and you'd need to expose the constructor to |
I think there is a misunderstanding! |
This does not work, but I think it illustrates what I want to do. data instance Sing (Vec l a) where
SVec :: Sing l -> Sing a -> Sing (Vec l a)
instance (SingI l, SingI a) => SingI (Vec l a) where
sing = SVec sing sing
stest = sing @Type @(Vec 13 Int) everything revolves around this class class InitRandom a where
init :: MonadRandom m => Sing a -> m a I want to initialize a Container polymorphically that is "dependent on it's type" (I don't know whether this makes sense?). |
I'm afraid I don't know how to respond, since a good chunk of the code in #303 (comment) is nonsense. For one thing, one never writes a data instance Sing (Vec l a) where ...
data instance Sing (z :: Vec l a) where ... Also, the constructor itself makes little sense to me: SVec :: Sing l -> Sing a -> Sing (Vec l a) The return type of a |
I think I just solved it... data Vec (n :: Nat) a = UnsafeMkVec { getVector :: V.Vector a }
deriving Show
data instance Sing (Vec l a) where
SVec :: Sing l -> Sing (Vec l a)
instance (SingI l) => SingI (Vec l a) where
sing = SVec sing
stest = sing @Type @(Vec 13 Int) Does this make sense? At least it compiles! |
This is definitely not what you want. A 'Sing a' should represent literally just 'a'. What you wrote isn't really a singleton and definitely does not make sense. If you want a way to create a random value from the shape, you might want something like:
And you could write an instance for your vector:
Note that the singleton in this case is a singleton on the 'Nat' in your vector type. This means you are essentially passing in the Nat as an argument to your
|
I feel a bit stupid 😀 of course instance SingI n => InitRandom (Vec n a) where
... would solve my problem. |
I have a fixed-length vector defined by:
is there some th-function that generates SingI-Instances for Vec so that I can access the length etc.?
The text was updated successfully, but these errors were encountered: