Hi Dan,<br><br><div class="gmail_quote">On Mon, Jun 1, 2009 at 8:39 PM, Dan Cook <span dir="ltr"><<a href="mailto:danielkcook@gmail.com">danielkcook@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br>
(Relatively new to Haskell here ..)<br>
<br>
So I have the following:<br>
<br>
data MyVal = Atom String<br>
| Bool Bool<br>
<br>
And I want to do something like this<br>
<br>
check :: (Bool -> MyVal) -> MyVal -> True<br>
check f (f x) = True<br>
check _ _ = False</blockquote><div><br>You may be confusing yourself here on one point. The type 'Bool' is already defined by the prelude, but the data constructor is not. So you are able to create a data constructor for MyVal that is called "Bool" and contains a Bool. I think this distinction is leading to the next probem I see. Type signatures have to contain types and not values in Haskell. 'True' is a value so it can't be placed in the type signature. The type of True is Bool. So I think you meant to ask about:<br>
check :: (Bool -> MyVal) -> MyVal -> Bool<br><br>Now if you define this function the first parameter can be any function Bool -> MyVal, not just the data constructors of MyVal. Also, the type of Atom :: String -> MyVal so you can't even pass it to 'check'.<br>
<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
<br>
What that means is I want to pass a MyVal constructor and a MyVal, and return True if the second argument was constructed with the first. More generally, I'd like to be able to do<br>
<br>
genCheck :: (* -> MyVal) -> MyVal -> True<br>
genCheck f (f x) = True<br>
genCheck _ _ = False<br>
<br>
So that I can pass in _any_ MyVal constructor and let the function just check if the second argument was constructed with the first, without caring which constructor it is.</blockquote><div><br>This strikes me as a job for template haskell, but the use of TH is beyond what I can explain :)<br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
<br>
What is the preferred way to do this, since neither of those functions compile?</blockquote><div><br>My preferred way, is just to define isAtom and isBool both with type MyVal -> Bool and use them where I need them. There are a number of tools to generate such things like DrIFT, TH, and maybe some others?<br>
<br>I hope that helps,<br>Jason<br></div></div><br>