由类型vs索引,包含idris中的类型

ill*_*out 26 types records dependent-type phantom-types idris

我目前正在使用Idris书籍进行类型驱动开发.

我在第6章中有两个与示例数据存储设计有关的问题.数据存储是一个命令行应用程序,允许用户设置存储在其中的数据类型,然后添加新数据.

以下是代码的相关部分(略有简化).你可以在Github上看到完整的代码:

module Main

import Data.Vect

infixr 4 .+.

-- This datatype is to define what sorts of data can be contained in the data store.
data Schema
  = SString
  | SInt
  | (.+.) Schema Schema

-- This is a type-level function that translates a Schema to an actual type.
SchemaType : Schema -> Type
SchemaType SString = String
SchemaType SInt = Int
SchemaType (x .+. y) = (SchemaType x, SchemaType y)

-- This is the data store.  It can potentially be storing any sort of schema.
record DataStore where
       constructor MkData
       schema : Schema
       size : Nat
       items : Vect size (SchemaType schema)

-- This adds new data to the datastore, making sure that the new data is
-- the same type that the DataStore holds.
addToStore
  : (dataStore : DataStore) -> SchemaType (schema dataStore) -> DataStore
addToStore (MkData schema' size' store') newitem =
  MkData schema' _ (addToData store')
  where
    addToData
      : Vect size' (SchemaType schema') -> Vect (size' + 1) (SchemaType schema')
    addToData xs = xs ++ [newitem]

-- These are commands the user can use on the command line.  They are able
-- to change the schema, and add new data.
data Command : Schema -> Type where
  SetSchema : (newSchema : Schema) -> Command schema
  Add : SchemaType schema -> Command schema

-- Given a Schema, this parses input from the user into a Command.
parse : (schema : Schema) -> String -> Maybe (Command schema)

-- This is the main workhorse of the application.  It parses user
-- input, turns it into a command, then evaluates the command and 
-- returns an updated DataStore.
processInput
  : (dataStore : DataStore) -> String -> Maybe (String, DataStore)
processInput dataStore@(MkData schema' size' items') input =
  case parse schema' input of
    Nothing => Just ("Invalid command\n", dataStore)
    Just (SetSchema newSchema) =>
      Just ("updated schema and reset datastore\n", MkData newSchema _ [])
    Just (Add item) =>
      let newStore = addToStore (MkData schema' size' items') item
      in Just ("ID " ++ show (size dataStore) ++ "\n", newStore)

-- This kicks off processInput with an empty datastore and a simple Schema
-- of SString.
main : IO ()
main = replWith (MkData SString _ []) "Command: " processInput
Run Code Online (Sandbox Code Playgroud)

这很有意义并且易于使用,但有一件事让我对设计产生了疑问. 为什么DataStore包含a Schema而不是被一个索引?

因为DataStore它没有被a索引Schema,所以我们可能写了一个不正确的addToStore函数,如下所示:

addToStore
  : (dataStore : DataStore) -> SchemaType (schema dataStore) -> DataStore
addToStore _ newitem =
  MkData SInt _ []
Run Code Online (Sandbox Code Playgroud)

这是我想象的更多类型安全代码的样子.你可以在Github上看到完整的代码:

module Main

import Data.Vect

infixr 4 .+.

data Schema
  = SString
 | SInt
 | (.+.) Schema Schema

SchemaType : Schema -> Type
SchemaType SString = String
SchemaType SInt = Int
SchemaType (x .+. y) = (SchemaType x, SchemaType y)

record DataStore (schema : Schema) where
       constructor MkData
       size : Nat
       items : Vect size (SchemaType schema)

addToStore
  : (dataStore : DataStore schema) ->
    SchemaType schema ->
    DataStore schema
addToStore {schema} (MkData size' store') newitem =
  MkData _ (addToData store')
  where
    addToData
      : Vect size' (SchemaType schema) -> Vect (size' + 1) (SchemaType schema)
    addToData xs = xs ++ [newitem]

data Command : Schema -> Type where
  SetSchema : (newSchema : Schema) -> Command schema
  Add : SchemaType schema -> Command schema

parse : (schema : Schema) -> String -> Maybe (Command schema)

processInput
  : (schema : Schema ** DataStore schema) ->
    String ->
    Maybe (String, (newschema ** DataStore newschema))
processInput (schema ** (MkData size' items')) input =
  case parse schema input of
    Nothing => Just ("Invalid command\n", (_ ** MkData size' items'))
    Just (SetSchema newSchema) =>
      Just ("updated schema and reset datastore\n", (newSchema ** MkData _ []))
    Just (Add item) =>
      let newStore = addToStore (MkData size' items') item
          msg = "ID " ++ show (size newStore) ++ "\n"
      in Just (msg, (schema ** newStore))

main : IO ()
main = replWith (SString ** MkData _ []) "Command: " processInput
Run Code Online (Sandbox Code Playgroud)

这是我的两个问题:

  1. 为什么本书没有使用更类型安全的DataStore类型(由索引编制的那个Schema)?是否有使用较少类型安全版本(仅包含a的版本Schema)获得的东西?

  2. 被另一种类型与另一种类型进行索引的类型的理论差异是什么?这种差异是否会根据您正在使用的语言而改变?

    例如,我想伊德里斯可能没有太大的区别,但Haskell有很大的不同.(对...?)

    我刚刚开始玩Idris(我并不是特别熟悉在Haskell中使用单例或GADT),所以我很难绕过它.如果你能指出我谈论这个的任何论文,我会非常感兴趣.

Boy*_*Jr. 1

根据评论,这是迂腐的。早期,使用依赖记录,因此不需要处理类型索引。后来,使用索引类型来限制(并更容易通过证明搜索找到)有效的实现。