如何在 Idris 中使用形式参数(值绑定)进行基本模式匹配?

How to do basic pattern matching with formal parameters (value binding) in Idris?

当我在 Idris 中尝试这个时,

contrived : (List a, Char, (Int, Double), String, Bool) -> Bool
contrived   ([]    ,  'b',  (1,   2.0),   "hi" , True) = False
contrived    (a,  b,  c,   d,  e) = True

我收到错误消息 Can't infer argument a to contrived, Can't infer argument a to List, Can't infer argument a to []。但是看看 Manning 的书,我没有发现我的方法有任何明显的句法问题。

您收到错误消息,因为 Idris 现在想知道 a(或者更确切地说 [] 是什么类型,当您在 REPL 中调用该函数时。您可以像这样指定此隐式信息:

contrived {a = Nat} ([],  'b',  (1, 2.0), "hi" , True)
> False

或者像这样:

contrived (the (List Nat) [],  'b',  (1, 2.0), "hi" , True)
> False

在不需要的 true 程序中:

EmptyList: List Nat
EmptyList = []

testCase: contrived (EmptyList, 'b', (1, 2.0), "hi", True) = False
testCase = Refl