From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ian Grant Newsgroups: gmane.lisp.guile.devel Subject: Re: implementation idea for infinite cons lists aka scon(e)s lists. Date: Fri, 12 Sep 2014 21:22:19 -0400 Message-ID: References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a11c266a89c236e0502e83ba6 X-Trace: ger.gmane.org 1410571358 9703 80.91.229.3 (13 Sep 2014 01:22:38 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 13 Sep 2014 01:22:38 +0000 (UTC) To: stefan.itampe@gmail.com, guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sat Sep 13 03:22:34 2014 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1XSc2l-0001J9-J7 for guile-devel@m.gmane.org; Sat, 13 Sep 2014 03:22:27 +0200 Original-Received: from localhost ([::1]:47951 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XSc2l-0005VL-5P for guile-devel@m.gmane.org; Fri, 12 Sep 2014 21:22:27 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47790) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XSc2g-0005Tk-RA for guile-devel@gnu.org; Fri, 12 Sep 2014 21:22:24 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XSc2e-00088o-Gq for guile-devel@gnu.org; Fri, 12 Sep 2014 21:22:22 -0400 Original-Received: from mail-we0-x233.google.com ([2a00:1450:400c:c03::233]:55192) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XSc2e-00088i-5J for guile-devel@gnu.org; Fri, 12 Sep 2014 21:22:20 -0400 Original-Received: by mail-we0-f179.google.com with SMTP id u56so1524636wes.38 for ; Fri, 12 Sep 2014 18:22:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=AJKLSwA0c1G/hW7hflE+O2vh1neXXKj65vRqomIRtYY=; b=P99wjC5Hmf+84aGqcwOKeozicEJ1dGEHivOZ38vDfwaXaiDsm2gPLVbxulIyuysnok F3pVch5weI9VsHTZK5Yry/wfOluqiv82OF9pltxMt/PpCM+oDR5JrLUNCaV8sLxwHgU3 GGO7cyyRDmqDn4uzvioS7w2dWjclEAMdSOq0q4Btrf8hEjopciZa/rLto3iWOnyg0XRf qFWYviiHTynGq+QxEHOmLTXyUgJjgq9ZTRmbWa+AoENyZNohZU4fNdonyot7OAsEqQs5 mTymIxyPFTs0tRYIVBTKvOxaqDJBFB7LICNSu4rIjHOL4i+96aSqqXxMU/ZukfFtrk68 Lhww== X-Received: by 10.180.187.144 with SMTP id fs16mr6563726wic.75.1410571339308; Fri, 12 Sep 2014 18:22:19 -0700 (PDT) Original-Received: by 10.194.43.69 with HTTP; Fri, 12 Sep 2014 18:22:19 -0700 (PDT) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c03::233 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:17442 Archived-At: --001a11c266a89c236e0502e83ba6 Content-Type: text/plain; charset=UTF-8 On the c-lambda page, you write: "In a sense C is simplistic and in many ways a glorified assembler but I actually like it." I first learned C quite a while ago. And up until three months ago, I would have agreed with this. But then I started using assembler in a way that I'd never used it before: I had written a bunch of C functions to interface lightning and libffi with Moscow ML. And it was hard work. The CAML byte-code interpreter uses a similar sort of cell encoding scheme, folding longs and pointers together, and passing 'vectors' of pointers around, often several deep. It's really easy to make a mistake with the casts and end up de-referencing a pointer to a pointer to a string reference, say, too many or too few times. Once I had the lightning primitives going, though, I found it got a lot easier. And to my genuine surprise I found the assembler easier to write and easier to understand than the C had been. And there was another benefit that wasn't so surprising, because I think it was one of the reasons why I was doing this: the assembler was much easier to generalise. It was almost trivial to compose assembler-generating ML functions together, and the result was that the fragments of assembler were automatically stacked up to implement, for me, quite complicated C function bindings for ML. But because the fragments of code were all being composed mechanically, they either worked first time (most of the time) or they failed every time. I have had very, very few of those bugs that show up once in a blue moon. One was, I think, a fairly predictable case of some SCM pointers that guile's GC wasn't protecting, because they were only referenced from CAML bytecode environments And another was, I think, a bug in my C code implementing off-heap malloc'ed buffers for ML functions. And there haven't been any others. But this is hundreds, if not thousands of 'lines' of assembler, because I use the same pieces of code over and over again, in different contexts. You might be interested to try it out. I have made some examples of how to do the Guile bindings for lightning, and you won't have any problem at all generating the scheme code to implement the whole lot. There are about 350 functions to bind, so it needs to be done programmatically, but I have set up an ML list which will translate trivially to a scheme s-exp to implement the definitions. And there are some examples of lightning code to access and create SCMs here: https://github.com/IanANGrant/red-october/blob/master/src/dynlibs/ffi/testguile.sml The example of multiple-use starts around line 450. I posted the example.scm.gz as an attachment to this message: http://lists.gnu.org/archive/html/guile-devel/2014-09/msg00042.html If you are interested, then I would like to ask you about how or if it might be possible to use Prolog to solve some logical equations and do automatic register allocation once a whole lot of assembler fragments have been stacked up. If you try it, you will see that it is easy to assign registers as scheme parameters and fix them in the caller, but there are some compromises one has to make, which are less than 100"% efficient. And I think maybe a bit of AI would be able to make them dynamically and according to context, which is definitely not what we would want to do manually, because it would destroy the simplicity and reliability of function composition for 'stacking fragments.' The manually specified steps have to be very uniform and regular, because we want the results to be _very_ reliable. Happy hacking. On Fri, Sep 12, 2014 at 4:08 PM, Ian Grant wrote: > > #| > > Basically a stream is > > [x,y,z,...] > > > But we want to gc the tail of the stream if not reachable. How to do > this? > > I don't understand. The tail is infinitely long. When do you want to GC > it? When your infinite memory is 50% full, or 75% full :-) > > I think you probably have a good idea, but it's just not at all clear from > these two messages. > > Do you know about co-induction and co-data? An ordinary (proper) list is > an example of an inductive structure: you have two constructors, a nullary > one '() which is like a constant or a constant function, it takes no > arguments and makes a list out of nothing. And you have a binary > constructor, cons, which takes a head a tail as arguments and makes a new > list. And then you can use these a bit like an induction proof in > mathematics: '() is the base case, and cons is the induction step which > takes you from a list, to a new longer list. This is a concrete datatype: > the elements it is made of are all represented in memory. > > The dual of this idea is a co-datatype like a stream, where you don't have > the concrete data structures anymore, you have what is called an _abstract > datatype_ which is a datatype that has no actual representation in the > machine: so you don't have the constructors '() and cons anymore, you just > have a single deconstructor, snoc, which, when it is applied to a stream, > maybe returns an element and a new stream, which is the tail, otherwise it > just returns something like #f which says "it ended!" In languages like > scheme and standard ML which do eager evaluation, streams are modelled > using either references (i.e. mutable cons cells) or eta-expansions > (thunks, (lambda () ...) with a 'unit' argument to delay evaluation), but > in lazy languages like haskell (and untyped lambda calculus under normal > order evaluation) you don't need any tricks, you can just write the co-data > types as ordinary lambda expressions, and the 'call by name' semantics mean > that these 'infinite tails' only get expanded (i.e. represented in the > memory) when they are 'observed' by applying the deconstructor. So like in > real life, the only garbage you have to deal with is the stuff that results > from what you make: the whole infinite substructure is all 'enfolded' > underneath and takes no space at all. It's just your observing it that > makes it concrete. > > There is a huge body of theory and an awful lot of scribbling been done > about this. There are mathematical texts where it's called 'category > theory' or 'non well-founded set theory' And it comes up in order theory as > 'fixedpoint calculus' and the theory of Galois connections. And in other > areas of computer science it's called bisimulation. To me it all seems to > be the same thing: "consing up one list while cdr'ing down another", but > there's probably no research mileage in saying things like that. > > Ian > > --001a11c266a89c236e0502e83ba6 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On the c-lambda page, you write:

=C2=A0=C2=A0 "= ;In a sense C is simplistic and in many ways a glorified
=C2=A0 =C2=A0 a= ssembler but I actually like it."

I first learned C quite a wh= ile ago. And up until three months ago, I would have agreed with this. But = then I started using assembler in a way that I'd never used it before: = I had written a bunch of C functions to interface lightning and libffi with= Moscow ML. And it was hard work. The CAML byte-code interpreter uses a sim= ilar sort of cell encoding scheme, folding longs and pointers together, and= passing 'vectors' of pointers around, often several deep. It's= really easy to make a mistake with the casts and end up de-referencing a p= ointer to a pointer to a string reference, say, too many or too few times.<= br>
Once I had the lightning primitives going, though, I foun= d it got a lot easier. And to my genuine surprise I found the assembler eas= ier to write and easier to understand than the C had been. And there was an= other benefit that wasn't so surprising, because I think it was one of = the reasons why I was doing this: the assembler was much easier to generali= se. It was almost trivial to compose assembler-generating ML functions toge= ther, and the result was that the fragments of assembler were automatically= stacked up to implement, for me, quite complicated C function bindings for= ML. But because the fragments of code were all being composed mechanically= , they either worked first time (most of the time) or they failed every tim= e. I have had very, very few of those bugs that show up once in a blue moon= . One was, I think, a fairly predictable case of some SCM pointers that gui= le's GC wasn't protecting, because they were only referenced from C= AML bytecode environments And another was, I think, a bug in my C code impl= ementing off-heap malloc'ed buffers for ML functions. And there haven&#= 39;t been any others. But this is hundreds, if not thousands of 'lines&= #39; of assembler, because I use the same pieces of code over and over agai= n, in different contexts.

You might be interested to try= it out. I have made some examples of how to do the Guile bindings for ligh= tning, and you won't have any problem at all generating the scheme code= to implement the whole lot. There are about 350 functions to bind, so it n= eeds to be done programmatically, but I have set up an ML list which will t= ranslate trivially to a scheme s-exp to implement the definitions. And ther= e are some examples of lightning code to access and create SCMs here:
=C2=A0=C2=A0 https://github.com/Ia= nANGrant/red-october/blob/master/src/dynlibs/ffi/testguile.sml

<= /div>
The example of multiple-use starts around line 450.

I posted the example.scm.gz as an attachment to this message:

= =C2=A0 http://lists.gnu.org/archive/html/guile-devel= /2014-09/msg00042.html

If you are interes= ted, then I would like to ask you about how or if it might be possible to u= se Prolog to solve some logical equations and do automatic register allocat= ion once a whole lot of assembler fragments have been stacked up. If you tr= y it, you will see that it is easy to assign registers as scheme parameters= and fix them in the caller, but there are some compromises one has to make= , which are less than 100"% efficient. And I think maybe a bit of AI w= ould be able to make them dynamically and according to context, which is de= finitely not what we would want to do manually, because it would destroy th= e simplicity and reliability of function composition for 'stacking frag= ments.' The manually specified steps have to be very uniform and regula= r, because we want the results to be _very_ reliable.
=C2=A0
Happy hacking.


<= div class=3D"gmail_quote">On Fri, Sep 12, 2014 at 4:08 PM, Ian Grant <ian.a.n.grant@googlemail.com> wrote:
> #|
> Basically a s= tream is
> [x,y,z,...]

> But we wa= nt to gc the tail of the stream if not reachable. How to do this?

I don't understand. The tail is infinitely long. When d= o you want to GC it? When your infinite memory is 50% full, or 75% full :-)=

I think you probably have a good idea, but it's just= not at all clear from these two messages.

Do you know ab= out co-induction and co-data? An ordinary (proper) list is an example of an= inductive structure: you have two constructors, a nullary one '() whic= h is like a constant or a constant function, it takes no arguments and make= s a list out of nothing. And you have a binary constructor, cons, which tak= es a head a tail as arguments and makes a new list. And then you can use th= ese a bit like an induction proof in mathematics: '() is the base case,= and cons is the induction step which takes you from a list, to a new longe= r list. This is a concrete datatype: the elements it is made of are all rep= resented in memory.

The dual of this idea is a co-datatyp= e like a stream, where you don't have the concrete data structures anym= ore, you have what is called an _abstract datatype_ which is a datatype tha= t has no actual representation in the machine: so you don't have the co= nstructors '() and cons anymore, you just have a single deconstructor, = snoc, which, when it is applied to a stream, maybe returns an element and a= new stream, which is the tail, otherwise it just returns something like #f= which says "it ended!" In languages like scheme and standard ML = which do eager evaluation, streams are modelled using either references (i.= e. mutable cons cells) or eta-expansions (thunks, (lambda () ...) with a &#= 39;unit' argument to delay evaluation), but in lazy languages like=C2= =A0 haskell (and untyped lambda calculus under normal order evaluation) you= don't need any tricks, you can just write the co-data types as ordinar= y lambda expressions, and the 'call by name' semantics mean that th= ese 'infinite tails' only get expanded (i.e. represented in the mem= ory) when they are 'observed' by applying the deconstructor. So lik= e in real life, the only garbage you have to deal with is the stuff that re= sults from what you make: the whole infinite substructure is all 'enfol= ded' underneath and takes no space at all. It's just your observing= it that makes it concrete.

There is a huge body of theor= y and an awful lot of scribbling been done about this. There are mathematic= al texts where it's called 'category theory' or 'non well-f= ounded set theory' And it comes up in order theory as 'fixedpoint c= alculus' and the theory of Galois connections. And in other areas of co= mputer science it's called bisimulation. To me it all seems to be the s= ame thing: "consing up one list while cdr'ing down another", = but there's probably no research mileage in saying things like that.

Ian


--001a11c266a89c236e0502e83ba6--