Positive integer type

后端 未结 6 1727
野性不改
野性不改 2020-11-28 10:17

In many articles about Haskell they say it allows to make some checks during compile time instead of run time. So, I want to implement the simplest check possible - allow a

6条回答
  •  無奈伤痛
    2020-11-28 10:51

    I know that this was answered a long time ago and I already provided an answer of my own, but I wanted to draw attention to a new solution that became available in the interim: Liquid Haskell, which you can read an introduction to here.

    In this case, you can specify that a given value must be positive by writing:

    {-@ myValue :: {v: Int | v > 0} #-}
    myValue = 5
    

    Similarly, you can specify that a function f requires only positive arguments like this:

    {-@ f :: {v: Int | v > 0 } -> Int @-}
    

    Liquid Haskell will verify at compile-time that the given constraints are satisfied.

提交回复
热议问题