纯脚本类型不统一

Purescript Types Do Not Unify

我在我的 Purescript 文件中定义了几个类型:

data Point = Point {
  x :: Int,
  y :: Int
}

data Rect = Rect {
  upperLeft :: Point,
  upperRight :: Point,
  lowerLeft :: Point,
  lowerRight :: Point
}

现在我想定义一个函数来检查两个矩形是否重叠:

rectsOverlap :: Rect -> Rect -> Boolean
rectsOverlap r s = (r.upperLeft.x > s.lowerRight.x && r.upperLeft.y > s.lowerRight.y)
  || (r.upperLeft.x < s.lowerRight.x && r.upperLeft.y < s.lowerRight.y)
  || (r.lowerLeft.x > s.upperRight.x && r.lowerLeft.y > s.upperRight.y)
  || (r.lowerLeft.x < s.upperRight.x && r.lowerLeft.y < s.upperRight.y)

但是,这会导致以下错误:

  Could not match type

    { upperLeft :: { x :: t0
                   | t1     
                   }        
    | t2                    
    }                       

  with type

    Rect


while checking that type Rect
  is at least as general as type { upperLeft :: { x :: t0
                                                | t1     
                                                }        
                                 | t2                    
                                 }                       
while checking that expression r
  has type { upperLeft :: { x :: t0
                          | t1     
                          }        
           | t2                    
           }                       
while checking type of property accessor r.upperLeft
while checking type of property accessor (r.upperLeft).x
in value declaration rectsOverlap

where t0 is an unknown type
      t1 is an unknown type
      t2 is an unknown type

根据我从这条消息中了解到的情况,编译器正在推断某种联合类型,其中 xt0 | t1,并且 upperLeft 也可以具有类型 t2 .当我删除我的类型注释时,编译器会为此函数推断出以下类型:

rectsOverlap :: forall t45 t49 t52 t59 t72 t76 t81 t85 t87 t94.
  Ord t52 => Ord t59 => Ord t59 => Ord t87 => Ord t94 => Ord t87 => Ord t94 => { upperLeft :: { x :: t52
                                                                                              , y :: t59
                                                                                              | t45
                                                                                              }
                                                                               , lowerLeft :: { x :: t87
                                                                                              , y :: t94
                                                                                              | t81
                                                                                              }
                                                                               | t72
                                                                               }
                                                                               -> { lowerRight :: { x :: t52
                                                                                                  , y :: t59
                                                                                                  | t49
                                                                                                  }
                                                                                  , upperRight :: { x :: t87
                                                                                                  , y :: t94
                                                                                                  | t85
                                                                                                  }
                                                                                  | t76
                                                                                  }
                                                                                  -> Boolean

很明显,它推断出比我的 Rect 类型更通用的类型。但我只想要一个狭义定义的函数。如果有人能阐明这里发生的事情,我将不胜感激。

PureScript 中的记录与Haskell中的记录不同

Haskell 没有大多数其他语言所理解的真实记录。 Haskell 的记录只是一种为 ADT 字段定义访问器函数的方法。例如下面两种类型几乎是等价的:

data A = A Int String
data B = B { x :: Int, y :: String }

除了类型 B 带有预定义的访问函数 x :: B -> Inty :: B -> String.

另一方面,PureScript 具有真实的、临时的、多态的、可扩展的记录,它们本身就是一个东西。 PureScript 中的记录定义如下:

{ x :: Int, y :: String }

没错,不需要data什么的。记录只是临时的,有点像 Haskell 中的元组。例如:

getX :: { x :: Int, y :: String } -> Int
getX r = r.x

当然,如果你愿意,你可以为你的记录声明一个别名,就像你可以为任何其他类型做的那样:

type XY = { x :: Int, y :: String }

getX :: XY -> Int
getX = r.x

您还可以将记录用作 ADT 的成员 - 同样,就像任何其他类型一样:

data XYWrapped = XYWrapped { x :: Int, y :: String }

但如果这样做,您需要通过模式匹配解包 ADT 才能获取记录。同样,就像任何其他类型一样:

getXWrapped :: XYWrapped -> Int
getXWrapped (XYWrapped r) = r.x

您也不仅限于包装一条记录(您猜对了:就像任何其他类型一样):

data XYPQ = XYPQ { x :: Int, y :: String } { p :: Char, q :: Int }

getXPlusQ :: XYPQ -> Int
getXPlusQ (XYPQ a b) = a.x + b.q

有了这些知识,您现在可以看到您的类型 PointRect 实际上不是记录,而是 wrap 单个记录(因此它们应该是 newtypes)。

这就是您遇到类型不匹配的原因。因为你写了 r.upperLeft,编译器推断 r 必须是一个包含名为 upperLeft 的字段的记录,但是你的类型签名说 r 是类型 Rect,这根本不是记录。所以编译器抱怨。

要解决此问题,您可以将类型重新定义为实际记录:

type Point = {
  x :: Int,
  y :: Int
}

type Rect = {
  upperLeft :: Point,
  upperRight :: Point,
  lowerLeft :: Point,
  lowerRight :: Point
}

或者您可以让函数解包 ADT:

rectsOverlap (Rect r) (Rect s) = 
    let (Point rUpperRight) = r.upperRight
        (Point rUpperLeft) = r.upperLeft
    ....

这有点乏味。所以我会坚持第一个选项。


另请注意,如上所述,PureScript 记录可以通过多态性实现多态和可扩展。有关更多信息,请参阅 .