榆树中的依赖类型

Tri*_*eno 4 types dependent-type elm idris

我想知道是否可以在Elm中进行某种依赖类型,如下所示,就像你在伊德里斯一样:

isQuestion : String -> Type
isQuestion (sentence) with (endsWith "?" sentence)
    | True = Question
    | False = Statement
Run Code Online (Sandbox Code Playgroud)

有没有一个库可以通过打字让我达到类似的效果?

far*_*mio 8

您可以使用联合类型执行类似的操作.

type Sentence 
    = Question String
    | Statement String

isQuestion : String -> Sentence
isQuestion sentence =
    case endsWith "?" sentence of
        True -> Question sentence
        False -> Statement sentence
Run Code Online (Sandbox Code Playgroud)

  • 嗯,这不是依赖类型.这只是很好的旧ADT(代数数据类型).另一个问题:作者真的需要在他的函数中使用依赖类型吗?可能不是,你的解决方案非常好. (7认同)