[Haskell-cafe] what is the fastest way to extract variables from a proposition?

Cetin Sert cetin.sert at gmail.com
Wed Feb 20 16:29:19 EST 2008


-- proposition
data Prp a = Var a
           | Not (Prp a)
           | Or  (Prp a) (Prp a)
           | And (Prp a) (Prp a)
           | Imp (Prp a) (Prp a)
           | Xor (Prp a) (Prp a)
           | Eqv (Prp a) (Prp a)
           | Cns Bool
           deriving (Show, Eq)

-- Here are to variable extraction methods

-- variable extraction reference imp.
-- Graham Hutton: Programming in Haskell, 107
vars_ :: Prp a → [a]
vars_ (Cns _)   = []
vars_ (Var x)   = [x]
vars_ (Not p)   = vars_ p
vars_ (Or  p q) = vars_ p ++ vars_ q
vars_ (And p q) = vars_ p ++ vars_ q
vars_ (Imp p q) = vars_ p ++ vars_ q
vars_ (Xor p q) = vars_ p ++ vars_ q
vars_ (Eqv p q) = vars_ p ++ vars_ q

-- variable extraction new * this is faster
vars :: Prp a → [a]
vars p = evs [p]
  where
    evs []           = []
    evs (Cns _  :ps) = []
    evs (Var x  :ps) = x:evs ps
    evs (Not p  :ps) = evs (p:ps)
    evs (Or  p q:ps) = evs (p:q:ps)
    evs (And p q:ps) = evs (p:q:ps)
    evs (Imp p q:ps) = evs (p:q:ps)
    evs (Xor p q:ps) = evs (p:q:ps)
    evs (Eqv p q:ps) = evs (p:q:ps)

-- for  : Not (Imp (Or (Var 'p') (Var 'q')) (Var p))
-- vars_: ['p','q','p']
-- vars : ['p','q','p']

-- order and the fact that 'p' appears twice being irrelevant:
-- is there an even faster way to do this?
--
-- Cetin Sert
-- www.corsis.de
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080220/acaf6d5c/attachment.htm


More information about the Haskell-Cafe mailing list