caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Andrej Bauer <Andrej.Bauer@andrej.com>
To: Caml list <caml-list@inria.fr>
Cc: skaller <skaller@users.sourceforge.net>
Subject: Re: [Caml-list] Coinductive semantics
Date: Sun, 22 Jan 2006 13:23:49 +0100	[thread overview]
Message-ID: <43D37955.90408@andrej.com> (raw)
In-Reply-To: <1137899816.885.45.camel@rosella>

skaller wrote:
> basically I don't know enough theory to turn the purely
> algebraic finite array types -- fairly useless in practice --
> into variable length arrays required in practice.
> [By variable I mean the length is an immutable dynamic type,
> not that you can change the length of the array]

If I understand you correctly, you have types [0], [1], [2], [3], ...,
one for each natural number 0, 1, 2, ... (to avoid confusion I want to
distinguish notationally between number n and type [n] here, even if
Felix does not do it). Let me call [n] "the type of indices of arrays of
length n". We may think of [n] as the set {0,1,2,...,n-1}.

You know how to deal with the type [n]->t, which is an array of length n
containing t's. Now you would like to "make n variable (dynamic)". This
sounds like a dependent type to me, i.e, you want not just a bunch of
types [0], [1], [2], ... but rather a _type constructor_ [-], which maps
from int to types. You may define it in dependent type theory (I am
going to use set-theretic notation so that I don't confuse everyone):

  [n] = {k : int | 0 <= k and k < n}

In set theory, this is interpreted as follows: an index of an array of
length n is an integer k. This integer must be between 0 and n.

In type theory, this is interpreted as follows: an index of an array of
length n is a _pair_ (k,p) where k is an integer and p is a proof of the
fact k is between 0 and n.

The type of arrays you're talking about is a dependent sum

  't array = sum (n:int) [n]->'t

An element of this type (according to type theory) consists of a pair
(n,a) where n is an integer and a is a map [n]->'t, i.e., an array of
length n. Elements of such an array are indexed by pairs (k,p).

If everyone believed in type theory, compilers would be easy to write.
Every time a programmer wanted to address an element of an array, he
would provide not just the index k but also the proof p that k is a
valid index. Compiler would just have to check that p is a valid proof
(easy when p is a formal proof). But programmers don't want to do that.

If we care about soundness and safety, we are left with two
possibilities: either the compiler or the run-time environment must
supply the missing proof p. For the run-time environment this is easy,
because k and n are always instantiated to particular constants and we
may simply check that 0<=k<n. But for the compiler, it is an impossible
task, since k and n may be arbitrarily complex expressions. The compiler
would have to be able to solve undecidable problems in order to
determine whether an index is valid. You're hitting the theoretical
limit of static type checking.

There may be special cases where compiler can determine whether the
index is ok. For example, if the index is a linear function of a bunch
of integer variables, which is typical of array computations as long as
you're _not_ simulating a matrix as a one-dimensional table or some
such, then you're in the realm of Presburger arithmetic (integers, +, -,
<, =, and multiplication by _constants_ only) which has a decision
procedure (double exponential time, if I remember it right). I am sure
people have looked into this sort of thing. Good luck!

Andrej


  reply	other threads:[~2006-01-22 12:22 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-01-05 18:23 Alessandro Baretta
2006-01-05 19:48 ` [Caml-list] " David Baelde
2006-01-06 13:12 ` Andrej Bauer
2006-01-10 11:10   ` Francisco J. Valverde Albacete
2006-01-11  8:34     ` Hendrik Tews
2006-01-11 12:19       ` skaller
2006-01-11 14:54         ` Andrej Bauer
2006-01-12  2:10           ` skaller
2006-01-12 14:03             ` Andrej Bauer
2006-01-12 21:54               ` skaller
2006-01-13 10:23                 ` Hendrik Tews
2006-01-13 14:42                   ` skaller
2006-01-18 12:58                     ` Hendrik Tews
2006-01-18 14:22                       ` skaller
2006-01-20  0:49                         ` William Lovas
2006-01-20  9:57                           ` Andrej Bauer
2006-01-20 18:59                             ` William Lovas
2006-01-20 20:59                               ` skaller
2006-01-21 18:36                                 ` Andrej Bauer
2006-01-22  3:16                                   ` skaller
2006-01-22 12:23                                     ` Andrej Bauer [this message]
2006-01-22 15:35                                       ` skaller
2006-01-22 17:26                                       ` Kenn Knowles
2006-01-22 21:52                                         ` Andrej Bauer
2006-01-21 19:06                               ` Andrej Bauer
2006-01-13 10:40                 ` Andrej Bauer
     [not found]                   ` <43C7B17A.1070503@barettadeit.com>
2006-01-14 16:53                     ` Andrej Bauer
2006-01-05 20:38 Don Syme
2006-01-06 15:33 ` Alessandro Baretta
2006-01-08 10:02   ` Andrej Bauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=43D37955.90408@andrej.com \
    --to=andrej.bauer@andrej.com \
    --cc=caml-list@inria.fr \
    --cc=skaller@users.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).