caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Arkady Andrukonis <grazingcows@yahoo.com>
To: "oleg@okmij.org" <oleg@okmij.org>,
	"bpientka@cs.mcgill.ca" <bpientka@cs.mcgill.ca>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] recursive records
Date: Wed, 13 Nov 2013 00:01:24 -0800 (PST)	[thread overview]
Message-ID: <1384329684.46964.YahooMailNeo@web163403.mail.gq1.yahoo.com> (raw)
In-Reply-To: <20131109040201.50663.qmail@www1.g3.pair.com>

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

The "short" answer is the definition of 'delay' does not have a type associativity or bindings. For a recursive definition to work well, you would need to resolve into primitive types. When you try to accumulate a definition introducing more layers of complexity, one thing is just like this other (example, a candle is like a wax object with a wick), we see this every day with compound "of" metaphors, the type engine gets confused. You are using a constructor to define other items, therefore it fails. In natural languages you can get away with it, but not in programming languages.

Hope this helps.

Arkady





On Friday, November 8, 2013 11:02 PM, "oleg@okmij.org" <oleg@okmij.org> wrote:
 

Brigitte Pientka wrote:

> type 'a susp = Susp of (unit -> 'a)
>
> type 'a str = {hd: 'a  ; tl : ('a str) susp}
>
> let rec ones = {hd = 1 ; tl = Susp (fun () -> ones)}
>
> This works fine and many examples can be elegantly written this way.  However,
> when I define the stream ones via the function delay, OCaml fails.
>
>   let delay f = Susp f
>    let rec ones = {hd = 1 ; tl = delay (fun () -> ones)};;
>                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> Error: This kind of expression is not allowed as right-hand side of `let rec'
>
> Could someone explain why this fails?

To be fair, OCaml already provides a few relaxations for let rec
definitions, which are described in Sec 7.3 of
        http://caml.inria.fr/pub/docs/manual-ocaml/extn.html
Alain Frisch has demonstrated one such extension: lazy.

I believe though there is a better way to represent your co-patterns
in OCaml -- using objects. Objects are already have a fix-points in
them, and objects are naturally coinductive: an object receives a
message, changes its state and results in an object ready for more
messages. The only think we can do with objects is to observe them,
but sending them messages. This sounds just like the definition of
co-induction. Here how it looks like:

(* defining the class type is not necessary, but helpful.
   It defines the type 'a str that is useful when writing signatures.
   I like to write signatures
*)
class type ['a] str = object ('self)
        method hd : 'a
        method tl : 'self
end;;

class ones = object (self) 
  method hd = 1
  method tl = self
end
;;

let ones = new ones;;

let take : int -> 'a str -> 'a list = fun n str ->
let rec loop acc str = function
   | n when n <= 0 -> List.rev acc
   | n -> loop (str#hd :: acc) (str#tl) (n-1) (* Co-patterns! str#hd and str#tl *)
in loop [] str n
;;

take 5 ones;;
   - : int list = [1; 1; 1; 1; 1]



-- 
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: 5151 bytes --]

      reply	other threads:[~2013-11-13  8:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-08 20:10 Brigitte Pientka
2013-11-08 20:17 ` Andreas Rossberg
2013-11-08 21:56   ` Alain Frisch
2013-11-09  4:02 ` oleg
2013-11-13  8:01   ` Arkady Andrukonis [this message]

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=1384329684.46964.YahooMailNeo@web163403.mail.gq1.yahoo.com \
    --to=grazingcows@yahoo.com \
    --cc=bpientka@cs.mcgill.ca \
    --cc=caml-list@inria.fr \
    --cc=oleg@okmij.org \
    /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).