objective: Composable objects

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]

Warnings:

Composable objects


[Skip to Readme]

Properties

Versions 0.0, 0.0.1, 0.0.2, 0.1, 0.2, 0.3, 0.4, 0.5, 0.5.1, 0.5.2, 0.5.2.1, 0.6, 0.6.1, 0.6.2, 0.6.3, 0.6.3.2, 0.6.3.3, 0.6.5, 0.6.5.1, 1, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.1, 1.1.1, 1.1.2, 1.2, 1.3, 1.3
Change log CHANGELOG.md
Dependencies base (>=4.9 && <5), exceptions (>=0.8), monad-skeleton (>=0.1.1 && <0.3), transformers (>=0.3 && <0.6), witherable (>=0.4 && <0.5) [details]
License BSD-3-Clause
Copyright Copyright (c) 2014-2021 Fumiaki Kinoshita
Author Fumiaki Kinoshita
Maintainer Fumiaki Kinoshita <fumiexcel@gmail.com>
Category Control
Home page https://github.com/fumieval/objective
Bug tracker http://github.com/fumieval/objective/issues
Source repo head: git clone https://github.com/fumieval/objective.git
Uploaded by FumiakiKinoshita at 2021-10-27T11:01:22Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for objective-1.3

[back to package description]

objective

Hackage Build Status

Paper: https://fumieval.github.io/papers/en/2015-Haskell-objects.html

This package provides composable objects and instances.

Introduction

The primal construct, Object, models object-oriented objects. Object f g represents an object.

newtype Object f g = Object { runObject :: forall x. f x -> g (x, Object f g) }

An object interprets a message f a and returns the result a and the next object Object f g, on g.

data Counter a where
  Increment :: Counter ()
  Print :: Counter Int

counter :: Int -> Object Counter IO
counter n = Object $ \case
  Increment -> return ((), counter (n + 1))
  Print -> print n >> return (n, counter n)

new :: Object f g -> IO (Instance f g) creates an instance of an object.

(.-) :: (MonadIO m, MonadMask m) => Instance f m -> f a -> m a sends a message to an instance. This can be used to handle instances in the typical OOP fashion.

> i <- new (counter 0)
> i .- Increment
> i .- Print
1
> i .- Increment
> i .- Print
2

Interestingly, Object (Skeleton t) m and Object t m are isomorphic (Skeleton is an operational monad). cascading lets objects to handle an operational monad.