HOME

TheInfoList



OR:

Substructural type systems are a family of
type system In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
s analogous to
substructural logic In logic, a substructural logic is a logic lacking one of the usual structural rules (e.g. of classical and intuitionistic logic), such as weakening, contraction, exchange or associativity. Two of the more significant substructural logics a ...
s where one or more of the
structural rule In the logical discipline of proof theory, a structural rule is an inference rule of a sequent calculus that does not refer to any logical connective but instead operates on the sequents directly. Structural rules often mimic the intended meta-the ...
s are absent or only allowed under controlled circumstances. Such systems can constrain access to
system resource In computing, a system resource, or simply resource, is any physical or virtual component of limited availability that is accessible to a computer. All connected devices and internal system components are resources. Virtual system resources in ...
s such as files, locks, and
memory Memory is the faculty of the mind by which data or information is encoded, stored, and retrieved when needed. It is the retention of information over time for the purpose of influencing future action. If past events could not be remembe ...
by keeping track of changes of state and prohibiting invalid states.


Different substructural type systems

Several type systems have emerged by discarding some of the
structural rule In the logical discipline of proof theory, a structural rule is an inference rule of a sequent calculus that does not refer to any logical connective but instead operates on the sequents directly. Structural rules often mimic the intended meta-the ...
s of exchange, weakening, and contraction: *''Ordered type systems'' (discard exchange, weakening and contraction): Every variable is used exactly once in the order it was introduced. *''Linear type systems'' (allow exchange, but neither weakening nor contraction): Every variable is used exactly once. *''Affine type systems'' (allow exchange and weakening, but not contraction): Every variable is used at most once. *''Relevant type systems'' (allow exchange and contraction, but not weakening): Every variable is used at least once. *''Normal type systems'' (allow exchange, weakening and contraction): Every variable may be used arbitrarily. The explanation for affine type systems is best understood if rephrased as “every ''occurrence'' of a variable is used at most once”.


Ordered type system

''Ordered types'' correspond to noncommutative logic where exchange, contraction and weakening are discarded. This can be used to model
stack-based memory allocation Stacks in computing architectures are regions of memory where data is added or removed in a last-in-first-out (LIFO) manner. In most modern computer systems, each thread has a reserved region of memory referred to as its stack. When a func ...
(contrast with linear types which can be used to model heap-based memory allocation). Without the exchange property, an object may only be used when at the top of the modelled stack, after which it is popped off, resulting in every variable being used exactly once in the order it was introduced.


Linear type systems

