handling NULL value in database query with Maybe (or other ...)

Hi, i have this query in SQL used by my Haskell code: let name = "A 20" -- return the list of N°BD from WDS for a given name let qry_head_WDS = "select DNUM from WDS where DISC = ?" :: Query putStrLn "before query WDS" bd_rows_WDS <- query conn qry_head_WDS (Only (name::String)) putStrLn "after query WDS" forM_ bd_rows_WDS $ \(Only a) -> putStrLn $ Text.unpack a works well if there is no NULL values in a database, but if NULL value for field N°BD i got this exception at runtime: before query WDS *** Exception: UnexpectedNull {errSQLType = "String", errHaskellType = "Text", errFieldName = "DNUM", errMessage = "unexpected null in table WDS of database sidonie"} *Main> i need some code ,perheaps uing Maybe to handle the NULL value in the field N°BD i think Haskell have special feature to program this , i read the article: https://lukeplant.me.uk/blog/posts/null-pointers-vs-none-vs-maybe/ but for now i have no concrete solution.... Regards, Damien -- Damien.Mattei@unice.fr, Damien.Mattei@oca.eu, UNS / OCA / CNRS

If you use Database.Sqlite.Simple library you need to declare returning type as "Maybe XYZ" mandatory., so type will be Only (Maybe String). When I forget about Maybe, I get then same error as you. 18.12.2018 12:16, Damien Mattei wrote:
Hi,
i have this query in SQL used by my Haskell code:
let name = "A 20"
-- return the list of N°BD from WDS for a given name let qry_head_WDS = "select DNUM from WDS where DISC = ?" :: Query putStrLn "before query WDS" bd_rows_WDS <- query conn qry_head_WDS (Only (name::String)) putStrLn "after query WDS"
forM_ bd_rows_WDS $ \(Only a) -> putStrLn $ Text.unpack a
works well if there is no NULL values in a database, but if NULL value for field N°BD i got this exception at runtime:
before query WDS *** Exception: UnexpectedNull {errSQLType = "String", errHaskellType = "Text", errFieldName = "DNUM", errMessage = "unexpected null in table WDS of database sidonie"} *Main>
i need some code ,perheaps uing Maybe to handle the NULL value in the field N°BD
i think Haskell have special feature to program this , i read the article: https://lukeplant.me.uk/blog/posts/null-pointers-vs-none-vs-maybe/
but for now i have no concrete solution....
Regards, Damien

unfortunately no i'm using Database.MySQL.Simple and the doc mention also: Handling null values The type of a result tuple will look something like this: (Text, Int, Int) Although SQL can accommodate NULL as a value for any of these types, Haskell cannot. If your result contains columns that may be NULL, be sure that you use Maybe in those positions of of your tuple. (Text, Maybe Int, Int) If query encounters a NULL in a row where the corresponding Haskell type is not Maybe, it will throw a ResultError exception. but when i put Maybe in my code like this: forM_ bd_rows_WDS $ \(Maybe a) -> putStrLn $ Text.unpack a i have this error at compilation: *Main> :load UpdateSidonie [1 of 1] Compiling Main ( UpdateSidonie.hs, interpreted ) UpdateSidonie.hs:255:27: error: Not in scope: data constructor ‘Maybe’ Perhaps you meant variable ‘maybe’ (imported from Data.Maybe) | 255 | forM_ bd_rows_WDS $ \(Maybe a) -> | ^^^^^ Failed, no modules loaded. Le 18/12/2018 11:59, Paul a écrit :
If you use Database.Sqlite.Simple library you need to declare returning type as "Maybe XYZ" mandatory., so type will be
Only (Maybe String). When I forget about Maybe, I get then same error as you.
18.12.2018 12:16, Damien Mattei wrote:
Hi,
i have this query in SQL used by my Haskell code:
let name = "A 20"
-- return the list of N°BD from WDS for a given name let qry_head_WDS = "select DNUM from WDS where DISC = ?" :: Query putStrLn "before query WDS" bd_rows_WDS <- query conn qry_head_WDS (Only (name::String)) putStrLn "after query WDS"
forM_ bd_rows_WDS $ \(Only a) -> putStrLn $ Text.unpack a
works well if there is no NULL values in a database, but if NULL value for field N°BD i got this exception at runtime:
before query WDS *** Exception: UnexpectedNull {errSQLType = "String", errHaskellType = "Text", errFieldName = "DNUM", errMessage = "unexpected null in table WDS of database sidonie"} *Main>
i need some code ,perheaps uing Maybe to handle the NULL value in the field N°BD
i think Haskell have special feature to program this , i read the article: https://lukeplant.me.uk/blog/posts/null-pointers-vs-none-vs-maybe/
but for now i have no concrete solution....
Regards, Damien
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- Damien.Mattei@unice.fr, Damien.Mattei@oca.eu, UNS / OCA / CNRS

