caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Jonathan Roewen" <jonathan.roewen@gmail.com>
To: OCaml <caml-list@yquem.inria.fr>
Subject: [Caml-list] Weird ref behaviour...
Date: Sat, 25 Feb 2006 22:15:51 +1300	[thread overview]
Message-ID: <ad8cfe7e0602250115md0409faga3670a233eb3d61b@mail.gmail.com> (raw)

Excuse my denseness, but can someone explain the following:

source:


let fold_string l s f = List.fold_left (fun a b -> if a = "" then f b
else a ^ s ^ (f b)) "" l;;

module Expr = struct
   type t = Entity of string
          | Unknown of string
          | Relation of (string * t list)
   let rec to_string = function
   | Entity x -> x
   | Unknown x -> x
   | Relation (_, []) -> ""
   | Relation (n, l) -> Printf.sprintf "%s(%s)" n (fold_string l "," to_string)
  end;;

open Expr;;

let relation n l = Relation (n,l);;
let unknown n = Unknown n;;
let entity n = Entity n;;

let test (a,b) =
    let bindings = ref [] in
    let rec test left right = match left, right with
    | Entity a, Entity b -> a = b
    | Relation (a,x), Relation (b,y) -> a = 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 := (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 = relation "Loves" [relation "Dog" [entity "Fred"];
entity "Fred"], relation "Loves" [unknown "x"; unknown "y"];;
let test_two = relation "Loves" [relation "Dog" [entity "Fred"];
entity "Fred"], relation "Loves" [relation "Dog" [unknown "y"];
unknown "y"];;
let test_three = 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 = (true, [])
# test test_three;;
Unified y to Dog(Fred)
- : bool * (string * Expr.t) list = (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


             reply	other threads:[~2006-02-25  9:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-02-25  9:15 Jonathan Roewen [this message]
2006-02-25 10:22 ` Pierre Etchemaite

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=ad8cfe7e0602250115md0409faga3670a233eb3d61b@mail.gmail.com \
    --to=jonathan.roewen@gmail.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;
as well as URLs for NNTP newsgroup(s).