Skip to content

Commit

Permalink
Add Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-adam committed Jan 7, 2022
1 parent 23c17a9 commit 982b366
Show file tree
Hide file tree
Showing 75 changed files with 1,266 additions and 573 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: all bench build check clean configure golden fast haddock hlint main repl report run stan stylish test update
.PHONY: all bench build check clean configure golden fast haddock hlint main output repl report run stan stylish test update

all: update fast bench

Expand Down Expand Up @@ -36,6 +36,9 @@ hlint:
main:
make stylish configure check build test

output:
if test -d .output; then rm -r .output; fi

repl:
cabal new-repl lib:helma

Expand Down
1 change: 1 addition & 0 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Tools and libraries:
* [x] [slist](https://kowainik.github.io/projects/slist) for sized list
* [x] [ListLike](https://hackage.haskell.org/package/ListLike) for list abstraction
* [ ] [mono-traversable](https://github.com/snoyberg/mono-traversable#readme) for list abstraction
* [ ] [lens](https://github.com/ekmett/lens#lens-lenses-folds-and-traversals) for for modification
* For production code:
* [x] [mtl](https://github.com/haskell/mtl) for IoC
* [X] [polysemy](https://github.com/polysemy-research/polysemy#readme) for IoC
Expand Down
7 changes: 7 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# 📅 Revision history for HelMA

## 0.6.12.0 -- 2022-01-07

* Add `Logger` based on Writer
* Refactor `Safe`
* Replace `CartesianProduct` to `ZipA`
* Use local `hlint`

## 0.6.11.2 -- 2021-12-14

* Fix version in cabal
Expand Down
374 changes: 374 additions & 0 deletions docs/reports/hlint.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reports/stan.html

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions helma.cabal
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cabal-version: 2.4

name: helma
version: 0.6.11.2
version: 0.6.12.0

synopsis: HELMA - Heavenly Esoteric Little Minimal Automaton for Esoteric Languages
description: Please see the README on GitHub at <https://github.com/helvm/helma#readme>
Expand Down Expand Up @@ -113,16 +113,21 @@ library
HelVM.Common.Containers.MTInsertDef
HelVM.Common.Containers.Util

HelVM.Common.Control.Control
HelVM.Common.Control.Message
HelVM.Common.Control.Logger
HelVM.Common.Control.Safe

HelVM.Common.Digit.Digitable
HelVM.Common.Digit.Digits
HelVM.Common.Digit.ToDigit

HelVM.Common.ListLikeUtil
HelVM.Common.NamedValue
HelVM.Common.ReadText
HelVM.Common.Safe
HelVM.Common.SequencesUtil
HelVM.Common.Util
HelVM.Common.ZipA

HelVM.HelMA.Automata.BrainFuck.Evaluator.TEvaluator
HelVM.HelMA.Automata.BrainFuck.Evaluator.IEvaluator
Expand Down Expand Up @@ -215,12 +220,13 @@ test-suite helma-test
other-modules:
Spec

HelVM.CartesianProduct
HelVM.Expectations
HelVM.GoldenExpectations
HelVM.MonadErrorSpec

HelVM.Common.Collections.MapListSpec
HelVM.Common.Control.LoggerSpec

HelVM.Common.ListLikeUtilSpec

HelVM.HelMA.Automata.BrainFuck.Evaluator.IEvaluatorSpec
Expand Down Expand Up @@ -262,6 +268,7 @@ test-suite helma-test

, helma

, dlist
, filepath
, ListLike
, mtl
Expand All @@ -280,7 +287,6 @@ benchmark helma-benchmark
hs-source-dirs: hs/benchmark
main-is: Main.hs
other-modules:
HelVM.CartesianProduct

HelVM.HelMA.Automata.BrainFuck.Evaluator.IEvaluatorBenchMark
HelVM.HelMA.Automata.BrainFuck.Evaluator.TEvaluatorBenchMark
Expand Down
5 changes: 4 additions & 1 deletion hlint.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#!/usr/bin/env bash

curl -sSL https://raw.github.com/ndmitchell/hlint/master/misc/run.sh | sh -s .
#curl -sSL https://raw.github.com/ndmitchell/hlint/master/misc/run.sh | sh -s .
hlint . --report=hlint.html --timing

mv hlint.html docs/reports
4 changes: 2 additions & 2 deletions hs/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import HelVM.HelMA.Automaton.API.TypeOptions

import HelVM.HelMA.Automaton.IO.BusinessIO

import HelVM.Common.Safe
import HelVM.Common.Control.Control

import HelVM.HelMA.Automaton.Types.CellType
import HelVM.HelMA.Automaton.Types.IntCellType
Expand Down Expand Up @@ -83,7 +83,7 @@ parse WS a = pPrintNoColor . flip (WS.parse WhiteTokenType) a
parse lang _ = tokenize lang

eval :: TypeOptions -> AsciiLabels -> Lang -> Source -> IO ()
eval options a lang s = (exceptTToIO . evalParams lang) params
eval options a lang s = (controlTToIO . evalParams lang) params
where params = EvalParams {asciiLabel = a , source = s , typeOptions = options}

evalParams :: BIO m => Lang -> EvalParams -> m ()
Expand Down
9 changes: 0 additions & 9 deletions hs/benchmark/HelVM/CartesianProduct.hs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import HelVM.HelMA.Automata.BrainFuck.Evaluator.IEvaluator
import HelVM.HelMA.Automata.BrainFuck.FileUtil

import HelVM.HelMA.Automaton.IO.MockIO

import HelVM.HelMA.Automaton.Types.CellType

import qualified Data.ListLike as LL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import HelVM.HelMA.Automaton.IO.MockIO
import HelVM.HelMA.Automaton.Types.RAMType
import HelVM.HelMA.Automaton.Types.StackType

import HelVM.CartesianProduct
import HelVM.Common.ZipA

import System.FilePath.Posix

Expand Down
4 changes: 2 additions & 2 deletions hs/src/HelVM/Common/Collections/MapList.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module HelVM.Common.Collections.MapList where

import HelVM.Common.Containers.LLIndexSafe
import HelVM.Common.Containers.LLInsertDef
import HelVM.Common.Safe
import HelVM.Common.Control.Safe

import Data.Default

Expand Down Expand Up @@ -79,7 +79,7 @@ instance {-# OVERLAPPING #-} IndexSafe (MapList a) a where
findMaybe = mapListFindMaybe
indexMaybe = mapListIndexMaybe
findSafe i = liftMaybeOrError "MapList.findSafe: index is not correct" . mapListFindMaybe i
indexSafe l = liftMaybeOrError "MapList.indexSafe: index is not correct" . mapListIndexMaybe l
indexSafe l = liftMaybeOrError "MapList.LLIndexSafe: index is not correct" . mapListIndexMaybe l

instance InsertDef (MapList a) a where
insertDef i e = fromIntMap . IntMap.insert i e . unMapList
Expand Down
12 changes: 6 additions & 6 deletions hs/src/HelVM/Common/Containers/LLIndexSafe.hs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{-# LANGUAGE UndecidableInstances #-}
module HelVM.Common.Containers.LLIndexSafe where

import HelVM.Common.Safe
import HelVM.Common.Control.Safe

import Data.ListLike

import Prelude hiding (break, divMod, drop, fromList, length, splitAt, swap, uncons)
import Prelude hiding (break, divMod, drop, fromList, length, splitAt, swap, uncons)

-- | Index
naturalIndexSafe :: (MonadSafeError m , IndexSafe full item) => full -> Natural -> m item
naturalIndexSafe :: (MonadSafe m , IndexSafe full item) => full -> Natural -> m item
naturalIndexSafe l = indexSafe l . fromIntegral

-- | Type Class
class IndexSafe full item | full -> item where
findWithDefault :: item -> Int -> full -> item
findMaybe :: Int -> full -> Maybe item
indexMaybe :: full -> Int -> Maybe item
findSafe :: MonadSafeError m => Int -> full -> m item
indexSafe :: MonadSafeError m => full -> Int -> m item
findSafe :: MonadSafe m => Int -> full -> m item
indexSafe :: MonadSafe m => full -> Int -> m item

instance ListLike full item => IndexSafe full item where
findWithDefault e i = fromMaybe e . findMaybe i
Expand All @@ -27,7 +27,7 @@ instance ListLike full item => IndexSafe full item where
indexSafe = indexSafeLL

-- | Internal functions
indexSafeLL :: (MonadSafeError m , ListLike full item) => full -> Int -> m item
indexSafeLL :: (MonadSafe m , ListLike full item) => full -> Int -> m item
indexSafeLL l i
| i < 0 = liftError "LLIndexSafe.indexSafeLL: index must be >= 0"
| length l <= i = liftError "LLIndexSafe.indexSafeLL: index must not found"
Expand Down
10 changes: 5 additions & 5 deletions hs/src/HelVM/Common/Containers/MTIndexSafe.hs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{-# LANGUAGE UndecidableInstances #-}
module HelVM.Common.Containers.MTIndexSafe where

import HelVM.Common.Safe
import HelVM.Common.Control.Safe

import Control.Type.Operator
import Data.MonoTraversable
import Data.Sequences

import Prelude hiding (break, divMod, drop, fromList, length, splitAt, swap, uncons)
import Prelude hiding (break, divMod, drop, fromList, length, splitAt, swap, uncons)

-- | Index
naturalIndexSafe :: (MonadSafeError m , IndexSafe seq , Num $ Index seq) => seq -> Natural -> m $ Element seq
naturalIndexSafe :: (MonadSafe m , IndexSafe seq , Num $ Index seq) => seq -> Natural -> m $ Element seq
naturalIndexSafe l = indexSafe l . fromIntegral

-- | Type Class
class IndexSafe seq where
findWithDefault :: Element seq -> Index seq -> seq -> Element seq
findMaybe :: Index seq -> seq -> Maybe $ Element seq
indexMaybe :: seq -> Index seq -> Maybe $ Element seq
findSafe :: MonadSafeError m => Index seq -> seq -> m $ Element seq
indexSafe :: MonadSafeError m => seq -> Index seq -> m $ Element seq
findSafe :: MonadSafe m => Index seq -> seq -> m $ Element seq
indexSafe :: MonadSafe m => seq -> Index seq -> m $ Element seq

instance IsSequence seq => IndexSafe seq where
findWithDefault e i = fromMaybe e . findMaybe i
Expand Down
6 changes: 3 additions & 3 deletions hs/src/HelVM/Common/Containers/Util.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module HelVM.Common.Containers.Util where

import HelVM.Common.Safe
import HelVM.Common.Control.Safe

import Relude.Extra

indexSafeByKey :: (MonadSafeError m , Show k , Ord k , Show v) => k -> Map k v -> m v
indexSafeByKey k mapKV = liftMaybeOrErrorTupleList [( "key" , show k) , ("map" , show mapKV)] $ lookup k mapKV
indexSafeByKey :: (MonadSafe m , Show k , Ord k , Show v) => k -> Map k v -> m v
indexSafeByKey k mapKV = liftMaybeOrErrorTupleList [("key" , show k) , ("mapKV" , show mapKV)] $ lookup k mapKV

showFoldable :: (Foldable c , Functor c , Show e) => c e -> Text
showFoldable f = fmconcat $ show <$> f
Expand Down
68 changes: 68 additions & 0 deletions hs/src/HelVM/Common/Control/Control.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module HelVM.Common.Control.Control (
controlTToIO,
controlToIO,

runControlT,
runControl,

safeWithMessagesToText,

controlT,
control,

safeWithMessages,

MonadControl,
ControlT,
Control,

UnitSafeWithMessages,
SafeWithMessages
) where

import HelVM.Common.Control.Logger
import HelVM.Common.Control.Message
import HelVM.Common.Control.Safe

import Control.Type.Operator

controlTToIO :: ControlT IO a -> IO a
controlTToIO a = safeWithMessagesToIO =<< runControlT a

controlToIO :: Control a -> IO a
controlToIO = safeToIO . removeLogger

runControlT :: ControlT m a -> m $ SafeWithMessages a
runControlT = runLoggerT . runSafeT

runControl :: Control a -> SafeWithMessages a
runControl a = runLogger $ runSafe <$> a

safeWithMessagesToIO :: SafeWithMessages a -> IO a
safeWithMessagesToIO (safe , _) = safeToIO safe

safeWithMessagesToText :: SafeWithMessages a -> Text
safeWithMessagesToText (safe , messages) = errorsToText messages <> safeToText safe

-- | Constructors

controlT :: Monad m => m a -> ControlT m a
controlT = safeT . loggerT

control :: a -> Control a
control = logger . pure

safeWithMessages :: a -> SafeWithMessages a
safeWithMessages = withMessages . pure

-- | Types

type MonadControl m = (MonadLogger m, MonadSafe m)

type ControlT m = SafeT (LoggerT m)

type Control a = Logger $ Safe a

type UnitSafeWithMessages = SafeWithMessages ()

type SafeWithMessages a = WithMessages (Safe a)
Loading

0 comments on commit 982b366

Please sign in to comment.