<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
type	B-Language
safety	I-Language
and	O
type	B-Language
soundness	I-Language
are	O
the	O
extent	O
to	O
which	O
a	O
programming	O
language	O
discourages	O
or	O
prevents	O
type	O
errors	O
.	O
</s>
<s>
Type	B-Language
safety	I-Language
is	O
sometimes	O
alternatively	O
considered	O
to	O
be	O
a	O
property	O
of	O
facilities	O
of	O
a	O
computer	O
language	O
;	O
that	O
is	O
,	O
some	O
facilities	O
are	O
type-safe	B-Language
and	O
their	O
usage	O
will	O
not	O
result	O
in	O
type	O
errors	O
,	O
while	O
other	O
facilities	O
in	O
the	O
same	O
language	O
may	O
be	O
type-unsafe	O
and	O
a	O
program	O
using	O
them	O
may	O
encounter	O
type	O
errors	O
.	O
</s>
<s>
Type	O
enforcement	O
can	O
be	O
static	O
,	O
catching	O
potential	O
errors	O
at	O
compile	B-Application
time	I-Application
,	O
or	O
dynamic	O
,	O
associating	O
type	O
information	O
with	O
values	O
at	O
run-time	B-Library
and	O
consulting	O
them	O
as	O
needed	O
to	O
detect	O
imminent	O
errors	O
,	O
or	O
a	O
combination	O
of	O
both	O
.	O
</s>
<s>
In	O
the	O
context	O
of	O
static	O
(	O
compile-time	B-Application
)	O
type	O
systems	O
,	O
type	B-Language
safety	I-Language
usually	O
involves	O
(	O
among	O
other	O
things	O
)	O
a	O
guarantee	O
that	O
the	O
eventual	O
value	O
of	O
any	O
expression	O
will	O
be	O
a	O
legitimate	O
member	O
of	O
that	O
expression	O
's	O
static	O
type	O
.	O
</s>
<s>
The	O
precise	O
requirement	O
is	O
more	O
subtle	O
than	O
this	O
—	O
see	O
,	O
for	O
example	O
,	O
subtyping	O
and	O
polymorphism	B-Application
for	O
complications	O
.	O
</s>
<s>
A	O
language	O
is	O
type-safe	B-Language
if	O
the	O
only	O
operations	O
that	O
can	O
be	O
performed	O
on	O
data	O
in	O
the	O
language	O
are	O
those	O
sanctioned	O
by	O
the	O
type	O
of	O
the	O
data	O
.	O
</s>
<s>
However	O
,	O
what	O
precisely	O
it	O
means	O
for	O
a	O
program	O
to	O
be	O
"	O
well	O
typed	O
"	O
or	O
to	O
"	O
go	O
wrong	O
"	O
are	O
properties	O
of	O
its	O
static	O
and	O
dynamic	O
semantics	B-Application
,	O
which	O
are	O
specific	O
to	O
each	O
programming	O
language	O
.	O
</s>
<s>
Consequently	O
,	O
a	O
precise	O
,	O
formal	O
definition	O
of	O
type	B-Language
soundness	I-Language
depends	O
upon	O
the	O
style	O
of	O
formal	O
semantics	B-Application
used	O
to	O
specify	O
a	O
language	O
.	O
</s>
<s>
In	O
1994	O
,	O
Andrew	O
Wright	O
and	O
Matthias	O
Felleisen	O
formulated	O
what	O
has	O
become	O
the	O
standard	O
definition	O
and	O
proof	O
technique	O
for	O
type	B-Language
safety	I-Language
in	O
languages	O
defined	O
by	O
operational	O
semantics	B-Application
,	O
which	O
is	O
closest	O
to	O
the	O
notion	O
of	O
type	B-Language
safety	I-Language
as	O
understood	O
by	O
most	O
programmers	O
.	O
</s>
<s>
Under	O
this	O
approach	O
,	O
the	O
semantics	B-Application
of	O
a	O
language	O
must	O
have	O
the	O
following	O
two	O
properties	O
to	O
be	O
considered	O
type-sound	O
:	O
</s>
<s>
A	O
number	O
of	O
other	O
formal	O
treatments	O
of	O
type	B-Language
soundness	I-Language
have	O
also	O
been	O
published	O
in	O
terms	O
of	O
denotational	B-Application
semantics	I-Application
and	O
structural	O
operational	O
semantics	B-Application
.	O
</s>
<s>
In	O
isolation	O
,	O
type	B-Language
soundness	I-Language
is	O
a	O
relatively	O
weak	O
property	O
,	O
as	O
it	O
essentially	O
just	O
states	O
that	O
the	O
rules	O
of	O
a	O
type	O
system	O
are	O
internally	O
consistent	O
and	O
cannot	O
be	O
subverted	O
.	O
</s>
<s>
Type	O
systems	O
can	O
prevent	O
wild	B-Error_Name
pointers	I-Error_Name
that	O
could	O
otherwise	O
arise	O
from	O
a	O
pointer	O
to	O
one	O
type	O
of	O
object	O
being	O
treated	O
as	O
a	O
pointer	O
to	O
another	O
type	O
.	O
</s>
<s>
More	O
sophisticated	O
type	O
systems	O
,	O
such	O
as	O
those	O
supporting	O
dependent	O
types	O
,	O
can	O
detect	O
and	O
reject	O
out-of-bound	O
accesses	O
,	O
preventing	O
potential	O
buffer	B-General_Concept
overflows	I-General_Concept
.	O
</s>
<s>
Logic	B-General_Concept
errors	I-General_Concept
originating	O
in	O
the	O
semantics	B-Application
of	O
different	O
types	O
.	O
</s>
<s>
Type	B-Language
safety	I-Language
is	O
usually	O
a	O
requirement	O
for	O
any	O
toy	O
language	O
(	O
i.e.	O
</s>
<s>
Many	O
languages	O
,	O
on	O
the	O
other	O
hand	O
,	O
are	O
too	O
big	O
for	O
human-generated	O
type	B-Language
safety	I-Language
proofs	O
,	O
as	O
they	O
often	O
require	O
checking	O
thousands	O
of	O
cases	O
.	O
</s>
<s>
Nevertheless	O
,	O
some	O
languages	O
such	O
as	O
Standard	B-Language
ML	I-Language
,	O
which	O
has	O
rigorously	O
defined	O
semantics	B-Application
,	O
have	O
been	O
proved	O
to	O
meet	O
one	O
definition	O
of	O
type	B-Language
safety	I-Language
.	O
</s>
<s>
Some	O
other	O
languages	O
such	O
as	O
Haskell	B-Language
are	O
believed	O
to	O
meet	O
some	O
definition	O
of	O
type	B-Language
safety	I-Language
,	O
provided	O
certain	O
"	O
escape	O
"	O
features	O
are	O
not	O
used	O
(	O
for	O
example	O
Haskell	B-Language
's	O
,	O
used	O
to	O
"	O
escape	O
"	O
from	O
the	O
usual	O
restricted	O
environment	O
in	O
which	O
I/O	O
is	O
possible	O
,	O
circumvents	O
the	O
type	O
system	O
and	O
so	O
can	O
be	O
used	O
to	O
break	O
type	B-Language
safety	I-Language
.	O
)	O
</s>
<s>
Type	B-Language
punning	I-Language
is	O
another	O
example	O
of	O
such	O
an	O
"	O
escape	O
"	O
feature	O
.	O
</s>
<s>
Regardless	O
of	O
the	O
properties	O
of	O
the	O
language	O
definition	O
,	O
certain	O
errors	O
may	O
occur	O
at	O
run-time	B-Library
due	O
to	O
bugs	O
in	O
the	O
implementation	O
,	O
or	O
in	O
linked	O
libraries	B-Library
written	O
in	O
other	O
languages	O
;	O
such	O
errors	O
could	O
render	O
a	O
given	O
implementation	O
type	O
unsafe	O
in	O
certain	O
circumstances	O
.	O
</s>
<s>
An	O
early	O
version	O
of	O
Sun	O
's	O
Java	B-Language
virtual	I-Language
machine	I-Language
was	O
vulnerable	O
to	O
this	O
sort	O
of	O
problem	O
.	O
</s>
<s>
Programming	O
languages	O
are	O
often	O
colloquially	O
classified	O
as	O
strongly	O
typed	O
or	O
weakly	O
typed	O
(	O
also	O
loosely	O
typed	O
)	O
to	O
refer	O
to	O
certain	O
aspects	O
of	O
type	B-Language
safety	I-Language
.	O
</s>
<s>
Type	B-Language
safety	I-Language
is	O
closely	O
linked	O
to	O
memory	B-Application
safety	I-Application
.	O
</s>
<s>
For	O
instance	O
,	O
in	O
an	O
implementation	O
of	O
a	O
language	O
that	O
has	O
some	O
type	O
which	O
allows	O
some	O
bit	O
patterns	O
but	O
not	O
others	O
,	O
a	O
dangling	B-Error_Name
pointer	I-Error_Name
memory	O
error	O
allows	O
writing	O
a	O
bit	O
pattern	O
that	O
does	O
not	O
represent	O
a	O
legitimate	O
member	O
of	O
into	O
a	O
dead	O
variable	O
of	O
type	O
,	O
causing	O
a	O
type	O
error	O
when	O
the	O
variable	O
is	O
read	O
.	O
</s>
<s>
Conversely	O
,	O
if	O
the	O
language	O
is	O
memory-safe	B-Application
,	O
it	O
cannot	O
allow	O
an	O
arbitrary	O
integer	O
to	O
be	O
used	O
as	O
a	O
pointer	O
,	O
hence	O
there	O
must	O
be	O
a	O
separate	O
pointer	O
or	O
reference	O
type	O
.	O
</s>
<s>
As	O
a	O
minimal	O
condition	O
,	O
a	O
type-safe	B-Language
language	O
must	O
not	O
allow	O
dangling	B-Error_Name
pointers	I-Error_Name
across	O
allocations	O
of	O
different	O
types	O
.	O
</s>
<s>
But	O
most	O
languages	O
enforce	O
the	O
proper	O
use	O
of	O
abstract	O
data	O
types	O
defined	O
by	O
programmers	O
even	O
when	O
this	O
is	O
not	O
strictly	O
necessary	O
for	O
memory	B-Application
safety	I-Application
or	O
for	O
the	O
prevention	O
of	O
any	O
kind	O
of	O
catastrophic	O
failure	O
.	O
</s>
<s>
Most	O
type-safe	B-Language
languages	O
use	O
garbage	B-General_Concept
collection	I-General_Concept
.	O
</s>
<s>
Pierce	O
says	O
,	O
"	O
it	O
is	O
extremely	O
difficult	O
to	O
achieve	O
type	B-Language
safety	I-Language
in	O
the	O
presence	O
of	O
an	O
explicit	O
deallocation	B-General_Concept
operation	O
"	O
,	O
due	O
to	O
the	O
dangling	B-Error_Name
pointer	I-Error_Name
problem	O
.	O
</s>
<s>
However	O
Rust	B-Application
is	O
generally	O
considered	O
type-safe	B-Language
and	O
uses	O
a	O
borrow	O
checker	O
to	O
achieve	O
memory	B-Application
safety	I-Application
,	O
instead	O
of	O
garbage	B-General_Concept
collection	I-General_Concept
.	O
</s>
<s>
In	O
object	B-Language
oriented	I-Language
languages	I-Language
type	B-Language
safety	I-Language
is	O
usually	O
intrinsic	O
in	O
the	O
fact	O
that	O
a	O
type	O
system	O
is	O
in	O
place	O
.	O
</s>
<s>
Each	O
time	O
a	O
new	B-Language
object	O
is	O
created	O
it	O
will	O
comply	O
with	O
that	O
contract	O
.	O
</s>
<s>
Each	O
function	O
that	O
exchanges	O
objects	O
derived	O
from	O
a	O
specific	O
class	O
,	O
or	O
implementing	O
a	O
specific	O
interface	B-Application
,	O
will	O
adhere	O
to	O
that	O
contract	O
:	O
hence	O
in	O
that	O
function	O
the	O
operations	O
permitted	O
on	O
that	O
object	O
will	O
be	O
only	O
those	O
defined	O
by	O
the	O
methods	O
of	O
the	O
class	O
the	O
object	O
implements	O
.	O
</s>
<s>
Exceptions	O
to	O
this	O
are	O
object	B-Language
oriented	I-Language
languages	I-Language
that	O
allow	O
dynamic	O
modification	O
of	O
the	O
object	O
structure	O
,	O
or	O
the	O
use	O
of	O
reflection	B-Language
to	O
modify	O
the	O
content	O
of	O
an	O
object	O
to	O
overcome	O
the	O
constraints	O
imposed	O
by	O
the	O
class	O
methods	O
definitions	O
.	O
</s>
<s>
Ada	B-Language
was	O
designed	O
to	O
be	O
suitable	O
for	O
embedded	B-Architecture
systems	I-Architecture
,	O
device	B-Application
drivers	I-Application
and	O
other	O
forms	O
of	O
system	B-Application
programming	I-Application
,	O
but	O
also	O
to	O
encourage	O
type-safe	B-Language
programming	O
.	O
</s>
<s>
To	O
resolve	O
these	O
conflicting	O
goals	O
,	O
Ada	B-Language
confines	O
type-unsafety	O
to	O
a	O
certain	O
set	O
of	O
special	O
constructs	O
whose	O
names	O
usually	O
begin	O
with	O
the	O
string	O
.	O
</s>
<s>
Unchecked_Deallocation	O
can	O
be	O
effectively	O
banned	O
from	O
a	O
unit	O
of	O
Ada	B-Language
text	O
by	O
applying	O
to	O
this	O
unit	O
.	O
</s>
<s>
It	O
is	O
expected	O
that	O
programmers	O
will	O
use	O
constructs	O
very	O
carefully	O
and	O
only	O
when	O
necessary	O
;	O
programs	O
that	O
do	O
not	O
use	O
them	O
are	O
type-safe	B-Language
.	O
</s>
<s>
The	O
SPARK	B-Language
programming	I-Language
language	I-Language
is	O
a	O
subset	O
of	O
Ada	B-Language
eliminating	O
all	O
its	O
potential	O
ambiguities	O
and	O
insecurities	O
while	O
at	O
the	O
same	O
time	O
adding	O
statically	B-Application
checked	I-Application
contracts	I-Application
to	O
the	O
language	O
features	O
available	O
.	O
</s>
<s>
SPARK	B-Language
avoids	O
the	O
issues	O
with	O
dangling	B-Error_Name
pointers	I-Error_Name
by	O
disallowing	O
allocation	O
at	O
run	B-Library
time	I-Library
entirely	O
.	O
</s>
<s>
Ada2012	O
adds	O
statically	B-Application
checked	I-Application
contracts	I-Application
to	O
the	O
language	O
itself	O
(	O
in	O
form	O
of	O
pre-	O
,	O
and	O
post-conditions	O
,	O
as	O
well	O
as	O
type	O
invariants	O
)	O
.	O
</s>
<s>
The	O
C	B-Language
programming	I-Language
language	I-Language
is	O
type-safe	B-Language
in	O
limited	O
contexts	O
;	O
for	O
example	O
,	O
a	O
compile-time	B-Application
error	I-Application
is	O
generated	O
when	O
an	O
attempt	O
is	O
made	O
to	O
convert	O
a	O
pointer	O
to	O
one	O
type	O
of	O
structure	O
to	O
a	O
pointer	O
to	O
another	O
type	O
of	O
structure	O
,	O
unless	O
an	O
explicit	O
cast	O
is	O
used	O
.	O
</s>
<s>
However	O
,	O
a	O
number	O
of	O
very	O
common	O
operations	O
are	O
non-type-safe	O
;	O
for	O
example	O
,	O
the	O
usual	O
way	O
to	O
print	O
an	O
integer	O
is	O
something	O
like	O
printf( 	O
"	O
%d	O
"	O
,	O
12	O
)	O
,	O
where	O
the	O
%d	O
tells	O
printf	O
at	O
run-time	B-Library
to	O
expect	O
an	O
integer	O
argument	O
.	O
</s>
<s>
In	O
addition	O
,	O
C	B-Language
,	O
like	O
Ada	B-Language
,	O
provides	O
unspecified	O
or	O
undefined	O
explicit	O
conversions	O
;	O
and	O
unlike	O
in	O
Ada	B-Language
,	O
idioms	O
that	O
use	O
these	O
conversions	O
are	O
very	O
common	O
,	O
and	O
have	O
helped	O
to	O
give	O
C	B-Language
a	O
type-unsafe	O
reputation	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
standard	O
way	O
to	O
allocate	O
memory	O
on	O
the	O
heap	O
is	O
to	O
invoke	O
a	O
memory	B-General_Concept
allocation	I-General_Concept
function	O
,	O
such	O
as	O
malloc	B-Language
,	O
with	O
an	O
argument	O
indicating	O
how	O
many	O
bytes	O
are	O
required	O
.	O
</s>
<s>
Pre-standardized	O
implementations	O
of	O
C	B-Language
required	O
an	O
explicit	O
cast	O
to	O
do	O
so	O
,	O
therefore	O
the	O
code	O
(	O
struct	B-Application
foo	O
*	O
)	O
malloc(sizeof(struct foo )	O
)	O
became	O
the	O
accepted	O
practice	O
.	O
</s>
<s>
Some	O
features	O
of	O
C++	O
that	O
promote	O
more	O
type-safe	B-Language
code	O
:	O
</s>
<s>
The	O
new	B-Language
operator	O
returns	O
a	O
pointer	O
of	O
type	O
based	O
on	O
operand	O
,	O
whereas	O
malloc	B-Language
returns	O
a	O
void	O
pointer	O
.	O
</s>
<s>
C++	O
code	O
can	O
use	O
virtual	O
functions	O
and	O
templates	B-Application
to	O
achieve	O
polymorphism	B-Application
without	O
void	O
pointers	O
.	O
</s>
<s>
Safer	O
casting	O
operators	O
,	O
such	O
as	O
dynamic	O
cast	O
that	O
performs	O
run-time	B-Library
type	O
checking	O
.	O
</s>
<s>
C++11	O
strongly-typed	O
enumerations	O
cannot	O
be	O
implicitly	O
converted	O
to	O
or	O
from	O
integers	O
or	O
other	O
enumeration	O
types	O
.	O
</s>
<s>
C++	O
explicit	O
constructors	O
and	O
C++11	O
explicit	O
conversion	O
operators	O
prevent	O
implicit	O
type	O
conversions	O
.	O
</s>
<s>
C#	B-Application
is	O
type-safe	B-Language
(	O
but	O
not	O
statically	O
type-safe	B-Language
)	O
.	O
</s>
<s>
It	O
has	O
inherent	O
support	O
for	O
run-time	B-Library
cast	O
validation	O
.	O
</s>
<s>
Casts	O
can	O
be	O
validated	O
by	O
using	O
the	O
"	O
as	O
"	O
keyword	O
that	O
will	O
return	O
a	O
null	O
reference	O
if	O
the	O
cast	O
is	O
invalid	O
,	O
or	O
by	O
using	O
a	O
C-style	O
cast	O
that	O
will	O
throw	O
an	O
exception	O
if	O
the	O
cast	O
is	O
invalid	O
.	O
</s>
<s>
See	O
C	B-Application
Sharp	I-Application
conversion	O
operators	O
.	O
</s>
<s>
Undue	O
reliance	O
on	O
the	O
object	O
type	O
(	O
from	O
which	O
all	O
other	O
types	O
are	O
derived	O
)	O
runs	O
the	O
risk	O
of	O
defeating	O
the	O
purpose	O
of	O
the	O
C#	B-Application
type	O
system	O
.	O
</s>
<s>
It	O
is	O
usually	O
better	O
practice	O
to	O
abandon	O
object	O
references	O
in	O
favour	O
of	O
generics	O
,	O
similar	O
to	O
templates	B-Application
in	O
C++	O
and	O
generics	B-Language
in	I-Language
Java	I-Language
.	O
</s>
<s>
The	O
Java	B-Language
language	I-Language
is	O
designed	O
to	O
enforce	O
type	B-Language
safety	I-Language
.	O
</s>
<s>
To	O
implement	O
the	O
type	B-Language
safety	I-Language
enforcement	O
,	O
each	O
object	O
,	O
before	O
usage	O
,	O
needs	O
to	O
be	O
allocated	B-General_Concept
.	O
</s>
<s>
Java	B-Language
allows	O
usage	O
of	O
primitive	O
types	O
but	O
only	O
inside	O
properly	O
allocated	B-General_Concept
objects	O
.	O
</s>
<s>
Sometimes	O
a	O
part	O
of	O
the	O
type	B-Language
safety	I-Language
is	O
implemented	O
indirectly	O
:	O
e.g.	O
</s>
<s>
The	O
operation	O
BigDecimal.divide( )	O
calculates	O
a	O
new	B-Language
object	O
as	O
the	O
division	O
of	O
two	O
numbers	O
expressed	O
as	O
BigDecimal	O
.	O
</s>
<s>
Standard	B-Language
ML	I-Language
has	O
rigorously	O
defined	O
semantics	B-Application
and	O
is	O
known	O
to	O
be	O
type-safe	B-Language
.	O
</s>
<s>
However	O
,	O
some	O
implementations	O
,	O
including	O
Standard	B-Application
ML	I-Application
of	I-Application
New	I-Application
Jersey	I-Application
(	O
SML/NJ	B-Application
)	O
,	O
its	O
syntactic	O
variant	O
Mythryl	O
and	O
MLton	B-Language
,	O
provide	O
libraries	B-Library
that	O
offer	O
unsafe	O
operations	O
.	O
</s>
<s>
These	O
facilities	O
are	O
often	O
used	O
in	O
conjunction	O
with	O
those	O
implementations	O
 '	O
foreign	B-Application
function	I-Application
interfaces	I-Application
to	O
interact	O
with	O
non-ML	O
code	O
(	O
such	O
as	O
C	B-Language
libraries	B-Library
)	O
that	O
may	O
require	O
data	O
laid	O
out	O
in	O
specific	O
ways	O
.	O
</s>
<s>
Another	O
example	O
is	O
the	O
SML/NJ	B-Application
interactive	B-Application
toplevel	I-Application
itself	O
,	O
which	O
must	O
use	O
unsafe	O
operations	O
to	O
execute	O
ML	O
code	O
entered	O
by	O
the	O
user	O
.	O
</s>
<s>
There	O
still	O
remained	O
unsafe	O
facilities	O
such	O
as	O
the	O
type	O
cast	O
syntax	O
and	O
variant	O
records	O
(	O
inherited	O
from	O
Pascal	B-Application
)	O
that	O
could	O
be	O
used	O
without	O
prior	O
import	O
.	O
</s>
<s>
Pascal	B-Application
has	O
had	O
a	O
number	O
of	O
type	B-Language
safety	I-Language
requirements	O
,	O
some	O
of	O
which	O
are	O
kept	O
in	O
some	O
compilers	O
.	O
</s>
<s>
Where	O
a	O
Pascal	B-Application
compiler	O
dictates	O
"	O
strict	O
typing	O
"	O
,	O
two	O
variables	O
cannot	O
be	O
assigned	O
to	O
each	O
other	O
unless	O
they	O
are	O
either	O
compatible	O
(	O
such	O
as	O
conversion	O
of	O
integer	O
to	O
real	O
)	O
or	O
assigned	O
to	O
the	O
identical	O
subtype	O
.	O
</s>
<s>
In	O
general	O
,	O
Common	B-Language
Lisp	I-Language
is	O
a	O
type-safe	B-Language
language	O
.	O
</s>
<s>
A	O
Common	B-Language
Lisp	I-Language
compiler	O
is	O
responsible	O
for	O
inserting	O
dynamic	O
checks	O
for	O
operations	O
whose	O
type	B-Language
safety	I-Language
cannot	O
be	O
proven	O
statically	O
.	O
</s>
<s>
A	O
program	O
compiled	O
in	O
such	O
a	O
mode	O
cannot	O
be	O
considered	O
type-safe	B-Language
.	O
</s>
<s>
The	O
following	O
examples	O
illustrates	O
how	O
C++	O
cast	O
operators	O
can	O
break	O
type	B-Language
safety	I-Language
when	O
used	O
incorrectly	O
.	O
</s>
