https://github.com/fumieval/objective
Purely functional objects
https://github.com/fumieval/objective
Last synced: 3 months ago
JSON representation
Purely functional objects
- Host: GitHub
- URL: https://github.com/fumieval/objective
- Owner: fumieval
- License: bsd-3-clause
- Created: 2014-09-18T08:17:45.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2021-10-27T11:01:05.000Z (over 4 years ago)
- Last Synced: 2025-11-16T13:35:56.400Z (7 months ago)
- Language: Haskell
- Homepage:
- Size: 174 KB
- Stars: 73
- Watchers: 10
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
objective
====
[](https://hackage.haskell.org/package/objective) [](http://travis-ci.org/fumieval/objective)
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.
```haskell
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`.
```haskell
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.
```haskell
> 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.