YAWQ (Yet Another Wash Question)

Thanks for everyone that's helped me out with my Wash questions. I have one more. I have a textInputField that I only want to display on a form in certain cituations. I can't figure out how to make this work. For instance: ... do if cond then x <- textInputField ... else () ... submit ... Well, two problems there... first, the scope of the x doesn't reach to the submit. Second, there's a type issue. So I thought maybe I could figure out something like this: ... do x <- if cond then textInputField ... else () still stuck with the typing problem. So I thought maybe something like this... ... do let x = if cond then Just $ textInputField ... else Nothing Though I suspect this won't work either, since the textInputField won't actually be processed at the right place in the program. I then thought maybe I could kludge by this with a hidden field, but I can't find one of those, either. Ideas, anyone? Thanks, John

I am making a wild guess because I do not have all the information in front of me right now but would this work ? ... do x <- if cond
then textInputField ... else return ()
note the addition of 'return' to the else clause ?
Jeremy Shaw.
On Feb 24, 2005 08:42 AM, John Goerzen
Thanks for everyone that's helped me out with my Wash questions. I have one more.
I have a textInputField that I only want to display on a form in certain cituations. I can't figure out how to make this work. For instance:
... do if cond then x <- textInputField ... else () ... submit ...
Well, two problems there... first, the scope of the x doesn't reach to the submit. Second, there's a type issue. So I thought maybe I could figure out something like this:
... do x <- if cond then textInputField ... else ()
still stuck with the typing problem.
So I thought maybe something like this...
... do let x = if cond then Just $ textInputField ... else Nothing
Though I suspect this won't work either, since the textInputField won't actually be processed at the right place in the program.
I then thought maybe I could kludge by this with a hidden field, but I can't find one of those, either.
Ideas, anyone?
Thanks, John
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- This message contains information which may be confidential and privileged. Unless you are the addressee (or authorized to receive for the addressee), you may not use, copy or disclose to anyone the message or any information contained in the message. If you have received the message in error, please advise the sender and delete the message. Thank you.

Jeremy Shaw
I am making a wild guess because I do not have all the information in front of me right now but would this work ?
... do x <- if cond
then textInputField ... else return ()
... Let me make another guess, probably an even wilder one, ... You have to return a common type for both branches of the if. In the code snippet from above, you either get back a handle to a text input field or () --- so the types won't fit. To unify the types of both branches, I guess you have to introduce an new "wrapper" data type that *mabye* holds a handle of an input field *m* with return value *a* and validity flag *x*. data MaybeInputField m a x = MaybeInputField (Maybe (m a x)) It is important that all "input field type constructors" take two type arguments---one for the return type, and another one for the validity. Otherwise you won't be able to pass the input field to a submit button. (MaybeInputField TexxtInputField) for example fits into that scheme. With that in place, you may be able to write something like the following ... ... do mH <- if cond then do h <- textInputField ... return (MaybeInputField (Just h)) else return (MaybeInputField Nothing) -Matthias
On Feb 24, 2005 08:42 AM, John Goerzen
wrote: Thanks for everyone that's helped me out with my Wash questions. I have one more.
I have a textInputField that I only want to display on a form in certain cituations. I can't figure out how to make this work. For instance:
... do if cond then x <- textInputField ... else () ... submit ...
Well, two problems there... first, the scope of the x doesn't reach to the submit. Second, there's a type issue. So I thought maybe I could figure out something like this:
... do x <- if cond then textInputField ... else ()
still stuck with the typing problem.
So I thought maybe something like this...
... do let x = if cond then Just $ textInputField ... else Nothing
Though I suspect this won't work either, since the textInputField won't actually be processed at the right place in the program.
I then thought maybe I could kludge by this with a hidden field, but I can't find one of those, either.
Ideas, anyone?
Thanks, John
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
--
This message contains information which may be confidential and privileged. Unless you are the addressee (or authorized to receive for the addressee), you may not use, copy or disclose to anyone the message or any information contained in the message. If you have received the message in error, please advise the sender and delete the message. Thank you. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Matthias Neubauer | Universität Freiburg, Institut für Informatik | tel +49 761 203 8060 Georges-Köhler-Allee 79, 79110 Freiburg i. Br., Germany | fax +49 761 203 8052

