0

How would it be possible in Haskell, to be able to solve (without using functions instead of types):

rewrite(And True False) = False
rewrite(And (And True False) True) = False ...

I tried the following

data MyLogic f a = And f a  deriving Show

rewrite(And a b)
 | a == False = False
 | b == False = False
 | otherwise = True

rewrite(And (And a b) c) = ...

But haskell compiler complains that a might not be a bool in the first rewrite.

3
  • 3
    I don't see the connection between your question and the title of your post. Please explain or change the title. Commented Jan 11, 2015 at 16:23
  • @HansLub: I try to create a function rewrite that supports both rewrite :: Logic (Logic t1 t2) t -> Bool AND rewrite :: Logic Bool Bool -> Bool. Commented Jan 11, 2015 at 16:34
  • that is much clearer, thanks! Commented Jan 11, 2015 at 17:06

4 Answers 4

4

Multiple function definitions with the same name is just another name for ad-hoc polymorphism, and that's what Haskell typeclasses are for.

{-# LANGUAGE FlexibleInstances, FlexibleContexts #-}

data MyLogic f a = And f a  deriving Show

class Rewritable a where
   rewrite :: a -> Bool

instance Rewritable (MyLogic Bool Bool) where
  rewrite(And a b)
     | a == False = False
     | b == False = False
     | otherwise = True


instance Rewritable (MyLogic a b) => Rewritable  (MyLogic (MyLogic a b)  Bool) where
  rewrite(And x c) = rewrite $ And (rewrite x) c
Sign up to request clarification or add additional context in comments.

7 Comments

The first instance should use &&, and I think this whole approach will fail the coherence check if you make it general enough. Unless maybe you're really really careful.
@dfeuer: Your proposal makes sense, but is not relevant to the problem "how do I define an ad-hoc polymorphic function rewrite"
My point is that if you try to make it accept And in the second argument, things will get very tricky, and maybe even impossible without the rightfully dreaded IncoherentInstances.
Yes, of course. I answered the question in the title, which makes sense, and considered the accompanying example (which makes a little less sense) as just that, an example. How to generalise this example is a different, much more difficult, question.
It actually didn't turn out to be so bad. See my answer.
|
4

Here's a generalization (completion, really) of Hans Lub's approach that should work with any expression of the sort you're considering.

{-# LANGUAGE FlexibleInstances, FlexibleContexts #-}
module MyLogic where

data MyLogic f a = And f a  deriving Show

class Rewritable a where
   rewrite :: a -> Bool

instance Rewritable Bool where
  rewrite = id

instance Rewritable (MyLogic Bool Bool) where
  rewrite(And a b) = a && b

instance Rewritable (MyLogic a b)
    => Rewritable  (MyLogic (MyLogic a b)  Bool) where
  rewrite(And x c) = rewrite $ And (rewrite x) c

instance Rewritable (MyLogic a b)
    => Rewritable  (MyLogic Bool (MyLogic a b)) where
  rewrite(And c x) = rewrite $ And c (rewrite x)

instance (Rewritable (MyLogic a b), Rewritable (MyLogic c d))
    => Rewritable (MyLogic (MyLogic a b) (MyLogic c d)) where
  rewrite(And x y) = rewrite $ And (rewrite x) (rewrite y)

Comments

0

How about this:

data MyLogic = Lit Bool
             | And MyLogic MyLogic deriving Show

rewrite :: Mylogic -> Bool
rewrite (Lit b) = b
rewrite (And a b) = rewrite a && rewrite b

2 Comments

This proposed solution doesn't seem to give you the ability to make a type: "And (And True False) True"?
No, you would need And (And (Lit True) (Lit False)) (Lit True), but I know of no way to do what you're describing. Haskell is staticly typed, you see. You can't just use two kinds of types in a data structure.
0

I'd build it like this

data MyLogic a = And (MyLogic a) a  deriving Show

rewrite :: (MyLogic Bool) -> Bool
rewrite (And f False) = False
rewrite (And f True) = rewrite f

1 Comment

This proposed solution doesn't seem to give you the ability to execute "rewrite (And True True)"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.