the URL of the doc i quoted is : http://hackage.haskell.org/package/mysql-simple-0.4.5/docs/Database-MySQL-Si... unfortunately there is no developped example to help me Le 18/12/2018 12:11, Damien Mattei a écrit :
unfortunately no i'm using Database.MySQL.Simple and the doc mention also:
Handling null values
The type of a result tuple will look something like this:
(Text, Int, Int)
Although SQL can accommodate NULL as a value for any of these types, Haskell cannot. If your result contains columns that may be NULL, be sure that you use Maybe in those positions of of your tuple.
(Text, Maybe Int, Int)
If query encounters a NULL in a row where the corresponding Haskell type is not Maybe, it will throw a ResultError exception.
but when i put Maybe in my code like this:
forM_ bd_rows_WDS $ \(Maybe a) -> putStrLn $ Text.unpack a
i have this error at compilation:
*Main> :load UpdateSidonie [1 of 1] Compiling Main ( UpdateSidonie.hs, interpreted )
UpdateSidonie.hs:255:27: error: Not in scope: data constructor ‘Maybe’ Perhaps you meant variable ‘maybe’ (imported from Data.Maybe) | 255 | forM_ bd_rows_WDS $ \(Maybe a) -> | ^^^^^ Failed, no modules loaded.
Le 18/12/2018 11:59, Paul a écrit :
If you use Database.Sqlite.Simple library you need to declare returning type as "Maybe XYZ" mandatory., so type will be
Only (Maybe String). When I forget about Maybe, I get then same error as you.
18.12.2018 12:16, Damien Mattei wrote:
Hi,
i have this query in SQL used by my Haskell code:
let name = "A 20"
-- return the list of N°BD from WDS for a given name let qry_head_WDS = "select DNUM from WDS where DISC = ?" :: Query putStrLn "before query WDS" bd_rows_WDS <- query conn qry_head_WDS (Only (name::String)) putStrLn "after query WDS"
forM_ bd_rows_WDS $ \(Only a) -> putStrLn $ Text.unpack a
works well if there is no NULL values in a database, but if NULL value for field N°BD i got this exception at runtime:
before query WDS *** Exception: UnexpectedNull {errSQLType = "String", errHaskellType = "Text", errFieldName = "DNUM", errMessage = "unexpected null in table WDS of database sidonie"} *Main>
i need some code ,perheaps uing Maybe to handle the NULL value in the field N°BD
i think Haskell have special feature to program this , i read the article: https://lukeplant.me.uk/blog/posts/null-pointers-vs-none-vs-maybe/
but for now i have no concrete solution....
Regards, Damien
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- Damien.Mattei@unice.fr, Damien.Mattei@oca.eu, UNS / OCA / CNRS