On Thu, Feb 24, 2005 at 09:01:06AM -0800, Jeremy Shaw wrote:
I am making a wild guess because I do not have all the information in front of me right now but would this work ?
... do x <- if cond
then textInputField ... else return ()
note the addition of 'return' to the else clause ?
Won't help since () is still a different type than textInputField is working with. -- John

On 24 Feb 2005, at 16:42, John Goerzen wrote:
Thanks for everyone that's helped me out with my Wash questions. I have one more.
I have a textInputField that I only want to display on a form in certain cituations. I can't figure out how to make this work. For instance:
... do if cond then x <- textInputField ... else () ... submit ...
Well, two problems there... first, the scope of the x doesn't reach to the submit. Second, there's a type issue. So I thought maybe I could figure out something like this:
Well, how could this possibly work (that is, how could 'x' be visible at the point of submission)? If you go down the 'else' branch, what value should x have? The scoping rule is like that for a very good reason... Once you know what value x is supposed to have down the else branch then you will be able to do something like: x <- if cond then textInputField ... else return ... Jules

On Thu, Feb 24, 2005 at 05:22:40PM +0000, Jules Bean wrote:
On 24 Feb 2005, at 16:42, John Goerzen wrote:
... do if cond then x <- textInputField ... else () ... submit ...
Well, two problems there... first, the scope of the x doesn't reach to the submit. Second, there's a type issue. So I thought maybe I could figure out something like this:
Well, how could this possibly work (that is, how could 'x' be visible at the point of submission)? If you go down the 'else' branch, what value should x have?
I'm assuming I would have two different calls to submit, one for the case where cond is true (that includes x), and one for the case where cond is false (that omits it).
Once you know what value x is supposed to have down the else branch then you will be able to do something like:
x <- if cond then textInputField ... else return ...
but I have to return something with the same type as textInputField, and the only things with that type are other input fields. I don't want something else there. I want nothing there.

On 24 Feb 2005, at 17:33, John Goerzen wrote:
On Thu, Feb 24, 2005 at 05:22:40PM +0000, Jules Bean wrote:
On 24 Feb 2005, at 16:42, John Goerzen wrote:
... do if cond then x <- textInputField ... else () ... submit ...
Well, two problems there... first, the scope of the x doesn't reach to the submit. Second, there's a type issue. So I thought maybe I could figure out something like this:
Well, how could this possibly work (that is, how could 'x' be visible at the point of submission)? If you go down the 'else' branch, what value should x have?
I'm assuming I would have two different calls to submit, one for the case where cond is true (that includes x), and one for the case where cond is false (that omits it).
Well saying that makes me think that the submit itself should be in the branches of the if ... then ... else ...
Once you know what value x is supposed to have down the else branch then you will be able to do something like:
x <- if cond then textInputField ... else return ...
but I have to return something with the same type as textInputField, and the only things with that type are other input fields. I don't want something else there. I want nothing there.
Well, that was a simplified example. You could replace textInputField with do { y <- textInputField ; return SomeComplexExpressionUsingY }. It may be that you want to use a Maybe type (as you suggested in your original mail), or perhaps an Either type for the two alternatives. Jules

On Thu, Feb 24, 2005 at 05:53:29PM +0000, Jules Bean wrote:
Well, how could this possibly work (that is, how could 'x' be visible at the point of submission)? If you go down the 'else' branch, what value should x have?
I'm assuming I would have two different calls to submit, one for the case where cond is true (that includes x), and one for the case where cond is false (that omits it).
Well saying that makes me think that the submit itself should be in the branches of the if ... then ... else ...
Possible, but ugly. I have about 50 lines of code that has to go in-between, so I'd be duplicating it. And, if I tried to make it into just another function, I'd have the same problem, I believe (scoping)

