作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在我的 Expr
中捕获类型有效性定义,在定义Add
时出现问题,预计后面跟着 Decimal
或 Whole
参数,但我不知道如何对它们进行模式匹配。以下是我的试验:
一审:
data DataType = Text | Decimal | Whole
data Expr : DataType -> Type where
Add : (Expr Decimal) -> (Expr Decimal) -> Expr Decimal
Add : (Expr Whole) -> (Expr Whole) -> Expr Whole
data DataType = Text | Decimal | Whole
data Expr : DataType -> Type where
Add : (Expr ty) -> (Expr ty) -> Expr ty
data DataType = Text | Decimal | Whole
data Expr : DataType -> Type where
Add : (Expr ty@(Decimal | Whole)) -> (Expr ty) -> Expr ty
Add
两次。在第二次试验中,我不知道如何添加
ty
的约束。必须是
Decimal
之一和
Whole
.第 3 次试验使用了一些尚不支持的虚构语法。
最佳答案
您基本上需要对 ty
施加约束。 .一种通用的方法是
data Numeric : DataType -> Type where
decimal-numeric : Numeric Decimal
whole-numeric : Numeric Whole
data Expr : DataType -> Type where
add : Numeric ty -> Expr ty -> Expr ty -> Expr ty
Numeric ty
使用实例/默认参数来使其更好用。论据
add
,取决于您使用的语言。究竟是什么
Numeric
类型由您决定。这里我使用了一个简单的依赖类型,但您也可以考虑使用 Haskell 类型类实例样式的函数记录。
data NumericType : Type where
Whole, Decimal : NumericType
data DataType : Type where
Numeric : NumericType -> DataType
String : DataType
data Expr : DataType -> Type where
Add : Expr (Numeric nty) -> Expr (Numeric nty) -> Expr (Numeric nty)
关于coq - 如何在 Idris/Agda/Coq 中模式匹配多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47056382/
我是一名优秀的程序员,十分优秀!