Thiago Negri | 16 Jan 16:03 2013
Picon

Type hierarchy

Hello.

How do I achieve type hierarchy in Haskell?

Suppose we have the following code:

foo :: A -> C
bar :: B -> C

I want something that allow me to say that B is a subtype of A, meaning:
1. I can use a value of type A where a value of type A is needed.
2. I can use a value of type B where a value of type B is needed.
3. I can use a value of type B where a value of type A is needed.
4. I can't use a value of type A where a value of type B is needed.

What are my options?

I've thought in type classes and data types with an attribute representing the extension. Any other way to do this?

Thanks,
Thiago.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Felipe Almeida Lessa | 16 Jan 16:57 2013
Picon

Re: Type hierarchy

For your particular constraints, it can be as easy as:

  class IsA a where
    toA :: a -> A

  foo' :: IsA a => a -> C
  foo' = foo . toA

However, you may asking the wrong question since it smells like you're
trying to embed OO into Haskell =).

Cheers,

On Wed, Jan 16, 2013 at 1:03 PM, Thiago Negri <evohunz <at> gmail.com> wrote:
> Hello.
>
> How do I achieve type hierarchy in Haskell?
>
> Suppose we have the following code:
>
> foo :: A -> C
> bar :: B -> C
>
> I want something that allow me to say that B is a subtype of A, meaning:
> 1. I can use a value of type A where a value of type A is needed.
> 2. I can use a value of type B where a value of type B is needed.
> 3. I can use a value of type B where a value of type A is needed.
> 4. I can't use a value of type A where a value of type B is needed.
>
> What are my options?
>
> I've thought in type classes and data types with an attribute representing
> the extension. Any other way to do this?
>
> Thanks,
> Thiago.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

--

-- 
Felipe.
Thiago Negri | 16 Jan 17:22 2013
Picon

Re: Type hierarchy

Thanks for the answer, but I'm trying to avoid type classes.

By the way, I'm not trying to embed OO into Haskell.

The binding to OpenGL declares GLenum as CUInt and GLboolean as CUChar, meaning I can't use a GLenum as a GLboolean or vice-versa.
The C spec allows the use of GLboolean values where GLenums are expected.

Maybe I'm taking the wrong approach...


2013/1/16 Felipe Almeida Lessa <felipe.lessa <at> gmail.com>
For your particular constraints, it can be as easy as:

  class IsA a where
    toA :: a -> A

  foo' :: IsA a => a -> C
  foo' = foo . toA

However, you may asking the wrong question since it smells like you're
trying to embed OO into Haskell =).

Cheers,

On Wed, Jan 16, 2013 at 1:03 PM, Thiago Negri <evohunz <at> gmail.com> wrote:
> Hello.
>
> How do I achieve type hierarchy in Haskell?
>
> Suppose we have the following code:
>
> foo :: A -> C
> bar :: B -> C
>
> I want something that allow me to say that B is a subtype of A, meaning:
> 1. I can use a value of type A where a value of type A is needed.
> 2. I can use a value of type B where a value of type B is needed.
> 3. I can use a value of type B where a value of type A is needed.
> 4. I can't use a value of type A where a value of type B is needed.
>
> What are my options?
>
> I've thought in type classes and data types with an attribute representing
> the extension. Any other way to do this?
>
> Thanks,
> Thiago.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



--
Felipe.

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Kim-Ee Yeoh | 16 Jan 17:35 2013

Re: Type hierarchy

On Wed, Jan 16, 2013 at 11:22 PM, Thiago Negri <evohunz <at> gmail.com> wrote:
The C spec allows the use of GLboolean values where GLenums are expected.

Some fixes off the top of my head (caveats apply):

* define a lift :: GLboolean -> GLenum
* use a typeclass GLenumlike
* if there aren't too many of them, roll a GLboolean specific function for every one taking GLenum

-- Kim-Ee
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane