简体   繁体   English

使用FlexibleContexts和FlexibleInstances有哪些陷阱?

[英]What are the pitfalls of using FlexibleContexts and FlexibleInstances?

Since these flexible contexts and instances aren't available in the Haskell standard, I assume there are potential problems when using them. 由于Haskell标准中没有这些灵活的上下文和实例,因此我假设使用它们时存在潜在的问题。 What are they? 这些是什么? Can they lead to some ambiguity, undecidability, overlapping instances, etc.? 它们会导致一些模棱两可,不确定性,实例重叠等吗?

There is a similar question that asks only about FlexibleInstances , not FlexibleContexts , but the answer only says "that it's safe to use them". 有一个类似的问题 ,仅询问FlexibleInstances ,而不询问FlexibleContexts ,但答案仅是“使用它们是安全的”。

I once stumbled upon the following. 我曾经偶然发现以下内容。 Answering this question , I first tried this code: 回答这个问题 ,我首先尝试了以下代码:

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}

class (Eq a, Show a) => Genome a where
    crossover       :: (Fractional b) => b -> a -> a -> IO (a, a)
    mutate          :: (Fractional b) => b -> a -> IO a
    develop         :: (Phenotype b a)  => a -> b

class (Eq a, Show a) => Phenotype a b | a -> b where
    --  In case of Coevolution where each phenotype needs to be compared to 
    --  every other in the population
    fitness         :: [a] -> a -> Int 
    genome          :: (Genome b) => a -> b    -- here, the problem

breed parents cross mute = do
    children <- mapM (\ (dad, mom) -> crossover cross (genome dad) (genome mom)) 
                     parents
    let ch1 = map fst children ++ map snd children
    mutated <- mapM (mutate mute) ch1
    return $ map develop mutated

And got a compilation error and a suggestion by GHCi to add the FlexibleContexts option. 并得到了编译错误和GHCi的建议以添加FlexibleContexts选项。 When I did, it compiled OK. 当我这样做时,它编译确定。 But this was actually not a right thing to do, as the constraint declaration introduced new scope for type variables, and b in genome 's type signature became completely unrelated to the one in the type class; 但这实际上不是正确的事,因为约束声明为类型变量引入了新的范围,并且genome类型签名中的b变得与类型类中的类型完全无关; yet FlexibleContexts provided a cover for this. 然而, FlexibleContexts为此提供了掩护。

With the constraint specified properly at the type class level, 在类型类级别正确指定约束后,

class (Eq a, Show a, Genome b) => Phenotype a b | a -> b where
    --  In case of Coevolution where each phenotype needs to be compared to 
    --  every other in the population
    fitness         :: [a] -> a -> Int 
    genome          :: a -> b

it passed compilation without requiring the FlexibleContexts option. 它通过了编译,而不需要FlexibleContexts选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FlexibleContexts 扩展有什么用? 你能用一个简单的例子解释一下吗? - What is the FlexibleContexts extension good for? Could you please explain it using a simple example? 如果没有FlexibleContexts,使用Char不会编译 - Using Char does not compile without FlexibleContexts 使用 STArray 的两个几乎相同的函数:为什么一个需要 FlexibleContexts,而另一个不需要? - Two almost identical functions using STArray: why does one requires FlexibleContexts, and the other does not? 在这种情况下真的需要 FlexibleContexts 吗? - Is FlexibleContexts really needed in this context? Haskell FlexibleInstances是该语言的稳定扩展吗? - Are Haskell FlexibleInstances a stable extension to the language? 有关“ FlexibleInstances”的类型类问题 - Type class problem concerning “FlexibleInstances” 可以通过FlexibleInstances“重载”返回不同的类型,还是匹配类型类? - Can “overloading” via FlexibleInstances return different types, or match on typeclasses? 在没有FlexibleContexts的情况下为Data.Functor.Compose编写Show实例 - Writing a Show instance for Data.Functor.Compose without FlexibleContexts ghc-7.10:非类型变量参数(使用FlexibleContexts允许此操作) - ghc-7.10: Non type-variable argument (Use FlexibleContexts to permit this) Haskell函数,它将variadic函数作为参数(并返回除func之外的其他内容),而不使用FlexibleInstances,纯Haskell2010 - Haskell function that takes a variadic function as an argument (and returns something else than that func) without FlexibleInstances, pure Haskell2010
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM