Agda
Safe HaskellNone
LanguageHaskell2010

Agda.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 Agda.TypeChecking.Free.Base

Methods

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

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

Foldable FlexRig' Source # 
Instance details

Defined in Agda.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 Agda.TypeChecking.Free.Base

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

Defined in Agda.TypeChecking.Pretty

Methods

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

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

Defined in Agda.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 Agda.TypeChecking.Free.Base

Methods

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

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

LensFlexRig (FlexRig' a) a Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

Singleton (Variable, FlexRig' ()) FlexRigMap Source # 
Instance details

Defined in Agda.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 Agda.TypeChecking.Free.Generic

Methods

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

Free EqualityView Source # 
Instance details

Defined in Agda.TypeChecking.Free.Generic

Free Level Source # 
Instance details

Defined in Agda.TypeChecking.Free.Generic

Methods

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

Free Sort Source # 
Instance details

Defined in Agda.TypeChecking.Free.Generic

Methods

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

Free Term Source # 
Instance details

Defined in Agda.TypeChecking.Free.Generic

Methods

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

Free Candidate Source # 
Instance details

Defined in Agda.TypeChecking.Monad.Base

Free CompareAs Source # 
Instance details

Defined in Agda.TypeChecking.Monad.Base

Free Constraint Source # 
Instance details

Defined in Agda.TypeChecking.Monad.Base

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

Defined in Agda.TypeChecking.Free.Generic

Methods

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

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

Defined in Agda.TypeChecking.Free.Generic

Methods

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

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

Defined in Agda.TypeChecking.Free.Generic

Methods

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

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

Defined in Agda.TypeChecking.Free.Generic

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

Defined in Agda.TypeChecking.Free.Generic

Methods

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

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

Defined in Agda.TypeChecking.Free.Generic

Methods

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

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

Defined in Agda.TypeChecking.Free.Generic

Methods

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

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

Defined in Agda.TypeChecking.Level

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

Defined in Agda.TypeChecking.Free.Generic

Methods

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

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

Defined in Agda.TypeChecking.Free.Generic

Methods

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

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

Defined in Agda.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 Agda.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 Agda.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 Agda.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 Agda.TypeChecking.Free.Generic

Methods

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

data FreeEnv' a b c Source #

The current context.

Constructors

FreeEnv 

Fields

Instances

Instances details
LensModality (FreeEnv' a b c) Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

LensQuantity (FreeEnv' a b c) Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

LensRelevance (FreeEnv' a b c) Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

LensFlexRig (FreeEnv' a b c) a Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

Methods

lensFlexRig :: Lens' (FreeEnv' a b c) (FlexRig' a) 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 Agda.TypeChecking.Free.Base

Eq IgnoreSorts Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

class (Singleton MetaId a, Monoid a, Monoid c) => IsVarSet a c | c -> a where Source #

Any representation c of a set of variables need to be able to be modified by a variable occurrence. This is to ensure that free variable analysis is compositional. For instance, it should be possible to compute `fv (v [u/x])` from `fv v` and `fv u`.

In algebraic terminology, a variable set a needs to be (almost) a left semimodule to the semiring VarOcc.

Methods

withVarOcc :: VarOcc' a -> c -> c Source #

Laws * Respects monoid operations: ``` withVarOcc o mempty == mempty withVarOcc o (x <> y) == withVarOcc o x <> withVarOcc o y ``` * Respects VarOcc composition: ``` withVarOcc oneVarOcc = id withVarOcc (composeVarOcc o1 o2) = withVarOcc o1 . withVarOcc o2 ``` * Respects VarOcc aggregation: ``` withVarOcc (o1 <> o2) x = withVarOcc o1 x <> withVarOcc o2 x ``` Since the corresponding unit law may fail, ``` withVarOcc mempty x = mempty ``` it is not quite a semimodule.

Instances

Instances details
IsVarSet () FlexRigMap Source #

Compose everything with the varFlexRig part of the VarOcc.

Instance details

Defined in Agda.TypeChecking.Free.Base

IsVarSet () VarCounts Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

IsVarSet () AllowedVar Source # 
Instance details

Defined in Agda.TypeChecking.MetaVars.Occurs

(Singleton MetaId a, Monoid a) => IsVarSet a (VarMap' a) Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

Methods

withVarOcc :: VarOcc' a -> VarMap' a -> VarMap' a Source #

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

Instances

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

Defined in Agda.TypeChecking.Free.Base

LensFlexRig (VarOcc' a) a Source #

Access to varFlexRig in VarOcc.

Instance details

Defined in Agda.TypeChecking.Free.Base

LensFlexRig (FreeEnv' a b c) a Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

Methods

lensFlexRig :: Lens' (FreeEnv' a b c) (FlexRig' a) Source #

newtype VarMap' a Source #

Constructors

VarMap 

Fields

Instances

Instances details
(Singleton MetaId a, Monoid a) => IsVarSet a (VarMap' a) Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

Methods

withVarOcc :: VarOcc' a -> VarMap' a -> VarMap' a Source #

Singleton Variable (VarMap' a) Source #

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

Instance details

Defined in Agda.TypeChecking.Free.Base

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

Defined in Agda.TypeChecking.Pretty

Methods

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

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

Defined in Agda.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 Agda.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 Agda.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 Agda.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 several dimensions. Currently, we have FlexRig and Modality.

Constructors

VarOcc 

Instances

Instances details
LensModality (VarOcc' a) Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

LensQuantity (VarOcc' a) Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

LensRelevance (VarOcc' a) Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

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

Defined in Agda.TypeChecking.Free.Base

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

Defined in Agda.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, irrelevant. This is also the absorptive element for composeVarOcc, if we ignore the MetaSet in Flexible.

Instance details

Defined in Agda.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₂) then 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 and Relevance: we care most about about StronglyRigid Relevant occurrences. E.g., if t₁ has a StronglyRigid occurrence and t₂ a Flexible occurrence, then (t₁,t₂) still has a StronglyRigid occurrence. Analogously, Relevant occurrences count most, as we wish e.g. to forbid relevant occurrences of variables that are declared to be irrelevant.

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

Instance details

Defined in Agda.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 Agda.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 Agda.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 Agda.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 Agda.TypeChecking.Free.Base

PrettyTCM MetaSet Source # 
Instance details

Defined in Agda.TypeChecking.Pretty

Methods

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

Null MetaSet Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

Monoid MetaSet Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

Semigroup MetaSet Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

Show MetaSet Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

Eq MetaSet Source # 
Instance details

Defined in Agda.TypeChecking.Free.Base

Methods

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

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

Singleton MetaId MetaSet Source # 
Instance details

Defined in Agda.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.

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

Same as allFreeVar except occurrences in sorts of types are ignored.

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

Compute the disjunction of a predicate on free variables.

anyFreeVarIgnoreAll :: (Int -> Bool) -> Term -> Bool Source #

Same as anyFreeVar except occurrences in sorts of types are ignored.

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?

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

Does a variable occur relevantly and outside of sorts of types?

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

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