caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] functional or imperative feature ?
@ 2013-07-21 20:50 habet_ms
  2013-07-21 20:57 ` [Caml-list] " Christopher Zimmermann
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: habet_ms @ 2013-07-21 20:50 UTC (permalink / raw)
  To: caml-list

Hi,
  are sequencing (;) and block structure (begin ... end) functional features
  or imperative ones ?

Thanks,

M.S

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Caml-list] Re: functional or imperative feature ?
  2013-07-21 20:50 [Caml-list] functional or imperative feature ? habet_ms
@ 2013-07-21 20:57 ` Christopher Zimmermann
  2013-07-22 11:42   ` oliver
  2013-07-21 20:58 ` [Caml-list] " David Allsopp
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 12+ messages in thread
From: Christopher Zimmermann @ 2013-07-21 20:57 UTC (permalink / raw)
  To: caml-list

On Sun, 21 Jul 2013 22:50:26 +0200 (CEST)
<habet_ms@yahoo.fr> wrote:

> Hi,
>   are sequencing (;) and block structure (begin ... end) functional
> features or imperative ones ?

sequencing is imperative.
begin and end are just an alias for '(' and ')' and are used in both
styles.

Christopher


-- 
OpenPGP public key: http://gmerlin.de/christopher.pub
fingerprint: 1917 680A 723C BF3D 2CA3  0E44 7E24 D19F 34B8 2A2A


^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: [Caml-list] functional or imperative feature ?
  2013-07-21 20:50 [Caml-list] functional or imperative feature ? habet_ms
  2013-07-21 20:57 ` [Caml-list] " Christopher Zimmermann
@ 2013-07-21 20:58 ` David Allsopp
  2013-07-22 11:20 ` Jean-Marc Alliot
  2013-07-22 14:59 ` Kristopher Micinski
  3 siblings, 0 replies; 12+ messages in thread
From: David Allsopp @ 2013-07-21 20:58 UTC (permalink / raw)
  To: habet_ms, caml-list

habet_ms@yahoo.fr wrote:
> Hi,
>   are sequencing (;) and block structure (begin ... end) functional
> features
>   or imperative ones ?

Any sequence

e1; e2; e3;;

can be rewritten

let _ = e1 in
let _ = e2 in
e3;;

(with the difference that in a sequence, the compiler warns you if e1 and e2 don't have type unit) 

It's not strictly speaking an imperative construct but it's only really of use in imperative programming.

Conversely begin ... end is neither explicitly functional nor imperative - it's simply a form of brackets. Consider nested matches (definitely functional):

let f x y = 
  match x with
    `Foo ->
      begin
        match y with
          `Bar -> true
        | _ -> failwith "Bar"
      end
  | _ ->
    failwith "Foo"

HTH,


David 

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] functional or imperative feature ?
  2013-07-21 20:50 [Caml-list] functional or imperative feature ? habet_ms
  2013-07-21 20:57 ` [Caml-list] " Christopher Zimmermann
  2013-07-21 20:58 ` [Caml-list] " David Allsopp
@ 2013-07-22 11:20 ` Jean-Marc Alliot
  2013-07-22 12:05   ` r.3
  2013-07-22 14:59 ` Kristopher Micinski
  3 siblings, 1 reply; 12+ messages in thread
From: Jean-Marc Alliot @ 2013-07-22 11:20 UTC (permalink / raw)
  To: caml-list

Le 21/07/2013 22:50, habet_ms@yahoo.fr a écrit :
> Hi,
>    are sequencing (;) and block structure (begin ... end) functional features
>    or imperative ones ?
>
> Thanks,
>
> M.S
>
Interesting question, which I sometimes get from my students.
I am not giving really an answer (I saw one from David Allsopp), but 
instead an advice;
Ocaml is not the best language to learn "pure" functional programming. 
If you really want to give it a try, then try Haskell, and if you really 
want to be "extra-pure", don't use at all Haskell syntactic sugar. You 
will find all the answers by yourself, maybe the hard way :-)

PS: That's not a Haskell vs Ocaml troll. I love both languages, and I am 
using ocaml much more than Haskell for everyday programming (I consider 
Ocaml easier to use and even easier to read, but that's strictly IMHO, 
and maybe just because I am more familiar with it).
It's just that, for learning pure functional programming, with lazy 
evaluation, Haskell is really good.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Re: functional or imperative feature ?
  2013-07-21 20:57 ` [Caml-list] " Christopher Zimmermann
