[Haskell-cafe] Functional Dependencies Help

3 views
Skip to first unread message

John Creighton

unread,
Apr 29, 2010, 9:47:24 AM4/29/10
to haskel...@haskell.org
I've been trying to apply some stuff I learned about functional
dependencies, but I run into one of two problems. I either end up with
inconsistent dependencies (OverlappingInstances doesn't seem to
apply) or I end up with infinite recursion. I want to be able to do
simple things like if a is a subset of b and b is a subset of c then a
is a subset of c. If a is a is a subset of b and b is a c then a is a
c.

Before I added the equality functions I had infinite recursion. Once I
put them them in then I have trouble with overlapping isntances.

{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}


data Noun = Noun deriving (Show) --10
data Verb = Verb deriving (Show) --
data Adjactive = Adjactive deriving (Show)

data Animal=Animal deriving (Show)
data Feline=Feline deriving (Show)
data Cat = Cat deriving (Show)

data Taby_Cat=Taby_Cat deriving (Show)
data T=T deriving (Show)
data F=F deriving (Show) --20


class Isa a b c | a b->c where isa::a->b->c

instance Isa Animal Noun T where isa a b = T --25

class IsSuperSet a b c | a b->c where
isSuperSet :: a->b->c

instance IsSuperSet Feline Cat T where --30
isSuperSet a b=T
instance IsSuperSet Animal Feline T where
isSuperSet a b=T
instance IsSuperSet a Animal F where
isSuperSet a b=F --35

class TypeNotEq d b c | d b->c where
typeNotEq :: a->b->c

instance (IsSuperSet d b c, --40
IsSuperSet a d c,
TypeNotEq a d T,
TypeNotEq b d T,
TypeEq c T T
)=>
IsSuperSet a b c where
isSuperSet a b=undefined::c

instance TypeNotEq a a c where
typeNotEq a b = undefined::c --50
instance TypeNotEq a b c where
typeNotEq a b = undefined::c
class TypeEq a b c | a b->c where
typeEq :: a->b->c
instance TypeEq a a c where
typeEq a b = undefined::c
instance TypeEq a b c where
typeEq a b = undefined::c

class ToBool a where
toBool :: a->Bool

instance ToBool T where
toBool a = True

instance ToBool F where
toBool a = False

myCat=Cat
bla=isSuperSet Animal Cat
_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

--
You received this message because you are subscribed to the Google Groups "Haskell-cafe" group.
To post to this group, send email to haskel...@googlegroups.com.
To unsubscribe from this group, send email to haskell-cafe...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/haskell-cafe?hl=en.

John Creighton

unread,
Apr 30, 2010, 8:18:10 PM4/30/10
to haskel...@haskell.org

On Apr 29, 7:47 am, John Creighton <johns2...@gmail.com> wrote:
> I've been trying to apply some stuff I learned about functional
> dependencies, but I run into one of two problems. I either end up with
> inconsistent dependencies (OverlappingInstances doesn't seem to
> apply) or I end up with infinite recursion. I want to be able to do
> simple things like if a is a subset of b and b is a subset of c then a
> is a subset of c. If a is a is a subset of b and b is a c then a is a
> c.
>
> Before I added the equality functions I had infinite recursion. Once I
> put them them in then I have trouble with overlapping instances.

I've been doing some reading and I think the following is an
improvement but I end up hanging the compiler so I can't tell what the
errors are. I'll see if their are any trace options that might be
helpfully for GHC.
{-# LANGUAGE EmptyDataDecls,
MultiParamTypeClasses,
ScopedTypeVariables,
FunctionalDependencies,
FlexibleInstances #-}

{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-} --10
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}


data Noun = Noun deriving (Show) --15
data Verb = Verb deriving (Show) --
data Adjactive = Adjactive deriving (Show)

data Animal=Animal deriving (Show)
data Feline=Feline deriving (Show) --20
data Cat = Cat deriving (Show)

data Taby_Cat=Taby_Cat deriving (Show)
data T=T deriving (Show)
data F=F deriving (Show) --25
--data Z=Z
--data S i = S i
--type One = S Z
--type Zero = Z
class Isa a b c | a b->c where isa::a->b->c --30

instance Isa Animal Noun T where isa a b = T --



