Mikan
Safe HaskellNone
LanguageHaskell2010

Mikan.TypeChecking.Free

Description

Computing the free variables of a term.

The distinction between rigid and strongly rigid occurrences comes from: Jason C. Reed, PhD thesis, 2009, page 96 (see also his LFMTP 2009 paper)

The main idea is that x = t(x) is unsolvable if x occurs strongly rigidly in t. It might have a solution if the occurrence is not strongly rigid, e.g.

x = f -> suc (f (x ( y -> k))) has x = f -> suc (f (suc k))

Jason C. Reed, PhD thesis, page 106

The most general function here is freeVarMap, which returns returns occurrence information about every free variable. It is also a legacy function in the sense that it used to be the only function for computing free occurrences, and it came with various functions for taking views of the resulting VarMap. You can find these under the "Legacy API" here.

There are also also a bunch of specialized and optimized implementations of traversals here.

If you want to write a new function for computing free occurrence information, your task is essentially to write a ComputeFree instance and invoke freeVars. These together implement a generic traversal which has a good chance of being converted to decent code by GHC. You can look at examples here and also look at the comments at ComputeFree.

Synopsis

Documentation

data FlexRig' a Source #

Depending on the surrounding context of a variable, it's occurrence can be classified as flexible or rigid, with finer distinctions.

The constructors are listed in increasing order (wrt. information content).

Constructors

Flexible !a

In arguments of metas. The set of metas is used by 'NonLinMatch' to generate the right blocking information. The semantics is that the status of a variable occurrence may change if one of the metas in the set gets solved. We may say the occurrence is tainted by the meta variables in the set.

WeaklyRigid

In arguments to variables and definitions.

Unguarded

In top position, or only under inductive record constructors (unit).

StronglyRigid

Under at least one and only inductive constructors.

Instances

Instances details
Functor FlexRig' Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

Methods

fmap :: (a -> b) -> FlexRig' a -> FlexRig' b #

(<$) :: a -> FlexRig' b -> FlexRig' a #

Foldable FlexRig' Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

Methods

fold :: Monoid m => FlexRig' m -> m #

foldMap :: Monoid m => (a -> m) -> FlexRig' a -> m #

foldMap' :: Monoid m => (a -> m) -> FlexRig' a -> m #

foldr :: (a -> b -> b) -> b -> FlexRig' a -> b #

foldr' :: (a -> b -> b) -> b -> FlexRig' a -> b #

foldl :: (b -> a -> b) -> b -> FlexRig' a -> b #

foldl' :: (b -> a -> b) -> b -> FlexRig' a -> b #

foldr1 :: (a -> a -> a) -> FlexRig' a -> a #

foldl1 :: (a -> a -> a) -> FlexRig' a -> a #

toList :: FlexRig' a -> [a] #

null :: FlexRig' a -> Bool #

length :: FlexRig' a -> Int #

elem :: Eq a => a -> FlexRig' a -> Bool #

maximum :: Ord a => FlexRig' a -> a #

minimum :: Ord a => FlexRig' a -> a #

sum :: Num a => FlexRig' a -> a #

product :: Num a => FlexRig' a -> a #