@ 2013-07-22 11:42   ` oliver
  2013-07-22 11:46     ` David Allsopp
  2013-07-22 11:51     ` Wojciech Meyer
  0 siblings, 2 replies; 12+ messages in thread
From: oliver @ 2013-07-22 11:42 UTC (permalink / raw)
  To: Christopher Zimmermann; +Cc: caml-list

Maybe it can be called just syntactical sugar...


========================================================================

let fun_a () = print_endline "A: FooBar";
               print_endline "A: Baz"

let fun_b () =
               let () = print_endline "B: FooBar" in
               let () = print_endline "B: Baz" in
               ()



let () =
   fun_a();
   fun_b()


(* or this one:, of course:

let () =
   let () = fun_a() in
   let () = fun_b() in
   ()

*)

========================================================================



Ciao,
   Oliver

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: [Caml-list] Re: functional or imperative feature ?
  2013-07-22 11:42   ` oliver
@ 2013-07-22 11:46     ` David Allsopp
  2013-07-22 14:23       ` oliver
  2013-07-22 11:51     ` Wojciech Meyer
  1 sibling, 1 reply; 12+ messages in thread
From: David Allsopp @ 2013-07-22 11:46 UTC (permalink / raw)
  To: caml-list

oliver wrote:
> Maybe it can be called just syntactical sugar...

Yes...

> ========================================================================
> 
> let fun_a () = print_endline "A: FooBar";
>                print_endline "A: Baz"
> 
> let fun_b () =
>                let () = print_endline "B: FooBar" in
>                let () = print_endline "B: Baz" in
>                ()

... but not for this translation! Consider

# 4; 3;;
Characters 0-1:
  4; 3;;
  ^
Warning 10: this expression should have type unit.
- : int = 3

By your translation, you'd have:

# let () = 4 in
  3;;
Characters 9-10:
  let () = 4 in
           ^
Error: This expression has type int but an expression was expected of type
         unit


David

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Re: functional or imperative feature ?
  2013-07-22 11:42   ` oliver
  2013-07-22 11:46     ` David Allsopp
@ 2013-07-22 11:51     ` Wojciech Meyer
  1 sibling, 0 replies; 12+ messages in thread
From: Wojciech Meyer @ 2013-07-22 11:51 UTC (permalink / raw)
  To: oliver; +Cc: Christopher Zimmermann, Caml List

[-- Attachment #1: Type: text/plain, Size: 1297 bytes --]

Moreover we can translate lets to lambdas:

let () = print_endline "Foo" in
let () = print_endline "Bar" in
let () = print_endline "Baz" in
   ()

(fun () -> (fun () -> print_endline "Baz") (print_endline "Bar"))
(print_endline "Foo")

Further transformation to use bind instead of function application and
wrapping the carried value into a type, leads to monads.

On Mon, Jul 22, 2013 at 12:42 PM, oliver <oliver@first.in-berlin.de> wrote:

> Maybe it can be called just syntactical sugar...
>
>
> ========================================================================
>
> let fun_a () = print_endline "A: FooBar";
>                print_endline "A: Baz"
>
> let fun_b () =
>                let () = print_endline "B: FooBar" in
>                let () = print_endline "B: Baz" in
>                ()
>
>
>
> let () =
>    fun_a();
>    fun_b()
>
>
> (* or this one:, of course:
>
> let () =
>    let () = fun_a() in
>    let () = fun_b() in
>    ()
>
> *)
>
> ========================================================================
>
>
>
> Ciao,
>    Oliver
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

[-- Attachment #2: Type: text/html, Size: 2257 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] functional or imperative feature ?
  2013-07-22 11:20 ` Jean-Marc Alliot
@ 2013-07-22 12:05   ` r.3
  0 siblings, 0 replies; 12+ messages in thread
From: r.3 @ 2013-07-22 12:05 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 37 bytes --]

habet, are you still following :-) 


[-- Attachment #2: Type: text/html, Size: 259 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Re: functional or imperative feature ?
  2013-07-22 11:46     ` David Allsopp
@ 2013-07-22 14:23       ` oliver
  0 siblings, 0 replies; 12+ messages in thread
From: oliver @ 2013-07-22 14:23 UTC (permalink / raw)
  To: David Allsopp; +Cc: caml-list

On Mon, Jul 22, 2013 at 11:46:55AM +0000, David Allsopp wrote:
> oliver wrote:
> > Maybe it can be called just syntactical sugar...
> 
> Yes...
> 
> > ========================================================================
> > 
> > let fun_a () = print_endline "A: FooBar";
> >                print_endline "A: Baz"
> > 
> > let fun_b () =
> >                let () = print_endline "B: FooBar" in
> >                let () = print_endline "B: Baz" in
> >                ()
> 
> ... but not for this translation! Consider
> 
> # 4; 3;;
> Characters 0-1:
>   4; 3;;
>   ^
> Warning 10: this expression should have type unit.
> - : int = 3
> 
> By your translation, you'd have:
> 
> # let () = 4 in
>   3;;
> Characters 9-10:
>   let () = 4 in
>            ^


let _ = 4 in _ = 4 in ();;

The unit-problem comes from let () = ...
                                ^^ 
                                  \
                                 unit


Ciao,
   Oliver

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] functional or imperative feature ?
  2013-07-21 20:50 [Caml-list] functional or imperative feature ? habet_ms
                   ` (2 preceding siblings ...)
  2013-07-22 11:20 ` Jean-Marc Alliot
