[Edu-sig] foundations,
Matthias Felleisen
matthias@rice.edu
Tue, 29 May 2001 07:43:13 -0500 (CDT)
Kirby writes:
I guess I'm feeling uneasy with a foundational approach that
doesn't bring in the concept of objects closer to the beginning.
The object concept is an important one. Just procedures and data,
without objects, seems a rather scattered/cluttered approach.
Shouldn't newcomers learn the object model right about where this
rational numbers example is presented? Certainly building rational
numbers as objects would be a standard exercise in a Python-focused
curriculum (might be a math class -- if the distinction matters).
You need to distinguish several things:
- class-based programming & object-oriented computation
- object-oriented programming & object-oriented computation
- modules and representation hiding
Functional programmers prefer modules over classes. In PLT Scheme we use
both. The idea is simple. You write a module and you export just as much
as you wish. For example,
(define-signature Sequence^
( build-sequence ; Element ... Element -> Sequence
iterator ; Sequence (Element -> X) -> Void
))
(define Sequence@
(unit/sig (Sequence^)
(import ...) ; imports
; -------------------------------------------------------
; body
(define-struct hide (>sequence)) ; a structure for encapsulating stuff
(define (build-sequence . x)
(make-hide ... x ...))
(define (iterator sequence function)
(let ([internal-representation (hide->sequence sequence)])
...))))
defines a module that creates an opaque type sequence by exporting
constructor and an iterator but no tools to get at the internal
representation. In other words, it is a mechanism for coupling data and
functions in an intimate manner (and units are first-class values on top).
It looks even better if you write this with ML signatures:
signature GROUP = sig
type Element
val plus : Element Element -> Element
val id : Element
val inv : Element -> Element
end
functor Group(type E) : GROUP = struct
type Element = E;
...
end
and you can parameterize functors and units over imported mathematical
structures:
functor VectorSpace(structure Field) : VECTORSPACE = struct
...
end
and then you can use Real or Complex or some other field to build vector
spaces.
-- Matthias
P.S. Someone asked whether I'd use ML first. No. Kids have enough trouble
understanding computation. Type checking and proves are confusing them even
more.