Hi Damien,
the problem is that you are trying to use a type (Maybe), but you should use a data constructor (in this case Just or Nothing) in the pattern match (see the error message).
Something like:
forM_ bd_rows_WDS $ \x ->
case x of
Nothing -> putStrLn "NULL"
Just y -> putStrLn $ Text.unpack y
Regards, Tom
Sent with ProtonMail Secure Email.
‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Tuesday, December 18, 2018 12:11 PM, Damien Mattei
unfortunately no i'm using Database.MySQL.Simple and the doc mention also:
Handling null values
The type of a result tuple will look something like this:
(Text, Int, Int)
Although SQL can accommodate NULL as a value for any of these types, Haskell cannot. If your result contains columns that may be NULL, be sure that you use Maybe in those positions of of your tuple.
(Text, Maybe Int, Int)
If query encounters a NULL in a row where the corresponding Haskell type is not Maybe, it will throw a ResultError exception.
but when i put Maybe in my code like this:
forM_ bd_rows_WDS $ \(Maybe a) ->
putStrLn $ Text.unpack a
i have this error at compilation:
*Main> :load UpdateSidonie [1 of 1] Compiling Main ( UpdateSidonie.hs, interpreted )
UpdateSidonie.hs:255:27: error: Not in scope: data constructor ‘Maybe’ Perhaps you meant variable ‘maybe’ (imported from Data.Maybe) | 255 | forM_ bd_rows_WDS $ \(Maybe a) ->
| ^^^^^
Failed, no modules loaded.
Le 18/12/2018 11:59, Paul a écrit :
If you use Database.Sqlite.Simple library you need to declare returning type as "Maybe XYZ" mandatory., so type will be Only (Maybe String). When I forget about Maybe, I get then same error as you. 18.12.2018 12:16, Damien Mattei wrote:
Hi, i have this query in SQL used by my Haskell code:
let name = "A 20"
-- return the list of N°BD from WDS for a given name let qry_head_WDS = "select DNUM from WDS where DISC = ?" :: Query putStrLn "before query WDS" bd_rows_WDS <- query conn qry_head_WDS (Only (name::String)) putStrLn "after query WDS"
forM_ bd_rows_WDS $ \\(Only a) -> putStrLn $ Text.unpack a
works well if there is no NULL values in a database, but if NULL value for field N°BD i got this exception at runtime: before query WDS *** Exception: UnexpectedNull {errSQLType = "String", errHaskellType = "Text", errFieldName = "DNUM", errMessage = "unexpected null in table WDS of database sidonie"} *Main> i need some code ,perheaps uing Maybe to handle the NULL value in the field N°BD i think Haskell have special feature to program this , i read the article: https://lukeplant.me.uk/blog/posts/null-pointers-vs-none-vs-maybe/ but for now i have no concrete solution.... Regards, Damien
Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
--
Damien.Mattei@unice.fr, Damien.Mattei@oca.eu, UNS / OCA / CNRS
Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

thank you Tom, it is the kind of answer that help me i insert this and it is ok now: forM_ bd_rows_WDS $ \(Only x) -> case x of Nothing -> putStrLn ("x =" ++ "NULL") Just x -> putStrLn $ ("x =" ++ Text.unpack x) i had seen a such solution on web but thought the Maybe was the solution, i hoped asked could avoid using sort of Null pointer as mentioned in this article: https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer... if someone else have a simpler solution email me Le 18/12/2018 13:34, tom.stejskal a écrit :
case x of Nothing -> putStrLn "NULL" Just y -> putStrLn $ Text.unpack y
-- Damien.Mattei@unice.fr, Damien.Mattei@oca.eu, UNS / OCA / CNRS

That is using Maybe and is not a null pointer! Just and Nothing are the constructors of Maybe. On Tue, Dec 18, 2018 at 03:43:35PM +0100, Damien Mattei wrote:
thank you Tom, it is the kind of answer that help me i insert this and it is ok now: forM_ bd_rows_WDS $ \(Only x) -> case x of Nothing -> putStrLn ("x =" ++ "NULL") Just x -> putStrLn $ ("x =" ++ Text.unpack x) i had seen a such solution on web but thought the Maybe was the solution, i hoped asked could avoid using sort of Null pointer as mentioned in this article: https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer... if someone else have a simpler solution email me
Le 18/12/2018 13:34, tom.stejskal a écrit :
case x of Nothing -> putStrLn "NULL" Just y -> putStrLn $ Text.unpack y

