<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Apr 11, 2014 at 9:50 AM, ke dou <span dir="ltr"><<a href="mailto:kd6ck@virginia.edu" target="_blank">kd6ck@virginia.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">Hi,<div><br></div><div>I want to define a typeclass that can convert my own types like MyBool, MyInt, MyOption to according Haskell types -- Bool, Int, Maybe.</div><div><br></div><div>Currently I can convert the first two types, but for MyOption, I don't how to do, since it is a polymophic type.</div>

<div><br></div><div>Here is my toy program:</div><div>--------------------------------------------------------------</div><div>{-# LANGUAGE TypeFamilies #-}<div><br></div><div>module Coercible where</div><div><br></div><div>

import qualified Prelude</div><div><br></div><div>data MyBool = MyTrue | MyFalse</div><div>data MyInt = Zero | One | Two</div><div>data MyOption a =</div><div>   Some a</div><div> | None</div><div><br></div><div>class Coercible a where</div>

<div>  type Return a :: *</div><div>  toHaskell :: a -> (Return a)</div><div><br></div><div>instance Coercible MyBool where</div><div>  type Return MyBool = Prelude.Bool</div><div>  toHaskell MyTrue = Prelude.True</div>

<div>  toHaskell MyFalse = Prelude.False</div><div><br></div><div>instance Coercible MyInt where</div><div>  type Return MyInt = Prelude.Int</div><div>  toHaskell Zero = 0</div><div>  toHaskell One = 1</div><div>  toHaskell Two = 2</div>

<div><br></div><div><div>instance Coercible (MyOption a) where</div><div>  type Return (MyOption a) = Prelude.Maybe a</div><div>  toHaskell (Some a) = Prelude.Just a</div><div>  toHaskell None = Prelude.Nothing</div></div>

<div>--------------------------------------------------------------------------</div><div>The problem occurs when I am trying to use "toHaskell (Some MyBool)", the error message is "Not in scope: data constructor `MyBool'".</div>

<div><div style="font-family:arial,sans-serif;font-size:13px">Any hints will be appreciated !!</div></div></div></div></blockquote><div><br></div><div>`Some MyBool` isn't valid Haskell. MyBool is the name of a type, there's no binding or constructor with that name. `toHaskell (Some MyTrue)` evaluates to `Just MyTrue` as expected.</div>
</div></div></div>