考虑数字文字1
。它有不同的类型,具体取决于它是用在等号的左边还是右边。中f True = 1
,1
有类型Num p => p
。但在f 1 = True
,1
有类型(Eq a, Num a) => a
。是否可以创建以相同方式工作的模式同义词?例如:
pattern LooksLikeFoo :: (Read a, Show a) => a
pattern LooksLikeFoo <- (show -> "Foo")
where LooksLikeFoo = read "Foo"
该模式仅在它同时具有Read
和Show
类时才有效,无论它位于等号的哪一侧。有没有办法让它Show a => a
在等号的左边有类型,但Read a => a
在右边?非工作伪代码:
pattern LooksLikeFoo :: Show a => a
pattern LooksLikeFoo <- (show -> "Foo")
where LooksLikeFoo :: Read a => a
LooksLikeFoo = read "Foo"