ok well i do not want to open a discussion here, but i do not see any benefit in Haskell using Nothing and Just compared to other language, it's the same than if i had written in C something like : if (x == NULL) printf("NULL\n"); else printf("%s",*x); i expected more from Haskell... ok reading twice the article in link, i admit there should be some adavantage using Nothing instead of null, such as type, and genericity and not being spcecific to alloated memory as in C ... Le 18/12/2018 16:16, Tom Ellis a écrit :
That is using Maybe and is not a null pointer!
Just and Nothing are the constructors of Maybe.
On Tue, Dec 18, 2018 at 03:43:35PM +0100, Damien Mattei wrote:
thank you Tom, it is the kind of answer that help me i insert this and it is ok now: forM_ bd_rows_WDS $ \(Only x) -> case x of Nothing -> putStrLn ("x =" ++ "NULL") Just x -> putStrLn $ ("x =" ++ Text.unpack x) i had seen a such solution on web but thought the Maybe was the solution, i hoped asked could avoid using sort of Null pointer as mentioned in this article: https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer... if someone else have a simpler solution email me
Le 18/12/2018 13:34, tom.stejskal a écrit :
case x of Nothing -> putStrLn "NULL" Just y -> putStrLn $ Text.unpack y
Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- Damien.Mattei@unice.fr, Damien.Mattei@oca.eu, UNS / OCA / CNRS

You seem rather confused about the topic. What magic were you expecting to
happen if something doesn't have a value?
The point of Maybe is it lets you (a) specify exactly where things can lack
a value (as, for example, when a database column can produce a NULL instead
of a value), and (b) explicitly handle those instead of hoping the program
somehow reads your mind to figure out how to deal with it or feeds you some
unexpected 'nil' or whatever.
Consider in this case that the database does not care if a column doesn't
happen to have any NULLs, it cares only that the column was not declared
NOT NULL and therefore *can* contain a NULL. Which is the same condition
where you use a Maybe someType in Haskell instead of simply someType.
In particular, if you were expecting a database NULL to turn into the empty
string or "NULL" or some other such in-band value, how were you expecting
to distinguish the case where the database row actually contains that
string? That's why databases have NULL to begin with: "no value" distinct
from any possible actual value. If you do not want this, declare the column
NOT NULL in the database, don't expect the client program to do magic for
you.
On Tue, Dec 18, 2018 at 10:34 AM Damien Mattei
ok well i do not want to open a discussion here, but i do not see any benefit in Haskell using Nothing and Just compared to other language, it's the same than if i had written in C something like : if (x == NULL) printf("NULL\n"); else printf("%s",*x);
i expected more from Haskell...
ok reading twice the article in link, i admit there should be some adavantage using Nothing instead of null, such as type, and genericity and not being spcecific to alloated memory as in C ...
Le 18/12/2018 16:16, Tom Ellis a écrit :
That is using Maybe and is not a null pointer!
Just and Nothing are the constructors of Maybe.
On Tue, Dec 18, 2018 at 03:43:35PM +0100, Damien Mattei wrote:
thank you Tom, it is the kind of answer that help me i insert this and it is ok now: forM_ bd_rows_WDS $ \(Only x) -> case x of Nothing -> putStrLn ("x =" ++ "NULL") Just x -> putStrLn $ ("x =" ++ Text.unpack x) i had seen a such solution on web but thought the Maybe was the solution, i hoped asked could avoid using sort of Null pointer as mentioned in this article:
https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer...
if someone else have a simpler solution email me
Le 18/12/2018 13:34, tom.stejskal a écrit :
case x of Nothing -> putStrLn "NULL" Just y -> putStrLn $ Text.unpack y
Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- Damien.Mattei@unice.fr, Damien.Mattei@oca.eu, UNS / OCA / CNRS _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- brandon s allbery kf8nh allbery.b@gmail.com

