A possibly working implementation of weak pairs added by Bunny351 on Sun May 28 14:29:18 2023

(import (chicken locative))
(import (chicken gc))

(define-record weak-pair car cdr)

(define (weak-cons x y)
  (make-weak-pair (make-weak-locative (vector x)) y))

(define (weak-car x)
  (let* ((w (weak-pair-car x))
         (o (locative->object w)))
    (and o (vector-ref o 0))))

(define (weak-pair/car? x)
  (let* ((w (weak-pair-car x))
         (o (locative->object w)))
    (and o #t)))

(define (weak-cdr x) (weak-pair-cdr x))

(define (weak-set-car! wp x)
  (let ((w (weak-pair-car x)))
    (if w 
        (locative-set! w x)
        (weak-pair-car-set! wp (make-weak-locative x)))))

(define (weak-set-cdr! wp x)
  (weak-pair-cdr-set! wp x))