gpt4 book ai didi

haskell - 如何将此实例方法定义移动到类默认值?

转载 作者:行者123 更新时间:2023-12-04 22:43:10 26 4
gpt4 key购买 nike

考虑这个类和一个示例实例。

目的是提供一个类型级别开关,允许转换基本类型 — Int ,在这种情况下, - 到一个经过验证的谓词子类型,以供将来使用。当然,这个类有点做作,但我从实际代码中提取了它,我会用更有用的方法填充它,除非我被卡住了。

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE TypeApplications #-}

module Collection where

import Data.Tagged

-- $setup
--
-- λ :set -XTypeApplications

class Collected phantom
where
type Element phantom = r | r -> phantom
type Element phantom = Tagged phantom Int

type Collection phantom = r | r -> phantom
type Collection phantom = Tagged phantom [Int]

collection :: Collection phantom

inCollection :: Int -> Maybe (Element phantom)

data Primes

instance Collected Primes
where
type Element Primes = Tagged Primes Int
type Collection Primes = Tagged Primes [Int]

collection = Tagged [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

-- inCollection :: Int -> Maybe (Element Primes)
inCollection element
| element `elem` unTagged (collection @Primes) = Just $ Tagged element
| otherwise = Nothing

-- ^
-- λ inCollection @Primes 7
-- Just (Tagged 7)
-- λ inCollection @Primes 8
-- Nothing

(这是一个可运行的代码,其中包含通过编写的 repl 测试。)

我将处理同一基本类型上的几个子类型,它们仅在 collection 的定义上有所不同。 ,而证明的方法是一致查找。因此,没有理由没有该方法的默认代码。但是,我无法设计出这样的代码。我的第一个草稿无法进行类型检查,而第二个经过调整的草稿运行,但似乎没有终止。

这是初稿:
...
{-# LANGUAGE DefaultSignatures #-}
...

class Collected phantom
...
inCollection :: Int -> Maybe (Element phantom)
default inCollection :: ( Element phantom ~ Tagged phantom Int
, Collection phantom ~ Tagged phantom [Int] )
=> Int -> Maybe (Element phantom)
inCollection element
| element `elem` unTagged collection = Just $ Tagged element
| otherwise = Nothing
...

这是第二个:
...
{-# LANGUAGE ScopedTypeVariables #-}
...

class Collected phantom
...
inCollection :: ...
default inCollection :: ...
inCollection ...
where
collection = (collection :: Collection phantom)

(仅显示了添加的部分。实例中的相应方法定义已删除。在第二稿中,除了编译指示之外,唯一添加的是尝试键入 collection 的最后一行。)

问题是什么?可以做什么?

最佳答案

看来以下将起作用。这个想法是实例化 collection在默认的正确类型(使用 collection @phantom )这也需要 ScopedTypeVariables .

更新:我现在看到这是您第二次尝试时尝试做的事情。问题是您的 where子句定义了一个无限循环,如 collection在 RHS 上是相同的 collection被绑定(bind)在 LHS 上(酌情插入面手掌)。

{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE TypeApplications #-}

module Collection where

import Data.Tagged

class Collected phantom
where
type Element phantom = r | r -> phantom
type Element phantom = Tagged phantom Int

type Collection phantom = r | r -> phantom
type Collection phantom = Tagged phantom [Int]

collection :: Collection phantom

inCollection :: Int -> Maybe (Element phantom)
default inCollection :: ( Collection phantom ~ Tagged phantom [Int]
, Element phantom ~ Tagged phantom Int)
=> Int -> Maybe (Element phantom)
inCollection element
| element `elem` unTagged (collection @phantom) = Just $ Tagged element
| otherwise = Nothing

data Primes

instance Collected Primes
where
type Element Primes = Tagged Primes Int
type Collection Primes = Tagged Primes [Int]

collection = Tagged [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

关于haskell - 如何将此实例方法定义移动到类默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51502879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com