John Goerzen
Possible, but ugly. I have about 50 lines of code that has to go in-between, so I'd be duplicating it. And, if I tried to make it into just another function, I'd have the same problem, I believe (scoping)
It's all much easier: as always, you just have to use template functions for all your pages. That's all you need ... :-) Here is a recipe how I typically structure my WASH applications: - First, I write down (html) code for all the web pages and abstract over all the varying parts (input fields, submit buttons, continuation pages, etc.). As result, I get (independent) template function for all the pages. Usually, each template function lives in a separate module/file. - In the end, I write a *controller* function that both generates all the different pages by filling the holes of the templates and also ties together all the consecutive pages. And that's all you need to solve your problem as well -- just use a template function twice! Below, you'll find a small web app that toggles between two varying input pages using the scheme I described above. -Matthias module Main where import CGI main = run controller -- controller controller :: CGI () controller = let input1 = tr $ td $ textInputField empty submit1 = \ h cont -> submit h cont empty page1 = stepOneTemplate input1 submit1 page2 out2 = \ t -> text t submit2 = \ cont -> submit0 cont empty page2 = stepTwoTemplate value out2 submit2 page3 input3 = empty submit3 = \ h cont -> submit F0 cont empty page3 = stepOneTemplate input3 submit3 page4 out4 = \ t -> text "No input, sorry!" submit4 = \ cont -> submit0 cont empty page4 = stepTwoTemplate (const undefined) out4 submit4 page1 in page1 -- page templates stepOneTemplate inputCode submitCode nextPage = do standardQuery "Input Page" $ table $ do do tr $ td $ text "Hello!" h <- inputCode tr $ td $ text "Press the button!" tr $ td $ submitCode h nextPage stepTwoTemplate validationCode outputCode submitCode nextPage h = do let i = validationCode h standardQuery "Result Page" $ table $ do tr $ td $ text "Your input was ..." tr $ td $ outputCode i tr $ td $ submitCode nextPage -- Matthias Neubauer | Universität Freiburg, Institut für Informatik | tel +49 761 203 8060 Georges-Köhler-Allee 79, 79110 Freiburg i. Br., Germany | fax +49 761 203 8052

On Fri, Feb 25, 2005 at 04:15:55PM +0100, Matthias Neubauer wrote:
standardQuery "Input Page" $ table $ do do tr $ td $ text "Hello!" h <- inputCode tr $ td $ text "Press the button!" tr $ td $ submitCode h nextPage
I like the idea, but... doesn't this still have a problem if inputCode creates multiple fields on the screen? That is, h would only hold the last one? -- John

John Goerzen
On Fri, Feb 25, 2005 at 04:15:55PM +0100, Matthias Neubauer wrote:
standardQuery "Input Page" $ table $ do do tr $ td $ text "Hello!" h <- inputCode tr $ td $ text "Press the button!" tr $ td $ submitCode h nextPage
I like the idea, but...
doesn't this still have a problem if inputCode creates multiple fields on the screen? That is, h would only hold the last one?
That's what F0, F1, F2, and friends are for. You use them to wrap multiple handles into a single object that you then pass to a submit button. E.g., my example, now with two buttons on the first step, looks as follows ... controller :: CGI () controller = let input1 = do h1 <- tr $ td $ textInputField empty h2 <- tr $ td $ textInputField empty return (F2 h1 h2) submit1 = \ h cont -> submit h cont empty page1 = stepOneTemplate input1 submit1 page2 vali2 = \ (F2 h1 h2) -> value h1 ++ " and " ++ value h2 out2 = \ t -> text t submit2 = \ cont -> submit0 cont empty page2 = stepTwoTemplate vali2 out2 submit2 page3 ... The templates and all the rest stays unchanged. -Matthias -- Matthias Neubauer | Universität Freiburg, Institut für Informatik | tel +49 761 203 8060 Georges-Köhler-Allee 79, 79110 Freiburg i. Br., Germany | fax +49 761 203 8052

