hashcons.scm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;;; cons-unique (aka hashcons)
  21. ;;; Apparently invented by Ershov (CACM 1, 8, August 1958, pp. 3--6)
  22. ;;; Re-introduced by E.Goto in 1974.
  23. ;;; Current version by GJS and Taylor Campbell 2010, improved by
  24. ;;; Taylor Campbell in 2011.
  25. (declare (usual-integrations))
  26. ;;; Given two arguments cons-unique returns a pair. If exactly the
  27. ;;; same two arguments were previously combined with cons-unique it
  28. ;;; returns the same pair it returned the first time.
  29. #|
  30. ;;; Test for correctness. This should never return.
  31. (let lp ((t (list 'a (cons 'b 3) 'c)))
  32. (let ((c1 (canonical-copy t)) (c2 (canonical-copy t)))
  33. (if (not (and (equal? t c1) (equal? t c2) (eq? c1 c2)))
  34. `(lose (,(hash c1) ,c1) (,(hash c2) ,c2))
  35. (lp (list 'a (cons 'b 3) 'c)))))
  36. ;Value: (lose (21 (a (b . 3) c)) (20 (a (b . 3) c)))
  37. ;;; This is what a loss looks like.
  38. |#
  39. (define cons-unique
  40. ;; I don't want to cons if unnecessary.
  41. (let ((the-test-pair (cons #f #f)))
  42. (define (generator) (weak-cons the-test-pair #f))
  43. (define (hashcons x y)
  44. (set-car! the-test-pair x)
  45. (set-cdr! the-test-pair y)
  46. (let ((weak-pair
  47. (hash-table/intern! the-cons-table
  48. the-test-pair
  49. generator)))
  50. (let ((the-canonical-pair (weak-car weak-pair)))
  51. (cond ((eq? the-canonical-pair the-test-pair)
  52. ;; test pair used, make a new one
  53. (set! the-test-pair (cons #f #f)))
  54. (else
  55. ;; clear test pair for next try.
  56. (set-car! the-test-pair #f)
  57. (set-cdr! the-test-pair #f)))
  58. the-canonical-pair)))
  59. hashcons))
  60. (define hash-cons cons-unique)
  61. ;;; Support for the hashcons system.
  62. (define (pair-eqv? u v)
  63. (and (eqv? (car u) (car v))
  64. (eqv? (cdr u) (cdr v))))
  65. ;; FBE: make second parameter optional and ignore it. The R6RS hash
  66. ;; procedures do not need a modulus parameter.
  67. ;; (define (pair-eqv-hash-mod key modulus)
  68. ;; (fix:remainder
  69. ;; (fix:xor (eqv-hash-mod (car key) modulus)
  70. ;; (eqv-hash-mod (cdr key) modulus))
  71. ;; modulus))
  72. (define* (pair-eqv-hash-mod key #:optional modulus)
  73. (fix:xor (eqv-hash-mod (car key) modulus)
  74. (eqv-hash-mod (cdr key) modulus)))
  75. (define the-cons-table
  76. ((weak-hash-table/constructor pair-eqv-hash-mod
  77. pair-eqv?
  78. #t)))
  79. ;;; Given a list structure, to get a canonical copy equal to the given
  80. ;;; list structure. Must canonicalize and share all substructure.
  81. (define (canonical-copy x)
  82. (define (recurse)
  83. (cons-unique (canonical-copy (car x))
  84. (canonical-copy (cdr x))))
  85. (if (pair? x)
  86. (let ((seen ; already canonical?
  87. (hash-table/get the-cons-table x #f)))
  88. (if seen
  89. (or (weak-car seen)
  90. (and (not (weak-pair/car? seen))
  91. (recurse)))
  92. (recurse)))
  93. x))
  94. (define (map-unique p lst)
  95. (if (pair? lst)
  96. (cons-unique (p (car lst))
  97. (map-unique p (cdr lst)))
  98. lst))
  99. #|
  100. ;;; For example...
  101. (define foo
  102. '(define (canonical-copy x)
  103. (if (pair? x)
  104. (let ((canonical-pair
  105. (hash-table/get the-cons-table x #f)))
  106. (or canonical-pair
  107. (let ((new
  108. (cons (canonical-copy (car x))
  109. (canonical-copy (cdr x)))))
  110. (hash-table/put! the-cons-table new new)
  111. new)))
  112. x)))
  113. (define bar
  114. '(define cons-unique
  115. ;; I don't want to cons if unnecessary.
  116. (let ((the-pair (cons #f #f)))
  117. (define (hashcons x y)
  118. (set-car! the-pair x)
  119. (set-cdr! the-pair y)
  120. (let ((canonical-pair
  121. (hash-table/get the-cons-table the-pair #f)))
  122. (or canonical-pair
  123. (let ((new the-pair))
  124. (hash-table/put! the-cons-table new new)
  125. (set! the-pair (cons #f #f))
  126. new))))
  127. hashcons)))
  128. (define cfoo
  129. (canonical-copy foo))
  130. ;Value: cfoo
  131. (eq? cfoo (canonical-copy foo))
  132. ;Value: #t
  133. (define cbar (canonical-copy bar))
  134. ;Value: cbar
  135. (define baz (caddr (caddr (caddr (caddr (caddr cfoo))))))
  136. ;Value: baz
  137. baz
  138. ;Value: (hash-table/put! the-cons-table new new)
  139. (define mum (caddr (caddr (caddr (car (cddddr (caddr (caddr cbar))))))))
  140. ;Value: mum
  141. mum
  142. ;Value: (hash-table/put! the-cons-table new new)
  143. (eq? baz mum)
  144. ;Value: #t
  145. |#