caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: oleg@pobox.com
To: caml-list@inria.fr
Cc: Pietro.Abate@anu.edu.au
Subject: (continuation monad) type problem...
Date: Wed, 12 Jul 2006 01:40:56 -0700 (PDT)	[thread overview]
Message-ID: <20060712084056.EC61DAC97@Adric.metnet.fnmoc.navy.mil> (raw)


Pietro Abate wrote about the problem typing the continuation monad.
The problem is that we have to specifically keep in mind the answer
type. For example:

let id x = x;;

module CONT = struct
  (* type 'a m = {cont: 'w . ('a -> 'w) -> 'w } *)
  type ('w,'a) m = {cont: ('a -> 'w) -> 'w }
  let return x = {cont = fun k -> k x}
  let (>>=) m f = {cont = fun k -> m.cont (fun x -> (f x).cont k) }
  let reset e = {cont = fun k -> k (e.cont id)}
  let shift e = {cont = fun k -> 
    (e (fun v -> {cont = fun c -> c (k v)})).cont id}
  let run m = m.cont id
end;;

With these shift/reset in place, we can express call/cc (assuming that
the whole program is wrapped in reset)

let callcc1 proc = CONT.shift (fun f -> 
  CONT.(>>=) (proc (fun v -> CONT.shift (fun _ -> f v))) f);;

whose inferred type

val callcc1 : (('a -> ('b, 'c) CONT.m) -> ('b, 'a) CONT.m) -> ('b, 'a) CONT.m =
  <fun>

seems quite right. We can test as follows:

let test1 = let module M = struct
  open CONT
  let result = run (
    let proc k = (k 10) >>= (fun v -> return (v+100)) in
    reset ((callcc1 proc) >>= (fun v -> return (v + 5)))
  )
  end in M.result
;;
 (* ==> 15 *)

The lack of polymorphism may be acceptable at times. If not, we have
to explicitly introduce typed prompts -- as was first proposed by
Gunter, Remy and Riecke. You can see the implementation of that in

 http://pobox.com/~oleg/ftp/Computation/Continuations.html#caml-shift

(the second solution: cc-monad, which is fully OCaml). The latter is
included in the monadic notation for OCaml, recently announced on this
list. 

Pietro Abate's message also showed an attempt to mix the continuation
and backtracking monad. That seems puzzling: the continuation monad
can express any other monad that could be expressed at all in the
language (see Filinski, Representing Monads, POPL94). Thus, the
continuation monad alone is sufficient for backtracking (as well as
many other things).



             reply	other threads:[~2006-07-12  8:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-12  8:40 oleg [this message]
2006-07-13  7:08 ` Pietro Abate
2006-07-14 16:41   ` [Caml-list] " Jacques Carette
2006-07-20  1:22 oleg

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=20060712084056.EC61DAC97@Adric.metnet.fnmoc.navy.mil \
    --to=oleg@pobox.com \
    --cc=Pietro.Abate@anu.edu.au \
    --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).