From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 13EA5BC0B for ; Wed, 3 Jan 2007 09:14:34 +0100 (CET) Received: from ptb-relay02.plus.net (ptb-relay02.plus.net [212.159.14.213]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l038EX7Y030182 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Wed, 3 Jan 2007 09:14:33 +0100 Received: from [80.229.56.224] (helo=[10.0.0.5]) by ptb-relay02.plus.net with esmtp (Exim) id 1H21GV-0000lM-TG for caml-list@yquem.inria.fr; Wed, 03 Jan 2007 08:14:28 +0000 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Symbolic computation Date: Wed, 3 Jan 2007 08:00:23 +0000 User-Agent: KMail/1.9.5 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200701030800.24255.jon@ffconsultancy.com> X-Miltered: at discorde with ID 459B65E9.004 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 ocaml:01 non-trivial:01 constructors:01 val:01 expr:01 expr:01 val:01 translating:01 functors:01 frog:98 polymorphic:01 rec:01 symbolic:01 symbolic:01 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 = val ( *: ) : expr -> expr -> expr = 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