caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@ozemail.com.au>
To: David Baelde <David.Baelde@ens-lyon.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] inference engine
Date: 14 Sep 2003 14:12:30 +1000	[thread overview]
Message-ID: <1063512749.23073.28.camel@pelican> (raw)
In-Reply-To: <20030913120100.281d9363.David.Baelde@ens-lyon.fr>

On Sat, 2003-09-13 at 20:01, David Baelde wrote:
>  
>  What would mean [> int] & 'a ? Which types belong to this one ?

At least int belongs. The idea is that

	[> int] & float

is an error, since it is at least int, and exactly float;
but int is not in the type float.

>  For me, the [<> ...] notion has a only meaning for objects.
> 
>  Actually, I don't understand very well your example, since
>  the constraint on x is not a good thing in ocaml (not necessary)
>  and if you remove it, "f x = x" allows polymorphism.
> 
>  Could you explain us again your idea ?

I'll try. At present, with

	let f (x:int) = x +. 1.0  

the inference engine goes:

	let f (x:int) * = x .+ 1.0 --> x is 'a
	let f (x:int) = x .+ * 1.0 --> 'a is restricted to float
	let f (x:int*) = x +. 1.0 --> float is restricted to int ERROR

where the * marks the position in the analysis: the constraint of
x to int is applied *after* the type is already deduced.

If ocaml had 'madatory typing' then the engine would go:

	let f (x:int*) = x .+ 1.0 -> x is int
	let f (x:int) = x .+ * 1.0 -> x is restriced to float ERRIR

Detecting the error here is desirable, because that's where it occurs.
Reporting, in the first case, that the constraint to int is incompatible
with the type infered (float) is not very useful, since there is no
indication HOW the inference was made, or where the error is:
the assumption is that the annotation is correct and the code
is wrong -- but the inference engine assumes the code is
correct and the annotation is wrong.

So with mandatory typing we get much better error diagnostics.
BUt Ocaml doesn't have mandatory typing: type annotations
are constraints applied *after* inference. My suggestion
is to carry the constraint into the analysis of the function
body, and diagnose a conflict as soon as possible.

To do that I'm suggesting to give x the type 'at least an int',
meaning any subsequent typing of x in the function body
must be constrainable to int -- and that is checked every time
additional information is obtained by the inference engine
to refine the type.

For another example:

	let f (x:int) = match x with | (a,b) -> ..

should give an error saying:

	let f (x:int) = match x with | (a,b) -> ..
	-------------------------------*****

	"Here x is used with type 'a * 'b,
	which is not compatible with the constraint int"


instead of

	let f(x:int) = match x with | (a,b) ->
	--------***

	"Here x is constrained to type int which
	is not compatible with type 'a * 'b"

YOu can see the second message isn't very helpful.
I KNOW it's an int. I said so. How did the compiler
decide it had type 'a * 'b?

I have to search through not only the function
body .. but also any functions that it calls.

in particular, in the case like:

	let rec f1 (x1:t1):r1 = ..
	and f2 (x2:t2):r2 = ...
	and f3 (x2:t3):r3 = ..
	...
	and f999 (x999:t999):r999 = ...

the fact the the constraints t1, r1, t2, t2, ..
are not applied until after the whole recursion
is typed, makes it almost impossible to
find a typing error, even by adding type
annotations: I'm not sure this is the case exactly,
but certainly if you forward call from
say f1 into f999, and f2 into f999, you can
get a type infered for f999 before the body
is seen, and even an error before the body,
and hence the annotation, is seen.

So the idea is simply to carry some information
into the inference process from  the annotation,
to get earlier "more precisely located" error
diagnostics.

In some circumstances, I have had to physically
reorder the functions to try to find where
the compiler got its weird typing idea from
(sure, I made an error, but it lied about
where the error was ..:)

I have a very large file which has about 30 mutually
mutually recursive functions in them that do rather nasty
lookup calculations .. its possible that the new intermodule
recursion will help here -- my functions can be fairly
logically partitioned, but they're still mutually recursive.

[Passing one to another to break the recursion is out
of the question .. they have too many arguments, it would
be too fragile]

As stated I'm not a type theorist, it's just an idea
to get some of the benefits mandatory typing would
provide, without actually having mandatory typing
(mandatory typing meaning that type annotations specify
the type, not just a constraint) -- pass the anotation
into the inference engine as a constraint not a type.


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2003-09-14  4:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-13  6:58 skaller
2003-09-13 10:01 ` David Baelde
2003-09-14  4:12   ` skaller [this message]
2003-09-14  9:29     ` Remi Vanicat
2003-09-14  9:48     ` Alain.Frisch
2003-09-16 19:46       ` skaller
2003-09-21 16:45         ` skaller
2003-09-21 20:43           ` Remi Vanicat

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=1063512749.23073.28.camel@pelican \
    --to=skaller@ozemail.com.au \
    --cc=David.Baelde@ens-lyon.fr \
    --cc=caml-list@inria.fr \
    /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).