monoid-absorbing-0.1.0.4: A library for (left, right) zero monoids and backtracking with cut

Copyright(c) 2015 Maciej Piróg
LicenseMIT
Maintainermaciej.adam.pirog@gmail.com
Stabilityexperimental
Safe HaskellSafe
LanguageHaskell98

Data.List.Cut

Contents

Description

A ListT-like monad transformer equipped with the cut operator known from Prolog.

Synopsis

The CutListT transformer

data CutListT' m a Source

Constructors

CCons a (m (CutListT' m a)) 
CNil 
CCut 

newtype CutListT m a Source

A monad transformer that behaves like the list transformer, but it allows Prolog's cut operator.

Constructors

CutListT 

Fields

unCutListT :: m (CutListT' m a)
 

retract :: Monad m => CutListT m a -> m () Source

Ignore the elements on the list and combine the monadic computations.

The CutList monad

type CutList = CutListT Identity Source

List with Prolog's cut operator.

cutToList :: CutList a -> [a] Source

Convert to a regular list.

cutFromList :: [a] -> CutList a Source

Convert from a regular list

Control functions

cut :: (Functor m, Monad m) => CutListT m () Source

Discard yet uninspected choices.

cutFail :: (Functor m, Monad m) => CutListT m () Source

Discard the uninspected choices and fail the current branch of computation. Equal to cut >> mzero.

scope :: (Functor m, Monad m) => CutListT m a -> CutListT m a Source

Delimit the scope of cuts in the argument.

Examples

takeWhile using lists with cut

We implement the functon takeWhile using cuts:

takeWhile :: (a -> Bool) -> [a] -> [a]
takeWhile p xs = toList $ do
  x <- fromList xs
  when (not $ p x) cutFail
  return x