@ 2013-07-22 14:59 ` Kristopher Micinski
  2013-07-23  0:16   ` oleg
  3 siblings, 1 reply; 12+ messages in thread
From: Kristopher Micinski @ 2013-07-22 14:59 UTC (permalink / raw)
  To: habet_ms; +Cc: caml-list

As others have mentioned, you can desugar sequenced statements to
lambdas, but that doesn't make it functional (in the pure sense) per
se.

The purest way to view it is that after desugaring, sequenced
statements turn into lambdas, which turn into (morally) binds from the
IO monad.  The observation is that when writing in ML, you are
implicitly writing in the IO monad.

Kris


On Sun, Jul 21, 2013 at 4:50 PM,  <habet_ms@yahoo.fr> wrote:
> Hi,
>   are sequencing (;) and block structure (begin ... end) functional features
>   or imperative ones ?
>
> Thanks,
>
> M.S
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] functional or imperative feature ?
  2013-07-22 14:59 ` Kristopher Micinski
@ 2013-07-23  0:16   ` oleg
  2013-07-23  2:15     ` Kristopher Micinski
  0 siblings, 1 reply; 12+ messages in thread
From: oleg @ 2013-07-23  0:16 UTC (permalink / raw)
  To: krismicinski; +Cc: caml-list, habet_ms


> The purest way to view it is that after desugaring, sequenced
> statements turn into lambdas, which turn into (morally) binds from the
> IO monad.  The observation is that when writing in ML, you are
> implicitly writing in the IO monad.

Peter Landin has described this process already in 1965. See the exact
quotation from his paper (the ()-passing trick) in
        http://okmij.org/ftp/Computation/IO-monad-history.html
(as well a few comments). That Peter Landin's paper also described
State and Writer monads, streams and delayed evaluation and even
stream fusion. 


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] functional or imperative feature ?
  2013-07-23  0:16   ` oleg
@ 2013-07-23  2:15     ` Kristopher Micinski
  0 siblings, 0 replies; 12+ messages in thread
From: Kristopher Micinski @ 2013-07-23  2:15 UTC (permalink / raw)
  To: oleg; +Cc: caml-list, habet_ms

[-- Attachment #1: Type: text/plain, Size: 1176 bytes --]

Thanks for pointing out this exposition, Oleg.  I had never thought of the
connection between monads and Landin's earlier paper.  However, I do
remember Appel's CPS book saying something to the effect of lambda being
the prototypical let, and this was further described to me by Michael Hicks
when he was doing his work on monadic programming for ML: in which ML is
"lifted" to the proper monad for the job.

http://research.microsoft.com/apps/pubs/?id=151802

Interesting history however, your pages are quite useful!


Kris
On Jul 22, 2013 8:16 PM, <oleg@okmij.org> wrote:

>
> > The purest way to view it is that after desugaring, sequenced
> > statements turn into lambdas, which turn into (morally) binds from the
> > IO monad.  The observation is that when writing in ML, you are
> > implicitly writing in the IO monad.
>
> Peter Landin has described this process already in 1965. See the exact
> quotation from his paper (the ()-passing trick) in
>         http://okmij.org/ftp/Computation/IO-monad-history.html
> (as well a few comments). That Peter Landin's paper also described
> State and Writer monads, streams and delayed evaluation and even
> stream fusion.
>
>

[-- Attachment #2: Type: text/html, Size: 1759 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2013-07-23  2:15 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-21 20:50 [Caml-list] functional or imperative feature ? habet_ms
2013-07-21 20:57 ` [Caml-list] " Christopher Zimmermann
2013-07-22 11:42   ` oliver
2013-07-22 11:46     ` David Allsopp
2013-07-22 14:23       ` oliver
2013-07-22 11:51     ` Wojciech Meyer
2013-07-21 20:58 ` [Caml-list] " David Allsopp
2013-07-22 11:20 ` Jean-Marc Alliot
2013-07-22 12:05   ` r.3
2013-07-22 14:59 ` Kristopher Micinski
2013-07-23  0:16   ` oleg
2013-07-23  2:15     ` Kristopher Micinski

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