Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* RE: Language Design
@ 2000-09-01 11:57 Dave Berry
  2000-09-01 17:48 ` Markus Mottl
  2000-09-01 19:12 ` Marcin 'Qrczak' Kowalczyk
  0 siblings, 2 replies; 13+ messages in thread
From: Dave Berry @ 2000-09-01 11:57 UTC (permalink / raw)
  To: caml-list

Given a value in a monad, e.g. IO v, how can I remove v from the Monad?
Surely this would be required to seamlessly integrate stateful and
functional code?

Dave the ingenue.


-----Original Message-----
From: Jerome Vouillon [mailto:Jerome.Vouillon@inria.fr]
Sent: Friday, August 25, 2000 4:42 PM
To: John Max Skaller; Francois.Pottier@inria.fr
Cc: caml-list@inria.fr
Subject: Re: Language Design


On Fri, Aug 25, 2000 at 06:16:06AM +1000, John Max Skaller wrote:
> Francois Pottier wrote:
>  
> > On Wed, Aug 23, 2000 at 03:55:36PM +1000, John Max Skaller wrote:
> > > What is _actually_ required is a seamless way to integrate
> > > stateful and function code:
> > 
> > Have you thought about employing some kind of monadic type system?
> 
> 	Yes, but I don't know enough to do it at the moment.
> [Also, it turns out monads are not general enough to write
> web services in, which puts me off a bit]

I think you should really consider using monads. Here is an example.

We define a value of type void to be either a continuation expecting a
string or a final function that do not expect anything.

    type void = Cont of (string -> void)
              | Term of (unit -> unit)

This is a procedure that does nothing.

    let unit : void = Term (fun () -> ())

And here is a possible implementation for read: the input string is
assigned to v and then there is nothing more to do.

    let read (v : string ref) : void = Cont (fun s -> v := s; unit)

The following combinator takes two procedures p and p' and make them
be evaluated in order.

    let rec seq (p : void) (p': void) : void =
      match p, p' with
        Cont c, _ ->
          Cont (fun s -> seq (c s) p')
      | Term t, Cont c ->
          Cont (fun s -> t (); c s)
      | Term t, Term t' ->
          Term (fun () -> t (); t' ())

Finally, we have an operator to assign a value v to a reference x.

    let set (x : 'a ref) (v : unit -> 'a) : void = Term (fun () -> x := v
())

Now we can for instance define a procedure that reads to strings and
returns their concatenation. This procedure does not need to know the
actual definition of type void.

    let read2 x =
      let a = ref "" in let b = ref "" in
      seq (read a) (seq (read b) (set x (fun () -> !a ^ !b)))

You can also use some symbols to make it more readable:

    let ($) = seq
    let (-<-) = set
    let read2 x =
      let a = ref "" in
      let b = ref "" in
      read a $
      read b $
      x -<- (fun () -> !a ^ !b)

We can try this procedure. First we define an evaluator. It takes the
input stream and a procedure call as inputs.

    let rec eval l p =
      match l, p with
        _,      Term t -> t ()
      | s :: r, Cont c -> eval r (c s)
      | _              -> ((* Stuck evaluation *))

Then we evaluate read2 when two strings "a" and "b" are given as
input:

  let x = ref "" in eval ["a"; "b"] (read2 x); !x

-- Jerome



^ permalink raw reply	[flat|nested] 13+ messages in thread
* Language Design
@ 2000-08-21 21:44 David McClain
  2000-08-23  5:55 ` John Max Skaller
  0 siblings, 1 reply; 13+ messages in thread
From: David McClain @ 2000-08-21 21:44 UTC (permalink / raw)
  To: caml-list

John Max Skaller said:
> I am designing a programming language (the compiler is written
in ocaml) which is a procedural language with 'purely functional'
expressions (using eager evaluation). Function closures can
access their context, which procedural statements my change between
building the closure and evaulating it. Procedural closures may
mutate their environment.

DM says:

I, for one, have fought for many years with languages that insisted on a
division between functions and procedures as you describe them. I have found
the unified "everything is a function" approach to be most appealing. In
particular, the worst offenders are those languages that insist on syntactic
distinctions such as Fortran, RSI/IDL, and Basic. I cannot be alone in
having difficulty remembering when a routine, whose result I don't really
need, is to be called as a function, or as a procedure.

I hope you find an answer to your question, but I do not look forward to
another such language.

Sincerely

David McClain, Sr. Scientist, Raytheon Systems Co., Tucson, AZ




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

end of thread, other threads:[~2000-09-06 19:42 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-09-01 11:57 Language Design Dave Berry
2000-09-01 17:48 ` Markus Mottl
2000-09-01 19:12 ` Marcin 'Qrczak' Kowalczyk
     [not found]   ` <39B5DD81.E2500203@maxtal.com.au>
2000-09-06  6:33     ` Marcin 'Qrczak' Kowalczyk
  -- strict thread matches above, loose matches on Subject: below --
2000-08-21 21:44 David McClain
2000-08-23  5:55 ` John Max Skaller
2000-08-24  9:12   ` Francois Pottier
2000-08-24 20:16     ` John Max Skaller
2000-08-25  9:52       ` Andreas Rossberg
2000-08-27 22:00         ` John Max Skaller
2000-08-28 23:11           ` Daan Leijen
2000-08-25 15:41       ` Jerome Vouillon
2000-08-27 22:21         ` John Max Skaller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox