Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Weird ref behaviour...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jonathan Roewen <jonathan.roewen@g...>
Subject: [Caml-list] Weird ref behaviour...
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