Skip to content

Commit

Permalink
experiment a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
noughtmare committed Apr 10, 2023
1 parent 2e5df18 commit 74ee38b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
30 changes: 19 additions & 11 deletions experiments/DataDependent.hs → experiments/Main.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE DeriveFunctor #-}
import Control.Applicative
import Data.Char
import Control.Applicative ( asum, Alternative((<|>), empty, many) )
import Data.Char ( digitToInt, intToDigit )

newtype Parser s a = Parser { parse :: String -> s -> [(a, String, s)] } deriving Functor

Expand All @@ -22,22 +22,30 @@ char c = Parser $ \xs s ->
c' : xs' | c == c' -> [(c, xs', s)]
_ -> []

put :: Parser s' s -> Parser s a -> Parser s' a
put :: Parser s' s -> Parser (s,s') a -> Parser s' a
put (Parser p) (Parser q) = Parser $ \xs s -> do
(s', xs', _) <- p xs s
(x, xs'', _) <- q xs' s'
pure (x, xs'', s)
(s', xs', t') <- p xs s
(x, xs'', (_,t'')) <- q xs' (s',t')
pure (x, xs'', t'')

modify :: (s -> [s]) -> Parser s ()
modify f = Parser $ \xs s -> [((), xs, s') | s' <- f s]

dependentReplicate :: Parser s Int -> Parser Int a -> Parser s [a]
dependentReplicate p1 p2 = put p1 rest where
rest = (:) <$ modify (\s -> [s | s > 0]) <*> p2 <* modify (\s -> [s - 1]) <*> rest
<|> [] <$ modify (\s -> [s | s == 0])
under :: Parser t a -> Parser (s, t) a
under (Parser p) = Parser $ \xs (s0,t) -> do
(x, xs', t') <- p xs t
pure (x, xs', (s0, t'))

dependentReplicate :: Parser s Int -> Parser s a -> Parser s [a]
dependentReplicate p1 p2 = put p1 $
many (modify (\s -> [s | fst s > 0]) *> under p2 <* modify (\(s,t) -> [(s - 1, t)]))
<* modify (\s -> [s | fst s == 0])

digit :: Parser s Int
digit = asum [digitToInt <$> char (intToDigit i) | i <- [0..9]]

main :: IO ()
main = print $ parse (dependentReplicate digit (char '.')) "4...." ()
main = do
print $ parse (dependentReplicate digit (char '.')) "4..." ()
print $ parse (dependentReplicate digit (char '.')) "4...." ()
print $ parse (dependentReplicate digit (char '.')) "4....." ()
4 changes: 2 additions & 2 deletions gigaparsec.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ executable gigaparsec-examples
hs-source-dirs: examples
build-depends: gigaparsec

executable gigaparsec-experiments-datadependent
executable gigaparsec-experiments
import: common
main-is: DataDependent.hs
main-is: Main.hs
hs-source-dirs: experiments
8 changes: 8 additions & 0 deletions hie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cradle:
cabal:
- path: "src/"
component: "lib:gigaparsec"
- path: "examples/"
component: "exe:gigaparsec-examples"
- path: "experiments/"
component: "exe:gigaparsec-experiments"

0 comments on commit 74ee38b

Please sign in to comment.