0

I was trying a simple program in SWI-Prolog using the CHR library.

The responses to the queries are obvious to me, but the tracing baffled me.

I started with only one rule.

and(X,Y,Z) <=> Y = 0 | Z = 0.

Tracing made sense

?-  and(A,B,C).
CHR:   (0) Insert: and(_73048,_73416,_73052) # <357>
CHR:   (1) Call: and(_73048,_73416,_73052) # <357> ? [creep]
CHR:   (1) Exit: and(_73048,_73416,_73052) # <357> ? [creep]
CHR:   (1) Wake: and(_73048,_73416,_73052) # <357> ? [creep]
CHR:   (1) Exit: and(_73048,_73416,_73052) # <357> ? [creep]
CHR:   (1) Redo: and(_73048,_73416,_73052) # <357>
CHR:   (0) Fail: and(_73048,_73416,_73052) # <357> ? [creep]
and(A, B, C) .

I added a new rule

and(X,Y,Z) <=> Y = 0 | Z = 0.
and(X,Y,Z) <=> X = 0 | Z = 0.

Now the tracing started to be strange for me. I expected a similar trace, but I got this...

?-  and(A,B,C).
CHR:   (0) Insert: and(_80150,_80184,_79780) # <488>
CHR:   (1) Call: and(_80150,_80184,_79780) # <488> ? [creep]
CHR:   (1) Exit: and(_80150,_80184,_79780) # <488> ? [creep]
CHR:   (1) Wake: and(_80150,_80184,_79780) # <488> ? [creep]
CHR:   (2) Wake: and(_80150,0,_79780) # <488> ? [creep]
...
and(A, B, C) .

Why this and(_80150,0,_79780)? Why was Y variable matched with 0?

jack malkovick
  • 503
  • 2
  • 14

1 Answers1

2

I think it was a silly question on my part... As a beginner I forgot sometimes about the host language.

Y = 0 in the guard in Prolog unified Y with 0 so I guess this is why I got the second rule in store.

The correct approach would have been

and(X,Y,Z) <=> Y == 0 | Z = 0.
jack malkovick
  • 503
  • 2
  • 14