首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Haskell函数,它将变量函数作为参数(并返回函数以外的内容),而不使用FlexibleInstances,纯Haskell2010

Haskell函数,它将变量函数作为参数(并返回函数以外的内容),而不使用FlexibleInstances,纯Haskell2010
EN

Stack Overflow用户
提问于 2011-12-04 12:56:25
回答 2查看 359关注 0票数 11

是否可以在没有FlexibleInstances的情况下表示下面的Haskell程序,即用纯Haskell2010表示?

代码语言:javascript
复制
{-# LANGUAGE FlexibleInstances #-}

class    Funk a       where  truth :: a  -> [Bool]
instance Funk [Bool]  where  truth =  \x ->  x
instance Funk Bool    where  truth =  \x -> [x]

instance Funk b => Funk (Bool -> b) where
    truth f = concat [truth (f True), truth (f False)]

这是受How to write a Haskell function that takes a variadic function as an argument上的答案启发的。

我怀疑问题在于,truth返回的不是作为参数的函数(它返回的是Bool,而不是[Bool])。

这个片段的目的是给出一个布尔函数的所有可能配置的所有计算的列表,即

代码语言:javascript
复制
Main> truth (\x y -> x && y)
[True,False,False,False]

Main> truth (\x y -> x || y)
[True,True,True,False]

最后,打印一个真值表,如下所示(见本文末尾的锅炉板,以查看生成此表的代码):

代码语言:javascript
复制
Main> main
T T T | T
T T F | T
T F T | T
T F F | F
F T T | T
F T F | F
F F T | T
F F F | T

下面是一些用于测试和可视化的锅炉板代码,这个功能的目的是:

代码语言:javascript
复制
class TruthTable a where
    truthTable :: Funk f => f -> a

instance TruthTable [String] where
    truthTable f = zipWith (++) (hCells (div (length t) 2)) (map showBool $ truth f)
        where
            showBool True = "| T"
            showBool False = "| F"
            hCells 1 = ["T ", "F "]
            hCells n = ["T " ++ x | x <- hCells (div n 2)] ++ ["F " ++ x | x <- hCells (div n 2)]

instance TruthTable [Char] where
    truthTable f = foldl1 join (truthTable f)
        where join a b = a ++ "\n" ++ b

instance TruthTable (IO a) where
    truthTable f = putStrLn (truthTable f) >> return undefined

main :: IO ()
main = truthTable (\x y z -> x `xor` y ==> z)

xor :: Bool -> Bool -> Bool
xor a b = not $ a == b

(==>) :: Bool -> Bool -> Bool
a ==> b = not $ a && not b
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-04 14:30:13

没问题:

代码语言:javascript
复制
class    Funk a                  where  truth :: a  -> [Bool]
instance (IsBool a) => Funk [a]  where  truth =  map toBool
instance Funk Bool               where  truth =  \x -> [x]

instance (IsBool a, Funk b) => Funk (a -> b) where
    truth f = concat [truth (f $ fromBool True), truth (f $ fromBool False)]

class IsBool a where
    toBool :: a -> Bool
    fromBool :: Bool -> a

instance IsBool Bool where
    toBool = id
    fromBool = id

如果你愿意的话,你甚至可以做“荣誉布尔人”,比如0和1的Integer等等。

票数 12
EN

Stack Overflow用户

发布于 2011-12-04 13:07:50

Haskell98的方法是为([] Bool)和((->) Bool b)使用新类型:

代码语言:javascript
复制
newtype LB = LB [Bool]
newtype FAB b = FAB (Bool -> b)

class    Funk a       where  truth :: a  -> [Bool]
instance Funk LB      where  truth =  \(LB x) -> x
instance Funk Bool    where  truth =  \x -> [x]

instance Funk b => Funk (FAB b) where
    truth (FAB f) = concat [truth (f True), truth (f False)]

然后,这部分编译不需要任何语言扩展。但它消除了让“真相”变得简单的用例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8375433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档