[英]Constraint on function type using data kind
我對數據類型感到困惑。 假設我們有
{-# LANGUAGE DataKinds #-}
...
data Format
= Photo
{ bytes :: Int
}
| Video
{ bytes :: Int
, durationSec :: Int
}
然后我想用升級類型制作 function:
createVideo :: Int -> Int -> 'Video
createVideo _ _ = error "Not implemented"
編譯器會向我們詢問參數,並通過它們給出 msg 'Video Int Int has kind `Format'。 我想要這種類似於 kotlin 的編譯時行為:
sealed class Format {
abstract val bytes: Int
data class Photo(override val bytes: Int) : Format()
data class Video(override val bytes: Int, val durationSec: Int) : Format()
}
private fun createVideo(bytes: Int, durationSec: Int) : Format.Video {
return Format.Video(bytes, durationSec)
}
fun main() {
val video: Format = createVideo(bytes = 0, durationSec = 0) // correct
val video2: Format.Video = createVideo(bytes = 0, durationSec = 0) // correct
val video3: Format.Photo = createVideo(bytes = 0, durationSec = 0) // compiler error
}
數據種類不提供在編譯時檢查數據結構值的直接機制。 換句話說,如果你有一個數據類型:
data Format
= Photo
{ bytes :: Int
}
| Video
{ bytes :: Int
, durationSec :: Int
}
適合表示照片和視頻,使用DataKinds
擴展將其提升到類型級別不允許您編寫僅接受 10 秒視頻的函數:
processTenSecondVideo :: Video bytes 10 -> IO () -- does not work
甚至生成一個保證為視頻的Format
值:
createVideo1 :: Int -> Int -> Format -- works
createVideo2 :: Int -> Int -> Video bytes duration -- does not work
從技術角度來看, DataKinds
擴展推廣的新類型Photo
和Video
是不支持值的一種類型(特別是新類型Format
)。 因此,雖然存在Int
類型的值(因為Int
屬於*
類型,即具有值的類型),但不存在Photo 0
或Video 128000 10
類型的值。 因此,您不能將這些類型用作函數的返回類型(或參數類型)。
那么,數據種類有什么用呢? 好吧,如果您想在編譯時以某種方式約束一個數據結構,那么您就不會提升該數據結構。 相反,您推廣其他數據結構以用作編寫類型級程序以約束目標數據結構的工具。
在您的示例中,您要對數據結構施加的約束確實非常適中:您要檢查數據結構是否是Video
。 您不需要數據類型來執行此操作。 如果您只是重新排列您的數據結構,常規的 Haskell 類型就足夠了。 將格式分為兩種類型:
data Photo = Photo {bytes :: Int}
data Video = Video {bytes :: Int, durationSec :: Int}
足以在編譯時區分視頻和非視頻。 如果您的程序的某些部分要使用可以是視頻或照片的值,則可以引入 sum 類型:
data Format = PhotoF Photo | VideoF Video
當約束變得更加復雜時,數據類型變得有用。 例如,假設您想審查照片視頻以確保它們對整個家庭都是安全的:
censorPhoto :: Photo -> Photo
censorVideo :: Video -> Video
並允許用戶生成屏幕截圖:
screenShot :: Video -> Photo
您可能希望在編譯時確保您不會意外地對視頻進行兩次審查,或者向年輕觀眾展示未經審查的視頻,或者讓某人通過截取未經審查的視頻並將其作為經過審查的照片傳遞來繞過您的審查。
您可以通過引入更多類型來實現這一點:
data UncensoredPhoto = UncensoredPhoto {bytes :: Int}
data UncensoredVideo = UncensoredVideo {bytes :: Int, durationSec :: Int}
data UncensoredFormat = UncensoredPhotoF UncensoredPhoto | UncensoredVideoF UncensoredVideo
data CensoredPhoto = CensoredPhoto {bytes :: Int}
data CensoredVideo = CensoredVideo {bytes :: Int, durationSec :: Int}
data CensoredFormat = CensoredPhotoF CensoredPhoto | CensoredVideoF CensoredVideo
data AnyPhoto = UncensoredPhotoA UncensoredPhoto | CensoredPhotoA CensoredPhoto
data AnyVideo = UncensoredVideoA UncensoredVideo | CensoredVideoA CensoredVideo
data AnyFormat = AnyPhotoF AnyPhoto | AnyVideoF AnyVideo
所以你可以寫這樣的東西:
censorFormat :: UncensoredFormat -> CensoredFormat
censoredScreenshot :: CensoredVideo -> CensoredPhoto
uncensoredScreenshot :: UncensoredVideo -> UncensoredPhoto
showAdult :: AnyFormat -> IO ()
showChild :: CensoredFormat -> IO ()
不過,這很混亂。 假設您想對視頻長度添加一些限制,以防止垃圾郵件發送者提交大量短視頻,或者避免占用您的服務器來審查非常長的視頻。 您想定義多少種沿ShortUncensoredVideo
行的類型?
在這種情況下,您可以使用數據類型來開發一種類型級別的“語言”來描述數據結構的屬性:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
-- these types/constructors will be promoted...
data Censoring = Censored | Uncensored
data Length = Short | Medium | Long
-- ...and used to tag our actual data structures
data Photo (c :: Censoring) = Photo { bytes :: Int }
data Video (c :: Censoring) (l :: Length) = Video {bytes :: Int, durationSec :: Int}
data Format (c :: Censoring) where
PhotoF :: Photo c -> Format c
VideoF :: Video c l -> Format c
現在我們可以編寫如下內容:
-- preserve censoring at compile time
screenShot :: Video c l -> Photo c
screenShot (Video b _) = Photo (b `div` 10)
-- only censor uncensored videos that aren't long
type family NotLong l where
NotLong Long = False
NotLong l = True
censorVideo :: (NotLong l ~ True) => Video Uncensored l -> Video Censored l
censorVideo (Video b l) = Video (b `div` 2) (l `div` 2)
-- show any format to adults
showAdult :: Format c -> IO ()
showAdult fmt = print fmt
-- only censored content for kids
showChild :: Format Censored -> IO ()
showChild fmt = print fmt
並在編譯時發現問題:
main = do
-- we can show a screenshot from a censored version of an uncensored short video to a child
showChild $ PhotoF . screenShot . censorVideo $ (Video 128000 1 :: Video 'Uncensored 'Short)
-- but the following are compilation errors
-- can't censor an already censored video
showAdult $ VideoF . censorVideo $ (Video 128000 1 :: Video 'Censored 'Short)
-- can't censor a long video
showAdult $ VideoF . censorVideo $ (Video 12800000 100 :: Video 'Uncensored 'Long)
-- can't show a child an uncensored screenshot
showChild $ PhotoF . screenShot $ (Video 128000 1 :: Video 'Uncensored 'Short)
請注意,提升的類型( Censored
、 Uncensored
、 Short
、 Medium
、 Long
)和種類( Censoring
和Length
)與它們描述的未經提升的類型Photo
、 Video
和Format
沒有直接關系。 正如我所說,這就是通常使用數據類型的方式。
完整代碼示例:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
-- these types/constructors will be promoted...
data Censoring = Censored | Uncensored
data Length = Short | Medium | Long
-- ...and used to tag our actual data structures
data Photo (c :: Censoring) = Photo { bytes :: Int }
data Video (c :: Censoring) (l :: Length) = Video {bytes :: Int, durationSec :: Int}
data Format (c :: Censoring) where
PhotoF :: Photo c -> Format c
VideoF :: Video c l -> Format c
instance Show (Format c) where show _ = "<Format>"
-- preserve censoring at compile time
screenShot :: Video c l -> Photo c
screenShot (Video b _) = Photo (b `div` 10)
-- only censor uncensored videos that aren't long
type family NotLong l where
NotLong Long = False
NotLong l = True
censorVideo :: (NotLong l ~ True) => Video Uncensored l -> Video Censored l
censorVideo (Video b l) = Video (b `div` 2) (l `div` 2)
-- show any format to adults
showAdult :: Format c -> IO ()
showAdult fmt = print fmt
-- only censored content for kids
showChild :: Format Censored -> IO ()
showChild fmt = print fmt
main = do
-- we can show a screenshot from a censored version of an uncensored short video to a child
showChild $ PhotoF . screenShot . censorVideo $ (Video 128000 1 :: Video 'Uncensored 'Short)
-- but the following are compilation errors
-- can't censor an already censored video
showAdult $ VideoF . censorVideo $ (Video 128000 1 :: Video 'Censored 'Short)
-- can't censor a long video
showAdult $ VideoF . censorVideo $ (Video 12800000 100 :: Video 'Uncensored 'Long)
-- can't show a child an uncensored screenshot
showChild $ PhotoF . screenShot $ (Video 128000 1 :: Video 'Uncensored 'Short)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.