Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Symbolic computation
Date: Wed, 3 Jan 2007 08:00:23 +0000	[thread overview]
Message-ID: <200701030800.24255.jon@ffconsultancy.com> (raw)


I've just updated our Benefits of OCaml page with a more elegant symbolic 
computation example:

  http://www.ffconsultancy.com/free/ocaml/symbolic.html

In particular, results are composed using a pair of non-trivial constructors 
that perform simple simplifications:

# let rec ( +: ) f g = match f, g with
    | Int n, Int m -> Int (n + m)
    | Int 0, f | f, Int 0 -> f
    | f, Add(g, h) -> f +: g +: h
    | f, g when f > g -> g +: f
    | f, g -> Add(f, g)

  and ( *: ) f g = match f, g with
    | Int n, Int m -> Int (n * m)
    | Int 0, _ | _, Int 0 -> Int 0
    | Int 1, f | f, Int 1 -> f
    | f, Mul(g, h) -> f *: g *: h
    | f, g when f > g -> g *: f
    | f, g -> Mul(f, g);;
val ( +: ) : expr -> expr -> expr = <fun>
val ( *: ) : expr -> expr -> expr = <fun>

I'm also translating this into F# for my forthcoming book "F# for Scientists". 
Even on an example as simple as this, F# has some significant benefits:

1. + and * can be overloaded for the expr type.
2. User-defined types can have their own comparison functions.
3. Set and Map are polymorphic (not functors).

Hopefully F#'s active patterns will also allow operators in patterns, allowing 
code like:

  let d f x = match f with
    | Var v when x=v -> Int 1
    | Int _ | Var _ -> Int 0
    | f + g -> d f x + d g x
    | f * g -> f * d g x + g * d f x

and:

    | f + (g + h) -> (f + g) + h

and so on.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


                 reply	other threads:[~2007-01-03  8:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=200701030800.24255.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.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