HaScheme

A Lazy dialect of Scheme, embedded in Scheme

  1. HaScheme
    1. Description
    2. Author
    3. Repository
    4. Requirements
    5. API
      1. (hascheme base)
      2. (hascheme case-lambda)
      3. (hascheme char)
      4. (hascheme complex)
      5. (hascheme cxr)
      6. (hascheme inexact)
      7. (hascheme control)
      8. (hascheme eager)
      9. (hascheme lists)
    6. Misc. Information
      1. How is it implemented?
      2. Fun (or pain) with Laziness
      3. Call-by-Need and Conditionals
      4. Multiple Values and Continuations
    7. Issues/Future Directions
  2. Version History
    1. License

Description

Scheme demonstrates that a very small number of rules for forming expressions, with no restrictions on how they are composed, suffice to form a practical and efficient programming language that is flexible enough to support most of the major programming paradigms in use today.

Revisedⁿ Reports on the Algorithmic Language Scheme (n ≥ 3, 1986–)

HaScheme is a library that implements a subset of the R7RS's libraries in a lazy way, using force and delay-force. When HaScheme is used by itself, it is a lazy functional programming language with the same syntax as Scheme, embedded within Scheme. HaScheme is implicitly lazy, and only requires strictness annotations, not laziness annotations. Scheme procedures can be easily wrapped to be used in HaScheme, and Scheme can call force on HaScheme promises to obtain values from HaScheme.

For instance, the following map implementation is both valid HaScheme (importing (hascheme base)) and Scheme (importing (scheme base)):

(define (map f list)
  (if (null? list)
      '()
      (cons (f (car list)) (map f (cdr list)))))

This procedure in HaScheme will return a promise. The function's body is only executed if, when attempting to force a promise containing the map, the contents of the map are needed for a computation.

Since HaScheme is lazy, one can write infinite lists with it:

(let ((N (let loop ((i 0))
           (cons (! i) (loop (+ i 1))))))
  (force (list-ref (map square N) 100))) ; => 100000

This code snippet will run in a constant amount of space. (The procedure ! is a strictness annotation to avoid thunk buildup. Since HaScheme is emedded within a call-by-value language, the strictness annotation will always force i at every iteration of the loop, even if neither part of the pair is accessed.)

Why use this?

  1. To have fun.
  2. To show Haskellers that you don't need some fancy static type system to have pervasive lazyness.
  3. To implement lazy code that can be used with strict code.

HaScheme does not support continuations, parameters, exceptions, or multiple value returns.

HaScheme works in Chibi too.

Author

Peter McGoron

Repository

https://software.mcgoron.com/hascheme/

Requirements

API

The HaScheme library namespace is structured in a similar way to R7RS-large's namespace, except it does not export any procedure that is side-effecting.

(hascheme base)

Exports everything from (scheme base) as lazy procedures, except

The functions act as one would expect they would act. Forcing (car x) forces x, but forcing (cons x y) does not force x or y.

Syntax that introduces procedures, like lambda, define, and named let make lazy procedures. Lazy procedures return a promise that when forced, force the body of the procedure.

Bodies are regular bodies: they can have internal defines.

Note that control syntax like if and cond are still syntax. They can be implemented as functions, but then they will have subtly different behavior when it comes to explicitly forcing values. Conditinoals force tests.

String, bytevector, and number constructors are always eager. List and vector constructors are lazy.

This library also exports seq.

[procedure] (seq x ... y)

Forces all arguments except its last argument. Returns its last argument unchanged.

[syntax] (define-record-type name (cstr name ...) predicate? (name accessor) ...)

Creates a record type, as if by the regular define-record-type. This form gives no way to create setters.

The constructor is always non-strict in its arguments, and the accessors always force their arguments.

[syntax] (do ((id init [update]) ...) (condition body ...) inner-body ...)

Like the do loop of before, except that the value that inner-body ... eventually evaluates to is forced for effect. Since inner-body ... is a body, nothing else is forced for effect. Consider using seq here.

(hascheme case-lambda)

Exports case-lambda, which creates lazy procedures.

(hascheme char)

Exports lazy procedure versions of (scheme char).

(hascheme complex)

Exports lazy procedure versions of (scheme complex).

(hascheme cxr)

Exports lazy procedure versions of (scheme cxr).

(hascheme inexact)

Exports lazy procedure versions of (scheme inexact).

(hascheme control)

Exports procedure versions of control syntax.

[procedure] (if x y [z])

Force x. If true, return y. If not, return z (or an unspecified value).

[procedure] (cond [x y] ...)

Force x. If true, return y. If not, continue on with the next pairs. If no pairs exist, return an unspecified value.

[procedure] (and x ...)

Force x. If true, force the next value, and so on. If there are no more values, return #t. If not, return #f.

[procedure] (or x ...)

Force x. If true, return x. Otherwise, force the next value, and so on. If there are no more values, return #f.

[procedure] (when pred . subsequents)

Force pred. If true, run (apply seq subsequents). Otherwise return an unspecified value.

[procedure] (unless pred . subsequents)

Force pred. If false, run (apply seq subsequents). Otherwise return an unspecified value.

(hascheme eager)

Procedures and syntax forms for eager evaluation.

[procedure] (! x)

Shorthand for (force x).

[syntax] (define-wrappers-from-strict (head source) ...)

Defines lazy-procedure variants of source that force all arguments. Head can either be (name formal ...) or name.

[syntax] (define-wrappers-for-lazy (head source) ...)

Define lazy-procedure variants of source that pass all arguments to a constructor. Head is defined as in define-wrappers-from-strict.

[syntax] (define-binary-wrapper (wrapper source) ...)