The most natural seeming thing is one permutation you don't mention:
do x <- if cond then Just $ textInputField ...
else return Nothing
...
... submit ... -- perhaps: maybe (return ()) submit x
-- or submit (fromMaybe <something> x)
. Does that do what you want?
mike
John Goerzen
Thanks for everyone that's helped me out with my Wash questions. I have one more.
I have a textInputField that I only want to display on a form in certain cituations. I can't figure out how to make this work. For instance:
... do if cond then x <- textInputField ... else () ... submit ...
Well, two problems there... first, the scope of the x doesn't reach to the submit. Second, there's a type issue. So I thought maybe I could figure out something like this:
... do x <- if cond then textInputField ... else ()
still stuck with the typing problem.
So I thought maybe something like this...
... do let x = if cond then Just $ textInputField ... else Nothing
Though I suspect this won't work either, since the textInputField won't actually be processed at the right place in the program.
I then thought maybe I could kludge by this with a hidden field, but I can't find one of those, either.
Ideas, anyone?
Thanks, John
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi guys, been spending some time compiling my app but nothing seems to work. here's the picture: - created a Main.hsc which include some external libs. - included this pragma is Main.hsc {-# OPTIONS -fglasgow-exts #-} -compiled Main.hsc with: $ hsc2hs -I/usr/local/pgsql/include Main.hsc all went well with this - tried compiling this way: $ ghc -o test -L/usr/local/pgsql/lib Main.hs here's the error: Main.o(.text+0xbd): In function `r289_entry': : undefined reference to `PQgetvalue' Main.o(.text+0x28f): In function `r28b_entry': : undefined reference to `PQexec' Main.o(.text+0x42f): In function `r28d_entry': : undefined reference to `PQreset' Main.o(.text+0x573): In function `r28f_entry': : undefined reference to `PQfinish' Main.o(.text+0x6b7): In function `r28h_entry': : undefined reference to `PQstatus' Main.o(.text+0x82b): In function `r28j_entry': : undefined reference to `PQconnectdb' collect2: ld returned 1 exit status any ideas? kind regards, eyan -- http://www.eyan.org Object-oriented programming offers a sustainable way to write spaghetti code.

On Fri, 25 Feb 2005 22:15:02 +0800, Edwin Eyan Moragas
Hi guys,
been spending some time compiling my app but nothing seems to work.
here's the picture: - created a Main.hsc which include some external libs.
- included this pragma is Main.hsc {-# OPTIONS -fglasgow-exts #-}
-compiled Main.hsc with: $ hsc2hs -I/usr/local/pgsql/include Main.hsc all went well with this
- tried compiling this way: $ ghc -o test -L/usr/local/pgsql/lib Main.hs here's the error:
Main.o(.text+0xbd): In function `r289_entry': : undefined reference to `PQgetvalue' Main.o(.text+0x28f): In function `r28b_entry': : undefined reference to `PQexec' Main.o(.text+0x42f): In function `r28d_entry': : undefined reference to `PQreset' Main.o(.text+0x573): In function `r28f_entry': : undefined reference to `PQfinish' Main.o(.text+0x6b7): In function `r28h_entry': : undefined reference to `PQstatus' Main.o(.text+0x82b): In function `r28j_entry': : undefined reference to `PQconnectdb' collect2: ld returned 1 exit status
any ideas?
Including header files isn't enough, you have to link with the libraries too. And have you seen HSQL? It already provides a postgresql binding. -- Friendly, Lemmih

On Fri, 25 Feb 2005 15:22:28 +0100, Lemmih
On Fri, 25 Feb 2005 22:15:02 +0800, Edwin Eyan Moragas
wrote: Including header files isn't enough, you have to link with the libraries too. And have you seen HSQL? It already provides a postgresql binding.
what is enough then? i have seen HSQL. the point of the exercise is to better equip myself with FFI. thanks for the response. kind regards eyan
participants (7)
-
Edwin Eyan Moragas
-
Jeremy Shaw
-
John Goerzen
-
Jules Bean
-
Lemmih
-
Matthias Neubauer
-
Mike Gunter