objective: Composable objects

[ bsd3, control, library ] [ Propose Tags ]

Composable objects


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 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 (info)
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:33Z
Distributions
Reverse Dependencies 3 direct, 0 indirect [details]
Downloads 21061 total (72 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

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.