All Topics  
Uniqueness type

 

   Email Print
   Bookmark   Link






 

Uniqueness type



 
 
In computing
Computing

Computing is usually defined as the activity of using and developing computer technology, computer hardware and computer software. It is the computer-specific part of information technology....
, 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 made to update the value in-place in the object code. In-place updates improve the efficiency of functional languages while maintaining referential transparency. Unique types can also be used to integrate functional and imperative programming.

ueness typing is best explained using an example.






Discussion
Ask a question about 'Uniqueness type'
Start a new discussion about 'Uniqueness type'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computing
Computing

Computing is usually defined as the activity of using and developing computer technology, computer hardware and computer software. It is the computer-specific part of information technology....
, 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 made to update the value in-place in the object code. In-place updates improve the efficiency of functional languages while maintaining referential transparency. Unique types can also be used to integrate functional and imperative programming.

Introduction

Uniqueness typing is best explained using an example. Consider a function readLine that reads the next line of text from a given file: function readLine(File f) returns String return line where String line = doImperativeReadLineSystemCall(f) end end

Now doImperativeReadLineSystemCall reads the next line from the file using an OS
Operating system

An operating system is an interface between hardware and applications; it is responsible for the management and coordination of activities and the sharing of the limited resources of the computer....
-level system call
System call

In computing, a system call is the mechanism used by an application program to request service from the kernel based on the Monolithic_kernel or to system servers on operating systems based on the microkernel-structure....
 which has the side-effect
Side effect (computer science)

In computer science, a subroutine or expression is said to produce a side effect if it modifies some state_ in addition to returning a value. For example, a function might modify a global or a static variable, modify one of its arguments, write data to a display or file, or read some data from other side-effecting functions....
 of changing the current position in the file. But this violates referential transparency because calling it multiple times with the same argument will return different results each time as the current position in the file gets moved. This in turn makes readLine violate referential transparency because it calls doImperativeReadLineSystemCall.

However, using uniqueness typing, we can construct a new version of readLine that is referentially transparent even though it's built on top of a function that's not referentially transparent: function readLine2(unique File f) returns (File, String) return (differentF, line) where String line = doImperativeReadLineSystemCall(f) File differentF = newFileFromExistingFile(f) end end

The "unique" declaration specifies that the type of f is unique; that is to say that f may never be referred to again by the caller of readLine2 after readLine2 returns, and this restriction is enforced by the type system
Type system

In computer science, a type system may be defined as "a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute."....
. And since readLine2 does not return f itself but rather a new, different file object differentF, this means that it's impossible for readLine2 to be called with f as an argument ever again, thus preserving referential transparency while allowing for side-effects to occur.

Programming languages

Uniqueness types are implemented in the functional programming languages Clean and Mercury. They are sometimes used for doing I/O
I/O

I/O may refer to:* Input/output, a system of communication for information processing systems* The input-output model, an economic model of flow prediction between sectors...
 operations in functional languages in lieu of monad
Monads in functional programming

In functional programming, a monad is a kind of abstract data type used to represent computations . Programs written in functional style can make use of monads to structure procedures that include sequenced operations, or to define arbitrary control flows ....
s.

Relationship to Linear typing

The term is often used interchangeably with Linear type, although often what is being discussed is technically uniqueness typing, as actual linear typing allows a non-linear value to be "cast" to a linear form, while still retaining multiple references to it. Uniqueness guarantees that a value has no other references to it, while linearity guarantees that no more references can be made to a value.

See also

  • Linear type
  • Linear logic
    Linear logic

    fr:Logique lin?aireLinear logic is a substructural logic invented by Jean-Yves Girard as a refinement of both classical_logic and intuitionistic logic, combining the symmetries of the former with many of the Constructivism_ properties of the latter....


External links


  • [https://www.cs.tcd.ie/~devriese/pub/ifl07-paper.pdf Uniqueness Typing Simplified]


Discussions of uniqueness typing in programming languages




Experiments with uniqueness typing (from a performance perspective)