3

考虑数字文字1。它有不同的类型,具体取决于它是用在等号的左边还是右边。中f True = 11有类型Num p => p。但在f 1 = True1有类型(Eq a, Num a) => a。是否可以创建以相同方式工作的模式同义词?例如:

pattern LooksLikeFoo :: (Read a, Show a) => a
pattern LooksLikeFoo <- (show -> "Foo")
  where LooksLikeFoo = read "Foo"

该模式仅在它同时具有ReadShow类时才有效,无论它位于等号的哪一侧。有没有办法让它Show a => a在等号的左边有类型,但Read a => a在右边?非工作伪代码:

pattern LooksLikeFoo :: Show a => a
pattern LooksLikeFoo <- (show -> "Foo")
  where LooksLikeFoo :: Read a => a
        LooksLikeFoo = read "Foo"
4

1 回答 1

3

目前这是不可能的,但 Edward Kmett 已经多次提出这种可能性,例如:https ://www.reddit.com/r/haskell/comments/eqhcgx/comment/fetsa54

于 2021-07-07T06:33:39.927 回答