unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* implementation idea for infinite cons lists aka scon(e)s lists.
@ 2014-09-10 20:10 Stefan Israelsson Tampe
  0 siblings, 0 replies; 7+ messages in thread
From: Stefan Israelsson Tampe @ 2014-09-10 20:10 UTC (permalink / raw)
  To: guile-devel, guile-user@gnu.org

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

#|
Basically a stream is
[x,y,z,...]

But we want to gc the tail of the stream if not reachable. How to do this?

Well consider a cons cell of
[tag,car,cdr]

and that the tag can be marked or not just as with the marking of
guile-log's
logical variables.

The technique to create persistant parts of the list is to just reference
the head of the part
under analysis. Then the tail of that is never reclaimed. This is a simple
version of gc-able
lists. Better versions might exists. But hey let's have fun ....
|#


;; This is a referetial structure, but it will not gc the end
(define id->data (make-weak-key-hash-table))

(define (new-cstream)
  (cons (cons 0 '()) (vector (cons 'cstream-id '()) '() '())))
(define (cstream-i       cs) (caar   cs))
(define (cstream-data    cs) (cdar  cs))
(define (cstream-id      cs) (vector-ref (cdr cs) 0))
(define (cstream-backlog cs) (vector-ref (cdr cs) 1))
(define (cstream-logback cs) (vector-ref (cdr cs) 2))
(define (update-cstream cs a b c d)
  (cons (cons a b) (vector (cstream-id cs) c d)))
(define (hash-new-cstream cs)
  (hashq-set! id->data (cstream-id cs) cs))

(define-syntax-rule (increment-cstream-count cstream val)
  (cons (cons (+ 1 (cstream-i cstream))
      (c-ref (scons val (c-unref (cstream-data cstream)))))
(cdr cstream)))

(define (update cstream val)
  (let ((i (cstream-i cstream)))
    (if (> i 30)
(let ((data    (c-ref (scons val (c-unref (cstream-data cstream)))))
      (backlog (cons data (cstream-backlog cstream)))
      (logback (cstream-logback cstream)))
  (hash-new-cstream
     (if (null? logback)
       (updtate-cstream cstream 0 data '() (cdr (reverse backlog)))
       (updtate-cstream cstream 0 data backlog (cdr logdata)))))
(increment-cstream-count cstream))))

(define (get-cstream-data cstream) (cstream-data cstream))

;; This is executed after the mark phase assuming the gc hack!
(define (sweep)
  (hash-for-each id->data
    (lambda (id cs)
      (let* ((data (cstream-data cs))
     (lb   (cstream-logback cs))
     (lb   (if (pair? lb) (car lb) '())))
(let lp ((d data))
  (if (eq? d lb)
      (let lp ((d d))
(if (and (pair? d) (marked-bit? d))
    (lp (cdr d))
    (if (pair? d)
(set-cdr! d '()))))
      (lp (cdr d))))))))

;; we have a WMARK procedure and a normal MARK procedure. WMARK will not set
;; the IS_MARKED bit of the containing scons, but that is what MARK will do
;; that is the normal marking procedure in the modded bdw-gc

;; c-ref makes a reference that is a box that will make sure that we WMARK
;; the scons list and c-unref will unbox the value

;; What is needed is special mark procedures
#|
Here is the schematic of the C mark procedures.
mark_scons(scm s)
{
 SCM tag = s[0];
 SCM x1  = s[1];
 SCM x2  = s[2];

 if(IS_MARKED(tag))
    MARK(x1)
    MARK(x2);
 else
    MARK(x1)
    WMARK(x2)
}

mark_ref(scm s)
{
 SCM tag = s[0]:
 SCM d   = s[1];
 WMARK(d);
}
|#

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

^ permalink raw reply	[flat|nested] 7+ messages in thread
* implementation idea for infinite cons lists aka scon(e)s lists.
@ 2014-09-12 20:08 Ian Grant
  2014-09-13  1:22 ` Ian Grant
  2014-09-13 11:13 ` Stefan Israelsson Tampe
  0 siblings, 2 replies; 7+ messages in thread
From: Ian Grant @ 2014-09-12 20:08 UTC (permalink / raw)
  To: stefan.itampe, guile-devel

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

> #|
> 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

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

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

end of thread, other threads:[~2014-09-15 18:38 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-09-10 20:10 implementation idea for infinite cons lists aka scon(e)s lists Stefan Israelsson Tampe
  -- strict thread matches above, loose matches on Subject: below --
2014-09-12 20:08 Ian Grant
2014-09-13  1:22 ` Ian Grant
2014-09-13 11:19   ` Stefan Israelsson Tampe
2014-09-15 14:37     ` Ian Grant
2014-09-13 11:13 ` Stefan Israelsson Tampe
2014-09-15 18:38   ` Ian Grant

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 read-only IMAP folder(s) and NNTP newsgroup(s).