Higher-Order
Functors
with
Sandip
K.
Transparent Biswas*
Department University
Signatures
of CIS
of Pennsylvania
Philadelphia, sbiswas@saul
PA
19104
.cis.upenn.
Abstract
edu
on the
underlying
representation
of the
set.
For
exam-
ple, The
programming
order
functors,
First-order egant
[3], i.e.
defined
SML/NJ. much
more
functors.
trade
this
a simpler
static
of the
the
past functors
is well a forML,
whether
module closer
we to
of higher-order
Modules
are an essential the
dresses
the
problem
dard
ML.
vides
for
SML
are first-order
trized
feature
development The
of
of extending
current
parameterized in
by providing
modules
modules.
Here
semantics
item
type
set
val
empty
val
singleton
val
union
val
m.udtems
pro-
The
in
this
re-
func-
for
can
always
zing
the
any
make
these
parameters
examples
detailing
tors. zed
For by
SML/NJ
●This
research
functors to
the
specific
1. In SML/NJ,
using more
uses
library
routines
example,
a set
a structure library was
supported
functors,
general
by parametri-
We
now
multiple by ONR
a structure an order set
functors
Contract
:
(set
: :
item *
->
set
set)
set
->
->
int
does
not
->
on a set till
set
contain
->
set,
using
containing the
available
the
function
which
iterates
is no change union,
functors,
set functorin
functors
set
there
redefined
signature
every
in cardinality.
nmdtems.
afunctor
closure
hasto
library.
But
in the
language,
In the
with
an ex-
redefined with the
higherfollowing
be defined,
functor
ExtendSet
(s :
two
funsig
(K : struct
as func-
(K:oRD_KEy)
ORD_KEY)
structure
parametrirelation.
0RD5ET
may
functors.
are implemented is
ord.key) .relat.ion
we
present
of higher-order
containing has
first-order
functor.
set
of higher-order
order
modules?
application
sets
=
:
absence
may IngeneraJ,
(ord_key * -> LibBase
Butthis tended
higher-order
:
(set->set)
a function
be parame-
higher-order
signature
closure:
for
Why
for
end
tors.
1.1
ORD-.SET,
.$.
ad-
ML
we remove
for
:
where,
=
ORD_SET
type
sup-
Functors
cannot
trees,
of Stan-
of Standard
or functors.
nature: static
system
sets
s ig of
paper
for
end
under-
which
This
module
system
modules
by parameterized
striction
the
module
language
systems.
cmpKey
signature
modules.
of any
large
ORD-SET,
ord_key
val
Introduction
ports
Set (K: ORDXEY)
ORD.KEY
type
can
that
:
and
s ig
toob-
a conceptual
trees,
by splay
signature
of first-
spirit
Splay
to be
language
in
helpsin
(K: ORDXEY)
by binary
functor
we have
[7] showsit
B-inarySet
represented
with
semantics
inthe
el-
of Standard
static
work
semantics
that
investigates
semantics, This
and
by functors,
recent
semantics
paper features
functors.
standing
static
functor represented
of higher-order
in a dialect
than
This
semantic
first-order
the
of higher-order
complicated
some
in
first-
modules.
a simple
structure
parameterized
semantics
Astudyof
provides by
have
type
is only
static
order
1
it
language
The
modules
But
ML
parametrized
the
an implementation
mally
tain
in
semantics.
understood.
Standard
modules
functors
static
modules seen
language i.e
open
The
= S(K);
Set
Set;
funclosuref
depending
ORD_SET)
=
s=...
end
NOOO14-89-J-3155.
This
Permission to copy without fee all or part of thk materfal is granted provided that the copies are not made or distributed for direct commercial acfvantaqe, the ACM copyright notice and the title of the publication and Its date appear, and notice is given that copyi is by permission of the Association of Computing Machinery.“$ o copy otherwke, or to republish, requires a fee and/or specific permission. POPL ’951/95 San Francisco CA USA @ 1995 ACM 0-89791 -692-1/95/0001 ....$3.50
functor
ExtendSet
Bi.narySet, has 2. There ating may
154
been
Splay extended
may two use
Set
be
two
functors the
by
two
may to the
applied
obtain function
independent F and
functors
the
functors
whose
result
tools
gener-
closure. software
G. A in
to
functors
particular
a very
specific
application way.
The
user
of
this
of the
application
functors,
gether.
A case
in
A standard
ator
the
uses
to
a routine
turned of
from
the
higher-order
calculator
know
how
they
being
the
token
Such
puts
parser
and
functors
CalcLrValsFun.
To create
generator
in
and
the
the
lexer.
is
the
lexer
(less
else
false
steps
::
rest2)
=
restl
rest2)
ure
int-order
:
DRDER =
struct
a
type
t
fun
less
= int (X:t)
(y:t)
=x
< y
end
named
following
(a2 a2)
end strut
a functor
a functor
the
restl) al
then
reFor
::
(K.less
absence
possible.
generates
(al
if
be able
functors
In
not
generates a parser
the
should
together
ML-Yacc
ML-Lex
CalcLexFun,
less to-
structure,
this
Calc,
structure
of theparsergener-
anapplication
which
named
the
are joined
parser
application
provided
analysis.
call
not
G, and
point
SML/NJ. forlexical
need
F and
structure
are
val
=
_
int_list_order
= Ord-List(int-order)
int-list-order.less
[1,2,31
[2,3,5]
necessary: The structure
CalcLrVals
the
=
generic
CalcLrValsFun (structure structuxe
= LrParser.
Token
CalcLex
Token)
Tokens
CalcPerser
= CalcLrVals.Tokens)
;
to the in
Consider
the
the
rest
ParserData= Lex
structure
LrParser
CalcLrVals.ParserData
: funsig
functor(X:
= CalcLex
though
to have
i.ntdist.order.t
of the
the
= int
program. case
is more
compli-
program:
(X
sig
(F
= LrPerser);
even
APP =
functor(F
sig
structure
checks,
higher-order
following
Join (structure
type
int-1.ist.order
ORDER, because
is transparent
functor
=
statically constrains
Transparency cated.
(structure
program
OrdList
signature
list
;
=
CalcLexFun structure
above
functor
functor
:
si.g
type
s;
type
t
;
type
t;
val
X:S
val
val
x:t
x:t
end)
end)
end)
X)
F = Functor(X
sig
:
type
t;
val
x:t
end)
struct If higher-order may
provide
tion
need
the
parser
functors not
Join’.
know
how
andlexer
functor
are allowed
afunctor
Join’
in the
A user
the
functors
are brought
language
using
thk
we
type
func-
returned
from
x
=
list
[ x.x
]
t
= int;
end
together.
(CalcLrValsFun,CalcLexFun)
s = X.t
val
=
structure
X = struct
structure
Y =
val
-
type
((APP
= car
F)
val
x
= 4 end
X)
(Y.x)
struct structure
CalcLrVals
If
=
(structure Token = LrPaser.Token) structure CalcLex =
tic
;
(structme
Tokens
In
Join( structure
ParserData
structure
Lex
structure
LrParser
[6]it
= LrPsrser);
is shown
importantly
sible
across
Signatures
of the
module
trcmsparcmtsignatures. constrained of the larly,
during
the
are
are
the
not
that
The
on
tities
of thecompo-
example
illustrates
concept,
case.
signature
ORDER = si.g
of
type
t
val
less
->
t
->
bool
end functor
Ord.List
(K
:
ORDER)
:
ORDER
=
struct type
t
fun
less I less
= K.t [] -
list _ = true []
155
equivalence
associated has
been
damental
problem
programs
have
final
to give
iden-
information have
signatures
signatures
when
we have
because
there
associated the
in the
fixed.
the
associated
with state
of
the
type
takingin way
argument
which
there
translucent
boundary.
com-
can
propagating
functors But
are
first-order
afunctor
set of functors problem
certain
is no uniform with
argument
recently
afunctor
of type
restricthe
higher-order
This
to explicitly
beyond
in
possible.
translucent
with
have
present
with
restricting
signatures
types
elimina-
[5,2]
fails
as arguments.
be
is pos-
that
programming
information
without
identity.
as proposedin [5],
argument
no
it is possible
is not
or abmodular
have
means
propagation
to
in
that
to transparent
formation
propagated
= false
shown
as an
propagating
accepted
but
regard
equivalent
functor : t
can
are the
information
abstract
which
signatures with
This
types This
with
variabIes types
a functor
not
signatures?
expressing
of type
boundaries.
boundaries
As
though
signatures
for
as abstract
function
pletely
seman-
is, what
opaque
acceptable
In [1], it is shown
modules. of
F, even
transparent
completely not
associated
type
limitations
program
the
thedependancy
readdressed
no propagation
Translucent
Simi-
that
to abstract
across
components
entities.
identities
following
of
a structureis
of the
as abstract
application,
propagated.
is the presence
after
identities
hidden
afunctor
of SML
means
by asignature
structure
nents
system
This
completely
function
expression. A key feature
to
with are
constructs
tions
that
that
argument
question
of programs,
More
indicate
in the
this
means
has to be propagated.
datatypes
structure
= CalcLex
tion
Transparent
s present
associated
stract
= CalcLrVals.ParserData
end
1.2
t and statically,
benefits
transparent This
check.
of APP must
Unimportant
;
=
completely
type
representation
between
= CalcLrVals.Tokens)
CalcParser
are
statically
known
CalcLexFun structure
signatures
must
CalcLrValsFun
be in-
in translucent is still
a fun-
signatures:
dependency
tobe
Completely not
transparent
require
Hence
tags
the
with
in
APP functor
generic
signatures,
signatures can
as in
stating
SML/NJ,
type
be written
the
semantics
do
it
semantics,
are we tied
two
elaboration:
way
has
been:
at
The
to
following
semantics may
is an
a semantic
the The
signature,
i.e.
is the
same
are left universally
quantified.
the
application
SML,
of
components
a functor
with
one Can
semantics functors.
dependencies
where
the
FF is
ist
as follows,
intuitive
simply-typed and
even
to
of
the
obvious
variables out
and,
most
when
strategy
can
process.
To
non-trivial.
are
going
to be
in
[4] for
as shown
general
unifiers
a unifier
exists
is to
which
is
variables
whether
to
dependencies
generativity,
works
A-terms
A-calculus
similar
matching
dependency
computing
a
other
of functor
propagate
absence
strategy
these
the
of a substitution tom. process? The rationale
signature
or simply-typed
its
have
and
elaboration
quantified
the
in
a semantics
the
unknown,
during
is because
functions
[7] was the
a strongly-
functors
time
application matching
in
for
a generativity where
we obtain
as universally
this
system
have
at definition
In are
them
that
This
are
a name
in
show
which which
the
not
to a semantics
functors,
be instantiated
a
a module,
by variables
behind present represent
of the with
module
does
time?
of first-order
such
with
components
with
has
a record
interface
represented
In
version
in SML
associated
of the
being
phase
which
application is merely the puted during the signature
of the
A formal
representation
of a structure.
unspecified
defined
with
specification
as that
SML.
is essentially
associated
semantic
presentation
or structure
which
types
structure.
in
A module
representation
giving
informal
functors
in [9, 10].
language,
that
Work
extremely
of first-order
be found
fields
Existing
a higher-order
typed
signatures.
Relation
2
to
information.
do not
ex-
is undecid-
able. functor
FF (X :signaturel)
=
(1)
structure]
A Language
3 If
~signaturel
tion
of
]
the
=
‘dfi.
functor
(Xl)
result
of elaboration
which
maps
X to
‘d~.
(Zl,
resulting
XI. then
expression
where
semantic
3.1
ZZ
semantic
The
represen-
with
domain
fi,
semantic
that
use
and
S1
of the
@(Z2)
as the
representation
of
a generic in the
static
contains
only
module
Only
semantics
presented order
in
case.
ture
and
[7],
for
Xl
be the
‘free’
X2
be the
result
which
of the
where
Algorithml
22
from
with
proceeds
are
to X
S1 is
Next,
returns tion
ML
is that are
to
and
FF then
the
variable
resulting
has a novel
the
same
we
which
a language
feature static
of
the
1 A free set
structure of
variables
may
afresh
would
tion
feature,
structure
like
at to
differ each
associated never
can If
with used
we (Vfi.
certain is,
if
tic
and
wish X)
to
not
as in
of
[5].
both
language
As
a structure. structures
contribute
to
presented
expressions
elabora-
as fields
any here
of struc-
The
the
is especially
This
their
gives
of the
as in
Type
sharing
is overlooked
names is obtained
here,
as this
is
defined
by
the
grammar
in a signature
body
path
where
Signature
int ::=
below.
I bool
/ T +
T I path
Signature
156
t / z.path
Components:
SC::=
Z is
before
type
Body:
has
t Itype
t =
r
I module
It
S , or a structure
Paths:r
static
an
presentation.
a-convertible
Expressions: ::=
as
is
algorithm:
of our
when
con-
SML,
semantics.
language
ques-
semana simpler
power
translation
internal
to
syntax-
specifications
constraints
convenient,
assignment. on the
to it
is at-
a signature
sharing
subject
having
semantic
Sharing in
expressive
the
to the
[2],
of principala new
signatures on
system
a simple
of exposition:
of changing in
property
fields
restriction
in In-
as a proof
allows
sharing
assumed that the fields body s are all distinct.
this
assigning
retaining
of shardiscussed
representation.
of functors.
a specification
orthogonal
by explicit
a simplifica-
is [E/@(x),
by
purpose
a matter
fields
the
elaboration
allowed.
translation
to present
of syntactic
the
As
functor
The
provide
like
This
pointed
paths.
semantic
prove
Accommodating really
its
language
is not
a specification
to be specified to
the
in the
a syntax-directed
we would
Such
while
no effect ap-
into
language
for
As
arbitrary
has
translation
As for
generativity, to
for
no identity of structures
prevents
paper,
counterparts.
issue
is eval-
equating
name.
merely
S1 matches
have
i’s allowe~.
signature
our
straints,
components,
we obtain
in
translation,
same
application.
address
is removed, semantics?
in
refer
the
type
same
fields
do
sharirw
it is necessary
this
is merely
What
to the
In
directed
),
of the
generativity.
applied
which
a path
intermediate
representation
of types
translation
tained
.X’ in Algorithml of the
the
generat-
expression
semantics semantic
and
immediately
an interme-
the
that
this
Let
If a structure
it is verified to the
to
semantics,
structures
allows
representation
elab-
semantics,
components.
applied
the
generated
which
a fresh
its
operationrd
structures
which
S’l
for
is in
[1 I],
ity.
expression.
Standard means
the
to generate
plication
description
S ML
for
an environas the
ing,
Let
Algorithml
operatiomd
First
references by
as per
language,
and
as follows:
substituted
uated,
is returned
description
wit h a defined
references
signature.
This
in
: signature.
is a step-by-step
be
expressions
Hence.
of a synt attic
of the
signature.
SML,
sharing
stead,
A func-
as follows:
with
what
first-
of struc-
functors.
of structural
But
as
the
an abstraction
is elaborated
FF is (AX
structural.
than
and
associated
X to xl.
representation
tion
(l),
SML/NJ,
representation
is no longer
of elaboration functor
in
complicated
of structures
1 structure
language,
contains
more
FF as in
maps
oration
functors
the semantic
signatures
declaration
diate
case,
representation
tor
ing
higher-order
is much
In this
functor
semantic
ment
for
is the
can
module
val
change
used,
functors
term
As
to be discussed.
the
expression. The
language
tures.
rules.
semantic
Modules
Language
and
functors.
Unlike Return
●
of the
structures
we
of the
such
syntax
both
as follows:
certain
Syntax
for Higher-Order
an environment
representation
~ with
) as per
Zh),
in
with
is computed
+(Z1
(Z1,
to a structure,
the
a substitution
matches
representa-
Vfl.
a functor,
is applied
S1,
e Obtain
by
semantic
of structural
If
ZZ),
representation
the
FF is given
is the tation
then
x : Sig
is
s ::= SClsc; s
Z
●
s
f unsig(x
VS(Z
Signature: Sig
::=
Structure
sigsend
funsig(x
: Sigl
raodule
z = m
Every
t= r
type
Struct~re
Body: ..— ..— SClsc;
Modu~e
Expressions: m
::=
: El)
Z)
signature
defined
by
E has
I m~mz
I structs
semantic
with
SZg) m
I m : S2g
Representation representation
appropriate
appropriate tween
Env
●
denotes
for
is similar
higher-order
for
the
Mod
to
functors,
absence
=
StrEnu
StrEnu
~
TypEnv
of
For
●
[9],
equality
be-
two
E
TypEnv
z
TypId
c
ModEnv
E
ModId
72)
E
B
NameSet
Func
y,
is the
set of all partial
to co-domain
a finite
subset
~
TypId,
ModId,
Y,
where
u Ez(x)
S[]
Mod x Mod
functions
every
[9],
which
are
NameSet
x Mod
=
for
from
function
are domains
are
used
types
and
away
as arguments,
present
in the
agated.
These
variables
: S ~
Env
&c[]
: S= ~
of the
is not
semantic Type
Send]
::=
Type tom
in
the
semantic
stamps:
represent
structure
known, are
a-
by the
needs
+
in ~~[type
higher-order
t]
the
The
ASExp(T),
I Ax.
H
grammar,
set of atomic
atomic
is a subset
The
in
X
s
of
Type atom
) U ASEXP(TZ
type
The
expressions. of
defined
)
with TE ment
S end,
its and
type its
appending
two
11 and
lists
VarList +
~
Mod
( TypEnvx ~
x
VarList
ModEnv)x
( TypEnvx
VarList
ModEnv)x
VarList
r w
is represented
components
module
object
a
+
by the
ME1)
, VI)
((TE2,
MEz)
, V2) = ~LISl
ruTEluMEl
((TEI
u TEz
, MEI
, v1@v2)
=
((t
w
: Z]r
type
as (TE,
being
in the
vv
let
(T,
in
(( O,x+T),
can
free-variables
the
in
and
the
Vl)
s
f(a)},
0))
synt attic
s is not
be abstracted
r w VI)
end]O[l
=
[f])
,
signature
known by
17.
=tlJUJ
concept:
type
({s*
and
environment
end)sig 0)+-
that, t
0) , 0)
~T_jr,
illustrates
a},
{f})
values =
~
variable.
all
typet
between
dependency
T,
, 0),
replaces
example
w
r w
U ME2)
new
((tw
=
= &[S~]
f(W)
is a completely
sig
idea
dency
Static
4.1
T-J
grammar
Sig,
the
statically.
a function
depenBut
variable
the j.
ME)
c StrEnv,
rules
elaboration
presented
of structure in
Figure
are the
signature
they
involve
a substitution.
2For are
157
Rules
here
an environ-
ME.
for
are
variables
an environment
generating
Semantics
Elaboration
guage
‘T E Mod.
generating
components
r w
[S.]
as fo~ows,
if T = TI
Z, as defined
as a semantic sig
+
:L[S]
r by their
following
The
●
=
merely
~[funsig(X:
subexpressions
signature
is represented
by
. . ..~~)
I H1 H2
4
A syntactic
otherwise
formed
r w
[r]r
&C[modulex
if T E Typeatom
ASExp(Tl
is defined
undefined
((TE1,
=r]
where
~, x are variables
{T}
{
if El (x)
VarList
~c
j
(Va(({t
Definition:
is defined
El(x)
=
r w
where
Twez
[xl.f(Hl,
Type
=
let
to be prop-
by
following
I Typel
intlbool
::=
denotes
Ez if Ez (x)
VarList ~
r w
functors
dependency
expressed
is defined
where
Type ~tom
+
r w
r w S]
identifiers,
When
but
[S.]
tL[Sc;
representation.
Typeatom
::= H
of
on
paths Type
the
set of iden-
argument-to-result
dependencies
TC
El,
E2(x)
Env
Env
&C[typet Definition:
from elements
domain
is defined
at compile-time.
the
functor
in the
list
: Sig ~
&L~]
t~
abstracted
are passed
domain,
are either
of X.
variables
exclusively
finite
Type
‘Z
tifiers/variables. In
which
12.
~[sig X
with
environments
11CNz, is the
●
Notation:
tion
any
El
x ModEnv
TE
X ~
as
&[].
of variables.
function,
and
+ Func
ME
Vs(z *
set of lists
to values,
{
g
representation function
or Mod.
structures.
M$
semantic
translation
the
is a partial
Type
of Signatures
of signatures
extensions
simphfications
(TE,
as
end set of variables
The
a unique
a syntax-directed
VarList
●
x I m.x
Semantic
is represented
Notation:
s
I functor(f-:
3.2
X2,
c Func.
Sigz
Components: ::=
+
valid
a most example,
matching
general if
substitutmns.
.f int
=
In
1.
rules, the
substitution int
then
expressions The
both
critical (4)
presence does .f/(kc.
and
in the
lan-
set
of rules
(5),
because
of higher-order not int)
exist,2 and
f/(Az.
and c)
(1)
r } m : r I- m.x r,f:%l-m:%
(TE,
ME)
where
r t-
functor(f
(2)
: ME(x)
: Z)m
(71 , V)
=
&[Z]
: W(Z
+
%)
r [1 (3)
(4)
rl-m
:?_
(17 DT,
Z,
Vu@)
where
(Z,
V)=
SID]r[l (5)
I’+(m:Z):@(?i)
(6) 17 E
struct
s end
: T
(7)
r 1- typet=r
:
({t
I+[7]r},0)
(8)
I’1--m:T
(9)
.rI-ruodulez=m:
Figure
there
is no decidable
quently form in for
show for
the
which
which
tion
all our
a most
format
versally is
by
grammer valid.
cult
to
these
has
a unique
the
Then
[8]:
semantics
which
satisfies
may
terms,
the
A
pro-
to
more
be closed the
notions
of subsumption
for
the
of the
set of free
end)
D
by
71,
Z,
V,
in the
be
where
when
to
U, as @( T2 ) and
described.
in range(r).
17 is replaced
~
is
@(Tz ) is
relation
set free variables in
~),
criteria
l,!- relation,
variables
Tz, V J
is provable
r
is a
TI must
In the formal
by
A,
where
A
is
range(r).
and
a simple
Definition:
al-
●
intuitive
than
wrt
(17
domain
parameter
definition
as
gorithm. The
relation with
general
necessary
capture
specifications
the
a substitution
is precisely
by
t end)
t = X.t
do as well.
Intuitively,
is diffi-
intuitions
these
type
type
uni-
these
be computed
of signature : si-g
(sig would
then
it
there
a functor funsi.g(x
J,l rela-
manages that
then
subsumptions
semantics
we capture
Elaboration
variables, the
7})
are
distinct If
of what
we go on to show
substitution
terms
definitional.
notion
for
{x++
syntactic
take on
1: Rules
we subse-
The
LA,
always
definitional
So instead
substitution
unique
in
induction
intuitive
whether
specifications.
exists.
completely
a completely
analyse
But
as arguments.
structural
becomes
intuitions.
unifier
is required,
some
In
matching.
are in a particular
unification
variables
semantics
are
for
substitution
defined,
for
terms
general
required
quantified
entire
this
algorithm
that
(0,
A
The
D
~,
domain
‘T2, V JJ @ iff
of ~,
I@/ = V,
a programmer
•~nV=O.
are: o Record
Polymorphism:
set
over
of fields
general
than
~,
If
another TI
structure
structure
Z
has
72 then
an
e TI , @ are closed
extra
wrt
A
25 is more o 72 is closed