class Parrent a b| a->b where -- Specific Cases
parrent :: a->b --

instance Parrent Cat Feline where --
parrent a = Feline --40
instance Parrent Feline Animal where --
parrent a= Animal --




class TypeOr a b c|a b->c where
typeOr :: a->b->c
instance TypeOr T T T where
typeOr a b = T --50
instance TypeOr T F T where
typeOr a b = T
instance TypeOr F T T where
typeOr a b = T
instance TypeOr F F T where
typeOr a b = T

class TypeEq' () x y b => TypeEq x y b | x y -> b
instance TypeEq' () x y b => TypeEq x y b
class TypeEq' q x y b | q x y -> b --60
class TypeEq'' q x y b | q x y -> b

instance TypeCast b T => TypeEq' () x x b
instance TypeEq'' q x y b => TypeEq' q x y b
instance TypeEq'' () x y F

-- see http://okmij.org/ftp/Haskell/typecast.html
class TypeCast a b | a -> b, b->a where typeCast :: a -> b
class TypeCast' t a b | t a -> b, t b -> a where typeCast' :: t->a-
>b
class TypeCast'' t a b | t a -> b, t b -> a where typeCast'' :: t->a-
>b --70

instance TypeCast' () a b => TypeCast a b where typeCast x =
typeCast' () x
instance TypeCast'' t a b => TypeCast' t a b where typeCast' =
typeCast''
instance TypeCast'' () a a where typeCast'' _ x = x

-- overlapping instances are used only for ShowPred
class EqPred a flag | a->flag where {}

-- Used only if the other
-- instances don't apply -- 80

class IsSuperSet a b c | a b->c where -- General Definition
isSuperSet :: a->b->c

--instance (TypeEq b Animal T,TypeEq c F T)=>IsSuperSet a b c where
--85
-- isSuperSet a b = F --
u=undefined
instance (
TypeEq a b iseq, --90
TypeEq Animal b isaninmal,
IsSuperSet' isaninmal iseq a b c3 --
) =>
IsSuperSet a b c3 where --
isSuperSet a b=(isSuperSet' (u::isaninmal) (u::iseq) (a::a)
(b::b))::c3

class IsSuperSet' isanimal iseq a b c| isanimal iseq a b->c where
isSuperSet' :: a->b->c

instance IsSuperSet' isanimal T a b T where
isSuperSet' a b = T

instance (Parrent b d, IsSuperSet a b c)=>IsSuperSet' F F a b c where
isSuperSet' a b = (isSuperSet a ((parrent (b::b)::d)))::c

instance IsSuperSet' T F a b F where
isSuperSet' a b = F

John Creighton

unread,
May 1, 2010, 12:16:47 PM5/1/10
to haskel...@haskell.org


On Apr 30, 6:18 pm, John Creighton <johns2...@gmail.com> wrote:
> On Apr 29, 7:47 am, John Creighton <johns2...@gmail.com> wrote:
>
> > I've been trying to apply some stuff I learned about functional
> > dependencies, but I run into one of two problems. I either end up with
> > inconsistent dependencies (OverlappingInstances  doesn't seem to
> > apply) or I end up with infinite recursion. I want to be able to do
> > simple things like if a is a subset of b and b is a subset of c then a
> > is a subset of c. If a is a is a subset of b and b is a c then a is a
> > c.
>
> > Before I added the equality functions I had infinite recursion. Once I
> > put them them in then I have trouble with overlapping instances.
>
> I've been doing some reading and I think the following is an
> improvement but I end up hanging the compiler so I can't tell what the
> errors are. I'll see if their are any trace options that might be
> helpfully for GHC.

So bellow I'll post the latest version of my code but first the errors
which seem very strange to me:

--------------------------------------------------------------------

