File: nsample.scm

package info (click to toggle)
chicken 0.990-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 15,052 kB
  • ctags: 32,419
  • sloc: ansic: 388,227; lisp: 30,001; sh: 7,641; makefile: 311
file content (191 lines) | stat: -rw-r--r-- 6,393 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
;;; nsample.scm - We use the browse benchmark for sampling the optimal nursery size - felix

(declare
 (always-bound plists:plist-table ##sys#hash-table-ref ##sys#hash-table-set! ##sys#setslot ##sys#check-symbol) )

(define plists:plist-table (make-vector 997 '()))

(define get
  (lambda (key prop)
    (##sys#check-symbol key)
    (let ((plist (##sys#hash-table-ref plists:plist-table key)))
      (or (and plist
	       (let ((a (##core#inline "C_i_assq" prop plist)))
		 (and a (##sys#slot a 1)) ) )
	  '() ) ) ) )

(define put
  (lambda (key prop val)
    (##sys#check-symbol key)
    (let ((plist (##sys#hash-table-ref plists:plist-table key)))
      (if plist
	  (let ((a (##core#inline "C_i_assq" prop plist)))
	    (if a
		(##sys#setslot a 1 val)
		(##sys#setslot 
		 plist 1
		 (##core#inline_allocate 
		  ("C_a_i_cons" 3)
		  (##core#inline_allocate ("C_a_i_cons" 3) prop val)
		  (##sys#slot plist 1) ) ) ) )
	  (##sys#hash-table-set! 
	   plists:plist-table key
	   (##core#inline_allocate 
	    ("C_a_i_cons" 3)
	    (##core#inline_allocate ("C_a_i_cons" 3) prop val)
	    '() ) ) ) ) ) )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; File:         browse.sc
;;; Description:  The BROWSE benchmark from the Gabriel tests
;;; Author:       Richard Gabriel
;;; Created:      8-Apr-85
;;; Modified:     14-Jun-85 18:44:49 (Bob Shaw)
;;;               16-Aug-87 (Will Clinger)
;;;               22-Jan-88 (Will Clinger)
;;;               24-Mar-96 (Qobi)
;;;               31-Mar-98 (Qobi)
;;;               26-Mar-00 (flw)
;;; Language:     Scheme (but see notes below)
;;; Status:       Public Domain
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Note:  This benchmark has been run only in implementations in which
;;; the empty list is the same as #f, and may not work if that is not true.
;;; Note:  This benchmark uses property lists.  The procedures that must
;;; be supplied are get and put, where (put x y z) is equivalent to Common
;;; Lisp's (setf (get x y) z).
;;; Note:  The Common Lisp version assumes that eq works on characters,
;;; which is not a portable assumption but is true in most implementations.
;;; This translation makes the same assumption about eq?.
;;; Note:  The gensym procedure was left as in Common Lisp.  Most Scheme
;;; implementations have something similar internally.
;;; Note:  The original benchmark took the car or cdr of the empty list
;;; 14,600 times.  Before explicit tests were added to protect the offending
;;; calls to car and cdr, MacScheme was spending a quarter of its run time
;;; in the exception handler recovering from those errors.

; 11/07/00 - felix:
;
; - Renamed 'match' to 'bmatch', because there exists a macro-definition of
;   'match' already.

;;; The next few definitions should be omitted if the Scheme implementation
;;; already provides them.

(define (append! x y)
  (if (null? x)
      y
      (do ((a x b) (b (cdr x) (cdr b))) ((null? b) (set-cdr! a y) x))))

(define (copy-tree x)
  (if (not (pair? x)) x (cons (copy-tree (car x)) (copy-tree (cdr x)))))


;;; BROWSE -- Benchmark to create and browse through
;;; an AI-like data base of units.

;;; n is # of symbols
;;; m is maximum amount of stuff on the plist
;;; npats is the number of basic patterns on the unit
;;; ipats is the instantiated copies of the patterns

(define *rand* 21)

(define (init n m npats ipats)
  (let ((ipats (copy-tree ipats)))
    (do ((p ipats (cdr p))) ((null? (cdr p)) (set-cdr! p ipats)))
    (do ((n n (- n 1))
	 (i m (cond ((zero? i) m) (else (- i 1))))
	 (name (gensym) (gensym))
	 (a '()))
	((= n 0) a)
      (set! a (cons name a))
      (do ((i i (- i 1))) ((zero? i)) (put name (gensym) #f))
      (put name
	   'pattern
	   (do ((i npats (- i 1)) (ipats ipats (cdr ipats)) (a '()))
	       ((zero? i) a)
	     (set! a (cons (car ipats) a))))
      (do ((j (- m i) (- j 1))) ((zero? j)) (put name (gensym) #f)))))

(define (browse-random)
  (set! *rand* (remainder (* *rand* 17) 251))
  *rand*)

(define (randomize l)
  (do ((a '())) ((null? l) a)
   (let ((n (remainder (browse-random) (length l))))
    (cond ((zero? n)
	   (set! a (cons (car l) a))
	   (set! l (cdr l))
	   l)
	  (else (do ((n n (- n 1)) (x l (cdr x)))
		  ((= n 1)
		   (set! a (cons (cadr x) a))
		   (set-cdr! x (cddr x))
		   x)))))))

(define (bmatch pat dat alist)
  (cond ((null? pat) (null? dat))
	((null? dat) #f)		;Qobi: used to depend on () being false
	((or (eq? (car pat) '?) (eq? (car pat) (car dat)))
	 (bmatch (cdr pat) (cdr dat) alist))
	((eq? (car pat) '*)
	 (or (bmatch (cdr pat) dat alist)
	     (bmatch (cdr pat) (cdr dat) alist)
	     (bmatch pat (cdr dat) alist)))
	(else (cond ((not (pair? (car pat)))
		     (cond ((eq? (string-ref (symbol->string (car pat)) 0) #\?)
			    (let ((val (assv (car pat) alist)))
			     (cond (val (bmatch (cons (cdr val) (cdr pat))
					       dat alist))
				   (else (bmatch (cdr pat)
						(cdr dat)
						(cons (cons (car pat)
							    (car dat))
						      alist))))))
			   ((eq? (string-ref (symbol->string (car pat)) 0) #\*)
			    (let ((val (assv (car pat) alist)))
			     (cond (val (bmatch (append (cdr val) (cdr pat))
					       dat alist))
				   (else
				    (do ((l '()
					    (append! l
						     (cons (if (null? d)
							       '()
							       (car d))
							   '())))
					 (e (cons '() dat) (cdr e))
					 (d dat (if (null? d) '() (cdr d))))
				      ((or (null? e)
					   (bmatch (cdr pat)
						  d
						  (cons (cons (car pat) l)
							alist)))
				       (if (null? e) #f #t)))))))
			   ;; Qobi: used to depend of missing ELSE returning #F
			   (else #f)))
		    (else (and (pair? (car dat))
			       (bmatch (car pat) (car dat) alist)
			       (bmatch (cdr pat) (cdr dat) alist)))))))

(define (browse)
  (investigate (randomize (init 100 10 4 '((a a a b b b b a a a a a b b a a a)
					   (a a b b b b a a (a a) (b b))
					   (a a a b (b a) b a b a))))
	       '((*a ?b *b ?b a *a a *b *a)
		 (*a *b *b *a (*a) (*b))
		 (? ? * (b a) * ? ?))))

(define (investigate units pats)
  (do ((units units (cdr units))) ((null? units))
   (do ((pats pats (cdr pats))) ((null? pats))
    (do ((p (get (car units) 'pattern) (cdr p))) ((null? p))
     (bmatch (car pats) (car p) '())))))


(let ([t0 (##sys#fudge 6)])
  (browse)
  (write (- (##sys#fudge 6) t0))
  (newline) )