[plt-scheme] How to make unit functors?
Matthew Flatt wrote:
> I think the only problem here is that `compound-unit/sig' doesn't both
> link and define unit bodies. It just links. To have new definitions,
> you need a new unit:
> (define inserter@
> (unit/sig set^
> (import (simple : simple-set^))
> (define empty simple:empty)
> (define member? simple:member?)
> (define insert1 simple:insert1)
> (define (insert s . xs)
> (foldl simple:insert1 s xs))))
>
> (define (simple-set->set simple@)
> (compound-unit/sig
> (import (list : list^))
> (link (SIMPLE : simple-set^ (simple@ LIST))
> (INSERTER : set^ (inserter@ SIMPLE)))
> (export (open INSERTER))))
Ah! Thank you very much. And you are absolutely right,
I got the two unit/sig and compound-unit/sig mixed up.
My final version looks like this:
(define (simple-set->set simple@)
(define set@
(unit/sig set^
(import (simple : simple-set^))
(define empty simple:empty)
(define member? simple:member?)
(define insert1 simple:insert1)
(define (insert s . xs)
(foldl simple:insert1 s xs))))
(compound-unit/sig
(import (list : list^))
(link (SIMPLE : simple-set^ (simple@ LIST))
(SET : set^ (set@ SIMPLE)))
(export (open SET))))
I am writing a small library of datastructures using signed units.
Given an implementation of a set of elements ordered under a given
order also represented as a signed unit.
I have now made a functor that turns a set implementation into an
implementation of bags. The bag implementation can then be
instantiated with the type of the elements.
The plan is to implement sets in more ways than one, and for free get
the same amount of bag implemenations.
This reminds me. I have used eq-hash-code to implement sets of
anything given the implementation of ordered sets. In the docs there
is the following garanty:
(eq-hash-code v) returns a number; for any two eq? values, the
returned number is always the same. The number is an exact integer
that is itself guaranteed to be eq? with any value representing the
same exact integer (i.e., it is a fixnum).
Is the same also true for symbols? That is does it hold for two
symbols s1 and s2 that
(= (eq-hash-code s1) (eq-hash-code s2)) = (symbol=? s1 s2)
?
--
Jens Axel Søgaard