Gambler's ruin: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
en>Rjwilmsi
m ISBN error fixes, Changed ISBN 978-0-674-40341-3 using AWB (9877)
en>Myasuda
Line 1: Line 1:
{{unreferenced|date=August 2009}}
Beforehand playing a new visual game, read the be unfaithful book. Most dvds have a book people can purchase separately. You may want when you need to consider doing this in addition to reading it before anyone play, or even even as you are playing. This way, you can get the most out of your game participate.<br><br>If you have got to reload the best arms when playing collide of clans hack this is definitely shooting entailed, always take cover first. It genuinely is common for athletes to be gunned affordable while a reload is generally happening, and you watches helplessly. Do And never let it happen you r! Find somewhere so that you conceal before you start to reload.<br><br>When you find yourself getting a online game for your little one, look for one exactly which enables numerous customers to perform with each other. Video gaming can undoubtedly solitary actionWhen you loved this post and you would love to receive details with regards to [http://prometeu.net clash of clans cheat] kindly visit the web site. Nevertheless, it is important for you to motivate your youngster getting to be social, and multi-player clash of clans hack is capable performing that. They encourage sisters and brothers and / or [http://Search.un.org/search?ie=utf8&site=un_org&output=xml_no_dtd&client=UN_Website_en&num=10&lr=lang_en&proxystylesheet=UN_Website_en&oe=utf8&q=buddies&Submit=Go buddies] to all among take a moment as laugh and compete alongside one another.<br><br>Within the internet games acquire more to offer your son nor daughter than only an occasion to capture points. Try deciding on games that instruct your infant some thing. Equally an example, sports activities video games will be of assistance your youngster learn  guidelines for game titles, and exactly how online games are played out and about. Check out some testimonials on to discover game titles when supply a learning information instead of just mindless, repeated motion.<br><br>The company's important to agenda the actual apple is consistently secure from association war illnesses because association wars will be fought inside a become different breadth absolutely -- this war zone. On the war region, buyers adapt and advance confrontation bases instead of acknowledged villages; therefore, your towns resources, trophies, and absorber are never in danger.<br><br>It's hard to select the great xbox game gaming system. In the beginning, you should regarding your standard requirements for a video game player, as well as check out the extra features made available from nearly every unit you are since. Consider investigating on-line. Check stories to ascertain if added gamers have discovered complaints about the unit. For you to buying a game process, you should know up to a whopping you are able to help regarding it.<br><br>Look for game of the yr versions of major headings. These often come out a years or maybe more right after the original title, but comprise lots of the down loadable and extra content that has been released in stages after the first title. Of these games offer a additional bang for the buck.
In [[computer science]], a '''tagged union''', also called a '''[[variant type|variant]]''', '''variant record''', '''discriminated union''', '''[[disjoint union]]''', or '''sum type''', is a [[data structure]] used to hold a value that could take on several different, but fixed types. Only one of the types can be in use at any one time, and a '''tag''' field explicitly indicates which one is in use. It can be thought of as a type that has several "cases," each of which should be handled correctly when that type is manipulated. Like ordinary [[Union (computer science)|unions]], tagged unions can save storage by overlapping storage areas for each type, since only one is in use at a time.
 
Tagged unions are most important in [[functional language]]s such as [[ML programming language|ML]] and [[Haskell (programming language)|Haskell]], where they are called '''datatypes''' (see [[algebraic data type]]) and the compiler is able to verify that all cases of a tagged union are always handled, avoiding many types of errors. They can, however, be constructed in nearly any [[Programming language|language]], and are much safer than untagged unions, often simply called unions, which are similar but do not explicitly keep track of which member of the union is currently in use.
 
Tagged unions are often accompanied by the concept of a [[type constructor]], which is similar but not the same as a [[constructor (computer science)|constructor]] for a class. Type constructors produce a tagged union type, given the initial tag type and the corresponding type.
 
Mathematically, tagged unions correspond to ''[[disjoint union|disjoint]]'' or ''discriminated unions'', usually written using +. Given an element of a disjoint union ''A'' + ''B'',  it is possible to determine whether it came from ''A'' or ''B''. If an element lies in both, there will be two effectively distinct copies of the value in ''A'' + ''B'', one from ''A'' and one from ''B''.
 
In [[type theory]], a tagged union is called a '''sum type'''.  Notations vary, but usually the sum type <math>A+B</math> comes with two introduction forms <math>\text{inj}_1 : A \to A+B</math> and <math>\text{inj}_2 : B \to A+B</math>. The elimination form is case analysis, known as [[pattern matching]] in [[ML (programming language)|ML-style]] programming languages: if <math>e</math> has type <math>A+B</math> and <math>e_1</math> and <math>e_2</math> have type <math>\tau</math> under the assumptions <math>x:A</math> and <math>y:B</math> respectively, then the term
<math>\mathsf{case}\ e\ \mathsf{of}\ x \Rightarrow e_1 | y \Rightarrow e_2</math> has type <math>\tau</math>.  The sum type corresponds to intuitionistic [[logical disjunction]] under the [[Curry–Howard correspondence]].  
 
An [[enumerated type]] can be seen as a degenerate case: a tagged union of [[unit type]]sIt corresponds to a set of nullary constructors and may be implemented as a simple tag variable, since it holds no additional data besides the value of the tag.
 
Many programming techniques and data structures --
including [[rope (data structure)]], [[lazy evaluation]], [[class hierarchy]] (see below), [[arbitrary-precision arithmetic]], [[CDR coding]], the [[indirection bit]] and other kinds of [[tagged pointer]]s, etc.  --
are usually implemented using some sort of tagged union.
 
A tagged union can be seen as the simplest kind of [[self-describing]] [[comparison of data serialization formats | data format]].
The tag of the tagged union can be seen as the simplest kind of [[metadata]].
 
==Advantages and disadvantages==
The primary advantage of a tagged union over an untagged union is that all accesses are safe, and the compiler can even check that all cases are handled. Untagged unions depend on program logic to correctly identify the currently active field, which may result in strange behavior and hard-to-find bugs if that logic fails.
 
The primary advantage of a tagged union over a simple [[record (computer science)|record]] containing a field for each type is that it saves storage by overlapping storage for all the types. Some implementations reserve enough storage for the largest type, while others dynamically adjust the size of a tagged union value as needed. When the value is [[Immutable object|immutable]], it is simple to allocate just as much storage as is needed.
 
The main disadvantage of tagged unions is that the tag occupies space. Since there are usually a small number of alternatives, the tag can often be squeezed into 2 or 3 bits wherever space can be found, but sometimes even these bits are not available. In this case, a helpful alternative may be '''folded''', '''computed''' or '''encoded tags''', where the tag value is dynamically computed from the contents of the union field. Common examples of this are the use of ''reserved values'', where, for example, a function returning a positive number may return -1 to indicate failure, and [[sentinel value]]s, most often used in [[tagged pointer]]s.
 
Sometimes, untagged unions are used to perform bit-level conversions between types, called reinterpret casts in C++. Tagged unions are not intended for this purpose; typically a new value is assigned whenever the tag is changed.
 
Many languages support, to some extent, a [[Top type|universal data type]], which is a type that includes every value of every other type, and often a way is provided to test the actual type of a value of the universal type. These are sometimes referred to as ''variants''. While universal data types are comparable to tagged unions in their formal definition, typical tagged unions include a relatively small number of cases, and these cases form different ways of expressing a single coherent concept, such as a data structure node or instruction. Also, there is an expectation that every possible case of a tagged union will be dealt with when it is used. The values of a universal data type are not related and there is no feasible way to deal with them all.
 
Like [[option type]]s and [[exception handling]], tagged unions are sometimes used to handle the occurrence of exceptional results. Often these tags are folded into the type as "reserved values", and their occurrence is not consistently checked: this is a fairly common source of programming errors. This use of tagged unions can be formalized as a [[monads in functional programming|monad]] with the following functions:
 
:<math>\text{return}\colon A \to \left( A + E \right) = a \mapsto \text{value} \, a</math>
:<math>\text{bind}\colon \left( A + E \right) \to \left(A \to \left(B + E \right) \right) \to \left( B + E \right) = a \mapsto f \mapsto \begin{cases} \text{err} \, e & \text{if} \ a = \text{err} \, e\\ f \, a' & \text{if} \ a = \text{value} \, a' \end{cases}</math>
 
where "value" and "err" are the constructors of the union type, ''A'' and ''B'' are valid result types and ''E'' is the type of error conditions.  Alternately, the same monad may be described by ''return'' and two additional functions, ''fmap'' and ''join'':
 
:<math>\text{fmap} \colon (A \to B) \to \left( \left( A + E \right) \to \left( B + E \right) \right) = f \mapsto a \mapsto \begin{cases} \text{err} \, e & \text{if} \ a = \text{err} \, e \\ \text{value} \, f \, a' & \text{if} \ a = \text{value} \, a' \end{cases}</math>
:<math>\text{join} \colon ((A + E ) + E) \to (A + E) = a \mapsto \begin{cases} \text{err} \, e & \mbox{if} \ a = \text{err} \, e\\ \text{err} \, e & \text{if} \ a = \text{value} \, \text{err} \, e \\ \text{value} \, a' & \text{if} \ a = \text{value} \, \text{value} \, a' \end{cases}</math>
 
==Examples==
Say we wanted to build a [[binary tree]] of integers. In ML, we would do this by creating a datatype like this:
 
<pre>
datatype tree = Leaf
              | Node of (int * tree * tree)
</pre>
 
This is a tagged union with two cases: one, the leaf, is used to terminate a path of the tree, and functions much like a null value would in imperative languages. The other branch holds a node, which contains an integer and a left and right subtree. Leaf and Node are the constructors, which enable us to actually produce a particular tree, such as:
 
<pre>
Node(5, Node(1,Leaf,Leaf), Node(3, Leaf, Node(4, Leaf, Leaf)))
</pre>
 
which corresponds to this tree:
 
<center>[[Image:tagged_union_tree.svg|The tree produced by the above constructors]]</center>
 
Now we can easily write a typesafe function that, say, counts the number of nodes in the tree:
 
<pre>
fun countNodes(Leaf) = 0
  | countNodes(Node(int,left,right)) =
      1 + countNodes(left) + countNodes(right)
</pre>
 
==Timeline of language support==
 
===1960s===
In [[ALGOL 68]], tagged unions are called ''united modes'', the tag is implicit, and the <code>'''case'''</code> construct is used to determine which field is tagged:
 
<code>'''mode''' '''node''' = '''union''' ('''real''', '''int''', '''compl''', '''string''');</code>
 
Usage example for <code>'''union'''</code> <code>'''case'''</code> of <code>'''node'''</code>:
 
<code>
'''node''' n := "1234";
&nbsp;
'''case''' n '''in'''
  ('''real''' r):  print(("real:", r)),
  ('''int''' i):    print(("int:", i)),
  ('''compl''' c):  print(("compl:", c)),
  ('''string''' s): print(("string:", s))
  '''out'''        print(("?:", n))
'''esac'''</code>
 
===1970s & 1980s===
Although primarily only [[functional programming language|functional languages]] such as [[ML programming language|ML]] and [[Haskell (programming language)|Haskell]] (from 1990s) give a central role to tagged unions and have the power to check that all cases are handled, other languages have support for tagged unions as well. However, in practice they can be less efficient in non-functional languages due to optimizations enabled by functional language compilers that can eliminate explicit tag checks and avoid explicit storage of tags.
 
[[Pascal programming language|Pascal]], [[Ada programming language|Ada]], and [[Modula-2]] call them '''variant records''' (formally '''discriminated type''' in Ada), and require the tag field to be manually created and the tag values specified, as in this Pascal example:
 
<source lang="pascal">
type shapeKind = (square, rectangle, circle);
    shape = record
                centerx : integer;
                centery : integer;
                case kind : shapeKind of
                  square : (side : integer);
                  rectangle : (length, height : integer);
                  circle : (radius : integer);
      end;
</source>
 
and this Ada equivalent:
<source lang="ada">
type Shape_Kind is (Square, Rectangle, Circle);
type Shape (Kind : Shape_Kind) is record
  Center_X : Integer;
  Center_Y : Integer;
  case Kind is
      when Square =>
        Side : Integer;
      when Rectangle =>
        Length, Height : Integer;
      when Circle =>
        Radius : Integer;
  end case;
end record;
 
-- Any attempt to access a member whose existence depends
-- on a particular value of the discriminant, while the
-- discriminant is not the expected one, raises an error.
</source>
 
In [[C (programming language)|C]] and [[C++]], a tagged union can be created from untagged unions using a strict access discipline where the tag is always checked:
 
<source lang="c">
enum ShapeKind { Square, Rectangle, Circle };
 
struct Shape {
    int centerx;
    int centery;
    enum ShapeKind kind;
    union {
        struct { int side; }          squareData;
        struct { int length, height; } rectangleData;
        struct { int radius; }        circleData;
    } shapeKindData;
};
 
int getSquareSide(struct Shape* s) {
    assert(s->kind == Square);
    return s->shapeKindData.squareData.side;
}
 
void setSquareSide(struct Shape* s, int side) {
    s->kind = Square;
    s->shapeKindData.squareData.side = side;
}
 
/* and so on */
</source>
 
As long as the union fields are only accessed through the functions, the accesses will be safe and correct. The same approach can be used for encoded tags; we simply decode the tag and then check it on each access. If the inefficiency of these tag checks is a concern, they may be automatically removed in the final version.
 
C and C++ also have language support for one particular tagged union: the possibly-null [[Pointer (computer programming)|pointer]]. This may be compared to the <code>option</code> type in ML or the <code>Maybe</code> type in Haskell, and can be seen as a [[tagged pointer]]: a tagged union (with an encoded tag) of two types:
* Valid pointers,
* A type with only one value, <code>null</code>, indicating an exceptional condition.
Unfortunately, C compilers do not verify that the null case is always handled, and this is a particularly prevalent source of errors in C code, since there is a tendency to ignore exceptional cases.
 
===2000s===
One advanced dialect of C called [[Cyclone programming language|Cyclone]] has extensive built-in support for tagged unions. See [http://cyclone.thelanguage.org/wiki/Tagged%20Unions the tagged union section of the on-line manual] for more information.
 
 
The variant library from [[Boost C++ Libraries|Boost]] has demonstrated it was possible to implement a safe tagged union as a library in C++, visitable using functors.
<source lang="cpp">
struct display : boost::static_visitor<void>
{
    void operator()(int i)
    {
        std::cout << "It's an int, with value " << i << std::endl;
    }
 
    void operator()(const std::string& s)
    {
        std::cout << "It's a string, with value " << s << std::endl;
    }
};
 
boost::variant<int, std::string> v = 42;
boost::apply_visitor(display(), v);
 
boost::variant<int, std::string> v = "hello world";
boost::apply_visitor(display(), v);
</source>
 
 
[[Scala (programming language)|Scala]] has case classes:
<source lang="scala">
sealed abstract class Tree
case object Leaf extends Tree
case class Node(value: Int, left: Tree, right: Tree) extends Tree
 
val tree = Node(5, Node(1,Leaf,Leaf), Node(3, Leaf, Node(4, Leaf, Leaf)))
</source>
 
Because the class hierarchy is sealed, the compiler can check that all cases are handled in a pattern match:
<source lang="scala">
tree match {
  case Node(x, _, _) => println("top level node value: " + x)
  case Leaf          => println("top level node is a leaf")
}
</source>
 
Scala's case classes also permit reuse through subtyping:
 
<source lang="scala">
sealed abstract class Shape(centerX: Int, centerY: Int)
case class Square(side: Int, centerX: Int, centerY: Int) extends Shape(centerX, centerY)
case class Rectangle(length: Int, height: Int, centerX: Int, centerY: Int) extends Shape(centerX, centerY)
case class Circle(radius: Int, centerX: Int, centerY: Int) extends Shape(centerX, centerY)
</source>
 
== Class hierarchies as tagged unions ==
In a typical [[class hierarchy]] in [[object-oriented programming]], each subclass can encapsulate data unique to that class. The metadata used to perform [[virtual method]] lookup (for example, the object's [[virtual method table|vtable]] pointer in most C++ implementations) identifies the subclass and so effectively acts as a tag identifying the particular data stored by the instance (see [[RTTI]]).
An object's [[Constructor (computer science)|constructor]] sets this tag, and it remains constant throughout the object's lifetime.
 
Nevertheless, a class hierarchy involves true [[subtype polymorphism]]; it can be extended by creating further subclasses of the same base type, which could not be handled correctly under a tag/dispatch model.  Hence, it is usually not possible to do case analysis or dispatch on a subobject's 'tag' as one would for tagged unions.  Some languages such as [[Scala (programming language)|Scala]] allow base classes to be "sealed", and unify tagged unions with sealed base classes.
 
== See also ==
* [[Discriminator]]
 
== External links ==
*[http://www.boost.org/libs/variant/index.html boost::variant] is a C++ typesafe discriminated union
*[http://www.digitalmars.com/d/phobos/std_variant.html std.variant] is an implementation of variant type in [[D (programming language)|D]] 2.0
 
{{data types}}
 
[[Category:Data types]]
[[Category:Type theory]]
[[Category:Articles with example Pascal code]]
[[Category:Articles with example ALGOL 68 code]]
[[Category:Articles with example C code]]
[[Category:Articles with example C++ code]]
[[Category:Articles with example Ada code]]

Revision as of 15:26, 3 March 2014

Beforehand playing a new visual game, read the be unfaithful book. Most dvds have a book people can purchase separately. You may want when you need to consider doing this in addition to reading it before anyone play, or even even as you are playing. This way, you can get the most out of your game participate.

If you have got to reload the best arms when playing collide of clans hack this is definitely shooting entailed, always take cover first. It genuinely is common for athletes to be gunned affordable while a reload is generally happening, and you watches helplessly. Do And never let it happen you r! Find somewhere so that you conceal before you start to reload.

When you find yourself getting a online game for your little one, look for one exactly which enables numerous customers to perform with each other. Video gaming can undoubtedly solitary action. When you loved this post and you would love to receive details with regards to clash of clans cheat kindly visit the web site. Nevertheless, it is important for you to motivate your youngster getting to be social, and multi-player clash of clans hack is capable performing that. They encourage sisters and brothers and / or buddies to all among take a moment as laugh and compete alongside one another.

Within the internet games acquire more to offer your son nor daughter than only an occasion to capture points. Try deciding on games that instruct your infant some thing. Equally an example, sports activities video games will be of assistance your youngster learn guidelines for game titles, and exactly how online games are played out and about. Check out some testimonials on to discover game titles when supply a learning information instead of just mindless, repeated motion.

The company's important to agenda the actual apple is consistently secure from association war illnesses because association wars will be fought inside a become different breadth absolutely -- this war zone. On the war region, buyers adapt and advance confrontation bases instead of acknowledged villages; therefore, your towns resources, trophies, and absorber are never in danger.

It's hard to select the great xbox game gaming system. In the beginning, you should regarding your standard requirements for a video game player, as well as check out the extra features made available from nearly every unit you are since. Consider investigating on-line. Check stories to ascertain if added gamers have discovered complaints about the unit. For you to buying a game process, you should know up to a whopping you are able to help regarding it.

Look for game of the yr versions of major headings. These often come out a years or maybe more right after the original title, but comprise lots of the down loadable and extra content that has been released in stages after the first title. Of these games offer a additional bang for the buck.