Pretty a => Pretty (FlexRig' a) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

PrettyTCM a => PrettyTCM (FlexRig' a) Source # 
Instance details

Defined in Mikan.TypeChecking.Pretty

Methods

prettyTCM :: MonadPretty m => FlexRig' a -> m Doc Source #

Show a => Show (FlexRig' a) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

Methods

showsPrec :: Int -> FlexRig' a -> ShowS #

show :: FlexRig' a -> String #

showList :: [FlexRig' a] -> ShowS #

Eq a => Eq (FlexRig' a) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

Methods

(==) :: FlexRig' a -> FlexRig' a -> Bool #

(/=) :: FlexRig' a -> FlexRig' a -> Bool #

LensFlexRig (FlexRig' a) a Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

class Free t where Source #

Methods

freeVars :: ComputeFree r => t -> Reader r (Collect r) Source #

Instances

Instances details
Free Clause Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => Clause -> Reader r (Collect r) Source #

Free Teletype Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Free Level Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => Level -> Reader r (Collect r) Source #

Free Sort Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => Sort -> Reader r (Collect r) Source #

Free Term Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => Term -> Reader r (Collect r) Source #

Free Candidate Source # 
Instance details

Defined in Mikan.TypeChecking.Monad.Base

Free CompareAs Source # 
Instance details

Defined in Mikan.TypeChecking.Monad.Base

Free Constraint Source # 
Instance details

Defined in Mikan.TypeChecking.Monad.Base

Free EqualityView Source # 
Instance details

Defined in Mikan.TypeChecking.Monad.Builtin

Free t => Free (Arg t) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => Arg t -> Reader r (Collect r) Source #

Free t => Free (Abs t) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => Abs t -> Reader r (Collect r) Source #

Free t => Free (Elim' t) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => Elim' t -> Reader r (Collect r) Source #

Free t => Free (Tele t) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => Tele t -> Reader r (Collect r) Source #

Free t => Free (Dom t) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => Dom t -> Reader r (Collect r) Source #

Free t => Free (PlusLevel' t) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Free t => Free (Type' t) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => Type' t -> Reader r (Collect r) Source #

Free t => Free (SingleLevel' t) Source # 
Instance details

Defined in Mikan.TypeChecking.Level

Free t => Free (List1 t) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => List1 t -> Reader r (Collect r) Source #

Free t => Free (Maybe t) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => Maybe t -> Reader r (Collect r) Source #

Free t => Free [t] Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => [t] -> Reader r (Collect r) Source #

(Free a, Free b) => Free (a, b) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => (a, b) -> Reader r (Collect r) Source #

(Free a, Free b, Free c) => Free (a, b, c) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => (a, b, c) -> Reader r (Collect r) Source #

(Free a, Free b, Free c, Free d) => Free (a, b, c, d) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => (a, b, c, d) -> Reader r (Collect r) Source #

(Free a, Free b, Free c, Free d, Free e) => Free (a, b, c, d, e) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Generic

Methods

freeVars :: ComputeFree r => (a, b, c, d, e) -> Reader r (Collect r) Source #

data IgnoreSorts Source #

Where should we skip sorts in free variable analysis?

Constructors

IgnoreNot

Do not skip.

IgnoreInAnnotations

Skip when annotation to a type.

IgnoreAll

Skip unconditionally.

Instances

Instances details
Show IgnoreSorts Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

Eq IgnoreSorts Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

class LensFlexRig o a | o -> a where Source #

Instances

Instances details
LensFlexRig (FlexRig' a) a Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

LensFlexRig (VarOcc' a) a Source #

Access to varFlexRig in VarOcc.

Instance details

Defined in Mikan.TypeChecking.Free.Base

newtype VarMap' a Source #

Constructors

VarMap 

Fields

Instances

Instances details
Singleton Variable (VarMap' a) Source #

A "set"-style Singleton instance with default/initial variable occurrence.

Instance details

Defined in Mikan.TypeChecking.Free.Base

PrettyTCM a => PrettyTCM (VarMap' a) Source # 
Instance details

Defined in Mikan.TypeChecking.Pretty

Methods

prettyTCM :: MonadPretty m => VarMap' a -> m Doc Source #

Semigroup a => Monoid (VarMap' a) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

Methods

mempty :: VarMap' a #

mappend :: VarMap' a -> VarMap' a -> VarMap' a #

mconcat :: [VarMap' a] -> VarMap' a #

Semigroup a => Semigroup (VarMap' a) Source #

Proper monoid instance for VarMap rather than inheriting the broken one from IntMap. We combine two occurrences of a variable using mappend.

Instance details

Defined in Mikan.TypeChecking.Free.Base

Methods

(<>) :: VarMap' a -> VarMap' a -> VarMap' a #

sconcat :: NonEmpty (VarMap' a) -> VarMap' a #

stimes :: Integral b => b -> VarMap' a -> VarMap' a #

Show a => Show (VarMap' a) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

Methods

showsPrec :: Int -> VarMap' a -> ShowS #

show :: VarMap' a -> String #

showList :: [VarMap' a] -> ShowS #

Eq a => Eq (VarMap' a) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

Methods

(==) :: VarMap' a -> VarMap' a -> Bool #

(/=) :: VarMap' a -> VarMap' a -> Bool #

data VarOcc' a Source #

Occurrence of free variables is classified by a FlexRig.

Constructors

VarOcc 

Fields

Instances

Instances details
Pretty a => Pretty (VarOcc' a) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

PrettyTCM a => PrettyTCM (VarOcc' a) Source # 
Instance details

Defined in Mikan.TypeChecking.Pretty

Methods

prettyTCM :: MonadPretty m => VarOcc' a -> m Doc Source #

Monoid a => Monoid (VarOcc' a) Source #

The neutral element for variable occurrence aggregation is least serious occurrence: flexible. This is also the absorptive element for composeVarOcc, if we ignore the MetaSet in Flexible.

Instance details

Defined in Mikan.TypeChecking.Free.Base

Methods

mempty :: VarOcc' a #

mappend :: VarOcc' a -> VarOcc' a -> VarOcc' a #

mconcat :: [VarOcc' a] -> VarOcc' a #

Semigroup a => Semigroup (VarOcc' a) Source #

The default way of aggregating free variable info from subterms is by adding the variable occurrences. For instance, if we have a pair (t₁,t₂) and t₁ has o₁ the occurrences of a variable x and t₂ has o₂ the occurrences of the same variable, then (t₁,t₂) has mappend o₁ o₂ occurrences of that variable.

From counting Quantity, we extrapolate this to FlexRig: we care most about about StronglyRigid occurrences. E.g., if t₁ has a StronglyRigid occurrence and t₂ a Flexible occurrence, then (t₁,t₂) still has a StronglyRigid occurrence.

VarOcc forms a semiring, and this monoid is the addition of the semiring.

Instance details

Defined in Mikan.TypeChecking.Free.Base

Methods

(<>) :: VarOcc' a -> VarOcc' a -> VarOcc' a #

sconcat :: NonEmpty (VarOcc' a) -> VarOcc' a #

stimes :: Integral b => b -> VarOcc' a -> VarOcc' a #

Show a => Show (VarOcc' a) Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

Methods

showsPrec :: Int -> VarOcc' a -> ShowS #

show :: VarOcc' a -> String #

showList :: [VarOcc' a] -> ShowS #

Eq a => Eq (VarOcc' a) Source #

Equality up to origin.

Instance details

Defined in Mikan.TypeChecking.Free.Base

Methods

(==) :: VarOcc' a -> VarOcc' a -> Bool #

(/=) :: VarOcc' a -> VarOcc' a -> Bool #

LensFlexRig (VarOcc' a) a Source #

Access to varFlexRig in VarOcc.

Instance details

Defined in Mikan.TypeChecking.Free.Base

composeFlexRig :: Semigroup a => FlexRig' a -> FlexRig' a -> FlexRig' a Source #

FlexRig composition (multiplicative operation of the semiring). For accumulating the context of a variable.

Flexible is dominant. Once we are under a meta, we are flexible regardless what else comes. We taint all variable occurrences under a meta by this meta.

WeaklyRigid is next in strength. Destroys strong rigidity.

StronglyRigid is still dominant over Unguarded.

Unguarded is the unit. It is the top (identity) context.

Legacy API

flexibleVars :: VarMap -> IntMap MetaSet Source #

Variables occuring in arguments of metas. These are only potentially free, depending how the meta variable is instantiated. The set contains the id's of the meta variables that this variable is an argument to.

freeVarMap :: Free t => t -> VarMap Source #

Return information about every free variable.

rigidVars :: VarMap -> VarSet Source #

Rigid variables: either strongly rigid, unguarded, or weakly rigid.

stronglyRigidVars :: VarMap -> VarSet Source #

Variables under only and at least one inductive constructor(s).

unguardedVars :: VarMap -> VarSet Source #

Variables at top or only under inductive record constructors λs and Πs. The purpose of recording these separately is that they can still become strongly rigid if put under a constructor whereas weakly rigid ones stay weakly rigid.

MetaSet

newtype MetaSet Source #

A set of meta variables. Forms a monoid under union.

Constructors

MetaSet 

Instances

Instances details
Pretty MetaSet Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

PrettyTCM MetaSet Source # 
Instance details

Defined in Mikan.TypeChecking.Pretty

Methods

prettyTCM :: MonadPretty m => MetaSet -> m Doc Source #

Null MetaSet Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

Monoid MetaSet Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

Semigroup MetaSet Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

Show MetaSet Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

Eq MetaSet Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

Methods

(==) :: MetaSet -> MetaSet -> Bool #

(/=) :: MetaSet -> MetaSet -> Bool #

Singleton MetaId MetaSet Source # 
Instance details

Defined in Mikan.TypeChecking.Free.Base

foldrMetaSet :: (MetaId -> a -> a) -> a -> MetaSet -> a Source #

Specialized traversals

allFreeVar :: Free t => (Int -> Bool) -> t -> Bool Source #

Compute the conjunction of a predicate on free variables.

anyFreeVar :: Free t => (Int -> Bool) -> t -> Bool Source #

Compute the disjunction of a predicate on free variables.

closed :: Free t => t -> Bool Source #

Is a term closed?

flexRigOccurrenceIn :: Free a => Nat -> a -> Maybe FlexRig Source #

Compute FlexRig for a single variable.

freeIn :: Free t => Nat -> t -> Bool Source #

Check if single variable occurs freely.

freeInIgnoringSorts :: Free a => Nat -> a -> Bool Source #

Check if a single variable occurs freely outside of sorts of types.

freeVarCounts :: Free t => t -> VarCounts Source #

Return how many times each free variable occurs.

freeVarList :: Free t => t -> [Int] Source #

Compute a (possibly non-unique) list of free variables, in preorder traversal order.

freeVarMapIgnoreAnn :: Free t => t -> VarMap Source #

Return information about every free variable, but ignore occurrences in sorts of types.

freeVarSet :: Free t => t -> VarSet Source #

Compute the set of free variables.

isBinderUsed :: Free a => Abs a -> Bool Source #

Is the variable bound by the abstraction actually used?

setFreeInIgnoring :: Free t => VarSet -> t -> VarSet Source #

Test for a set of variables whether each variable occurs outside of sort annotations. Return the set of variables that don't occur.

flexRigToBlocker :: FlexRig -> Blocker Source #

Extract a blocker from an occurrence