小编noh*_*wnd的帖子

如何约束输入类型和输出类型是一样的?

我正在与Manning的Idris进行类型驱动开发.给出了一个示例,教导如何将函数限制为类型族中的给定类型.我们有Vehicle一个使用类型PowerSource要么是PedalPetrol,我们需要编写一个函数refill是typechecks仅适用于使用汽油作为其powersource车辆.

下面的代码有效,但不保证重新填充a Car会产生a Car而不是a Bus.如何更改refill函数的签名以仅允许Car在给定a Car和生成Bus时生成Bus

data PowerSource
  = Pedal
  | Petrol

data Vehicle : PowerSource -> Type 
  where
    Bicycle : Vehicle Pedal
    Car : (fuel : Nat) -> Vehicle Petrol
    Bus : (fuel : Nat) -> Vehicle Petrol

refuel : Vehicle Petrol -> Nat -> Vehicle Petrol
refuel (Car fuel) x        = Car (fuel + x)
refuel …
Run Code Online (Sandbox Code Playgroud)

type-safety dependent-type idris

5
推荐指数
1
解决办法
65
查看次数

标签 统计

dependent-type ×1

idris ×1

type-safety ×1