This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

[Caml-list] Re: OCAML Downcasting?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2004-09-22 (12:03) From: Alain Frisch Subject: Re: [Caml-list] Re: OCAML Downcasting?
```On Wed, 22 Sep 2004, Marcin 'Qrczak' Kowalczyk wrote:

> This is the catch: "where possible". I would add: "where practical";
> sometimes the cost outweights the benefits. For example static
> detection of possible division by 0 would be impractical. You would
> have to either embed a proof checker in the language in order to be
> able to convince the compiler that the number can't be 0, or not use
> exceptions at all and have all partial functions return a result in
> "option" type or similar, with manual propagation of errors.

You don't necessarily have to use a proof checker. What about using a type
system, or a(nother kind of) static analysis ?  E.g. detection of division
by 0 can be done with interval arithmetic, and it might work well in
practice.

> And guess what? No language I know checks division by 0 statically
> (except proof checkers, but they are not suitable for writing big
> programs - too much work).

CDuce version 0.2.1+1

# fun (x : Int) : Int = 1 div x;;
Warning at chars 22-29:
This operator may fail
- : Int -> Int = <fun>

# fun (x : 0--*) : Int = 1 div x;;
Warning at chars 23-30:
This operator may fail
- : 0--* -> Int = <fun>

# fun (x : 1--*) : Int = 1 div x;;
- : 1--* -> Int = <fun>

# fun (x : 1--*) : Int = 1 div (x + x);;
- : 1--* -> Int = <fun>

# fun (x : 1--*) : Int = 1 div (x - x + x);;
Warning at chars 23-40:
This operator may fail
- : 1--* -> Int = <fun>

(type 1--* means: "positive integers")

To get rid of warning (or compile-time error) when the type system is not
precise enough (the last example above), you can always do an
explicit check:

# fun (x : 1--*) : Int =
match x - x + x with
| y & (1--*) -> 1 div y
| _ -> raise "Bla";;
- : 1--* -> Int = <fun>

-- Alain

-------------------
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

```