haskell / haskell/transformers

Alternative instance for ContT

Open
#77 14 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
5
Forks
10
Avg merge
2h 21m
Merged PRs (30d)
1

Description

[original issue 69 by dfranke]


instance (Monoid r, Applicative m) => Alternative (ContT r m) where

empty = ContT $ const (pure mempty)
(ContT f) <|> (ContT g) = ContT $ \c -> liftA2 (<>) (f c) (g c)

Some ways this can be useful:


  1. You're using ContT to implement a backtracking search. r is Maybe (First Result)) for some Result :: *. a <|> b then means "try a then try b".


  2. You have a function inferType :: Expr a -> (a -> Type) -> Type where Expr a represents an AST containing free variables in a, and (a -> Type) is an environment which maps free variables to their type. Type has a monoid instance where mempty is the type of bottom and (<>) is unification; you form a Cont Type a by way of cont (inferType e). Then (<|>) will lift (<>) from working in the empty environment to working in arbitrary ones.


Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.