如何在haskell中执行另一个函数组合的功能

时间:2015-08-17 14:33:31

标签: haskell

我想搜索并验证满足eeval1eeval2eeval3eeval4的定义并打印结果定义

如果我定义f,则可以是x+yx*yg可以是x+yx*y 并运行它们的组合,可以有4种组合 如果每个可能的定义满足四个组合,则它是真正的定义

当前MAMB函数只能作为运算符+*,并且具有冗余组合,例如某些组合不具有MAMB

MA(x, MB(x,y)) + MA(MB(x,y), y) >= MA(MB(x,y), MB(x,y))
MA(x, MB(x,y)) + MA(x, MB(x,y)) >= MA(MB(x,y), y)
MA(x, MB(x,y))*MA(x, MB(x,y)) >= MA(MB(x,y), y)
MA(x, MB(x,y)) + MA(x, MB(x,y)) >= MB(MA(x,y), y)

you can see MA as function f, MB as function g
f(x, g(x,y)) + f(g(x,y), y) >= f(g(x,y), g(x,y))
f(x, g(x,y)) + f(x, g(x,y)) >= f(g(x,y), y)

Haskell代码:

import Data.List
import Data.Array
--import Data.Graph 
import Control.Monad
import Math.Combinat
import Math.Core.Utils
import Math.Core.Field
import Math.Algebras.VectorSpace
import Math.Algebras.Structures
--import Math.CommutativeAlgebra.GroebnerBasis
--import Math.CommutativeAlgebra.Polynomial
--import Math.Algebras.Matrix
import System.IO
import qualified Data.Map as M

type Vertex = Int
type Table a = Array Vertex a
type Graph e = Table [(e, Vertex)]
type Bounds  = (Vertex, Vertex)
type Edge e = (Vertex, e, Vertex)

type Labeling a = Vertex -> a
data LabGraph n e = LabGraph (Graph e) (Labeling n)

data Operation
  = And
  | Or
  | MA
  | MB
  | Impl
    deriving Show

data Mree x
 = Meaf x
 | Mode (Mree x) Operation (Mree x)
   deriving Show

splits :: [a] -> [([a], [a])]
splits xs = zip (inits xs) (tails xs)

getAllTrees :: [a] -> [Mree a]
getAllTrees [] = []
getAllTrees [x] = return $ Meaf x
getAllTrees xs = do
  (left, right) <- splits xs
  guard $ not (null left) && not (null right)
  leftT <- getAllTrees left
  rightT <- getAllTrees right
  op <- [MA, MB, And, Or]
  return $ Mode leftT op rightT

eeval1 :: Mree Double -> Double
eeval1 (Meaf x) = x
eeval1 (Mode l And r) = eeval1 l * eeval1 r
eeval1 (Mode l Or r) = eeval1 l + eeval1 r
eeval1 (Mode l MA r) = eeval1 l + eeval1 r
eeval1 (Mode l MB r) = eeval1 l + eeval1 r

eeval2 :: Mree Double -> Double
eeval2 (Meaf x) = x
eeval2 (Mode l And r) = eeval2 l * eeval2 r
eeval2 (Mode l Or r) = eeval2 l + eeval2 r
eeval2 (Mode l MA r) = eeval2 l + eeval2 r
eeval2 (Mode l MB r) = eeval2 l * eeval2 r

eeval3 :: Mree Double -> Double
eeval3 (Meaf x) = x
eeval3 (Mode l And r) = eeval3 l * eeval3 r
eeval3 (Mode l Or r) = eeval3 l + eeval3 r
eeval3 (Mode l MA r) = eeval3 l * eeval3 r
eeval3 (Mode l MB r) = eeval3 l + eeval3 r

eeval4 :: Mree Double -> Double
eeval4 (Meaf x) = x
eeval4 (Mode l And r) = eeval4 l * eeval4 r
eeval4 (Mode l Or r) = eeval4 l + eeval4 r
eeval4 (Mode l MA r) = eeval4 l * eeval4 r
eeval4 (Mode l MB r) = eeval4 l * eeval4 r

let allparams = replicateM 5 [1.0, 2.0, 3.0, 4.0, 5.0]
let alltrees = [getAllTrees c | x <- allparams, c <- [x]]
eeval(alltrees!!0!!0)

1 个答案:

答案 0 :(得分:0)

这是你想要做的吗?

efuncs = [ ("eeval1", eeval1), ("eeval2", eeval2), ("eeval3", eeval3), ("eeval4", eeval4) ]

example = do
  let allparams = replicateM 5 [1.0, 2.0, 3.0, 4.0, 5.0]
  let alltrees = [getAllTrees c | x <- allparams, c <- [x]]
      t = alltrees !! 0 !! 0
  putStrLn $ "tree: " ++ show t
  forM_ efuncs $ \(name, eval) -> do
    putStrLn $ "- " ++ name ++ ": " ++ show (eval t)

正在运行example打印出来:

tree: Mode (Meaf 1.0) MA (Mode (Meaf 1.0) MA (Mode (Meaf 1.0) MA (Mode (Meaf 1.0) MA (Meaf 1.0))))
- eeval1: 5.0
- eeval2: 5.0
- eeval3: 1.0
- eeval4: 1.0