首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Idris2:依赖和类型判别

Idris2:依赖和类型判别
EN

Stack Overflow用户
提问于 2021-05-22 03:49:36
回答 1查看 60关注 0票数 0

这可能是非常基本的依赖类型编程问题,但我找不到任何关于它的东西。问题是:“我有一堆消息,我可能需要处理,也可能不需要处理,这取决于一些配置。我如何在类型级别区分需要处理的消息和不需要处理的消息”。

例如,我有一些配置和一些消息。

代码语言:javascript
复制
record Configuration where
  constructor MkConfiguration
  handlesOpenClose: Bool
  handlesWillSave: Bool

config : Configuration
config = MkConfiguration { openClose: True, willSave: False }

data Message = FileOpened -- Handle when openClose = True
             | FileClosed -- Handle when openClose = True
             | WillSave   -- Handle when willSave = True

我现在希望能够写出这样的东西:

代码语言:javascript
复制
GetMessagesForConfig : Configuration -> Type
GetMessagesForConfig config = {- 
 config.openClose = true so FileOpened and FileClosed have to be handled,
 config.willSave  = false so WillSave does not have to be handled
-}

MessagesForConfig : Type
MessagesForConfig = GetMessagesForConfig config

handleMessage : MessagesForConfig -> Response
handleMessage FileOpened = {- do something -}
handleMessage FileClosed = {- do something -}
handleMessage WillSave impossible

这是可能的吗,或者像这样的事情?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-22 08:49:37

实现这一点的一种简单方法是不执行开放联合之类的操作:

代码语言:javascript
复制
data Message : (openClose : Bool) -> (willSave : Bool) -> Type where
    FileOpened : Message True a
    FileClosed : Message True a
    WillSave   : Message a True
代码语言:javascript
复制
handleMessage : Messages True False -> Response
handleMessage FileOpened = {- do something -}
handleMessage FileClosed = {- do something -}
handleMessage WillSave impossible
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67643156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档