From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id C14D3BBBC for ; Sat, 25 Feb 2006 10:15:54 +0100 (CET) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.203]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k1P9FruZ026816 for ; Sat, 25 Feb 2006 10:15:53 +0100 Received: by wproxy.gmail.com with SMTP id i20so461029wra for ; Sat, 25 Feb 2006 01:15:53 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=pJXcAbx247dvjWp3UksiR2VBxizau7CpdXrgd7a2/FYoP4+g1sY8aZ/geztEZF769ok1jQYcRe51kZWSpM49YFrhI0Y138dYWaF55yqvrx7xjgq51sDGF0UobaXTos5EVL0tMkpKVGMZPp8SFrTsvop9zOU5adsP88Vf+oMGpik= Received: by 10.64.153.11 with SMTP id a11mr2636150qbe; Sat, 25 Feb 2006 01:15:52 -0800 (PST) Received: by 10.65.35.8 with HTTP; Sat, 25 Feb 2006 01:15:51 -0800 (PST) Message-ID: Date: Sat, 25 Feb 2006 22:15:51 +1300 From: "Jonathan Roewen" To: OCaml Subject: [Caml-list] Weird ref behaviour... MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Miltered: at concorde with ID 44002049.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 expr:01 struct:01 rec:01 printf:01 sprintf:01 expr:01 bindings:01 rec:01 bindings:01 printf:01 toplevel:01 bool:01 bool:01 non-empty:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_BY_IP autolearn=disabled version=3.0.3 Excuse my denseness, but can someone explain the following: source: let fold_string l s f =3D List.fold_left (fun a b -> if a =3D "" then f b else a ^ s ^ (f b)) "" l;; module Expr =3D struct type t =3D Entity of string | Unknown of string | Relation of (string * t list) let rec to_string =3D function | Entity x -> x | Unknown x -> x | Relation (_, []) -> "" | Relation (n, l) -> Printf.sprintf "%s(%s)" n (fold_string l "," to_str= ing) end;; open Expr;; let relation n l =3D Relation (n,l);; let unknown n =3D Unknown n;; let entity n =3D Entity n;; let test (a,b) =3D let bindings =3D ref [] in let rec test left right =3D match left, right with | Entity a, Entity b -> a =3D b | Relation (a,x), Relation (b,y) -> a =3D b && List.fold_left2 (fun a b c -> a && (test b c)) true x y | Entity _ as a, Unknown b | (Relation _ as a), Unknown b -> begin try test a (List.assoc b !bindings) with Not_found -> bindings :=3D (b, a) :: !bindings; Printf.printf "Unified %s to %s\n" b (to_string a); true end | _ -> false in test a b, !bindings let test_one =3D relation "Loves" [relation "Dog" [entity "Fred"]; entity "Fred"], relation "Loves" [unknown "x"; unknown "y"];; let test_two =3D relation "Loves" [relation "Dog" [entity "Fred"]; entity "Fred"], relation "Loves" [relation "Dog" [unknown "y"]; unknown "y"];; let test_three =3D relation "Loves" [relation "Dog" [entity "Fred"]; entity "Fred"], relation "Loves" [unknown "y"; unknown "y"];; Toplevel session: # test test_one;; Unified x to Dog(Fred) Unified y to Fred - : bool * (string * Expr.t) list =3D (true, []) # test test_three;; Unified y to Dog(Fred) - : bool * (string * Expr.t) list =3D (false, []) How can the return value have an empty list???? In fact, for test_three to correctly return false, the list -has to be- non-empty! Jonathan