home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / guile / 1.6 / ice-9 / poe.scm < prev    next >
Encoding:
Text File  |  2006-06-19  |  4.5 KB  |  147 lines

  1. ;;; installed-scm-file
  2.  
  3. ;;;;     Copyright (C) 1996, 2001, 2003, 2005 Free Software Foundation, Inc.
  4. ;;;; 
  5. ;;;; This program is free software; you can redistribute it and/or modify
  6. ;;;; it under the terms of the GNU General Public License as published by
  7. ;;;; the Free Software Foundation; either version 2, or (at your option)
  8. ;;;; any later version.
  9. ;;;; 
  10. ;;;; This program is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ;;;; GNU General Public License for more details.
  14. ;;;; 
  15. ;;;; You should have received a copy of the GNU General Public License
  16. ;;;; along with this software; see the file COPYING.  If not, write to
  17. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. ;;;; Boston, MA 02110-1301 USA
  19. ;;;;
  20. ;;;; As a special exception, the Free Software Foundation gives permission
  21. ;;;; for additional uses of the text contained in its release of GUILE.
  22. ;;;;
  23. ;;;; The exception is that, if you link the GUILE library with other files
  24. ;;;; to produce an executable, this does not by itself cause the
  25. ;;;; resulting executable to be covered by the GNU General Public License.
  26. ;;;; Your use of that executable is in no way restricted on account of
  27. ;;;; linking the GUILE library code into it.
  28. ;;;;
  29. ;;;; This exception does not however invalidate any other reasons why
  30. ;;;; the executable file might be covered by the GNU General Public License.
  31. ;;;;
  32. ;;;; This exception applies only to the code released by the
  33. ;;;; Free Software Foundation under the name GUILE.  If you copy
  34. ;;;; code from other Free Software Foundation releases into a copy of
  35. ;;;; GUILE, as the General Public License permits, the exception does
  36. ;;;; not apply to the code that you add in this way.  To avoid misleading
  37. ;;;; anyone as to the status of such modified files, you must delete
  38. ;;;; this exception notice from them.
  39. ;;;;
  40. ;;;; If you write modifications of your own for GUILE, it is your choice
  41. ;;;; whether to permit this exception to apply to your modifications.
  42. ;;;; If you do not wish that, delete this exception notice.
  43. ;;;; 
  44.  
  45.  
  46. (define-module  (ice-9 poe)
  47.   :use-module (ice-9 hcons)
  48.   :export (pure-funcq perfect-funcq))
  49.  
  50.  
  51.  
  52.  
  53. ;;; {Pure Functions}
  54. ;;; 
  55. ;;; A pure function (of some sort) is characterized by two equality
  56. ;;; relations: one on argument lists and one on return values.
  57. ;;; A pure function is one that when applied to equal arguments lists
  58. ;;; yields equal results.
  59. ;;;
  60. ;;; If the equality relationship on return values can be eq?, it may make
  61. ;;; sense to cache values returned by the function.  Choosing the right
  62. ;;; equality relation on arguments is tricky.
  63. ;;;
  64.  
  65.  
  66. ;;; {pure-funcq}
  67. ;;;
  68. ;;; The simplest case of pure functions are those in which results
  69. ;;; are only certainly eq? if all of the arguments are.  These functions
  70. ;;; are called "pure-funcq", for obvious reasons.
  71. ;;;
  72.  
  73.  
  74. (define funcq-memo (make-weak-key-hash-table 523)) ; !!! randomly selected values
  75. (define funcq-buffer (make-gc-buffer 256))
  76.  
  77. (define (funcq-hash arg-list n)
  78.   (let ((it (let loop ((x 0)
  79.                (arg-list arg-list))
  80.           (if (null? arg-list)
  81.           (modulo x n)
  82.           (loop (logior x (hashq (car arg-list) 4194303))
  83.             (cdr arg-list))))))
  84.     it))
  85.  
  86. ;; return true if lists X and Y are the same length and each element is `eq?'
  87. (define (eq?-list x y)
  88.   (if (null? x)
  89.       (null? y)
  90.       (and (not (null? y))
  91.        (eq? (car x) (car y))
  92.        (eq?-list (cdr x) (cdr y)))))
  93.  
  94. (define (funcq-assoc arg-list alist)
  95.   (if (null? alist)
  96.       #f
  97.       (if (eq?-list arg-list (caar alist))
  98.       (car alist)
  99.       (funcq-assoc arg-list (cdr alist)))))
  100.  
  101.  
  102. (define (pure-funcq base-func)
  103.   (lambda args
  104.     (let ((cached (hashx-get-handle funcq-hash funcq-assoc funcq-memo (cons base-func args))))
  105.       (if cached
  106.       (begin
  107.         (funcq-buffer (car cached))
  108.         (cdr cached))
  109.         
  110.       (let ((val (apply base-func args))
  111.         (key (cons base-func args)))
  112.         (funcq-buffer key)
  113.         (hashx-set! funcq-hash funcq-assoc funcq-memo key val)
  114.         val)))))
  115.  
  116.  
  117.  
  118. ;;; {Perfect funq}
  119. ;;;
  120. ;;; A pure funq may sometimes forget its past but a perfect
  121. ;;; funcq never does.
  122. ;;;
  123.  
  124. (define (perfect-funcq size base-func)
  125.   (define funcq-memo (make-hash-table size))
  126.  
  127.   (lambda args
  128.     (let ((cached (hashx-get-handle funcq-hash funcq-assoc funcq-memo (cons base-func args))))
  129.       (if cached
  130.       (begin
  131.         (funcq-buffer (car cached))
  132.         (cdr cached))
  133.         
  134.       (let ((val (apply base-func args))
  135.         (key (cons base-func args)))
  136.         (funcq-buffer key)
  137.         (hashx-set! funcq-hash funcq-assoc funcq-memo key val)
  138.         val)))))
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.