Justin Le (@mstk)
C◦mp◦se Conference 2019, June 24
Slide available at https://talks.jle.im.
Matches:
Doesn’t match:
("a","")
("a","cdcdcd")
("b","cd")
Regular Language Base Members
Regular Language Operations
RS
, sequence one after the otherR|S
, one or the otherR*
, the repetition of R
class Functor f => Applicative f where
-- | Always succeed, consuming nothing
pure :: a -> f a
-- | Concatenation
(<*>) :: f (a -> b) -> f a -> f b
class Applicative f => Alternative f where
-- | Always fails to match
empty :: f a
-- | Alternation
(<|>) :: f a -> f a -> f a
-- | Reptition
many :: f a -> f [a]
Components of a Regular Language
empty
pure x
<*>
<|>
many
Alternative
instance, “offloading” the logic
foldMap
class Semigroup m where
(<>) :: m -> m -> m
class Monoid m where
mempty :: m
type FreeMonoid = [] :: Type -> Type
-- ^ take a type; ^ return a type
instance Semigroup (FreeMonoid a)
instance Monoid (FreeMonoid a)
injectFM :: a -> FreeMonoid a
runFM :: Monoid m => (a -> m) -> (FreeMonoid a -> m)
runFM f
-- ^ substitute injectFM for f
Find an Alternative
where:
Prim a
= consumption<*>
= sequencing consumption<|>
= backtrackingPrim a
can be interpreted as consumption of state<*>
sequences consumption of state<|>
is backtracking of stateprocessPrim :: Prim a -> StateT String Maybe a
processPrim (Prim c x) = do
d:ds <- get -- ^ match on stream
guard (c == d) -- ^ fail unless match
put ds -- ^ update stream
return x -- ^ return result value
matchPrefix :: Regexp a -> String -> Maybe a
matchPrefix re = evalStateT (runAlt processPrim re)
Alternative
functionality to StateT
: empty
, <*>
, pure
, empty
, many
.Prim
-processing functionality with processPrim
: liftAlt
.Is this you?
“My problem is modeled by some (commonly occurring) structure over some primitive base.”