在where子句

时间:2016-02-15 10:10:24

标签: idris

where子句中的tutorial部分提供了两个条件,可以在f子句中省略函数where的类型声明:

  
      
  • f出现在顶级定义的右侧
  •   
  • f的类型可以从第一个应用程序中完全确定
  •   

我的问题是:这两个条件之间的关系是什么? '和','或','互相排斥',是否有人暗示对方?

1 个答案:

答案 0 :(得分:3)

必须满足这两个条件,例如:

test1 : List Int -> List Int
test1 xs = map inc xs
  where
    inc a = 1 + a

让我们看一下反例,其中只有一个条件得到满足。

test2 : List Int -> List Int
test2 xs = map proxy xs
  where
    inc a = 1 + a
    proxy : Int -> Int
    proxy a = inc a

此处,inc未显示在右侧,但可以确定为Int -> Int

test3 : List Int -> List Int
test3 xs = map (cast . inc . cast) xs
  where
    inc a = 1 + a

接下来,inc出现在右侧,但无法确定类型(因为它可能是Nat -> NatInt32 -> Int32,...),因此cast的类型1}}不能。

test2test3只有在向inc提供类型声明时才会编译。

相关问题