Skip to content

Commit

Permalink
Merge pull request #47 from msakai/feature/comment-as-writer
Browse files Browse the repository at this point in the history
Use Writer monad instead of State to annotate comments
  • Loading branch information
msakai authored Nov 9, 2024
2 parents b02af74 + 3709417 commit fa0e5ac
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
18 changes: 9 additions & 9 deletions Codec/TPTP/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Codec.TPTP.QuickCheck
import Control.Applicative
import Control.Monad
import Control.Monad.Identity
import Control.Monad.State
import Control.Monad.Writer (Writer, runWriter)
import Data.Data
import Data.Function
import Data.Monoid hiding(All)
Expand All @@ -34,21 +34,21 @@ type Term = T Identity
-- * Formulae and terms decorated with state

-- | First-order formulae decorated with state
type FormulaST s = F (State s)
type FormulaW s = F (Writer s)

-- | Terms decorated with state
type TermST s = T (State s)
type TermW s = T (Writer s)

-- | First-order formulae decorated with comments
type FormulaC = FormulaST [String]
type FormulaC = FormulaW [String]

-- | Terms decorated with comments
type TermC = TermST [String]
type TermC = TermW [String]

-- | Forget comments in formulae decorated with comments
forgetFC :: FormulaC -> Formula
forgetFC (F f) = F . return $
case evalState f [] of
case fst $ runWriter f of
BinOp f1 op f2 -> BinOp (forgetFC f1) op (forgetFC f2)
InfixPred t1 pred_ t2 -> InfixPred (forgetTC t1) pred_ (forgetTC t2)
PredApp aw ts -> PredApp aw (fmap forgetTC ts)
Expand All @@ -58,7 +58,7 @@ forgetFC (F f) = F . return $
-- | Forget comments in terms decorated with comments
forgetTC :: TermC -> Term
forgetTC (T t) = T . return $
case evalState t [] of
case fst $ runWriter t of
Var v -> Var v
NumberLitTerm d -> NumberLitTerm d
DistinctObjectTerm s -> DistinctObjectTerm s
Expand Down Expand Up @@ -216,8 +216,8 @@ type TPTP_Input = TPTP_Input_ Identity
deriving (Eq,Ord,Show,Read,Data,Typeable)
-}

-- | A line of a TPTP file: Annotated formula (with the comment string embbeded in the State monad ), comment or include statement
type TPTP_Input_C = TPTP_Input_ (State [String])
-- | A line of a TPTP file: Annotated formula (with the comment string embbeded in the Writer monad ), comment or include statement
type TPTP_Input_C = TPTP_Input_ (Writer [String])

-- | Forget comments in a line of a TPTP file decorated with comments
forgetTIC :: TPTP_Input_C -> TPTP_Input
Expand Down
6 changes: 3 additions & 3 deletions ParserC.y
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Codec.TPTP.Base
import System.IO
import System.IO.Unsafe
import Control.Monad.Identity
import Control.Monad.State
import Control.Monad.Writer
}

%name parseTPTPwithComment
Expand Down Expand Up @@ -505,8 +505,8 @@ class Commented c where
instance Commented Identity where
withComments = const

instance Commented (State [String]) where
withComments (F mf) s = F $ do { f <- mf; put s; return f }
instance Commented (Writer [String]) where
withComments (F mf) s = F $ do { f <- mf; tell s; return f }


stripQuotes which (x:xs) = go xs
Expand Down
1 change: 1 addition & 0 deletions changelog.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.6.0.0

* migrate to `prettyprinter` package family from `ansi-wl-pprint` package
* Use `Writer` moand instead of `State` to annotate comments

## 0.5.1.0

Expand Down

0 comments on commit fa0e5ac

Please sign in to comment.