Como desconstruir um SNat (singletons)
Estou experimentando tipos de dependentes em Haskell e me deparei com o seguinte nopapel do pacote 'singletons':
replicate2 :: forall n a. SingI n => a -> Vec a n
replicate2 a = case (sing :: Sing n) of
SZero -> VNil
SSucc _ -> VCons a (replicate2 a)
Então, eu tentei implementar isso sozinho, apenas para ter uma idéia de como funciona:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Singletons
import Data.Singletons.Prelude
import Data.Singletons.TypeLits
data V :: Nat -> * -> * where
Nil :: V 0 a
(:>) :: a -> V n a -> V (n :+ 1) a
infixr 5 :>
replicateV :: SingI n => a -> V n a
replicateV = replicateV' sing
where replicateV' :: Sing n -> a -> V n a
replicateV' sn a = case sn of
SNat -> undefined -- what can I do with this?
Agora o problema é que oSing
instância paraNat
não temSZero
ouSSucc
. Existe apenas um construtor chamadoSNat
.
> :info Sing
data instance Sing n where
SNat :: KnownNat n => Sing n
Isso é diferente de outros singletons que permitem a correspondência, comoSTrue
eSFalse
, como no exemplo a seguir (inútil):
data Foo :: Bool -> * -> * where
T :: a -> Foo True a
F :: a -> Foo False a
foo :: forall a b. SingI b => a -> Foo b a
foo a = case (sing :: Sing b) of
STrue -> T a
SFalse -> F a
Você pode usarfromSing
para obter um tipo de base, mas é claro que isso permite ao GHC verificar o tipo do vetor de saída:
-- does not typecheck
replicateV2 :: SingI n => a -> V n a
replicateV2 = replicateV' sing
where replicateV' :: Sing n -> a -> V n a
replicateV' sn a = case fromSing sn of
0 -> Nil
n -> a :> replicateV2 a
Então, minha pergunta: como implementarreplicateV
?
EDITAR
A resposta dada por erisco explica por que minha abordagem de desconstruir umSNat
não funciona. Mas mesmo com otype-natural
biblioteca, não consigo implementarreplicateV
para oV
tipo de dadosusando o build-in do GHCNat
tipos.
Por exemplo, o seguinte código compila:
replicateV :: SingI n => a -> V n a
replicateV = replicateV' sing
where replicateV' :: Sing n -> a -> V n a
replicateV' sn a = case TN.sToPeano sn of
TN.SZ -> undefined
(TN.SS sn') -> undefined
Mas isso não parece fornecer informações suficientes para o compilador inferir sen
é0
ou não. Por exemplo, o seguinte fornece um erro do compilador:
replicateV :: SingI n => a -> V n a
replicateV = replicateV' sing
where replicateV' :: Sing n -> a -> V n a
replicateV' sn a = case TN.sToPeano sn of
TN.SZ -> Nil
(TN.SS sn') -> undefined
Isso fornece o seguinte erro:
src/Vec.hs:25:28: error:
• Could not deduce: n1 ~ 0
from the context: TN.ToPeano n1 ~ 'TN.Z
bound by a pattern with constructor:
TN.SZ :: forall (z0 :: TN.Nat). z0 ~ 'TN.Z => Sing z0,
in a case alternative
at src/Vec.hs:25:13-17
‘n1’ is a rigid type variable bound by
the type signature for:
replicateV' :: forall (n1 :: Nat) a1. Sing n1 -> a1 -> V n1 a1
at src/Vec.hs:23:24
Expected type: V n1 a1
Actual type: V 0 a1
• In the expression: Nil
In a case alternative: TN.SZ -> Nil
In the expression:
case TN.sToPeano sn of {
TN.SZ -> Nil
(TN.SS sn') -> undefined }
• Relevant bindings include
sn :: Sing n1 (bound at src/Vec.hs:24:21)
replicateV' :: Sing n1 -> a1 -> V n1 a1 (bound at src/Vec.hs:24:9)
Portanto, meu problema original ainda permanece, ainda não consigo fazer nada útil com oSNat
.