;;;; -*- Scheme -*-
;;
;; bnawki example for HLSIM
;;
;; David DeRoure Sep 14 1997
;; dder@martigny.ai.mit.edu
;;
;; This definition file is used by bnawki1.scm.  It should be compiled 
;; with (cf "bnawki1d") and loaded with (load "bnawki1d").  See bnawki1s.scm 
;; for the simulation setup.  See the HLSIM documentation for further 
;; details.

(declare (usual-integrations))

;; Update table according to information in message (id, state).
;; This is either a new processor and state, or a state update
;; for an existing processor.

(define (update-table! table message)
  (let ((v (assq (car message) table)))   
    (if v
	(begin (set-cdr! v (cdr message)) table)
	(cons (cons (car message) (cdr message)) table))))

;; Count fraction of neighbors which have state #T and hence 
;; determine next state of this processor by calling the rules 
;; procedure.  The old-state argument is just passed through 
;; to generate.

(define (next-state table old-state)
  (let next ((l table) (len 0) (count 0))
    (if (null? l)
	(rules len count old-state)
	(next (cdr l) (1+ len) (if (cdar l) (1+ count) count)))))

;; Given total number of neighbors, the number of neighbors with state #T
;; and the old state of this processor, rules returns the new state 
;; of this processor.  

;; e.g. With high fraction => #T and low fraction => #F
;; then processors will clump together by "peer pressure".

(define (rules n c old-state)
  (cond ( (< c (* n 1/3)) #F )
	( (> c (* n 2/3)) #T )
	( else old-state )))

;; e.g. With high fraction => #F, some processors are #F due
;; to "overpopulation" and clumps are avoided.
; 
; (define (rules n c old-state)
;   (cond ( (<= c (* n 1/4)) #F )
; 	( (<= c (* n 2/4)) #T )
; 	( (<= c (* n 3/4)) old-state )
; 	( else #F )))

;; end of bnawki1d.scm
