[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Alex Baretta <alex@b...> |
| Subject: | Re: [Caml-list] Restarting a piece of code |
Andrej Bauer wrote:
>
> Can we avoid having to pass treshold around directly? You can imagine
> that users won't be too thrilled about this sort of thing.
I think you can. See arithmetic functions as building blocks for
computations---rather than computations themselves---which are
parametric with respect to the threshold value.
let unit_expression _ = assert false
let imaginary_unit_expression _ = assert false
let addition_expression _ = assert false
let negative_expression _ = assert false
let multiplication_expression _ = assert false
let inverse_expression _ = assert false
let exponential_expression _ = assert false
let logarithm_expression _ = assert false
exception Overflow
let rec compute grow_threshold threshold_transformation = fun
computation
threshold
->
try
computation
(compute grow_threshold)
(threshold_transformation threshold)
with Overflow ->
let new_threshold = grow_threshold threshold in
computation
(compute grow_threshold)
(threshold_transformation new_threshold)
(* Here we define the primitive operations of the algebraic structure *)
(* We don't mind if the primitive operations are a little heavy, so *)
(* long as it is easy to compose them to form complex computations. *)
let one = fun compute threshold -> unit_expression threshold
let i = fun compute threshold -> imaginary_unit_expression threshold
let add x y = fun compute threshold ->
let x' = compute x threshold in
let y' = compute y threshold in
addition_expression x y threshold
let neg x = fun compute threshold ->
let x' = compute x threshold in
negative_expression x threshold
let mul x y = fun compute threshold ->
let x' = compute x threshold in
let y' = compute y threshold in
multiplication_expression x y threshold
let inv x = fun compute threshold ->
let x' = compute x threshold in
inverse_expression x threshold
let exp x = fun compute threshold ->
let x' = compute x threshold in
exponential_expression x threshold
let log x = fun compute threshold ->
let x' = compute x threshold in
logarithm_expression x threshold
(* Let's say these are all the basic computations we need. Now we can *)
(* start building more computations on top of these. *)
let sub x y = add x (neg y)
let div x y = mul x (inv y)
let pow x y = exp (mul (log x) y)
let root x y = exp (mul (log x) (inv y))
let two = add one one
let twoi = add i i
let cos x = div (add (exp (mul i x)) (exp (neg (mul i x)))) two
let sin x = div (sub (exp (mul i x)) (exp (neg (mul i x)))) twoi
> I sense monads. Or am I looking for dynamic binding?
You are looking for partial evaluation/multistage programming, but you
don't necessarily have to delve into MetaOcaml to solve your problem. As
you can see you can generate a homomorphism from the calculus of
imaginary numbers to the calculus of computations of imaginary numbers
which can be directly represented in Ocaml.
> Best regards,
>
> Andrej
>
--
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL
tel. +39 02 370 111 55
fax. +39 02 370 111 54
Our technology:
The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>
The FreerP Project
<http://www.freerp.org/>