;;; Implementation-specific procedures
;;;
;;; Copyright (C) 2002 Anton van Straaten <anton@ppsolutions.com>
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License, 
;;; version 2, as published by the Free Software Foundation.
;;;
;;; This program is distributed in the hope that it will be useful, 
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, see http://www.gnu.org/copyleft/gpl.html
;;;
;;; -------------------------------------------------------------------------

Show source file in small font In domain-implementations: Link from ds:identity to it's cross reference table entry 
(define ds:identity (lambda (x) x)) ;;; E, the domain of expressed values Show source file in small font In domain-implementations: Link from ds:inject-value to it's cross reference table entry 4.3. Domain Implementations
(define ds:inject-value ds:identity) ;;; Sequence interface ;;; ;;; These procedures implement the 'sequence' data type used by the semantics. ;;; Currently implemented simply as lists in host Scheme. Show source file in small font In domain-implementations: Link from ds:append to it's cross reference table entry 4.3. Domain Implementations
(define ds:append append) Show source file in small font In domain-implementations: Link from ds:first to it's cross reference table entry 4.3. Domain Implementations
(define ds:first car) Show source file in small font In domain-implementations: Link from ds:length to it's cross reference table entry 4.3. Domain Implementations
(define ds:length length) Show source file in small font In domain-implementations: Link from ds:rest to it's cross reference table entry 4.3. Domain Implementations
(define ds:rest cdr) Show source file in small font In domain-implementations: Link from ds:second to it's cross reference table entry 4.3. Domain Implementations
(define ds:second cadr) Show source file in small font In domain-implementations: Link from ds:sequence to it's cross reference table entry 4.3. Domain Implementations
(define ds:sequence list) Show source file in small font In domain-implementations: Link from ds:third to it's cross reference table entry 4.3. Domain Implementations
(define ds:third caddr) Show source file in small font In domain-implementations: Link from ds:sequence? to it's cross reference table entry 4.3. Domain Implementations
(define ds:sequence? pair?) ;;; M, the domain of miscellaneous values ;;; ;;; Members are {false, true, null, undefined, unspecified} Show source file in small font In domain-implementations: Link from ds:false to it's cross reference table entry 4.3. Domain Implementations
(define ds:false #f) Show source file in small font In domain-implementations: Link from ds:true to it's cross reference table entry 4.3. Domain Implementations
(define ds:true #t) Show source file in small font In domain-implementations: Link from ds:undefined to it's cross reference table entry 4.3. Domain Implementations
(define ds:undefined '*undefined*) Show source file in small font In domain-implementations: Link from ds:unspecified to it's cross reference table entry 4.3. Domain Implementations
(define ds:unspecified '*unspecified*) ;; The value of '() is required for the auxiliary function 'list' to ;; work correctly, given our direct mapping of types to the host Scheme. Show source file in small font In domain-implementations: Link from ds:null to it's cross reference table entry 
(define ds:null '()) Show source file in small font In domain-implementations: Link from ds:misc-domain to it's cross reference table entry 
(define ds:misc-domain (list ds:false ds:true ds:null ds:undefined ds:unspecified)) Show source file in small font In domain-implementations: Link from ds:misc? to it's cross reference table entry 4.3. Domain Implementations
(define ds:misc? (lambda (m) (if (member m ds:misc-domain) #t #f))) Show source file in small font In domain-implementations: Link from ds:project-misc to it's cross reference table entry 4.3. Domain Implementations
(define ds:project-misc ds:identity) ;;; L, the domain of locations ;;; ;;; Assumes all integers are locations, but not all in use Show source file in small font In domain-implementations: Link from ds:location? to it's cross reference table entry 4.3. Domain Implementations
(define ds:location? integer?) Show source file in small font In domain-implementations: Link from ds:location-eq? to it's cross reference table entry 4.3. Domain Implementations
(define ds:location-eq? =) Show source file in small font In domain-implementations: Link from ds:project-location to it's cross reference table entry 4.3. Domain Implementations
(define ds:project-location ds:identity) ;;; F, the domain of procedure values Show source file in small font In domain-implementations: Link from ds:project-procedure to it's cross reference table entry 4.3. Domain Implementations
(define ds:project-procedure ds:identity) Show source file in small font In domain-implementations: Link from ds:procedure? to it's cross reference table entry 4.3. Domain Implementations
(define ds:procedure? (lambda (e) (if (and (ds:sequence? e) ;; required guard (= (ds:length e) 2) ;; overcautious (ds:location? (ds:first e)) ;; really paranoid (procedure? (ds:second e))) ;; the real test #t #f))) ;;; R, the domain of numbers Show source file in small font In domain-implementations: Link from ds:number? to it's cross reference table entry 4.3. Domain Implementations
(define ds:number? number?) Show source file in small font In domain-implementations: Link from ds:project-number to it's cross reference table entry 4.3. Domain Implementations
(define ds:project-number ds:identity) ;;; Ep, the domain of pairs. ;; Pairs are sequences of <LxLxT>. Following is a complete test, which is ;; overkill in the current implementation - simply testing length would ;; be sufficient. ;; ;; However, implementation of a vector type would change this. Given the ;; current naive implementation of locations, for example, a two-element ;; vector might look like a pair. Show source file in small font In domain-implementations: Link from ds:pair? to it's cross reference table entry 4.3. Domain Implementations
(define (ds:pair? e) (if (and (ds:sequence? e) (= (ds:length e) 3) (ds:location? (ds:first e)) (ds:location? (ds:second e)) (boolean? (ds:third e))) #t #f)) Show source file in small font In domain-implementations: Link from ds:project-pair to it's cross reference table entry 4.3. Domain Implementations
(define ds:project-pair ds:identity) ;;; Q, the domain of symbols Show source file in small font In domain-implementations: Link from ds:symbol? to it's cross reference table entry 4.3. Domain Implementations
(define ds:symbol? symbol?) Show source file in small font In domain-implementations: Link from ds:project-symbol to it's cross reference table entry 4.3. Domain Implementations
(define ds:project-symbol ds:identity) ;;; Es, the domain of strings Show source file in small font In domain-implementations: Link from ds:string? to it's cross reference table entry 4.3. Domain Implementations
(define ds:string? string?) Show source file in small font In domain-implementations: Link from ds:project-string to it's cross reference table entry 4.3. Domain Implementations
(define ds:project-string ds:identity) ;;; H, the domain of characters Show source file in small font In domain-implementations: Link from ds:char? to it's cross reference table entry 4.3. Domain Implementations
(define ds:char? char?) Show source file in small font In domain-implementations: Link from ds:project-char to it's cross reference table entry 4.3. Domain Implementations
(define ds:project-char ds:identity) ;;; S, the domain of stores ;;; ;;; todo: move to own file to support pluggability. ;;; Closely coupled to location implementation. ;; new : S -> (L + {error}) ;; ;; No allowance for error here - the supply of natural numbers is fairly extensive... ;; ;; Borrowed from Queinnec. Note that the use of (s 0) means that every newly ;; allocated location causes the previous value of (s 0) to be replaced, resulting ;; in a store procedure with a call chain double the length it might otherwise be, ;; which has cost on every access to the store. See below for Clinger's alternative. Show source file in small font In domain-implementations: Link from ds:new-impl to it's cross reference table entry 4.3. Domain Implementations
(define ds:new-impl (lambda (s) (+ 1 (s 0)))) ;; Will Clinger posted the following implementation of 'new' on c.l.s. ;; By looping through entire store to find an unallocated location, it avoids ;; the issue mentioned above - performance cost is shifted from every store ;; access, to allocation time. Might be interesting to measure the performance ;; difference... ;; ;; (define (new s) ;; (define (loop s a) ;; (if (second (s a)) ;; (loop s (+ a 1)) ;; a)) ;; (loop s 0)) ;; ;; expand-store : S -> L -> S ;; ;; Store new largest memory address in location zero ;; and implement unused locations ;; ;; Adapted from Queinnec Show source file in small font In domain-implementations: Link from ds:expand-store to it's cross reference table entry 4.3. Domain Implementations
(define (ds:expand-store s a-max) (ds:substitute s a-max 0)) ;; initial-store implements a store for which all integers are ;; valid locations, but are initially flagged as not in use. ;; ;; Adapted from Queinnec Show source file in small font In domain-implementations: Link from ds:initial-store to it's cross reference table entry 4.3. Domain Implementations
(define (ds:initial-store) (ds:expand-store (lambda (a) (if (ds:location? a) (ds:sequence ds:unspecified ds:false) (ds:wrong-wrong "Invalid location"))) 0)) ;; Implements substitution operation - f[v/s] ;; ;; f:function; v:value; s:selector ;; from R5RS 'notation': r[x/i] - substitution r with x for i ;; ;; Used by: ;; update: s[e/a] ;; extends: r[a/ide] ;; ;; Relies on eqv? being appropriate for all argument types Show source file in small font In domain-implementations: Link from ds:substitute to it's cross reference table entry 4.3. Domain Implementations
(define ds:substitute (lambda (f v s) (lambda (s1) (if (eqv? s s1) v (f s1))))) ;; Implements substitution for location/value bindings. ;; If the specified location is unallocated, the store is ;; expanded via ds:expand-store. ;; ;; Corresponds to s[e/a] in the DS ;; ;; Note that the sequence of arguments here is determined ;; by the order required from the output of the L2T tool. Show source file in small font In domain-implementations: Link from ds:substitute-location to it's cross reference table entry 4.3. Domain Implementations
(define ds:substitute-location (lambda (s e a) (ds:substitute (if (ds:location-eq? a (ds:new-impl s)) (ds:expand-store s a) s) e a)))