''Linear types'' correspond to
linear logic Linear logic is a substructural logic proposed by French logician Jean-Yves Girard as a refinement of classical and intuitionistic logic, joining the dualities of the former with many of the constructive properties of the latter. Although the ...
and ensure that objects are used exactly once. This allows the system to safely deallocate an object after its use, or to design software interfaces that guarantee a resource cannot be used once it has been closed or transitioned to a different state. The Clean programming language makes use of
uniqueness type In computing, a unique type guarantees that an object is used in a single-threaded way, with at most a single reference to it. If a value has a unique type, a function applied to it can be optimized to update the value in-place in the object cod ...
s (a variant of linear types) to help support concurrency,
input/output In computing, input/output (I/O, i/o, or informally io or IO) is the communication between an information processing system, such as a computer, and the outside world, such as another computer system, peripherals, or a human operator. Inputs a ...
, and in-place update of
arrays An array is a systematic arrangement of similar objects, usually in rows and columns. Things called an array include: {{TOC right Music * In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
. Linear type systems allow
references A reference is a relationship between Object (philosophy), objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to ''refer to'' the second object. ...
but not aliases. To enforce this, a reference goes out of scope after appearing on the right-hand side of an assignment, thus ensuring that only one reference to any object exists at once. Note that passing a reference as an
argument An argument is a series of sentences, statements, or propositions some of which are called premises and one is the conclusion. The purpose of an argument is to give reasons for one's conclusion via justification, explanation, and/or persu ...
to a function is a form of assignment, as the function parameter will be assigned the value inside the function, and therefore such use of a reference also causes it to go out of scope. The single-reference property makes linear type systems suitable as programming languages for
quantum computing A quantum computer is a computer that exploits quantum mechanical phenomena. On small scales, physical matter exhibits properties of wave-particle duality, both particles and waves, and quantum computing takes advantage of this behavior using s ...
, as it reflects the no-cloning theorem of quantum states. From the
category theory Category theory is a general theory of mathematical structures and their relations. It was introduced by Samuel Eilenberg and Saunders Mac Lane in the middle of the 20th century in their foundational work on algebraic topology. Category theory ...
point of view, no-cloning is a statement that there is no
diagonal functor In category theory, a branch of mathematics, the diagonal functor \mathcal \rightarrow \mathcal \times \mathcal is given by \Delta(a) = \langle a,a \rangle, which maps objects as well as morphisms. This functor can be employed to give a succinct a ...
which could duplicate states; similarly, from the
combinatory logic Combinatory logic is a notation to eliminate the need for quantified variables in mathematical logic. It was introduced by Moses Schönfinkel and Haskell Curry, and has more recently been used in computer science as a theoretical model of com ...
point of view, there is no K-combinator which can destroy states. From the
lambda calculus In mathematical logic, the lambda calculus (also written as ''λ''-calculus) is a formal system for expressing computability, computation based on function Abstraction (computer science), abstraction and function application, application using var ...
point of view, a variable x can appear exactly once in a term. Linear type systems are the internal language of closed symmetric monoidal categories, much in the same way that
simply typed lambda calculus The simply typed lambda calculus (), a form of type theory, is a typed interpretation of the lambda calculus with only one type constructor () that builds function types. It is the canonical and simplest example of a typed lambda calculus. The ...
is the language of Cartesian closed categories. More precisely, one may construct
functor In mathematics, specifically category theory, a functor is a Map (mathematics), mapping between Category (mathematics), categories. Functors were first considered in algebraic topology, where algebraic objects (such as the fundamental group) ar ...
s between the category of linear type systems and the category of closed symmetric monoidal categories.


Affine type systems

''Affine types'' are a version of linear types allowing to ''discard'' (i.e. ''not use'') a resource, corresponding to
affine logic Affine logic is a substructural logic whose proof theory rejects the structural rule of contraction. It can also be characterized as linear logic with weakening. The name "affine logic" is associated with linear logic, to which it differs by ...
. An affine resource ''can'' be used ''at most'' once, while a linear one ''must'' be used ''exactly'' once.


Relevant type system

''Relevant types'' correspond to relevant logic which allows exchange and contraction, but not weakening, which translates to every variable being used at least once.


The resource interpretation

The nomenclature offered by substructural type systems is useful to characterize
resource management In organizational studies, resource management is the efficient and effective development of an organization's resources when they are needed. Such resources may include the financial resources, inventory, human skills, production resources, or ...
aspects of a language. Resource management is the aspect of language safety concerned with ensuring that each allocated resource is deallocated exactly once. Thus, the resource interpretation is only concerned with uses that transfer ownership – ''moving'', where ownership is the responsibility to free the resource. Uses that don't transfer ownership – ''borrowing'' – are not in scope of this interpretation, but ''lifetime semantics'' further restrict these uses to be between allocation and deallocation.


Resource-affine types

Under the resource interpretation, an ''affine'' type can not be spent more than once. As an example, the same variant of Hoare's vending machine can be expressed in English, logic and in
Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH) ...
: What it means for to be an affine type in this example (which it is unless it implements the trait) is that trying to spend the same coin twice is an invalid program that the compiler is entitled to reject: let coin = Coin ; let candy = buy_candy(coin); // The lifetime of the coin variable ends here. let drink = buy_drink(coin); // Compilation error: Use of moved variable that does not possess the Copy trait. In other words, an affine type system can express the typestate pattern: Functions can consume and return an object wrapped in different types, acting like state transitions in a
state machine A finite-state machine (FSM) or finite-state automaton (FSA, plural: ''automata''), finite automaton, or simply a state machine, is a mathematical model of computation. It is an abstract machine that can be in exactly one of a finite number o ...
that stores its state as a type in the caller's context – a ''typestate''. An
API An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
can exploit this to statically enforce that its functions are called in a correct order. What it doesn't mean, however, is that a variable can't be used without using it up: // This function just borrows a coin: The ampersand means borrow. fn validate(_: &Coin) -> Result<(), ()> // The same coin variable can be used infinitely many times // as long as it is not moved. let coin = Coin ; loop What Rust is not able to express is a coin type that cannot go out of scope – that would take a linear type.


Resource-linear types

Under the resource interpretation, a ''linear'' type not only ''can'' be moved, like an affine type, but ''must'' be moved – going out of scope is an invalid program. An attraction with linear types is that destructors become regular functions that can take arguments, can fail and so on. This may for example avoid the need to keep state that is only used for destruction. A general advantage of passing function dependencies explicitly is that the order of function calls – destruction order – becomes statically verifiable in terms of the arguments' lifetimes. Compared to internal references, this does not require lifetime annotations as in Rust. As with manual resource management, a practical problem is that any early return, as is typical of error handling, must achieve the same cleanup. This becomes pedantic in languages that have stack unwinding, where every function call is a potential early return. However, as a close analogy, the semantic of implicitly inserted destructor calls can be restored with deferred function calls.


Resource-normal types

Under the resource interpretation, a ''normal'' type does not restrict how many times a variable can be moved from. C++ (specifically nondestructive move semantics) falls in this category. auto coin = std::unique_ptr(); auto candy = buy_candy(std::move(coin)); auto drink = buy_drink(std::move(coin)); // This is valid C++.


Programming languages

The following programming languages support linear or affine types: * ATS * Clean *
Idris Idris may refer to: People * Idris (name), a list of people and fictional characters with the given name or surname * Idris (prophet), Islamic prophet in the Qur'an, traditionally identified with Enoch, an ancestor of Noah in the Bible * Idris ...
* Mercury * F*
LinearML

Alms
*
Haskell Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
with Glasgow Haskell Compiler (GHC) 9.0.1 or above
Granule
*
Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH) ...
*
Swift Swift or SWIFT most commonly refers to: * SWIFT, an international organization facilitating transactions between banks ** SWIFT code * Swift (programming language) * Swift (bird), a family of birds It may also refer to: Organizations * SWIF ...
5.9 and above


See also

*
Effect system In computing, an effect system is a formal system that describes the computational effects of computer programs, such as side effects. An effect system can be used to provide a compile-time check of the possible effects of the program. The effec ...
*
Linear logic Linear logic is a substructural logic proposed by French logician Jean-Yves Girard as a refinement of classical and intuitionistic logic, joining the dualities of the former with many of the constructive properties of the latter. Although the ...
*
Affine logic Affine logic is a substructural logic whose proof theory rejects the structural rule of contraction. It can also be characterized as linear logic with weakening. The name "affine logic" is associated with linear logic, to which it differs by ...


References

{{Reflist, 2 Type theory