隐式建造单身人士

发布于 2025-02-04 09:00:57 字数 1418 浏览 2 评论 0原文

我正在尝试隐式构建单例,然后将其传递到单身人士上的巫婆计算中。我想帮助GHC推断这种类型,但我还没有找到一种方法。

singletons [d| data Mood = Angry
                         | Sad
                         | Neutral
                         | Happy
                         | Excited deriving (Read, Eq, Ord, Show) |]

singletons [d| data Intensity = None
                              | Low
                              | Medium
                              | High
                              | Extreme deriving (Show, Read, Eq, Ord, Enum) |]


singletons [d| newtype MoodReport = MR (Mood, Intensity) deriving Show |]


moodReportToSing :: forall (a :: Mood) (b :: Intensity). (SingI a, SingI b) => MoodReport -> SMoodReport ('MR '(a,b))
moodReportToSing (MR (a,b)) = sing :: Sing ('MR '(a, b))


addSingMoodReports :: forall a a' (b :: Intensity) (c :: Intensity). SMoodReport ('MR '(a, b)) -> SMoodReport ('MR '(a, c)) -> SMoodReport ('MR '(a, b <> c))
addSingMoodReports (SMR (STuple2 a b)) (SMR (STuple2 a' c)) = SMR $ STuple2 a (b %<> c)


unsafeAddMoodReports :: MoodReport -> MoodReport -> MoodReport
unsafeAddMoodReports (MR (a, c)) (MR (b, d)) = MR (a, d <> c)

moodreeporttoming仍然是明确的,需要打字才能使用。某种程度上的问题在于它会丢失类型数据(或者如果没有类型数据,我不知道如何使用该数据),并且其中只有友好的数据,我想将其传递给addsingmoodreports 功能。无论如何,是否可以隐含地将它们推断出GHC的类型或旁路类型搜索?

I'm trying to implicitly construct a singleton and then pass it into a function witch computes on singletons. I want to help GHC infer the type and I haven't figured out a way to do it.

singletons [d| data Mood = Angry
                         | Sad
                         | Neutral
                         | Happy
                         | Excited deriving (Read, Eq, Ord, Show) |]

singletons [d| data Intensity = None
                              | Low
                              | Medium
                              | High
                              | Extreme deriving (Show, Read, Eq, Ord, Enum) |]


singletons [d| newtype MoodReport = MR (Mood, Intensity) deriving Show |]


moodReportToSing :: forall (a :: Mood) (b :: Intensity). (SingI a, SingI b) => MoodReport -> SMoodReport ('MR '(a,b))
moodReportToSing (MR (a,b)) = sing :: Sing ('MR '(a, b))


addSingMoodReports :: forall a a' (b :: Intensity) (c :: Intensity). SMoodReport ('MR '(a, b)) -> SMoodReport ('MR '(a, c)) -> SMoodReport ('MR '(a, b <> c))
addSingMoodReports (SMR (STuple2 a b)) (SMR (STuple2 a' c)) = SMR $ STuple2 a (b %<> c)


unsafeAddMoodReports :: MoodReport -> MoodReport -> MoodReport
unsafeAddMoodReports (MR (a, c)) (MR (b, d)) = MR (a, d <> c)

the moodReportToSing is still explicit and needs TypeApplications in order to to be used. The problem with SomeSing is that it loses type data (or if it doesn't, I don't know how to use that data) and only has kind data in it and I want to pass these to the addSingMoodReports function. Is there anyway to make these implicitly, help GHC infer their type or bypass type-erasure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文