//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2020, 2026 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available
// under the terms of the MIT License which is available at
// https://opensource.org/licenses/MIT
//
// SPDX-License-Identifier: MIT
//////////////////////////////////////////////////////////////////////////////

include::../_part_attributes.asciidoc[]

indexterm:[CIF to CIF transformer,remove unused algebraic variables]
indexterm:[remove unused algebraic variables]
indexterm:[algebraic variables,remove unused]

:leveloffset: -2

[[tools-cif2cif-chapter-remove-unused-alg-vars]]
== Remove unused algebraic variables

This CIF to CIF transformation removes unused algebraic variables and their equations from the specification.
As these removed algebraic variables are defined but not used, removing them does not alter the behavior modeled by the CIF specification.

=== Supported specifications

This transformation supports a subset of CIF specifications.
The following restrictions apply:

* Component definitions and component instantiations are not supported.

=== Preprocessing

No preprocessing is currently performed by this CIF to CIF transformation.
To increase the subset of specifications that can be transformed, apply the following <<tools-cif2cif-chapter-index,CIF to CIF transformations>> (in the given order):

* <<tools-cif2cif-chapter-elim-comp-def-inst>>

=== Implementation details

An algebraic variable is considered to be _unused_ in a CIF specification if it only occurs as an algebraic variable definition.

[source, cif]
----
alg bool var1 = false;
alg bool var2 = true;

plant A:
  uncontrollable a;
  location:
    initial;
    edge a when var1;
end
----

As algebraic variable `var2` is defined but not mentioned elsewhere in the specification it is removed.
Algebraic variable `var1` is mentioned in an edge guard.
It is therefore considered _used_ and preserved as you can see in the resulting specification:

[source, cif]
----
alg bool var1 = false;

plant A:
  uncontrollable a;
  location:
    initial;
    edge a when var1;
end
----

As algebraic variables may be used to define other algebraic variables, removal of an algebraic variable may trigger more removals.
For instance, consider the following specification:

[source, cif]
----
alg bool var1 = false;
alg bool var2 = not var1;
alg bool var3 = var2;

plant A:
  uncontrollable a;
  location:
    initial;
    edge a when var1;
end
----

Algebraic variable `var3` is only defined but not mentioned elsewhere.
It is thus considered _unused_ and removed.
After the removal of `var3`, algebraic variable `var2` is not mentioned elsewhere anymore.
It is thus now considered _unused_ as well and also removed.
Algebraic variable `var1` is considered _used_ as before and not removed.

The result is therefore:

[source, cif]
----
alg bool var1 = false;

plant A:
  uncontrollable a;
  location:
    initial;
    edge a when var1;
end
----

=== Renaming

n/a

=== Size considerations

Since information is removed from the specification, the specification can only become smaller as a result of this transformation.

=== Optimality

n/a

=== Annotations

This transformation does not process or add any annotations.
As this transformation removes all unused algebraic variables from the specification, their annotations are removed as well.
