使用数据类型对函数类型进行约束

Tru*_*arg 2 haskell types functional-programming data-kinds kotlin-sealed

我对数据类型感到困惑。假设我们有

{-# LANGUAGE DataKinds #-}
...
data Format
  = Photo
      { bytes :: Int
      }
  | Video
      { bytes       :: Int
      , durationSec :: Int
      }
Run Code Online (Sandbox Code Playgroud)

我想让 then 具有提升类型的功能:

createVideo :: Int -> Int -> 'Video
createVideo _ _ = error "Not implemented"
Run Code Online (Sandbox Code Playgroud)

编译器会询问我们参数,并用它们给出消息“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 
}
Run Code Online (Sandbox Code Playgroud)

在repl中https://pl.kotl.in/2G9E1Cbgs

K. *_*uhr 6

数据类型不提供在编译时检查数据结构的值的直接机制。换句话说,如果您有一个数据类型:

data Format
  = Photo
      { bytes :: Int
      }
  | Video
      { bytes       :: Int
      , durationSec :: Int
      }
Run Code Online (Sandbox Code Playgroud)

适合表示照片和视频,使用DataKinds扩展将其提升到类型级别不允许编写仅接受 10 秒视频的函数:

processTenSecondVideo :: Video bytes 10 -> IO ()    -- does not work
Run Code Online (Sandbox Code Playgroud)

甚至生成一个Format保证是视频的值:

createVideo1 :: Int -> Int -> Format                 -- works
createVideo2 :: Int -> Int -> Video bytes duration   -- does not work
Run Code Online (Sandbox Code Playgroud)

从技术角度来看,扩展所提升的新类型Photo和是不支持值的类型(具体来说,新类型)。因此,虽然 type 的值存在(因为is 属于 kind ,即具有值的类型的种类),但不存在 type或 的值。因此,您不能使用这些类型作为函数的返回类型(或参数类型)。VideoDataKindsFormatIntInt*Photo 0Video 128000 10

那么,数据类型有什么用呢?好吧,如果您有一个想要在编译时以某种方式约束的数据结构,那么您就不会升级数据结构。相反,您可以推广其他数据结构作为编写类型级程序来约束目标数据结构的工具。

在您的示例中,您想要对数据结构施加的约束实际上非常适度:您想要检查数据结构是否是Video. 您不需要数据类型来执行此操作。如果您只是稍微重新排列一下数据结构,常规 Haskell 类型就足够了。将格式分为两种类型:

data Photo = Photo {bytes :: Int}
data Video = Video {bytes :: Int, durationSec :: Int}
Run Code Online (Sandbox Code Playgroud)

足以在编译时区分视频和非视频。如果您想要在程序的某些部分使用可以是视频或照片的值,则可以引入 sum 类型:

data Format = PhotoF Photo | VideoF Video
Run Code Online (Sandbox Code Playgroud)

当约束变得更加复杂时,数据类型就变得有用。例如,假设您想要审查照片视频以确保全家人的安全:

censorPhoto :: Photo -> Photo
censorVideo :: Video -> Video
Run Code Online (Sandbox Code Playgroud)

并允许用户生成屏幕截图:

screenShot :: Video -> Photo
Run Code Online (Sandbox Code Playgroud)

您可能希望在编译时确保不会意外地对视频进行两次审查,或者向年轻观众展示未经审查的视频,或者让某人通过截取未经审查的视频的屏幕截图并将其冒充为经过审查的照片来绕过您的审查。

您可以通过引入更多类型来实现此目的:

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
Run Code Online (Sandbox Code Playgroud)

所以你可以写这样的东西:

censorFormat :: UncensoredFormat -> CensoredFormat
censoredScreenshot :: CensoredVideo -> CensoredPhoto
uncensoredScreenshot :: UncensoredVideo -> UncensoredPhoto
showAdult :: AnyFormat -> IO ()
showChild :: CensoredFormat -> IO ()
Run Code Online (Sandbox Code Playgroud)

不过,这相当混乱。假设您想对视频长度添加一些限制,以防止垃圾邮件发送者提交大量短视频,或者避免占用您的服务器来审查很长的视频。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
Run Code Online (Sandbox Code Playgroud)

现在我们可以写这样的东西:

-- 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
Run Code Online (Sandbox Code Playgroud)

并在编译时捕获问题:

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)
Run Code Online (Sandbox Code Playgroud)

请注意,提升的类型 ( Censored, Uncensored, Short, Medium, Long) 和种类 (Censoring和)与它们所描述的Length未提升的类型Photo, , 没有直接关系。正如我所说,这就是数据类型的典型使用方式。VideoFormat

完整代码示例:

{-# 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)
Run Code Online (Sandbox Code Playgroud)