caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Oliver Bandel <oliver@first.in-berlin.de>
To: Markus Elfring <Markus.Elfring@web.de>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Detecting missed checks for error indications in ML code
Date: Sun, 24 Jan 2021 15:34:20 +0100	[thread overview]
Message-ID: <20210124143420.443qexv5dvgtqpbm@first.in-berlin.de> (raw)
In-Reply-To: <41485ac7-50cf-08fe-4206-b9f863c4d2f5@web.de>

Hi,


Quoting  Markus Elfring <Markus.Elfring@web.de> (snt: 2021-01-24 12:42 +0100 CET) (rcv: 2021-01-24 12:42 +0100 CET):
> Hello,
> 
> OCaml belongs also to the category of functional programming languages.
> Thus the last expression in a function usually becomes the result of
> the data processing which was specified in a ML source file.
> This would usually happen also if the return value from a function call
> indicates an error code (when a function implementation does not throw an exception
> according to an error situation).
[...]

It is very common in other languages to somehow encode "errors" or
"missing values" via the result itself, by selecting a more or less
"obvius" (at the time of writing the function) value as representing an
error or missing value.
Let's call it the socilogogists approach, because I often saw
sociologists encode their data with natural numbers (starting at 1),
for say 7 or so categories, and the "no data available" was encoded as 99.

(R offers NA and NaN, but sociologists tend(ed) to use SPSS or Excel and
the 99 as exceptional value was common for decades; I think it's still
handled this way today.)

Similarly in Perl or other languages, often the empty string is used
  (undef would be better; like NULL in C, which makes sense, just don't
   dereference it; in python you have None; ...)
for indicating a certain return value, which is used like the
sociologist's 99.

But OCaml isn't just only a functional language.
It's also a typed language. And the type system can help you at this
point.
You can use the option-type for indicating missing data.
So, None and Some "" would be different.
The None indicates that there is no data available, while the Some ""
says: hey, we have data, but it's the empty string.
In that way, the empty string is not indicating a problem anymore.
And that means, that the problem you have in mind disappears!
(And if getting the empty string from the source of your data is
  indeed only possible in case of an error, return None instead of Some
  "" and you have made the conversion to a type safe return value. From
  the outside then there will be no Some "" in the set of  possibly
  returned values.)

> 
> I got the impression that corresponding error checks can occasionally be missing.
> How do you think about to detect such cases by advanced source code analysis?

No need to thest these kind of problems, as long as you use the type
system to help you as mentioned above.

Of course you can stick to the sociologists way of coding your data and
return values also in OCaml. But if you use the possibilities the
OCaml's type system offers, all that clutter with problematic values is
excluded from the beginning. No need for extra testing. Just look at the
signature of your functions.

Since a while OCaml also offers a result-type, which you can use.
It might be better than the option type in more general cases.
And if predefined types don't offer what you are looking for, just
define your own type.

Ciao,
  Oliver


  parent reply	other threads:[~2021-01-24 14:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-24 11:42 Markus Elfring
2021-01-24 14:08 ` Hendrik Boom
2021-01-24 15:28   ` Markus Elfring
2021-01-24 14:34 ` Oliver Bandel [this message]
2021-01-24 15:52   ` Markus Elfring
2021-01-24 16:07     ` Oliver Bandel
2021-01-24 16:21       ` Markus Elfring
2021-01-25  3:59         ` Yawar Amin
2021-01-25 18:55           ` Markus Elfring
2021-01-24 16:18     ` Oliver Bandel
2021-01-24 16:50       ` Markus Elfring
2021-01-29 21:14         ` [Caml-list] Error handling in FPLs (Re: Detecting missed checks for error indications in ML code) Oliver Bandel
2021-01-30 11:51           ` Markus Elfring

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210124143420.443qexv5dvgtqpbm@first.in-berlin.de \
    --to=oliver@first.in-berlin.de \
    --cc=Markus.Elfring@web.de \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).