'Nat'

2016-11-21 18 views
6

ile eşleşemedi Bir dizenin N karakterden daha az olmasını garanti eden bir tür oluşturmaya çalışıyorum.'Nat'

src/Simple/Reporting/Metro2/TextMax.hs:18:50: error: 
• Couldn't match kind ‘*’ with ‘Nat’ 
    When matching the kind of ‘Proxy’ 
• In the first argument of ‘natVal’, namely ‘(Proxy :: Proxy n)’ 
    In the second argument of ‘($)’, namely ‘natVal (Proxy :: Proxy n)’ 
    In the second argument of ‘(<=)’, namely 
    ‘(fromIntegral $ natVal (Proxy :: Proxy n))’ 

bu hatayı anlamıyorum:

{-# LANGUAGE KindSignatures #-} 
{-# LANGUAGE OverloadedStrings #-} 
{-# LANGUAGE ScopedTypeVariables #-} 
{-# LANGUAGE DataKinds #-} 

import GHC.TypeLits (Symbol, Nat, KnownNat, natVal, KnownSymbol, symbolVal) 
import Data.Text (Text) 
import qualified Data.Text as Text 
import Data.Proxy (Proxy(..)) 

data TextMax (n :: Nat) = TextMax Text 
    deriving (Show) 

textMax :: KnownNat n => Text -> Maybe (TextMax n) 
textMax t 
    | Text.length t <= (fromIntegral $ natVal (Proxy :: Proxy n)) = Just (TextMax t) 
    | otherwise = Nothing 

Bu

hata veriyor. Neden çalışmıyor? I've used almost this exact same code to get the natVal of n in other places and it works.

Bunu yapmanın daha iyi bir yolu var mı?

textMax :: forall n. KnownNat n => Text -> Maybe (TextMax n) 
textMax t 
    | Text.length t <= (fromIntegral $ natVal (Proxy :: Proxy n)) = Just (TextMax t) 
    | otherwise = Nothing 
+1

Yan sözler: ScopedTypeVariables bilerek ve Proxy n ek açıklamada nKnownNat n kısıtlama aynı n olması için – gallais

cevap

7

Sen textMax imzasını içinde açık forall ihtiyaç açarak ` PolyKinds 'uzantısı, ghc bir poli-kinded' Proxy 'fikri ile daha hafiftir ve hata mesajı farklı hale gelir ve tipik bir' ScopedTypeVariables 'sorunu olduğunu daha açık hale getirir.