Cómo deconstruir un SNat (singletons)

Estoy experimentando con tipos dependientes en Haskell y encontré lo siguiente en elpapel del paquete '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)

Así que intenté implementar esto yo mismo, solo para tener una idea de cómo 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?

Ahora el problema es que elSing instancia paraNat no tieneSZero oSSucc. Solo hay un constructor llamadoSNat.

> :info Sing
data instance Sing n where
  SNat :: KnownNat n => Sing n

Esto es diferente de otros singletons que permiten la coincidencia, comoSTrue ySFalse, como en el siguiente ejemplo (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

Puedes usarfromSing para obtener un tipo base, pero esto, por supuesto, permite que GHC verifique el tipo del vector de salida:

-- 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

Entonces mi pregunta: cómo implementarreplicateV?

EDITAR

La respuesta dada por erisco explica por qué mi enfoque de deconstruir unSNat No funciona. Pero incluso con eltype-natural biblioteca, no puedo implementarreplicateV Para elV tipo de datosutilizando el incorporado de GHCNat tipos.

Por ejemplo, el siguiente 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

Pero esto no parece proporcionar suficiente información al compilador para inferir sin es0 o no. Por ejemplo, lo siguiente da un error de compilación:

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

Esto da el siguiente error:

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)

Entonces, mi problema original sigue siendo, todavía no puedo hacer nada útil con elSNat.

Respuestas a la pregunta(2)

Su respuesta a la pregunta