could not deduce (IsSuperSet'
isanimal iseq isanimal iseq1 (a -> b -> c3) )
from the context (IsSuperSet a b c2,
Typeeq a b iseq1,
TypeEq Animal b isaninmal,
IsSuperSet' isanimal iseq1 a b c3)
arising from a use of 'isSuperSet'' at logicp2.hs:92:25-74
Possible fix:
add (IsSuperSet'
isanimal iseq isanimal iseq1 (a -> b -> c3)) to context
of the declaration
or add an instance delaration for
(IsSuperSet' isanimal iseq isanimal iseq1 (a -> b -> c3))
In the expression:
(isSuperSet' (u :: isanimal) (u :: iseq) (a :: a)
(b ::b)) :: c3
In the definition of 'isSuperset':
isSuperset a b
= (isSuperSet' (u :: isanimal) (u :: iseq) (a ::
a) (b :: b))
in the instance delaration for 'IsSuperSet a b c3'

------------------------------------------------

Now what is strange about these errors, is the type signature haskell
is asking me to supply instance declarations for. For instance

add (IsSuperSet'
isanimal iseq isanimal iseq1 (a -> b -> c3)) to context
of the declaration

Why are the first and the third argument the same. Moreover why do the
third and forth arguments look like types related to my Boolean data
types. I see nothing in my code that could result in Haskell needing
this type signature. Also I'm not entirely sure about the (a -> b ->
c3). Is this the normal way for Haskell to show the type signature of
functional dependencies. My code is bellow and was compiled on GHCi.

--------------------------------------------------------

{-# LANGUAGE EmptyDataDecls,
MultiParamTypeClasses,
ScopedTypeVariables,
FunctionalDependencies,
OverlappingInstances,
FlexibleInstances,
UndecidableInstances#-}

{-# LANGUAGE TypeOperators #-}
class IsSuperSet a b c | a b -> c where -- General Definition
isSuperSet :: a->b->c

--instance (TypeEq b Animal T,TypeEq c F T)=>IsSuperSet a b c where
--85
-- isSuperSet a b = F --
u=undefined
instance (
TypeEq a b iseq, --90
TypeEq Animal b isaninmal,
IsSuperSet' isaninmal iseq a b c3 --
) =>
IsSuperSet a b c3 where --
isSuperSet a b=(isSuperSet' (u::isaninmal) (u::iseq) (a::a)
(b::b))::c3

class IsSuperSet' isanimal iseq a b c| isanimal iseq a b -> c where
isSuperSet' :: a->b->c

instance IsSuperSet' isanimal T a b T where
isSuperSet' a b = T

instance (
Parrent b d,
IsSuperSet a d c
)=>
IsSuperSet' F F a b c where
isSuperSet' a b = (isSuperSet a ((parrent (b::b))::d))::c

instance IsSuperSet' T F a b F where
isSuperSet' a b = F


class ToBool a where
toBool :: a->Bool

instance ToBool T where
toBool a = True

instance ToBool F where
toBool a = False

myCat=Cat

John Creighton

unread,
May 1, 2010, 7:12:58 PM5/1/10
to haskel...@haskell.org
The errors in the older code were due to not supplying enough input
arguments to all my class instance declarations. My final code works
and is pasted bellow:

{-# LANGUAGE EmptyDataDecls,
MultiParamTypeClasses,
ScopedTypeVariables,
FunctionalDependencies,
OverlappingInstances,
FlexibleInstances,
UndecidableInstances #-}

{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}


data Noun = Noun deriving (Show) --10
data Verb = Verb deriving (Show) --
data Adjactive = Adjactive deriving (Show)

data Animal=Animal deriving (Show)
data Feline=Feline deriving (Show)
data Cat = Cat deriving (Show)

data Taby_Cat=Taby_Cat deriving (Show)
data T=T deriving (Show)
data F=F deriving (Show) --20


class Isa a b c | a b->c where isa::a->b->c

instance Isa Animal Noun T where isa a b = T --25

class IsSuperSet a b c | a b->c where -- General Definition
isSuperSet :: a->b->c
class IsSuperSet' a b c | a b->c where -- Specific Cases
isSuperSet :: a->b->c

instance IsSuperSet' Feline Cat T where --30
isSuperSet a b=T
instance IsSuperSet' Animal Feline T where
isSuperSet a b=T
instance IsSuperSet' a Animal F where
isSuperSet a b=F --35


instance (IsSuperSet' d b c, --40
IsSuperSet a d c,
)=>
IsSuperSet a b c where
isSuperSet a b=undefined::c


class ToBool a where
toBool :: a->Bool

instance ToBool T where
toBool a = True

instance ToBool F where
toBool a = False

myCat=Cat
bla=isSuperSet Animal Cat
bla2=isSuperSet Cat Animal
Reply all
Reply to author
Forward
0 new messages