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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
23 messages Options
12
Reply | Threaded
Open this post in threaded view
|

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

Damien Mattei
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

--
[hidden email], [hidden email], 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.
Reply | Threaded
Open this post in threaded view
|

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

Paul
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.
Reply | Threaded
Open this post in threaded view
|

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

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.

--
[hidden email], [hidden email], 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.
Reply | Threaded
Open this post in threaded view
|

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

Damien Mattei
the URL  of the doc i quoted is :

http://hackage.haskell.org/package/mysql-simple-0.4.5/docs/Database-MySQL-Simple.html

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.
>

--
[hidden email], [hidden email], 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.
Reply | Threaded
Open this post in threaded view
|

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

tom.stejskal
In reply to this post by Damien Mattei
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 <[hidden email]> wrote:

> 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.
>
> --
>
> [hidden email], [hidden email], 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.


_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

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

Damien Mattei
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-science/
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

--
[hidden email], [hidden email], 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.
Reply | Threaded
Open this post in threaded view
|

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

Tom Ellis-5
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-science/
> 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.
Reply | Threaded
Open this post in threaded view
|

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

Neil Mayhew
In reply to this post by Damien Mattei
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-science/
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.
Reply | Threaded
Open this post in threaded view
|

Re: (SPAM 3)Re: handling NULL value in database query with Maybe (or other ...)

Damien Mattei
In reply to this post by Tom Ellis-5
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-science/
>> 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.
>

--
[hidden email], [hidden email], 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.
Reply | Threaded
Open this post in threaded view
|

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

Damien Mattei
In reply to this post by Neil Mayhew
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-science/
>> 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.
>

--
[hidden email], [hidden email], 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.
Reply | Threaded
Open this post in threaded view
|

Re: (SPAM 3)Re: handling NULL value in database query with Maybe (or other ...)

Brandon Allbery
In reply to this post by Damien Mattei
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 <[hidden email]> 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...

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-science/
>> 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.
>

--
[hidden email], [hidden email], 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

_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: (SPAM 3)Re: handling NULL value in database query with Maybe (or other ...)

Ian Denhardt
In reply to this post by Damien Mattei
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.
Reply | Threaded
Open this post in threaded view
|

Re: (SPAM 3)Re: handling NULL value in database query with Maybe (or other ...)

Michael Walker
In reply to this post by Damien Mattei
On Tue, 18 Dec 2018 at 15:34, Damien Mattei <[hidden email]> 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?

