kure-2.18.6: Combinators for Strategic Programming
Copyright(c) 2012--2021 The University of Kansas
LicenseBSD3
MaintainerNeil Sculthorpe <neil.sculthorpe@ntu.ac.uk>
Stabilitybeta
Portabilityghc
Safe HaskellSafe-Inferred
LanguageHaskell2010

Language.KURE.Combinators.Transform

Description

This module provides a variety of combinators over Transform and Rewrite.

Synopsis

Transformation Combinators

idR :: Monad m => Rewrite c m a Source #

The identity rewrite.

successT :: Monad m => Transform c m a () Source #

An always successful transformation.

contextT :: Monad m => Transform c m a c Source #

Extract the current context.

exposeT :: Monad m => Transform c m a (c, a) Source #

Expose the current context and value.

liftContext :: (c -> c') -> Transform c' m a b -> Transform c m a b Source #

Lift a transformation to operate on a derived context.

readerT :: (a -> Transform c m a b) -> Transform c m a b Source #

Look at the argument to the transformation before choosing which Transform to use.

resultT :: (m b -> n d) -> Transform c m a b -> Transform c n a d Source #

Convert the monadic result of a transformation into a result in another monad.

catchesT :: MonadCatch m => [Transform c m a b] -> Transform c m a b Source #

Deprecated: Please use catchesM instead.

Attempt each transformation until one succeeds, then return that result and discard the rest of the transformations.

mapT :: (Traversable t, Monad m) => Transform c m a b -> Transform c m (t a) (t b) Source #

Map a transformation over a list.

joinT :: Transform c m (m a) a Source #

An identity transformation that resembles a monadic join.

guardT :: MonadFail m => Transform c m Bool () Source #

Fail if the Boolean is False, succeed if the Boolean is True.

Rewrite Combinators

tryR :: MonadCatch m => Rewrite c m a -> Rewrite c m a Source #

Catch a failing rewrite, making it into an identity.

andR :: (Foldable f, Monad m) => f (Rewrite c m a) -> Rewrite c m a Source #

Perform a collection of rewrites in sequence, requiring all to succeed.

orR :: (Functor f, Foldable f, MonadCatch m) => f (Rewrite c m a) -> Rewrite c m a Source #

Perform a collection of rewrites in sequence, succeeding if any succeed.

(>+>) :: MonadCatch m => Rewrite c m a -> Rewrite c m a -> Rewrite c m a Source #

Perform two rewrites in sequence, succeeding if one or both succeed.

repeatR :: MonadCatch m => Rewrite c m a -> Rewrite c m a Source #

Repeat a rewrite until it fails, then return the result before the failure. Requires at least the first attempt to succeed.

acceptR :: MonadFail m => (a -> Bool) -> Rewrite c m a Source #

Look at the argument to a rewrite, and choose to be either idR or a failure.

acceptWithFailMsgR :: MonadFail m => (a -> Bool) -> String -> Rewrite c m a Source #

As acceptR, but takes a custom failure message.

accepterR :: MonadFail m => Transform c m a Bool -> Rewrite c m a Source #

A generalisation of acceptR where the predicate is a Transform.

changedR :: (MonadCatch m, Eq a) => Rewrite c m a -> Rewrite c m a Source #

Makes an rewrite fail if the result value equals the argument value.

changedByR :: MonadCatch m => (a -> a -> Bool) -> Rewrite c m a -> Rewrite c m a Source #

Makes a rewrite fail if the result value and the argument value satisfy the equality predicate. This is a generalisation of changedR. changedR = changedByR (==)

sideEffectR :: Monad m => (c -> a -> m ()) -> Rewrite c m a Source #

An identity rewrite with side-effects.

Monad Transformers

anyR Support

These are useful when defining congruence combinators that succeed if any child rewrite succeeds. See the "Expr" example, or the HERMIT package.

data AnyR m a Source #

The AnyR transformer, in combination with wrapAnyR and unwrapAnyR, causes a sequence of rewrites to succeed if at least one succeeds, converting failures to identity rewrites.

Instances

Instances details
Monad m => Monad (AnyR m) Source # 
Instance details

Defined in Language.KURE.Combinators.Transform

Methods

(>>=) :: AnyR m a -> (a -> AnyR m b) -> AnyR m b #

(>>) :: AnyR m a -> AnyR m b -> AnyR m b #

return :: a -> AnyR m a #

Monad m => Functor (AnyR m) Source # 
Instance details

Defined in Language.KURE.Combinators.Transform

Methods

fmap :: (a -> b) -> AnyR m a -> AnyR m b #

(<$) :: a -> AnyR m b -> AnyR m a #

MonadFail m => MonadFail (AnyR m) Source # 
Instance details

Defined in Language.KURE.Combinators.Transform

Methods

fail :: String -> AnyR m a #

Monad m => Applicative (AnyR m) Source # 
Instance details

Defined in Language.KURE.Combinators.Transform

Methods

pure :: a -> AnyR m a #

(<*>) :: AnyR m (a -> b) -> AnyR m a -> AnyR m b #

liftA2 :: (a -> b -> c) -> AnyR m a -> AnyR m b -> AnyR m c #

(*>) :: AnyR m a -> AnyR m b -> AnyR m b #

(<*) :: AnyR m a -> AnyR m b -> AnyR m a #

MonadCatch m => MonadCatch (AnyR m) Source # 
Instance details

Defined in Language.KURE.Combinators.Transform

Methods

catchM :: AnyR m a -> (String -> AnyR m a) -> AnyR m a Source #

wrapAnyR :: MonadCatch m => Rewrite c m a -> Rewrite c (AnyR m) a Source #

Wrap a Rewrite using the AnyR monad transformer.

unwrapAnyR :: MonadFail m => Rewrite c (AnyR m) a -> Rewrite c m a Source #

Unwrap a Rewrite from the AnyR monad transformer.

oneR Support

These are useful when defining congruence combinators that succeed if one child rewrite succeeds (and the remainder are then discarded). See the "Expr" example, or the HERMIT package.

data OneR m a Source #

The OneR transformer, in combination with wrapOneR and unwrapOneR, causes a sequence of rewrites to only apply the first success, converting the remainder (and failures) to identity rewrites.

Instances

Instances details
Monad m => Monad (OneR m) Source # 
Instance details

Defined in Language.KURE.Combinators.Transform

Methods

(>>=) :: OneR m a -> (a -> OneR m b) -> OneR m b #

(>>) :: OneR m a -> OneR m b -> OneR m b #

return :: a -> OneR m a #

Monad m => Functor (OneR m) Source # 
Instance details

Defined in Language.KURE.Combinators.Transform

Methods

fmap :: (a -> b) -> OneR m a -> OneR m b #

(<$) :: a -> OneR m b -> OneR m a #

MonadFail m => MonadFail (OneR m) Source # 
Instance details

Defined in Language.KURE.Combinators.Transform

Methods

fail :: String -> OneR m a #

Monad m => Applicative (OneR m) Source # 
Instance details

Defined in Language.KURE.Combinators.Transform

Methods

pure :: a -> OneR m a #

(<*>) :: OneR m (a -> b) -> OneR m a -> OneR m b #

liftA2 :: (a -> b -> c) -> OneR m a -> OneR m b -> OneR m c #

(*>) :: OneR m a -> OneR m b -> OneR m b #

(<*) :: OneR m a -> OneR m b -> OneR m a #

MonadCatch m => MonadCatch (OneR m) Source # 
Instance details

Defined in Language.KURE.Combinators.Transform

Methods

catchM :: OneR m a -> (String -> OneR m a) -> OneR m a Source #

wrapOneR :: MonadCatch m => Rewrite c m g -> Rewrite c (OneR m) g Source #

Wrap a Rewrite using the OneR monad transformer.

unwrapOneR :: MonadFail m => Rewrite c (OneR m) a -> Rewrite c m a Source #

Unwrap a Rewrite from the OneR monad transformer.