Defines wrapper as a non-strict n-ary comparison function. It will force the first two arguments to it, and if the comparison returns false, the procedure returns false and none of the other values are forced. Otherwise it will keep on forcing arguments until there are no more arguments or one of the comparisons fail.

[syntax] (let*! ((formal expr) ...) body ...)

Forces each expr, binds the result to formal, and executes body ....

[syntax] (let*-seq ((formal expr) ...) body ...)

Returns a promise to force each expr, bind it to formal, and then execute body ....

This library also exports seq.

(hascheme lists)

This library exports all identifiers from SRFI-1, except for

All procedures that normally return multiple values return lists.

In addition, this library exports other procedures.

[procedure] (length>=? list n)
[procedure] (length>? list n)
[procedure] (length<=? list n)
[procedure] (length<? list n)
[procedure] (length=? list n)

Determine the size of the list by looking at only n elements. This will decide the size of finite or infinite lists in all cases where n is not infinite. All list lengths are less than or equal to infinity, and all lists are not greater than infinity.

[procedure] (unzip lists)

Returns a list, whose first element is the first element of each lists, the second element is the second element of each lists, and so on until the first empty list.

[procedure] (list-iterate proc base)

This procedure is based off of the one in SRFI-41.

Create an infinite list where the first element is (proc base), and the second element is (proc (proc base)), etc.

This procedure is eager in base.

Misc. Information

How is it implemented?

The implementation follows the formula of SRFI-45 with some changes for usability.

  1. All procedure bodies are implicitly wrapped in delay-force.
  2. Scheme procedures that require their values are, in addition, wrapped such that forcing their function forces the necessary arguments. A Scheme procedure that is a constructor does not force its arguments.
  3. Control flow forces the value of the test expression.

Three critical things are needed to make HaScheme ergonomic:

  1. Promises are distinct from other types.
  2. Forcing a value returns the value.
  3. Forcing (delay-force x) is equivalent to a tail call to (force x).

Chicken does all three. There is nothing fancy going on here, just some (relatively) easy-to-understand syntax-rules macros. There is a shim for implementations that don't offer the above guarantees.

Fun (or pain) with Laziness

You need to be careful with lazy functions because they can cause space leaks. This is a problem in general with lazy languages likein Haskell). Here is an example:

(define (list-tail list n)
  (if (zero? n)
      list
      (list-tail (cdr list) (- n 1))))

Thunks will build up over time in the list, so it must be forced.

(define (list-tail list n)
  (if (zero? n)
      list
      (list-tail (force (cdr list)) (- n 1))))

Note that n is never explicitly forced: it is implicitly forced by the control flow.

The first code block has the attractive property that it operates the same way on finite lists in both Scheme and HaScheme, while the second one could differ in exotic cases (like promises that return promises). Instead of writing force, the operator ! is used:

(define (list-tail list n)
  (if (zero? n)
      list
      (list-tail (! (cdr list)) (- n 1))))

Now if one were to define ! as the identity function in regular, call-by-value Scheme, then the block above will evaluate to the same value in both HaScheme and Scheme, in bounded space.

Ok, now we have fixed our space leak issues. Right? Let's try another infinite list trick: a list of all natural numbers.

(define naturals (list-tabulate +inf.0 (lambda (x) x)))
(! (list-tail naturals 1000000000))

This also leaks! This is because the promises are making new cons cells, and storing them in naturals. We need to organize things to make sure the program can clean up.

(! (list-tail (list-tabulate +inf.0 (lambda (x) x)) 1000000000))

This will run in bounded space.

Call-by-Need and Conditionals

Since call-by-need will only execute a function when needed, conditional forms like if can be implemented as functions and not syntax. In fact, (hascheme control) implements if, and, or, and the cond as functions, meaning one can pass them around as values.

For instance:

(define (map f l)
  (cond
    ((null? l) '())
    ((pair? l) (cons (f (car l)) (cdr l)))
    (else (error "not a list" l))))

implemented with (hascheme control) is

(define (map f l)
  (cond
   (null? l) '()
   (pair? l) (cons f (car l) (cdr l))
   #t (error "not a list" l)))

Neat, right? Well, if we go to list-tail we have a problem:

(define (list-tail list n)
  (if (zero? n)
      list
      (list-tail (! (cdr list)) (- n 1))))

Since if is now a function, Scheme (our call-by-value host language) will attempt to reduce (! (cdr list)) every time, even when we don't need to. We could go back to syntactic if, or we could add some wrapper to the procedure. The seq function (named after the function in Haskell) takes n forms, forces the first n-1, and returns the nth form.

(define (list-tail list n)
  (if* (zero? n)
       list
       (seq (cdr list)
            (list-tail (cdr list) (- n 1)))))

Multiple Values and Continuations

HaScheme doesn't have call/cc. call/cc is not a function because it does not return, so that's strike one for inclusion in a pure language. Reified continuations make sense in a call-by-value language, because there is a definite evaluation order (outermost first), but a lazy language can execute any code at basically any time.

A future implementation might be able to use SRFI-226's delimited control structures to implement continuations, because they are actual functions.

Multiple values are specified as returning values to their continuation. Since HaScheme does not (conceptually) have continuations, multiple values have to be interpreted differently. But a bigger issue occurs because a promise is a single value. It cannot be decomposed into more values without forcing the promise.

Issues/Future Directions

Version History

0.2.0
Added missing do form, misc. fixes, alot more tests, lists library, change license to 0BSD
0.1.0
Initial Release

License

Copyright (C) Peter McGoron 2025

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Some tests and support code (not used in the CHICKEN version) were adapted from code written by André van Tonder.

Copyright (C) André van Tonder (2003). All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.