求解依赖于约束的 class 的约束
Solving the constraint of a class that depends on a constraint
我正在使用 ConstraintKinds
和 MultiParamTypeClasses
来获得一个由另一个 class.
参数化的 class
假设Foo m
表示满足约束m
的类型是指向的,即(m a) => a
。从那我尝试用它来构造一些 Bar m
.
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
class Foo m where
foo :: (m a) => a
data Bar m where
Bar :: ((m a) => a) -> Bar m
bar :: Foo m => Bar m
bar = Bar foo
但是我得到以下错误。
Could not deduce (Foo m0) arising from a use of ‘foo’
from the context: Foo m
从上下文看来 m0
应该是 m
。没有办法让这个工作吗?
我认为现在的问题是我可以先定义一些 Foo Pointed a
,然后再定义一些 Foo Pointed2 a
,在那种情况下 foo
不知道要 return , 正确的?这个想法是我会调用 (bar :: Bar Pointed)
或 (bar :: Bar Pointed2)
并得到两个不同的结果。这样的事情可能吗?为什么不呢?
看起来您可以使用类型应用程序来帮助 GHC 确定您正在谈论的约束:
bar :: forall m . (Foo m) => Bar m
bar = Bar (foo @m)
我正在使用 ConstraintKinds
和 MultiParamTypeClasses
来获得一个由另一个 class.
假设Foo m
表示满足约束m
的类型是指向的,即(m a) => a
。从那我尝试用它来构造一些 Bar m
.
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
class Foo m where
foo :: (m a) => a
data Bar m where
Bar :: ((m a) => a) -> Bar m
bar :: Foo m => Bar m
bar = Bar foo
但是我得到以下错误。
Could not deduce (Foo m0) arising from a use of ‘foo’
from the context: Foo m
从上下文看来 m0
应该是 m
。没有办法让这个工作吗?
我认为现在的问题是我可以先定义一些 Foo Pointed a
,然后再定义一些 Foo Pointed2 a
,在那种情况下 foo
不知道要 return , 正确的?这个想法是我会调用 (bar :: Bar Pointed)
或 (bar :: Bar Pointed2)
并得到两个不同的结果。这样的事情可能吗?为什么不呢?
看起来您可以使用类型应用程序来帮助 GHC 确定您正在谈论的约束:
bar :: forall m . (Foo m) => Bar m
bar = Bar (foo @m)