module Unison.PrettyPrintEnv.MonadPretty
( MonadPretty,
Env (..),
runPretty,
addTypeVars,
willCaptureType,
withBoundTerm,
withBoundTerms,
)
where
import Control.Lens (views)
import Control.Monad.Reader (MonadReader, Reader, local, runReader)
import Data.Set qualified as Set
import Unison.Prelude
import Unison.PrettyPrintEnv (PrettyPrintEnv)
import Unison.Util.Set qualified as Set
import Unison.Var (Var)
import Unison.Var qualified as Var
type MonadPretty v m = (Var v, MonadReader (Env v) m)
data Env v = Env
{ forall v. Env v -> Set v
boundTerms :: !(Set v),
forall v. Env v -> Set v
boundTypes :: !(Set v),
forall v. Env v -> Set v
freeTerms :: !(Set v),
forall v. Env v -> PrettyPrintEnv
ppe :: !PrettyPrintEnv
}
deriving stock ((forall x. Env v -> Rep (Env v) x)
-> (forall x. Rep (Env v) x -> Env v) -> Generic (Env v)
forall x. Rep (Env v) x -> Env v
forall x. Env v -> Rep (Env v) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall v x. Rep (Env v) x -> Env v
forall v x. Env v -> Rep (Env v) x
$cfrom :: forall v x. Env v -> Rep (Env v) x
from :: forall x. Env v -> Rep (Env v) x
$cto :: forall v x. Rep (Env v) x -> Env v
to :: forall x. Rep (Env v) x -> Env v
Generic)
modifyTypeVars :: (MonadPretty v m) => (Set v -> Set v) -> m a -> m a
modifyTypeVars :: forall v (m :: * -> *) a.
MonadPretty v m =>
(Set v -> Set v) -> m a -> m a
modifyTypeVars = (Env v -> Env v) -> m a -> m a
forall a. (Env v -> Env v) -> m a -> m a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local ((Env v -> Env v) -> m a -> m a)
-> ((Set v -> Set v) -> Env v -> Env v)
-> (Set v -> Set v)
-> m a
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter (Env v) (Env v) (Set v) (Set v)
-> (Set v -> Set v) -> Env v -> Env v
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter (Env v) (Env v) (Set v) (Set v)
#boundTypes
addTypeVars :: (MonadPretty v m) => [v] -> m a -> m a
addTypeVars :: forall v (m :: * -> *) a. MonadPretty v m => [v] -> m a -> m a
addTypeVars = (Set v -> Set v) -> m a -> m a
forall v (m :: * -> *) a.
MonadPretty v m =>
(Set v -> Set v) -> m a -> m a
modifyTypeVars ((Set v -> Set v) -> m a -> m a)
-> ([v] -> Set v -> Set v) -> [v] -> m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set v -> Set v -> Set v
forall a. Ord a => Set a -> Set a -> Set a
Set.union (Set v -> Set v -> Set v)
-> ([v] -> Set v) -> [v] -> Set v -> Set v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [v] -> Set v
forall a. Ord a => [a] -> Set a
Set.fromList
willCaptureType :: (MonadPretty v m) => [v] -> m Bool
willCaptureType :: forall v (m :: * -> *). MonadPretty v m => [v] -> m Bool
willCaptureType [v]
vs = LensLike' (Const Bool) (Env v) (Set v) -> (Set v -> Bool) -> m Bool
forall s (m :: * -> *) r a.
MonadReader s m =>
LensLike' (Const r) s a -> (a -> r) -> m r
views LensLike' (Const Bool) (Env v) (Set v)
#boundTypes (Set v -> Set v -> Bool
forall a. Ord a => Set a -> Set a -> Bool
Set.intersects ([v] -> Set v
forall a. Ord a => [a] -> Set a
Set.fromList [v]
vs))
withBoundTerm :: (MonadPretty v m) => v -> m a -> m a
withBoundTerm :: forall v (m :: * -> *) a. MonadPretty v m => v -> m a -> m a
withBoundTerm v
v =
(Env v -> Env v) -> m a -> m a
forall a. (Env v -> Env v) -> m a -> m a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (ASetter (Env v) (Env v) (Set v) (Set v)
-> (Set v -> Set v) -> Env v -> Env v
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter (Env v) (Env v) (Set v) (Set v)
#boundTerms (v -> Set v -> Set v
forall a. Ord a => a -> Set a -> Set a
Set.insert (v -> v
forall v. Var v => v -> v
Var.reset v
v)))
withBoundTerms :: (MonadPretty v m) => [v] -> m a -> m a
withBoundTerms :: forall v (m :: * -> *) a. MonadPretty v m => [v] -> m a -> m a
withBoundTerms [v]
vs =
(Env v -> Env v) -> m a -> m a
forall a. (Env v -> Env v) -> m a -> m a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (ASetter (Env v) (Env v) (Set v) (Set v)
-> (Set v -> Set v) -> Env v -> Env v
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter (Env v) (Env v) (Set v) (Set v)
#boundTerms (Set v -> Set v -> Set v
forall a. Ord a => Set a -> Set a -> Set a
Set.union ((v -> Set v) -> [v] -> Set v
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (v -> Set v
forall a. a -> Set a
Set.singleton (v -> Set v) -> (v -> v) -> v -> Set v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v -> v
forall v. Var v => v -> v
Var.reset) [v]
vs)))
runPretty :: (Var v) => PrettyPrintEnv -> Reader (Env v) a -> a
runPretty :: forall v a. Var v => PrettyPrintEnv -> Reader (Env v) a -> a
runPretty PrettyPrintEnv
ppe Reader (Env v) a
m =
Reader (Env v) a -> Env v -> a
forall r a. Reader r a -> r -> a
runReader
Reader (Env v) a
m
Env
{ $sel:boundTerms:Env :: Set v
boundTerms = Set v
forall a. Set a
Set.empty,
$sel:boundTypes:Env :: Set v
boundTypes = Set v
forall a. Set a
Set.empty,
$sel:freeTerms:Env :: Set v
freeTerms = Set v
forall a. Set a
Set.empty,
PrettyPrintEnv
$sel:ppe:Env :: PrettyPrintEnv
ppe :: PrettyPrintEnv
ppe
}