Skip to content

Instantly share code, notes, and snippets.

@pedrominicz
Last active January 25, 2020 01:31
Show Gist options
  • Save pedrominicz/cb600d71cde6d960f431e49fa87d6010 to your computer and use it in GitHub Desktop.
Save pedrominicz/cb600d71cde6d960f431e49fa87d6010 to your computer and use it in GitHub Desktop.
CutList: Lists with Prolog-like Cut!
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE TypeFamilies #-}
module Cut where
import Control.Monad
import GHC.Exts
data CutList a
= Cons a (CutList a)
| Nil
| Cut
instance IsList (CutList a) where
type Item (CutList a) = a
fromList (x:xs) = Cons x (fromList xs)
fromList [] = Nil
toList (Cons x xs) = x : toList xs
toList _ = []
cut :: CutList a
cut = Cut
instance Functor CutList where
fmap f (Cons x xs) = Cons (f x) (fmap f xs)
fmap f Nil = Nil
fmap f Cut = Cut
instance Applicative CutList where
pure x = Cons x Nil
(<*>) = ap
append :: CutList a -> CutList a -> CutList a
append (Cons x xs) ys = Cons x (append xs ys)
append Nil ys = ys
append Cut ys = Nil
instance Monad CutList where
Cons x xs >>= f = append (f x) (xs >>= f)
Nil >>= f = Nil
Cut >>= f = Cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment