A Language Prototyping Tool Based On Sem
A Language Prototyping Tool Based On Sem
1 Introduction
αkβ , Lα | Rβ
We also omit the type constructors in some definitions for brevity. The no-
tions we use from category theory are defined in the paper, so it is not a prereq-
uisite.
return : α → M α
(≫=) : M α → (α → M β) → M β
Id α ,α
return = λx → x
m ≫= f = f x
In the rest of the paper, we will use the do-notation defined as:
do { m; e } ≡ m ≫= λ → do { e }
do { x ← m; e } ≡ m ≫= λ x → do { e }
do { let exp; e } ≡ let exp in do { e }
do { e } ≡e
Name Operations
Environment Access rdEnv : M Env
inEnv : Env → M α → M α
State transformer update : (State → State) → M State
fetch : M State
set : State → M State
TEnv M α , Env → M α
return x = λρ → returnx
x ≫= f = λρ → (x ρ)≫=(λa → f a ρ)
lift x = λρ → x ≫=return
rdEnv = λρ → returnρ
inEnv ρ x = λ →xρ
State transformer
As in the case of monads, functors also come from category theory but can easily
be defined in a functional programming setting. A functor F can be defined as a
type constructor that transforms values of type α into values of type F α and a
function mapF : (α → β) → F α → F β.
The fixpoint of a functor F can be defined as
µF , In (F (µF))
T x , N Int | x + x | x − x
mapT : (α → β) → (T α → Tβ)
mapT f (N n) =n
mapT f (x1 + x2 ) = f x1 + f x2
mapT f (x1 − x2 ) = f x1 − f x2
Once we have the shape functor T, we can obtain the recursive datatype as
the fixpoint of T
Term , µT
(F ⊕ G) x , F x k G x
where mapF ⊕ G is
mapF ⊕ G : (α → β) → (F ⊕ G) α → (F ⊕ G) β
mapF ⊕ G f (L x ) = L (mapF f x )
mapF ⊕ G f (R x ) = R (mapG f x )
B x = B Bool | x == x | x < x
Expr , µ(T ⊕ B)
h . ϕ = ψ . mapF h
1
In the rest of the paper we omit the definition of map functions as they can be
automatically derived from the shape of the functor.
We consider a new category with F-algebras as objects and homomorphisms
between F-algebras as morphisms. In this category, In : F(µF) → µF is an initial
object, i.e. for any F-algebra ϕ : F α → α there is a unique homomorphism
([ϕ]) : µF → α satisfying the above equation.
([ϕ]) is called fold or catamorphism and satisfies a number of calculational
properties [3,6,35,42]. It can be defined as:
([ ]) : (Fα → α) → (µF → α)
([ϕ]) (In x ) = ϕ ( mapF ([ϕ]) x )
⊕ : (F α → α) → (G α → α) → (F ⊕ G)α → α
(ϕ ⊕ ψ)(L x ) = ϕ x
(ϕ ⊕ ψ)(R x ) = ψ x
bimapF : (α → γ) → (β → δ) → (F α β → F γ δ)
The fixpoint of two bifunctors F and G is a pair of values (µ1 FG,µ2 FG) that
can be defined as:
ϕ:Fαβ → α
ψ :Gαβ → β
([ , ])1 : (F α β → α) → (G α β → β) → (µ1 FG → α)
([ϕ, ψ])1 (In1 x ) = ϕ (bimapF ([ϕ, ψ])1 ([ϕ, ψ])2 x )
([ , ])2 : (F α β → α) → (G α β → β) → (µ2 FG → β)
([ϕ, ψ])2 (In2 x ) = ψ (bimapG ([ϕ, ψ])1 ([ϕ, ψ])2 x )
The sum of two bifunctors F and G is a new bifunctor F ⊞ G and can be
defined as:
(F ⊞ G) α β , F α β k G α β
where the bimap operator is
bimapF⊞G : (α → γ) → (β → δ) → ((F ⊞ G) α β → ((F ⊞ G) γ δ)
bimapF⊞G f g (L x ) = L (bimapF⊞G f g x )
bimapF⊞G f g (R x ) = R (bimapF⊞G f g x )
In order to extend two-sorted algebras, we define the operators ⊞1 and ⊞2
as:
(⊞1 ) : (F α β → α) → (G α β → α) → (F ⊞ G) α β → α
(φ1 ⊞1 φ2 ) (L x ) = φ1 x
(φ2 ⊞1 φ2 ) (R x ) = φ2 x
(⊞2 ) : (F α β → β) → (G α β → β) → (F ⊞ G) α β → β
(ψ1 ⊞2 ψ2 ) (L x ) = ψ1 x
(ψ2 ⊞2 ψ2 ) (R x ) = ψ2 x
ǫ22 : (F β → β) → F22 α β → β
ǫ22 ϕ x = ϕ x
4 Specification of a simple imperative language
V x , V Name
We will define commands in two steps. Firstly, sequence and assignments are
defined using the bifunctor S
S e c , c ; c | String := e
Secondly, control structures (conditional and loops) are defined using the
bifunctor R
R e c , If e c c | While e c
E , (T ⊕ B ⊕ V)21
C , S ⊞ R
Imp , µ2 E R
In this simple language, the computational structure needs to access the envi-
ronment and to transform a global state. We will use the monad Comp which
is obtained by transforming the identity monad using the monad transformers
TState and TEnv defined in table 2.
and the domain value of commands is the null type ()2 indicating that com-
mands do not return any value. The state and environment are defined as:
where Loc represent memory locations. We will also use the notation ς ⊲ {x /v }
to represent the updated state ς which assigns v to x .
In the same way, the specification of conditional and repetitive commands is:
References
1. Language Prototyping System. http://lsi.uniovi.es/~labra/LPS/LPS.html.
2. D. Álvarez, L. Tajes, F. Álvarez, M. A. Dı́az, R. Izquierdo, and J. M. Cueva.
An object-oriented abstract machine as the substrate for an object-oriented op-
erating system. In J. Bosch and S. Mitchell, editors, Object Oriented Technology
ECOOP’97, Jÿvaskÿla, Finland, June 1997. Springer Verlag, LNCS 1357.
3. Roland Backhouse, Patrik Jansson, Johan Jeuring, and Lambert Meertens. Generic
programming - an introduction. In S. D. Swierstra, P. R. Henriques, and J. N.
Oliveira, editors, Advanced Functional Programming, volume 1608 of Lecture Notes
in Computer Science. Springer, 1999.
4. L. S. Barbosa. Components as processes: An exercise in coalgebraic modeling. In
S. F. Smith and C. L. Talcott, editors, FMOODS’2000 - Formal Methods for Open
Object-Oriented Distributed Systems, pages 397–417, Stanford, USA, September
2000. Kluwer Academic Publishers.
5. N. Benton, J. Hughes, and E. Moggi. Monads and effects. In International Summer
School On Applied Semantics APPSEM’2000, Caminha, Portugal, 2000.
6. R. Bird and Oege de Moor. Algebra of Programming. Prentice Hall, 1997.
7. Pietro Ceciarelli and Engenio Moggi. A syntactic approach to modularity in de-
notational semantics. In 5th Biennial Meeting on Category Theory and Computer
Science, volume CTCS-5. CWI Technical Report, 1993.
8. M. A. Dı́az, D. Álvarez, A. Garcı́a-Mendoza, F. Álvarez, L. Tajes, and J. M. Cueva.
Merging capabilities with the object model of an object-oriented abstract machine.
In S. Demeyer and J. Bosch, editors, Ecoop’98 Workshop on Distributed Object
Security, volume 1543, pages 273–276. LNCS, 1998.
9. Luc Duponcheel. Writing modular interpreters using catamorphisms, subtypes and
monad transformers. Utrecht University, 1995.
10. David Espinosa. Semantic Lego. PhD thesis, Columbia University, 1995.
11. Maarten M. Fokkinga. Law and Order in Algorithmics. PhD thesis, University of
Twente, February 1992.
12. Maarten M. Fokkinga. Monadic maps and folds for arbitrary datatypes. Mem-
oranda Informatica 94-28, Dept. of Computer Science, Univ. of Twente, June
1994.
13. Benedict R. Gaster and Mark P. Jones. A Polymorphic Type System for Exten-
sible Records and Variants. Technical Report NOTTCS-TR-96-3, Department of
Computer Science, University of Nottingham, November 1996.
14. William Harrison and Samuel Kamin. Modular compilers based on monad trans-
formers. In Proceedings of the IEEE International Conference on Computer Lan-
guages, 1998.
15. William Harrison and Samuel Kamin. Compilation as metacomputation: Binding
time separation in modular compilers. In 5th Mathematics of Program Construction
Conference, MPC2000, Ponte de Lima, Portugal, June 2000.
16. R. Hinze. A generic programming extension for Haskell. In E. Meijer, editor,
Proceedings of the 3rd Haskell Workshop, Paris, France, September 1999. The
proceedings appear as a technical report of Universiteit Utrecht, UU-CS-1999-28.
17. Ralf Hinze. Deriving backtracking monad transformers. In Roland Backhouse
and J.Ñ. Oliveira, editors, Proceedings of the 2000 International Conference on
Functional Programming, Montreal, Canada, September 2000.
18. Ralf Hinze. A new approach to generic functional programming. In Conference
Record of POPL’00: The 27th ACM SIGPLAN-SIGACT Symposium on Principles
of Programming Languages, January 2000.
19. Z. Hu and H. Iwasaki. Promotional transformation of monadic programs. In Work-
shop on Functional and Logic Programming, Susono, Japan, 1995. World Scientific.
20. P. Hudak. Domain-specific languages. In Peter H. Salus, editor, Handbook of Pro-
gramming Languages, volume III, Little Languages and Tools. Macmillan Technical
Publishing, 1998.
21. H. Iwasaki, Z. Hu, and M. Takeichi. Towards manipulation of mutually recursive
functions. In 3rd. Fuji International Symposium on Functional and Logic Program-
ming (FLOPS’98), Kyoto, Japan, 1998. World Scientific.
22. B. Jacobs and E. Poll. A monad for basic java semantics. In T. Rus, editor,
Algebraic Methodology and Software Technology, number 1816 in LNCS. Springer,
2000.
23. Bart Jacobs. Coalgebraic reasoning about classes in object-oriented languages. In
Coalgebraic Methods in Computer Science, number 11. Electronic Notes in Com-
puter Science, 1998.
24. Mark P. Jones. First-class Polymorphism with Type Inference. In Proceedings of
the Twenty Fourth Annual ACM SIGPLAN-SIGACT Symposium on Principles of
Programming Languages, Paris, France, January 15-17 1997.
25. Mark P. Jones and L. Duponcheel. Composing monads. YALEU/DCS/RR 1004,
Yale University, New Haven, CT, USA, 1993.
26. S. Kamin. Research on domain-specific embedded languages and program genera-
tors. Electronic Notes in Theoretical Computer Science, Elsevier Press, 12, 1998.
27. Richard B. Kieburtz. Designing and implementing closed domain-specific lan-
guages. Invited talk at the Workshop on Semantics, Applications and Implemen-
tation of Program Generation (SAIG), 2000.
28. J. E. Labra. An implementation of modular monadic semantics using folds and
monadic folds. In Workshop on Research Themes on Functional Programming,
Third International Summer School on Advanced Functional Programming, Braga
- Portugal, 1998.
29. J. E. Labra, J. M. Cueva, and C. Luengo. Language prototyping using modular
monadic semantics. In 3rd Latin-American Conference on Functional Program-
ming, Recife - Brazil, March 1999.
30. J. E. Labra, J. M. Cueva, and M. C. Luengo. Modular development of interpreters
from semantic building blocks. In The 12th Nordic Workshop on Programming
Theory, Bergen, Norway, October 2000. University of Bergen.
31. Sheng Liang. Modular Monadic Semantics and Compilation. PhD thesis, Graduate
School of Yale University, May 1998.
32. Sheng Liang and Paul Hudak. Modular denotational semantics for compiler con-
struction. In Programming Languages and Systems – ESOP’96, Proc. 6th European
Symposium on Programming, Linköping, volume 1058 of Lecture Notes in Com-
puter Science, pages 219–234. Springer-Verlag, 1996.
33. Sheng Liang, Paul Hudak, and Mark P. Jones. Monad transformers and modular
interpreters. In 22nd ACM Symposium on Principles of Programming Languages,
San Francisco, CA. ACM, January 1995.
34. Grant Malcolm. Data structures and program transformation. Science of Computer
Programming, 14:255–279, 1990.
35. E. Meijer, M. M. Fokkinga, and R. Paterson. Functional programming with ba-
nanas, lenses, envelopes and barbed wire. In Functional Programming and Com-
puter Architecture, pages 124–144. Springer-Verlag, 1991.
36. E. Meijer and J. Jeuring. Merging monads and folds for functional programming.
In J. Jeuring and E. Meijer, editors, Advanced Functional Programming, Lecture
Notes in Computer Science 925, pages 228–266. Springer-Verlag, 1995.
37. Erik Meijer. Calculating Compilers. PhD thesis, University of Nijmegen, February
1992.
38. E. Moggi. Metalanguages and applications. In A. M. Pitts and P. Dybjer, edi-
tors, Semantics and Logics of Computation, Publications of the Newton Institute.
Cambridge University Press, 1997.
39. Eugenio Moggi. An abstract view of programming languages. Technical Report
ECS-LFCS-90-113, Edinburgh University, Dept. of Computer Science, June 1989.
Lecture Notes for course CS 359, Stanford University.
40. Eugenio Moggi. Notions of computacion and monads. Information and Computa-
tion, (93):55–92, 1991.
41. Alberto Pardo. Fusion of monadic (co)recursive programs. In Roland Back-
house and Tim Sheard, editors, Proceedings Workshop on Generic Program-
ming, WGP’98, Marstrand, Sweden, 18 June 1998. Dept. of Computing Science,
Chalmers Univ. of Techn., and Göteborg Univ., June 1998.
42. Tim Sheard and Leonidas Fegaras. A fold for all seasons. In Proceedings 6th
ACM SIGPLAN/SIGARCH Intl. Conf. on Functional Programming Languages
and Computer Architecture, FPCA’93, Copenhagen, Denmark, 9–11 June 1993,
pages 233–242. ACM, New York, 1993.
43. Guy L. Steele, Jr. Building interpreters by composing monads. In Conference
record of POPL ’94, 21st ACM SIGPLAN-SIGACT Symposium on Principles of
Programming Languages: Portland, Oregon, January 17–21, 1994, pages 472–492,
New York, USA, 1994. ACM Press.
44. L. Tajes, F. Álvarez, D. Álvarez, M. A. Dı́az, and J.M. Cueva. A computational
model for a distributed object-oriented operating system based on a reflective
abstract machine. In S. Demeyer and J. Bosch, editors, Ecoop’98 Workshop on
Reflective Object-Oriented Programming and Systems, volume 1543, pages 382–
383. LNCS, 1998.
45. Daniele Turi and Jan Rutten. On the foundations of final coalgebra semantics:
non-well-founded sets, partial orders, metric spaces. Mathematical Structures in
Computer Science, 8(5):481–540, 1998.
46. Arie van Deursen, Paul Klint, and Joost Visser. Domain-specific languages: An
annotated bibliography. ACM SIGPLAN Notices, 35(6):26–36, June 2000.
47. P. Wadler. Comprehending monads. In ACM Conference on Lisp and Functional
Programming, pages 61–78, Nice, France, June 1990. ACM Press.
48. P. Wadler. Monads for functional programming. Lecture notes for Marktoberdorf
Summer School on Program Design Calculi, Springer-Verlag, Aug 1992.
49. P. Wadler. The essence of functional programming. In POPL ’92, Albuquerque,
1992.