* [Caml-list] Weird ref behaviour...
@ 2006-02-25 9:15 Jonathan Roewen
2006-02-25 10:22 ` Pierre Etchemaite
0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Roewen @ 2006-02-25 9:15 UTC (permalink / raw)
To: OCaml
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2006-02-25 10:22 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-25 9:15 [Caml-list] Weird ref behaviour Jonathan Roewen
2006-02-25 10:22 ` Pierre Etchemaite
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).