Quoting Damien Mattei (2018-12-18 10:34:15)
if (x == NULL) printf("NULL\n"); else printf("%s",*x);
C will also allow you to just write: printf("%s", *x); Without the if, which will silently compile without complaint, and then segfault (at best) or corrupt memory at runtime. With Maybe there's no way to try to access the value if it isn't actually there. As others have pointed out, your database has a concept of null (to which the article's complaints are spot on), so somehow you have to model the fact that it may not actually return a string. There are more ergonomic ways of dealing with it than having the same case expression everywhere; other replies suggested using the `maybe` function to supply a default, or modifying the database schema to say NOT NULL -- but you have to deal with it somehow. The advantage of Maybe is: * It makes the possibility of no-value explicit, and not something you can forget to handle. * It makes it possible to express that no-value is *not* a possibility, when that is the case (by just not using a Maybe). -Ian

➢ The advantage of Maybe is... This is not advantage of Maybe, but advantage of pattern-matching which allows you to “extract” value only from “Just a” branch and no way to get value from Nothing. But wait, I can get the same exception as in C/C++: fromJust Nothing without “if isJust...”! So, Damien is right here – no difference with C and Haskell is not more safe than C here – and you CAN omit checking and to get run time exception. I’m not sure about SML, may be it does not allow to get value in so dangerous way, but Haskell allows, like C. You will not get warning at compile time even 😊 But in C# 8 you will get warning with such dangerous using of Nullable. Common rule is to use pattern-matching. The same technique, sure, is possible in C – you need to wrap value in some structure and to use special functions/macros to extract value, modern C++ libraries already have “option” types. From: Ian Denhardt Sent: 18 декабря 2018 г. 21:01 To: Damien Mattei; haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] (SPAM 3)Re: handling NULL value in database querywith Maybe (or other ...) Quoting Damien Mattei (2018-12-18 10:34:15)
if (x == NULL) printf("NULL\n"); else printf("%s",*x);
C will also allow you to just write: printf("%s", *x); Without the if, which will silently compile without complaint, and then segfault (at best) or corrupt memory at runtime. With Maybe there's no way to try to access the value if it isn't actually there. As others have pointed out, your database has a concept of null (to which the article's complaints are spot on), so somehow you have to model the fact that it may not actually return a string. There are more ergonomic ways of dealing with it than having the same case expression everywhere; other replies suggested using the `maybe` function to supply a default, or modifying the database schema to say NOT NULL -- but you have to deal with it somehow. The advantage of Maybe is: * It makes the possibility of no-value explicit, and not something you can forget to handle. * It makes it possible to express that no-value is *not* a possibility, when that is the case (by just not using a Maybe). -Ian _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

Quoting Paul (2018-12-18 14:28:53)
But wait, I can get the same exception as in C/C++: fromJust Nothing without "if isJust..."! So, Damien is right here - no difference with C and Haskell is not more safe than C here - and you CAN omit checking and to get run time exception.
First, C doesn't give you an exception, it just tramples over memory. The OS jumps through hoops to make it likely that it will start with memory that will cause it to be killed before it does any real damage, but it's still a different beast. But if you really want that behavior could also just do: case x of Just x -> x Nothing -> unsafeCoerce 0 ..and pattern matching doesn't save you. So yes, if you heap on enough pedantry, you can claim that Haskell is not safer than C. But this argument is divorced from the reality of what writing code is actually like in these two languages. Segfaults and memory corruption are the norm in C, and not in Haskell.
C# 8
[...]
modern C++
I did say C, not C# or C++ which are entirely different languages. Type-level nil tracking actually does give you the benefits I describe, though it's not as nice as proper sum types more generally. I did not say that Haskell's Maybe is the only way to achieve this. -Ian

On Tue, 18 Dec 2018 at 15:34, Damien Mattei
ok well i do not want to open a discussion here, but i do not see any benefit in Haskell using Nothing and Just compared to other language, it's the same than if i had written in C something like : if (x == NULL) printf("NULL\n"); else printf("%s",*x);
i expected more from Haskell...
Well, you have to remember to do that in C, but in Haskell the compiler won't let you use a "Maybe a" as an "a". They're totally different things. That seems a pretty huge benefit to me, even ignoring everything else. What more could you want? -- Michael Walker (http://www.barrucadu.co.uk)

i had many answers, i can not answer to all,first thanks just afew words, i did not want to have a C beahvior at all,i try to escape from the C world... (the solution with Just and Nothing help a lot) i espected something like that too, the answer have been given to me in comp.lang.haskell: bd_rows_WDS :: [Maybe Text] <- query conn qry_head_WDS (Only (name::String)) but it fails to compile: *Main> :load UpdateSidonie [1 of 1] Compiling Main ( UpdateSidonie.hs, interpreted ) UpdateSidonie.hs:213:5: error: Illegal type signature: ‘[Maybe Text]’ Type signatures are only allowed in patterns with ScopedTypeVariables | 213 | bd_rows_WDS :: [Maybe Text] <- query conn qry_head_WDS (Only (name::String)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed, no modules loaded. and i completely agree that Haskell is more safe than C/C++ even if as quoted here: https://esham.io/ it requires ,to do basic things " a Ph.D. and three Monoid instances "... quoted from "But zsh in particular lets you write text manipulation one-liners which in Haskell would require a Ph.D. and three Monoid instances. " :-) Le 18/12/2018 20:06, Michael Walker a écrit :
On Tue, 18 Dec 2018 at 15:34, Damien Mattei
wrote: ok well i do not want to open a discussion here, but i do not see any benefit in Haskell using Nothing and Just compared to other language, it's the same than if i had written in C something like : if (x == NULL) printf("NULL\n"); else printf("%s",*x);
i expected more from Haskell...
Well, you have to remember to do that in C, but in Haskell the compiler won't let you use a "Maybe a" as an "a". They're totally different things.
That seems a pretty huge benefit to me, even ignoring everything else. What more could you want?
-- Damien.Mattei@unice.fr, Damien.Mattei@oca.eu, UNS / OCA / CNRS

So, add this extension. I think it would be: {-# LANGUAGE ScopedTypeVariables #-} ... (bd_rows_WDS :: [Maybe Text]) <- query conn qry_head_WDS (Only (name::String)) @Ian: Example with unsafeCoerce is not correct: how is "unsafeCoerce" related to Maybe/pattern matching? True is that Haskell is not safe, for example, it has unsafe functions fromJust, etc, while pattern-matching is only the right way to "extract" value from Maybe (OK, also "maybe" function). Ian, by the way, how can you guarantee that Segmentation Fault will not happen in Haskell application? I hit even freezing of Haskell apps, Haskell is not a language with such assertions. May be it will be Rust, but Haskell - never. 19.12.2018 11:34, Damien Mattei wrote:
i had many answers, i can not answer to all,first thanks just afew words, i did not want to have a C beahvior at all,i try to escape from the C world... (the solution with Just and Nothing help a lot) i espected something like that too, the answer have been given to me in comp.lang.haskell:
bd_rows_WDS :: [Maybe Text] <- query conn qry_head_WDS (Only (name::String))
but it fails to compile:
*Main> :load UpdateSidonie [1 of 1] Compiling Main ( UpdateSidonie.hs, interpreted )
UpdateSidonie.hs:213:5: error: Illegal type signature: ‘[Maybe Text]’ Type signatures are only allowed in patterns with ScopedTypeVariables | 213 | bd_rows_WDS :: [Maybe Text] <- query conn qry_head_WDS (Only (name::String)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed, no modules loaded.
and i completely agree that Haskell is more safe than C/C++ even if as quoted here: https://esham.io/ it requires ,to do basic things " a Ph.D. and three Monoid instances "...
quoted from "But zsh in particular lets you write text manipulation one-liners which in Haskell would require a Ph.D. and three Monoid instances. " :-)
Le 18/12/2018 20:06, Michael Walker a écrit :
On Tue, 18 Dec 2018 at 15:34, Damien Mattei
wrote: ok well i do not want to open a discussion here, but i do not see any benefit in Haskell using Nothing and Just compared to other language, it's the same than if i had written in C something like : if (x == NULL) printf("NULL\n"); else printf("%s",*x);
i expected more from Haskell... Well, you have to remember to do that in C, but in Haskell the compiler won't let you use a "Maybe a" as an "a". They're totally different things.
That seems a pretty huge benefit to me, even ignoring everything else. What more could you want?

Am Mi., 19. Dez. 2018 um 13:24 Uhr schrieb Paul
[...] @Ian: Example with unsafeCoerce is not correct: how is "unsafeCoerce" related to Maybe/pattern matching?
I think Ian just wanted to demonstrate that it is possible to crash Haskell programs in the same way as C programs if you really want to. OTOH, the name "unsafeCoerce" of the function used and its type (a -> b) alone should be very visible warning signs. ;-) Mere mortals should better forget about its existence, but unsafeCoerce can be useful if it is wrapped correctly. So it's a bit like a buzz saw: Quite useful if you need it, but *very* dangerous unless you know what you're doing...
True is that Haskell is not safe, for example, it has unsafe functions fromJust, etc, while pattern-matching is only the right way to "extract" value from Maybe (OK, also "maybe" function).
If you consider a language "safe" or not depends on your POV: Haskell is safe in the sense that programs written in it (without unsafeFOO stuff and without any FFI involved) do not segfault randomly like e.g. C programs. fromJust uses non-exhaustive pattern matching, which raises an exception if it encounters Nothing. This is fundametally different from e.g. dereferencing NULL/nullptr or accessing random memory.
Ian, by the way, how can you guarantee that Segmentation Fault will not happen in Haskell application? I hit even freezing of Haskell apps, Haskell is not a language with such assertions. May be it will be Rust, but Haskell - never.
Every turing complete language *will* include the possibility of non-termination (which is what I guess you mean by "freezing), so this part is irrelevant. But even if you ignore that, every useful programming language on earth will give you the possibility of crashing, if you want it or not: At one point you will need to call out to native/C land to use OS and/or library functionality. Well, unless you want to rewrite your OS and all libraries in your shiny new and safe language...

Quoting Sven Panne (2018-12-19 09:03:21)
I think Ian just wanted to demonstrate that it is possible to crash Haskell programs in the same way as C programs if you really want to. OTOH,
the name "unsafeCoerce" of the function used and its type (a -> b) alone should be very visible warning signs. ;-) Mere mortals should better forget about its existence,
..and this was more my point -- while it *is* possible to cause the same bad behavior in Haskell as in C, the language is constructed in such a way that it's hard to do so by accident.
Ian, by the way, how can you guarantee that Segmentation Fault will not happen in Haskell application?
You can't, and indeed it has happened in the past, sometimes due to compiler bugs, sometimes due to misuse of stuff like unsafeCoerce or the FFI. But it's literally never happened to me personally. My point is it's not about guarantees so much as whether getting things right is easy. Forgetting to check for NULL in C is a massive source of real-world bugs. Forgetting to check for Nothing in Haskell isn't. Note that the distinction *does* matter if you're thinking of building something like lambdabot, where you may be running malicious code. But for most purposes it doesn't.

Quoting Sven Panne (2018-12-19 09:03:21)
I think Ian just wanted to demonstrate that it is possible to crash Haskell programs in the same way as C programs if you really want to. OTOH,
the name "unsafeCoerce" all is in the name... ;-) it's unsafe and coercitive... i will always
Le 19/12/2018 20:35, Ian Denhardt a écrit : try to avoid this in my code... of the function used and its type (a ->
b) alone should be very visible warning signs. ;-) Mere mortals should better forget about its existence,
..and this was more my point -- while it *is* possible to cause the same bad behavior in Haskell as in C, the language is constructed in such a way that it's hard to do so by accident.
Ian, by the way, how can you guarantee that Segmentation Fault will not happen in Haskell application?
You can't, and indeed it has happened in the past, sometimes due to compiler bugs, sometimes due to misuse of stuff like unsafeCoerce or the FFI.
But it's literally never happened to me personally.
My point is it's not about guarantees so much as whether getting things right is easy. Forgetting to check for NULL in C is a massive source of real-world bugs. Forgetting to check for Nothing in Haskell isn't.
Note that the distinction *does* matter if you're thinking of building something like lambdabot, where you may be running malicious code. But for most purposes it doesn't. _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- Damien.Mattei@unice.fr, Damien.Mattei@oca.eu, UNS / OCA / CNRS

On 2018-12-18 7:43 AM, Damien Mattei wrote:
i hoped asked could avoid using sort of Null pointer as mentioned in this article: https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer... if someone else have a simpler solution email me
You can avoid it: make the DNUM column non-NULL in your database schema. That "worst mistake" applies to databases as well as programming languages! :-) If you have to allow NULL in the database, then you have to handle Nothing in your code. However, a neater way of doing it is to use the maybe function: forM_ bd_rows_WDS $ \(Only x) -> putStrLn $ "x =" ++ maybe "" Text.unpack x This allows you to provide a default value in your code for when the column is NULL.

cool... forM_ bd_rows_WDS $ \(Only x) -> putStrLn $ "x =" ++ maybe "NULL" Text.unpack x it worked... thank you , i will which on i use because i need to forget all NULL values ,get just an answer strange lazy evaluation at runtime i had error at this: putStrLn "before query WDS" bd_rows_WDS <- query conn qry_head_WDS (Only (name::String)) putStrLn "after query WDS" and it has been solved 20 lines below withe maybe: forM_ bd_rows_WDS $ \(Only x) -> putStrLn $ "x =" ++ maybe "NULL" Text.unpack x that now deal with NULL values i suppose it's because until some printing is done nothing is evaluated before in lazy programming... Le 18/12/2018 16:22, Neil Mayhew a écrit :
On 2018-12-18 7:43 AM, Damien Mattei wrote:
i hoped asked could avoid using sort of Null pointer as mentioned in this article: https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer... if someone else have a simpler solution email me
You can avoid it: make the DNUM column non-NULL in your database schema. That "worst mistake" applies to databases as well as programming languages! :-)
If you have to allow NULL in the database, then you have to handle Nothing in your code. However, a neater way of doing it is to use the maybe function:
forM_ bd_rows_WDS $ \(Only x) -> putStrLn $ "x =" ++ maybe "" Text.unpack x
This allows you to provide a default value in your code for when the column is NULL.
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- Damien.Mattei@unice.fr, Damien.Mattei@oca.eu, UNS / OCA / CNRS

Le 18/12/2018 16:22, Neil Mayhew a écrit :
On 2018-12-18 7:43 AM, Damien Mattei wrote:
i hoped asked could avoid using sort of Null pointer as mentioned in this article: https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer... if someone else have a simpler solution email me
You can avoid it: make the DNUM column non-NULL in your database schema. That "worst mistake" applies to databases as well as programming languages! :-)
yes :-) but it is not me that create the database,it's a 20 years old database....
If you have to allow NULL in the database, then you have to handle Nothing in your code. However, a neater way of doing it is to use the maybe function:
forM_ bd_rows_WDS $ \(Only x) -> putStrLn $ "x =" ++ maybe "" Text.unpack x
This allows you to provide a default value in your code for when the column is NULL.
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- Damien.Mattei@unice.fr, Damien.Mattei@oca.eu, UNS / OCA / CNRS
participants (9)
-
Brandon Allbery
-
Damien Mattei
-
Ian Denhardt
-
Michael Walker
-
Neil Mayhew
-
Paul
-
Sven Panne
-
Tom Ellis
-
tom.stejskal