Skip to content

Commit c7289e9

Browse files
chshershvrom911
authored andcommitted
[#78] Better docs, prepare for the release (#118)
* [#78] Better docs, prepare for the release Resolves #78 * Update co-log-core/src/Colog/Core/Action.hs Co-Authored-By: chshersh <[email protected]> * Minor fixes to docs * Minor
1 parent 765ba3f commit c7289e9

File tree

8 files changed

+123
-25
lines changed

8 files changed

+123
-25
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Build status](https://img.shields.io/travis/kowainik/co-log.svg?logo=travis)](https://travis-ci.org/kowainik/co-log)
55

66
| | | | |
7-
| :------------ | :--------------------------------- | :---------------------------------------: | :---------------------------------------------------- |
7+
| :------------ | :--------------------------------- | :---------------------------------------- | :---------------------------------------------------- |
88
| `co-log-core` | [![Hackage][hk-img-core]][hk-core] | [![Stackage LTS][lts-img-core]][lts-core] | [![Stackage Nightly][nightly-img-core]][nightly-core] |
99
| `co-log` | [![Hackage][hk-img]][hk] | [![Stackage LTS][lts-img]][lts] | [![Stackage Nightly][nightly-img]][nightly] |
1010
| `co-log-polysemy` | [![Hackage][hk-img-ps]][hk-ps] | [![Stackage LTS][lts-img-ps]][lts-ps] | [![Stackage Nightly][nightly-img-ps]][nightly-ps] |
@@ -24,8 +24,10 @@ The repository contains the following packages:
2424
on `co-log-core` and the [`polysemy`](http://hackage.haskell.org/package/polysemy) extensible effects library.
2525
* [`co-log-benchmark`](co-log-benchmark): Benchmarks of the `co-log` library.
2626

27-
See [How to start using `co-log`?](co-log/README.md) for simple example of the
28-
`co-log` library usage.
27+
See the following tutorial series about the library:
28+
29+
* [Intro: Using `LogAction`](https://github.com/kowainik/co-log/blob/master/co-log/tutorials/1-intro/Intro.md)
30+
* [Using custom monad that stores `LogAction` inside its environment](https://github.com/kowainik/co-log/blob/master/co-log/tutorials/2-custom/Custom.md)
2931

3032
## Benchmarks
3133

co-log-core/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
`co-log-core` uses [PVP Versioning][1].
44
The change log is available [on GitHub][2].
55

6-
## Unreleased
6+
## 0.1.2.0 — May 5, 2019
77

88
* [#85](https://github.com/kowainik/co-log/issues/85):
99
Move `overLogAction` to `HasLog` typeclass
1010
* [#101](https://github.com/kowainik/co-log/issues/101):
1111
Add `logActionL` lens with default implementation to `HasLog` type class.
1212
* [#99](https://github.com/kowainik/co-log/issues/99):
1313
Add comonadic combinators: `duplicate` and `multiplicate`.
14+
* [#78](https://github.com/kowainik/co-log/issues/78):
15+
Improve documentation significantly.
1416

1517
## 0.1.1 — Nov 15, 2018
1618

co-log-core/co-log-core.cabal

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 2.4
22
name: co-log-core
3-
version: 0.1.1
3+
version: 0.2.0.0
44
synopsis: Composable Contravariant Comonadic Logging Library
55
description:
66
This package provides core types and functions to work with the @LogAction@ data type which is both simple and powerful.
@@ -14,6 +14,12 @@ description:
1414
The ideas behind this package are described in the following blog post:
1515
.
1616
* [co-log: Composable Contravariant Combinatorial Comonadic Configurable Convenient Logging](https://kowainik.github.io/posts/2018-09-25-co-log)
17+
.
18+
See the following packages for different implementations based on @co-log-core@:
19+
.
20+
* [co-log](http://hackage.haskell.org/package/co-log): taggless final implementations.
21+
* [co-log-polysemy](http://hackage.haskell.org/package/co-log-polysemy): extensible
22+
effects implementation based on @polysemy@.
1723

1824
homepage: https://github.com/kowainik/co-log
1925
bug-reports: https://github.com/kowainik/co-log/issues

co-log-core/src/Colog/Core.hs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,27 @@ Copyright: (c) 2018-2019 Kowainik
33
License: MIT
44
Maintainer: Kowainik <[email protected]>
55
6-
Exports all core functionality.
6+
Exports all core functionality. @co-log-core@ is a lightweight package that
7+
defines only core data type and various combinators to work with it.
8+
9+
Fundamentals of @co-log-core@ are based on the following data type:
10+
11+
@
12+
__newtype__ LogAction m msg = LogAction
13+
{ unLogAction :: msg -> m ()
14+
}
15+
@
16+
17+
This data type provides extremely composable and flexible interface by having
18+
many instances of the standard algebraic data types.
19+
20+
The package has the following structure:
21+
22+
* __"Colog.Core.Action":__ definition of the main data type and its combinators.
23+
* __"Colog.Core.Class":__ 'HasLog' typeclass that describes how different values
24+
(e.g. application environment) can store and modify 'LogAction'.
25+
* __"Colog.Core.IO":__ basic loggers that work with 'Control.Monad.IO.Class.MonadIO' and 'String'.
26+
* __"Colog.Core.Severity":__ logger severity.
727
-}
828
module Colog.Core
929
( module Colog.Core.Action

co-log-core/src/Colog/Core/Action.hs

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Colog.Core.Action
1818
, foldActions
1919

2020
-- * Contravariant combinators
21+
-- $contravariant
2122
, cfilter
2223
, cmap
2324
, (>$<)
@@ -26,18 +27,21 @@ module Colog.Core.Action
2627
, cmapM
2728

2829
-- * Divisible combinators
30+
-- $divisible
2931
, divide
3032
, conquer
3133
, (>*<)
3234
, (>*)
3335
, (*<)
3436

3537
-- * Decidable combinators
38+
-- $decidable
3639
, lose
3740
, choose
3841
, (>|<)
3942

4043
-- * Comonadic combinators
44+
-- $comonad
4145
, extract
4246
, extend
4347
, (=>>)
@@ -77,8 +81,11 @@ can be either 'IO' or some custom pure monad.
7781
Key design point here is that 'LogAction' is:
7882
7983
* 'Semigroup'
80-
* Contravariant
81-
* Comonad
84+
* 'Monoid'
85+
* 'Data.Functor.Contravariant.Contravariant'
86+
* 'Data.Functor.Contravariant.Divisible.Divisible'
87+
* 'Data.Functor.Contravariant.Divisible.Decidable'
88+
* 'Control.Comonad.Comonad'
8289
-}
8390
newtype LogAction m msg = LogAction
8491
{ unLogAction :: msg -> m ()
@@ -193,6 +200,16 @@ foldActions actions = LogAction $ \a -> for_ actions $ \(LogAction action) -> ac
193200
-- Contravariant combinators
194201
----------------------------------------------------------------------------
195202

203+
{- $contravariant
204+
205+
Combinators that implement interface in the spirit of the following typeclass:
206+
207+
@
208+
__class__ Contravariant f __where__
209+
contramap :: (a -> b) -> f b -> f a
210+
@
211+
-}
212+
196213
{- | Takes predicate and performs given logging action only if predicate returns
197214
'True' on input logging message.
198215
-}
@@ -320,6 +337,17 @@ cmapM f (LogAction action) = LogAction (f >=> action)
320337
-- Divisible combinators
321338
----------------------------------------------------------------------------
322339

340+
{- $divisible
341+
342+
Combinators that implement interface in the spirit of the following typeclass:
343+
344+
@
345+
__class__ Contravariant f => Divisible f __where__
346+
conquer :: f a
347+
divide :: (a -> (b, c)) -> f b -> f c -> f a
348+
@
349+
-}
350+
323351
{- | @divide@ combinator from @Divisible@ type class.
324352
325353
>>> logInt = LogAction print
@@ -383,6 +411,17 @@ infixr 4 *<
383411
-- Decidable combinators
384412
----------------------------------------------------------------------------
385413

414+
{- $decidable
415+
416+
Combinators that implement interface in the spirit of the following typeclass:
417+
418+
@
419+
__class__ Divisible f => Decidable f __where__
420+
lose :: (a -> Void) -> f a
421+
choose :: (a -> Either b c) -> f b -> f c -> f a
422+
@
423+
-}
424+
386425
-- | @lose@ combinator from @Decidable@ type class.
387426
lose :: (a -> Void) -> LogAction m a
388427
lose f = LogAction (absurd . f)
@@ -418,6 +457,18 @@ infixr 3 >|<
418457
-- Comonadic combinators
419458
----------------------------------------------------------------------------
420459

460+
{- $comonad
461+
462+
Combinators that implement interface in the spirit of the following typeclass:
463+
464+
@
465+
__class__ Functor w => Comonad w __where__
466+
extract :: w a -> a
467+
duplicate :: w a -> w (w a)
468+
extend :: (w a -> b) -> w a -> w b
469+
@
470+
-}
471+
421472
{- | If @msg@ is 'Monoid' then 'extract' performs given log action by passing
422473
'mempty' to it.
423474
@@ -434,15 +485,15 @@ extract action = unLogAction action mempty
434485
435486
>>> f (LogAction l) = l ".f1" *> l ".f2"
436487
>>> g (LogAction l) = l ".g"
437-
>>> unLogAction logStringStdout "foo"
488+
>>> logStringStdout <& "foo"
438489
foo
439-
>>> unLogAction (extend f logStringStdout) "foo"
490+
>>> extend f logStringStdout <& "foo"
440491
foo.f1
441492
foo.f2
442-
>>> unLogAction (extend g $ extend f logStringStdout) "foo"
493+
>>> (extend g $ extend f logStringStdout) <& "foo"
443494
foo.g.f1
444495
foo.g.f2
445-
>>> unLogAction (logStringStdout =>> f =>> g) "foo"
496+
>>> (logStringStdout =>> f =>> g) <& "foo"
446497
foo.g.f1
447498
foo.g.f2
448499
-}
@@ -477,7 +528,7 @@ in duplicate logger <& ([3, 4], [42, 10])
477528
478529
__Implementation note:__
479530
480-
True and fair translation of the @duplicate@ function from the 'Comonad'
531+
True and fair translation of the @duplicate@ function from the 'Control.Comonad.Comonad'
481532
interface should result in the 'LogAction' of the following form:
482533
483534
@

co-log-core/src/Colog/Core/Class.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,30 @@ functions.
2929
It also provides the useful lens 'logActionL' with the default implementation using type
3030
class methods. The default one could be easily overritten under your instances.
3131
32-
TODO: laws
32+
Every instance of the this typeclass should satisfy the following laws:
33+
34+
1. __Set-Get:__ @'getLogAction' ('setLogAction' l env) ≡ l@
35+
2. __Get-Set:__ @'setLogAction' ('getLogAction' env) env ≡ env@
36+
3. __Set-Set:__ @'setLogAction' l2 ('setLogAction' l1 env) ≡ 'setLogAction' l2 env@
37+
4. __Set-Over:__ @'overLogAction' f env ≡ 'setLogAction' (f $ 'getLogAction' env) env@
3338
-}
3439
class HasLog env msg m where
3540
{-# MINIMAL getLogAction, (setLogAction | overLogAction) #-}
41+
42+
-- | Extracts 'LogAction' from the environment.
3643
getLogAction :: env -> LogAction m msg
3744

45+
-- | Sets 'LogAction' to the given one inside the environment.
3846
setLogAction :: LogAction m msg -> env -> env
3947
setLogAction = overLogAction . const
4048
{-# INLINE setLogAction #-}
4149

50+
-- | Applies function to the 'LogAction' inside the environment.
4251
overLogAction :: (LogAction m msg -> LogAction m msg) -> env -> env
4352
overLogAction f env = setLogAction (f $ getLogAction env) env
4453
{-# INLINE overLogAction #-}
4554

55+
-- | Lens for 'LogAction' inside the environment.
4656
logActionL :: Lens' env (LogAction m msg)
4757
logActionL = lens getLogAction (flip setLogAction)
4858
{-# INLINE logActionL #-}

co-log-core/src/Colog/Core/IO.hs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ Copyright: (c) 2018-2019 Kowainik
33
License: MIT
44
Maintainer: Kowainik <[email protected]>
55
6-
Introduces logging actions working in 'MonadIO'.
6+
Introduces logging actions working in 'MonadIO'. These actions are very basic
7+
and inefficient because they use the 'String' data type. If you don't want to
8+
have extra dependencies and performance of logging is not the bottleneck of your
9+
application, then these functions should be enough. Otherwise use functions from
10+
the "Colog.Actions" module from the @co-log@ package.
711
-}
812

913
module Colog.Core.IO
@@ -27,13 +31,17 @@ import Colog.Core.Action (LogAction (..))
2731
import Control.Monad.IO.Class (MonadIO, liftIO)
2832
import System.IO (Handle, IOMode (AppendMode), hPrint, hPutStrLn, stderr, withFile)
2933

34+
{- $setup
35+
>>> import Colog.Core.Action
36+
-}
37+
3038
----------------------------------------------------------------------------
3139
-- String
3240
----------------------------------------------------------------------------
3341

3442
{- | Action that prints 'String' to stdout.
3543
36-
>>> unLogAction logStringStdout "foo"
44+
>>> logStringStdout <& "foo"
3745
foo
3846
-}
3947
logStringStdout :: MonadIO m => LogAction m String
@@ -43,7 +51,7 @@ logStringStdout = LogAction (liftIO . putStrLn)
4351

4452
{- | Action that prints 'String' to stderr.
4553
46-
>>> unLogAction logStringStderr "foo"
54+
>>> logStringStderr <& "foo"
4755
foo
4856
-}
4957
logStringStderr :: MonadIO m => LogAction m String
@@ -53,7 +61,7 @@ logStringStderr = logStringHandle stderr
5361

5462
{- | Action that prints 'String' to 'Handle'.
5563
56-
>>> unLogAction (logStringHandle stderr) "foo"
64+
>>> logStringHandle stderr <& "foo"
5765
foo
5866
-}
5967
logStringHandle :: MonadIO m => Handle -> LogAction m String
@@ -68,7 +76,7 @@ opening file each time we need to write to it.
6876
6977
Opens file in 'AppendMode'.
7078
71-
>>> logger action = unLogAction action "foo"
79+
>>> logger action = action <& "foo"
7280
>>> withLogStringFile "/dev/stdout" logger
7381
foo
7482
-}
@@ -83,7 +91,7 @@ withLogStringFile path action = withFile path AppendMode $ action . logStringHan
8391

8492
{- | Action that prints to stdout using 'Show'.
8593
86-
>>> unLogAction logPrint 5
94+
>>> logPrint <& 5
8795
5
8896
-}
8997
logPrint :: forall a m . (Show a, MonadIO m) => LogAction m a
@@ -93,7 +101,7 @@ logPrint = LogAction $ liftIO . print
93101

94102
{- | Action that prints to stderr using 'Show'.
95103
96-
>>> unLogAction logPrintStderr 5
104+
>>> logPrintStderr <& 5
97105
5
98106
-}
99107
logPrintStderr :: forall a m . (Show a, MonadIO m) => LogAction m a
@@ -103,7 +111,7 @@ logPrintStderr = logPrintHandle stderr
103111

104112
{- | Action that prints to a 'Handle' using 'Show'.
105113
106-
>>> unLogAction (logPrintHandle stderr) 5
114+
>>> logPrintHandle stderr <& 5
107115
5
108116
-}
109117
logPrintHandle :: forall a m . (Show a, MonadIO m) => Handle -> LogAction m a
@@ -129,7 +137,7 @@ withLogPrintFile path action = withFile path AppendMode $ action . logPrintHandl
129137
{- | Lifts a LogAction over IO into a more general Monad.
130138
131139
>>> logToStdout = LogAction putStrLn
132-
>>> unLogAction (liftLogIO logToStdout) "foo"
140+
>>> liftLogIO logToStdout <& "foo"
133141
foo
134142
-}
135143
liftLogIO :: MonadIO m => LogAction IO msg -> LogAction m msg

co-log-core/src/Colog/Core/Severity.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Instead of using full names of the constructors you can instead use one-letter
6767
patterns. To do so you can import and use the pattern:
6868
6969
@
70-
import Colog (pattern D)
70+
__import__ Colog (__pattern__ D)
7171
7272
example :: WithLog env Message m => m ()
7373
example = log D "I'm using severity pattern"
@@ -89,7 +89,6 @@ pattern W <- Warning where W = Warning
8989
pattern E <- Error where E = Error
9090
{-# COMPLETE D, I, W, E #-}
9191

92-
9392
-- | Filters messages by the given 'Severity'.
9493
filterBySeverity
9594
:: Applicative m

0 commit comments

Comments
 (0)