hoop: Object-Oriented Programming in Haskell

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Library for object-oriented programming in Haskell.


[Skip to Readme]

Properties

Versions 0.3.0.0, 0.3.0.0
Change log None available
Dependencies base (>=4.7 && <5.0), containers, haskell-src-exts (>=1.16), haskell-src-meta (>=0.6), lens (>=4.10), mtl (>=2.1), parsec (>=3.1.9), pretty, template-haskell (>=2.14), text [details]
License MIT
Copyright Copyright (c) Michael B. Gale
Author Michael B. Gale
Maintainer m.gale@warwick.ac.uk
Category Language
Home page https://github.com/mbg/hoop#readme
Bug tracker https://github.com/mbg/hoop/issues
Source repo head: git clone https://github.com/mbg/hoop
Uploaded by mbg at 2020-07-04T19:10:06Z

Modules

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for hoop-0.3.0.0

[back to package description]

hoop

A Haskell library for object-oriented programming which allows programmers to use objects in ordinary Haskell programs. In particular, the library achieves the following design objectives (to avoid ambiguity with Haskell's type classes, we refer to classes in the object-oriented sense as object classes):

Examples

The test folder contains a number of examples of the library in action, illustrating the various features.

As a quick tutorial, a simple expression language can be implemented using the library as shown below. Note that the bodies of the two implementations of the eval method are ordinary Haskell expressions. The .! operator is an ordinary Haskell operator used to call methods on objects and this is just an ordinary Haskell definition, too.

[state|
abstract state Expr where
    eval :: Int

state Val : Expr where
    data val = 0 :: Int

    eval = do
        r <- this.!val
        return r

state Add : Expr where 
    data left :: Expr 
    data right :: Expr 

    eval = do 
        x <- this.!left.!eval 
        y <- this.!right.!eval 
        return (x+y)
|]

someExpr :: Add 
someExpr = new @Add (upcast $ new @Val 4, upcast $ new @Val 7)

someExprResult :: Int 
someExprResult = result (someExpr.!eval)

If we evaluate someExprResult, the result is 11 as expected. We can note some points of interest here that differ from popular object-oriented programming languages:

Indeed, we can cast the Add object to an Expr object, call eval on it, and still get the correct result:

> let e = upcast someExpr in result (e.!eval)
11

Overview of the process