HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

ModuleAgda-2.7.0.1Haskell2010

Agda.TypeChecking.Free.Lazy

Computing the free variables of a term lazily.

We implement a reduce (traversal into monoid) over internal syntax for a generic collection (monoid with singletons). This should allow a more efficient test for the presence of a particular variable.

Worst-case complexity does not change (i.e. the case when a variable does not occur), but best case-complexity does matter. For instance, see Agda.TypeChecking.Substitute.mkAbs: each time we construct a dependent function type, we check whether it is actually dependent.

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

Under coinductive constructors, occurrences are never strongly rigid. Also, function types and lambdas do not establish strong rigidity. Only inductive constructors do so. (See issue 1271).

For further reading on semirings and semimodules for variable occurrence, see e.g. Conor McBrides "I got plenty of nuttin'" (Wadlerfest 2016). There, he treats the "quantity" dimension of variable occurrences.

The semiring has an additive operation for combining occurrences of subterms, and a multiplicative operation of representing function composition. E.g. if variable x appears o in term u, but u appears in context q in term t then occurrence of variable x coming from u is accounted for as q o in t.

Consider example (λ{ x → (x,x)}) y:

  • Variable x occurs once unguarded in x.

  • It occurs twice unguarded in the aggregation x x

  • Inductive constructor , turns this into two strictly rigid occurrences.

If , is a record constructor, then we stay unguarded.

  • The function ({λ x → (x,x)}) provides a context for variable y. This context can be described as weakly rigid with quantity two.

  • The final occurrence of y is obtained as composing the context with the occurrence of y in itself (which is the unit for composition). Thus, y occurs weakly rigid with quantity two.

It is not a given that the context can be described in the same way as the variable occurrence. However, for quantity it is the case and we obtain a semiring of occurrences with 0, 1, and even ω, which is an absorptive element for addition.

  • 18 types
  • 3 classes
  • 30 values
  • PackageAgda-2.7.0.1
  • Exports51
  • LanguageHaskell2010
  • LicenceMIT
  • SourceLazy.hs

Set of meta variables.

4 declarations
newtypenewtype MetaSet
#

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

Instances8Eq, Show, Semigroup, Monoid, Null, Singleton, …
  • Eq MetaSetDefined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Show MetaSetDefined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Semigroup MetaSetDefined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Monoid MetaSetDefined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Null MetaSetDefined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Singleton MetaId MetaSetDefined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • IsVarSet MetaSet SingleFlexRigDefined in Agda-2.7.0.1 · Agda.TypeChecking.Free
  • IsVarSet MetaSet SingleVarOccDefined in Agda-2.7.0.1 · Agda.TypeChecking.Free

Flexible and rigid occurrences (semigroup)

12 declarations
datadata FlexRig' a
#

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 'Agda.TypeChecking.Rewriting.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.

Instances6Functor, Foldable, Eq, Show, LensFlexRig, Singleton
valuecomposeFlexRig :: Semigroup a => FlexRig' a -> FlexRig' a -> FlexRig' a
#

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.

Multi-dimensional feature vector for variable occurrence (semigroup)

5 declarations
datadata VarOcc' a
#

Occurrence of free variables is classified by several dimensions. Currently, we have FlexRig and Modality.

Instances8Eq, Show, Semigroup, Monoid, LensModality, LensQuantity, …
  • Eq a => Eq (VarOcc' a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy

    Equality up to origin.

  • Show a => Show (VarOcc' a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Semigroup a => Semigroup (VarOcc' a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy

    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.

  • (Semigroup a, Monoid a) => Monoid (VarOcc' a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy

    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.

  • LensModality (VarOcc' a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • LensQuantity (VarOcc' a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • LensRelevance (VarOcc' a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • LensFlexRig (VarOcc' a) aDefined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy

    Access to varFlexRig in VarOcc.

valuetopVarOcc :: VarOcc' a
#

The absorptive element of variable occurrence under aggregation: strongly rigid, relevant.

valuecomposeVarOcc :: Semigroup a => VarOcc' a -> VarOcc' a -> VarOcc' a
#

First argument is the outer occurrence (context) and second is the inner. This multiplicative operation is to modify an occurrence under a context.

Storing variable occurrences (semimodule).

7 declarations
classclass (Singleton MetaId a, Semigroup a, Monoid a, Semigroup c, Monoid c) => IsVarSet a c | c -> a where
#

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

    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.

Instances11IsVarSet, …
newtypenewtype VarMap' a
#

Constructors

Instances6Singleton, IsVarSet, Eq, Show, Semigroup, Monoid

Simple flexible/rigid variable collection.

3 declarations
newtypenewtype FlexRigMap
#
Instances5Show, Semigroup, Monoid, IsVarSet, Singleton

Environment for collecting free variables.

19 declarations
datadata FreeEnv' a b c
#

The current context.

Constructors

Instances5LensModality, LensQuantity, LensRelevance, LensFlexRig, Monoid

Recursively collecting free variables.

1 declaration
classclass Free t where
#

Gather free variables in a collection.

Methods

Instances27Free, …
  • Free ClauseDefined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free EqualityViewDefined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free LevelDefined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free SortDefined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free TermDefined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free CandidateDefined in Agda-2.7.0.1 · Agda.TypeChecking.Monad.Base
  • Free CompareAsDefined in Agda-2.7.0.1 · Agda.TypeChecking.Monad.Base
  • Free ConstraintDefined in Agda-2.7.0.1 · Agda.TypeChecking.Monad.Base
  • Free DisplayFormDefined in Agda-2.7.0.1 · Agda.TypeChecking.Monad.Base
  • Free DisplayTermDefined in Agda-2.7.0.1 · Agda.TypeChecking.Monad.Base
  • Free NLPSortDefined in Agda-2.7.0.1 · Agda.TypeChecking.Rewriting.NonLinPattern · orphan
  • Free NLPTypeDefined in Agda-2.7.0.1 · Agda.TypeChecking.Rewriting.NonLinPattern · orphan
  • Free NLPatDefined in Agda-2.7.0.1 · Agda.TypeChecking.Rewriting.NonLinPattern · orphan

    Only computes free variables that are not bound (see nlPatVars), i.e., those in a PTerm.

  • Free t => Free (Arg t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free t => Free (WithHiding t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free t => Free (Abs t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free t => Free (Dom t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free t => Free (PlusLevel' t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free t => Free (Tele t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free t => Free (Type' t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free t => Free (Elim' t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free t => Free (SingleLevel' t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Level
  • Free t => Free (Maybe t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free t => Free [t]Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • Free t => Free (Named nm t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • (Free t, Free u) => Free (t, u)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy
  • (Free t, Free u, Free v) => Free (t, u, v)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.Lazy

Orphan instances

1 instance