--
Michael Walker (http://www.barrucadu.co.uk)
_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: (SPAM 3)Re: handling NULL value in database querywith Maybe (or other ...)

Paul
In reply to this post by Ian Denhardt
  • 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: [hidden email]
Sent: 18 декабря 2018 г. 21:01
To: [hidden email]; [hidden email]
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.

 


_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: (SPAM 3)Re: handling NULL value in database querywith Maybe (or other ...)

Ian Denhardt
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
_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

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

Damien Mattei
In reply to this post by Neil Mayhew


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-science/
>> 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.
>

--
[hidden email], [hidden email], 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.
Reply | Threaded
Open this post in threaded view
|

Re: (SPAM 3)Re: handling NULL value in database query with Maybe (or other ...)

Damien Mattei
In reply to this post by Michael Walker
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 <[hidden email]> 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?
>

--
[hidden email], [hidden email], 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.
Reply | Threaded
Open this post in threaded view
|

Re: (SPAM 3)Re: handling NULL value in database query with Maybe (or other ...)

Paul
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 <[hidden email]> 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?
>>

_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: (SPAM 3)Re: handling NULL value in database query with Maybe (or other ...)

Sven Panne-2
Am Mi., 19. Dez. 2018 um 13:24 Uhr schrieb Paul <[hidden email]>:
[...] @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...

_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

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

Damien Mattei
In reply to this post by Damien Mattei
thank tou for your C/HASKELL "rosetta stone" about the Monad it helps a
lot understanding the thing...

i always thougt Monads are powerful but they are not easy sot
manipulates syntactically for a beginner in Haskell.

yes in fact what i secretly expected  was a way to discard NULL fields
automagically and it seems really possible with Haskell.

Le 19/12/2018 11:27, Mario Lang a écrit :

> Damien Mattei <[hidden email]> writes:
>
>> 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...
>
> Haskell actually offers more.
>
> 1. The type of "Maybe a" prevents you from ignoring the fact that "a" might
> be null.  In C, you can accidentally write
>
> printf("%s",*x);
>
> and risking a segfault.  The compiler will not care.  IN Haskell, the
> types dont even allow you to write something like that.  You would be
> forced to check for Just or Nothing in one of several ways.
>
> Now, "Maybe a" doesn't only limit your abilities, it also offers you the
> functions from the Functor, Applicative and Monad typeclasses.
>
> 2. Functor allows you to apply a function to the "a", as long as there is
> some "a" inside the "Maybe a".  So, "fmap f x" is roughly equivalent to:
>
> typeOfA f(typeOfA);
> typeOfA *x func(typeOfA *x) {
>   if (x) {
>     typeOfA *val = malloc(sizeof(typeOfA));
>     *val = f(*x);
>     return val;
>   }
>   return NULL;
> }
> func(x);
>
> (note that pure Haskell doesn't have assignment, so the translation to C
> is a little bit more verbose then it might be if you were using values
> on the stack.)

i tried this:

 let resFMAP = fmap (\(Only a) -> (putStrLn ("a =" ++ Text.unpack a)))
bd_rows_WDS

but compile complains now that i use a Maybe variable instead of a plain
one,strange because this exactly what i expected from Maybe use it to
automatically discard the Nothing values (avoiding to putStrln them):
Prelude> :load UpdateSidonie
[1 of 1] Compiling Main             ( UpdateSidonie.hs, interpreted )

UpdateSidonie.hs:293:75: error:
    • Couldn't match type ‘Maybe Text’ with ‘Text’
      Expected type: [Only Text]
        Actual type: [Only (Maybe Text)]
    • In the second argument of ‘fmap’, namely ‘bd_rows_WDS’
      In the expression:
        fmap (\ (Only a) -> (putStrLn ("a =" ++ unpack a))) bd_rows_WDS
      In an equation for ‘resFMAP’:
          resFMAP
            = fmap (\ (Only a) -> (putStrLn ("a =" ++ unpack a)))
bd_rows_WDS
    |
293 |     let resFMAP = fmap (\(Only a) -> (putStrLn ("a =" ++
Text.unpack a))) bd_rows_WDS
    |
     ^^^^^^^^^^^
Failed, no modules loaded.

bd_rows_WDS is defined like this with a Maybe:

(bd_rows_WDS :: [Only (Maybe Text)]) <- query conn qry_head_WDS (Only
(name::String))

anyway i know enought in haskell now to remove myself the Nothing (NULL)
values of the list doing that (it worked):

-- remove the records having N°BD NULL
    let fltWDS = Prelude.filter (\(Only a) ->
                                    case a of
                                      Nothing -> False
                                      Just a -> True)
                 bd_rows_WDS

>
> So you do not need to write an explicit if in Haskell.  The Functor
> instance of Maybe "knows" how to deal will null cases.
> In particular, if it encounters a Nothing, it returns a Nothing,
> ignoring the provided function.
>
> 3. Applicative allows you to apply a function to the "a"s of several "Maybe a"
> values, returning a Maybe wrapping the result of that function.
> So "f <$> x <*> y" is equivalent to
>
> typeOfA f(TypeOfA);
> typeOfA *func(typeOfA *x, typeOfA *y) {
>   if (x && y) {
>     typeOfA *val = malloc(sizeof(typeOfA));
>     *val = f(*x, *y);
>     return val;
>   }
>   return NULL;
> }
> func(x, y);
>
> The amount of boilerplate eliminated grows.
>
> 3. Monad allows you to chain several null-checks without having to write
> out the nesting.  This time, f, g and h return a "Maybe a", not just an "a":
>
> do
>   a <- f x
>   b <- g a
>   h b
>  
> or
>
> f x >>= \a -> g a >>= \b -> h b
>
> is equvalent to something like
>
> typeOfA *f(typeOfA);
> typeOfA *g(typeOfA);
> typeOfA *h(typeOfA);
> typeOfA *func(typeOfA x) {
>   if (x) {
>     type *a = f(*x);
>     if (a) {
>       type *b = g(*a);
>       if (b) {
>         return f(*b);
>       }
>     }
>   }
>   return NULL;
> }
>
> or, in Haskell:
>
> case f x of
>   Just a -> case g a of
>     Just b -> h b
>     Nothing -> Nothing
>   Nothing -> Nothing
>
> The Monad typeclass really pays off, as it saves you from doing the null
> checks and dereferencing.  The nested "if null then null" stuff goes all
> away, and you can chain several functions together that return a
> possible null value.
>
>> 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 ...
>
> Exactly.  As I see it, the Functor, Applicative and Monad typeclasses
> of the Maybe type give you the tools
> necessary to reduce the boilerplate that results from forcing you to
> deal with each and every null case in your code.
>

--
[hidden email], [hidden email], 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.
12