Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Michael Walter <michael.walter@gmail.com>
To: skaller@users.sourceforge.net
Cc: William Lovas <wlovas@stwing.upenn.edu>, caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] environment idiom
Date: Mon, 13 Dec 2004 04:29:24 -0500	[thread overview]
Message-ID: <877e9a17041213012926f7e18a@mail.gmail.com> (raw)
In-Reply-To: <1102916483.2768.204.camel@pelican.wigram>

On 13 Dec 2004 16:41:23 +1100, skaller <skaller@users.sourceforge.net> wrote:
> [...]
> I am desiring to show here that 'the ability to
> reason about correctness' depends on what encoding
> of what you're asking the question about.
Agreed.

> Clearly even a functional encoding of the Sieve
> is still a procedural program at the level of
> calculating primes. However it is pure at the
> detail level. This only helps, and only perhaps
> helps, at reasoning if the code faithfully implements
> the algorithm -- if you try to reason about it's
> ability to calculate primes you inevitably have to
> account for the fact the algorithm isn't functional.
What is "the detail level"? Like the "language level" in contrast to
the level of the current abstraction (for instance, State monad)?

Different note: I think you are missing out an important property of
the functional encoding, which is its purity wrt composability.
Suppose one of the few obvious types of the sieve, for instance:
  sieve :: Integer -> [Integer]
You can clearly see that the sieve is a pure function, and actually
the type system proves this for you (modulo cheating, err,
unsafePerformIO et al). This makes it very easy and safe to use the
function (and also to test it, btw!).

In constrast, in a language such as C++ you cannot assume that..
  vector<unsigned> sieve(unsigned);
has no side effects.

Also consider the "print" part of your algorithm, which I ignored so
far. In C++ it would be very easy to add it to sieve() thus making the
function virtually useless to use but in the special case where you
want to print the number instantly.

In Haskell, you *could* have a sieve :: Integer -> IO [Integer], but
what you would really do is to decouple the sieve and I/O (and this is
made kinda "attractive" by the expressiveness of the language and the
type system). So what you would do is something like:
  printPrimes :: Integer -> IO ()
  printPrimes :: mapM_ print . sieve
(modulo obvious types made at 4 AM :-)

There is also an interesting point about algorithms which I'm sure you
are aware of. Often when analyzing algorithms for their correctness,
you reason about the state before and after a certain step in the
algorithm. That is, you make the state and its change explicit. Sounds
like sieveStep :: State -> State, huh? :-) I think that is also what
makes monads in Haskell attractive. They are very nice to use from a
programmer's perspective (IMO), but they also allow good reasoning (as
they are usually purely defined or at least definable).

> This is probably harder to do in Ocaml, where the
> places that transparency is lost aren't so easy to
> find, and the effects on the whole program not quite
> so easy to isolate.
Yep! Monads actually allow nice embedding of many paradigms, not only
the imperative.

4:29 AM-ly yours,
Michael


  reply	other threads:[~2004-12-13  9:29 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-09  2:07 HENRIKSON, JEFFREY
2004-12-09  4:47 ` [Caml-list] " Jacques Garrigue
2004-12-09  6:02   ` Michael Walter
2004-12-09 11:28     ` Jacques Garrigue
2004-12-09 20:02     ` pad
2004-12-09 23:11       ` Jacques Garrigue
2004-12-10  2:30         ` skaller
2004-12-09  9:09 ` Richard Jones
2004-12-09 13:12   ` [Caml-list] " Ville-Pertti Keinonen
2004-12-10 11:59     ` Richard Jones
2004-12-10 10:52 ` [Caml-list] " Andrej Bauer
2004-12-10 12:13   ` Richard Jones
2004-12-10 23:35     ` Jacques Garrigue
2004-12-11  2:30   ` skaller
2004-12-11 14:31     ` Andrej Bauer
2004-12-11 18:13       ` Markus Mottl
2004-12-11 23:56         ` skaller
2004-12-12  2:36           ` William Lovas
2004-12-12  5:33             ` skaller
2004-12-12 19:09               ` Michael Walter
2004-12-13  0:48                 ` skaller
2004-12-13  2:03                   ` Michael Walter
2004-12-13  2:05                     ` Michael Walter
     [not found]                       ` <877e9a170412121844b633bb8@mail.gmail.com>
2004-12-13  2:45                         ` Michael Walter
2004-12-13  6:18                           ` skaller
2004-12-13  7:08                             ` skaller
2004-12-13  9:56                             ` Michael Walter
2004-12-13 12:59                               ` skaller
2004-12-13  8:56                           ` Thomas Fischbacher
2004-12-13  9:21                             ` Jacques Garrigue
2004-12-13 10:05                               ` Michael Walter
2004-12-13 10:29                                 ` Thomas Fischbacher
2004-12-13 21:16                                   ` Michael Walter
2004-12-13 10:20                               ` Thomas Fischbacher
2004-12-13 12:09                                 ` Jacques Garrigue
2004-12-13 12:48                                   ` Thomas Fischbacher
2004-12-13 14:09                                   ` skaller
2004-12-13 21:39                                     ` Michael Walter
2004-12-13 13:22                                 ` skaller
2004-12-13 16:54                                   ` Marcin 'Qrczak' Kowalczyk
2004-12-13 18:44                                   ` Thomas Fischbacher
2004-12-13 10:11                             ` Michael Walter
2004-12-13 11:46                             ` skaller
2004-12-13  5:41                     ` skaller
2004-12-13  9:29                       ` Michael Walter [this message]
2004-12-13 12:30                         ` skaller
2004-12-13 13:49                           ` Martin Berger
2004-12-12 23:03           ` Thomas Fischbacher
2004-12-13  1:26             ` skaller
2004-12-13  8:37               ` Thomas Fischbacher
2004-12-13 10:53                 ` skaller
2004-12-13 11:38                   ` Martin Berger
2004-12-13 13:33                     ` skaller
2004-12-13 12:01                   ` Thomas Fischbacher
2004-12-13 13:41                     ` skaller
2004-12-11 23:29       ` skaller
2004-12-12  0:21         ` Jacques Carette

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=877e9a17041213012926f7e18a@mail.gmail.com \
    --to=michael.walter@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=skaller@users.sourceforge.net \
    --cc=wlovas@stwing.upenn.edu \
    /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