Skip to content

Commit 6c83939

Browse files
Copilothzhangxyz
andcommitted
Add variable support tests for egg
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
1 parent adf78f4 commit 6c83939

1 file changed

Lines changed: 165 additions & 0 deletions

File tree

tests/test_egg.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,168 @@ async def test_egg_cancellation(temp_db):
198198
await task
199199
except asyncio.CancelledError:
200200
pass
201+
202+
203+
# Variable-based tests
204+
@pytest.mark.asyncio
205+
async def test_egg_symmetry_with_variables(temp_db):
206+
"""Test symmetry with variables: given a(`x)=b(`x) and idea b(t)=a(t), should produce b(t)=a(t)."""
207+
addr, engine, session = temp_db
208+
209+
# Add fact a(`x)=b(`x) with variable
210+
async with session() as sess:
211+
sess.add(Facts(data="----\n(binary == (unary a `x) (unary b `x))\n"))
212+
sess.add(Ideas(data="----\n(binary == (unary b t) (unary a t))\n"))
213+
await sess.commit()
214+
215+
# Run the main function with a timeout
216+
task = asyncio.create_task(main(addr, engine, session))
217+
await asyncio.sleep(0.3)
218+
task.cancel()
219+
try:
220+
await task
221+
except asyncio.CancelledError:
222+
pass
223+
224+
# Verify that fact b(t)=a(t) was produced
225+
async with session() as sess:
226+
facts = await sess.scalars(select(Facts))
227+
fact_data = [f.data for f in facts.all()]
228+
assert "----\n(binary == (unary b t) (unary a t))\n" in fact_data
229+
230+
231+
@pytest.mark.asyncio
232+
async def test_egg_transitivity_with_variables(temp_db):
233+
"""Test transitivity with variables: given a(`x)=b(`x), b(`x)=c(`x), should produce a(t)=c(t)."""
234+
addr, engine, session = temp_db
235+
236+
# Add facts a(`x)=b(`x) and b(`x)=c(`x)
237+
async with session() as sess:
238+
sess.add(Facts(data="----\n(binary == (unary a `x) (unary b `x))\n"))
239+
sess.add(Facts(data="----\n(binary == (unary b `x) (unary c `x))\n"))
240+
sess.add(Ideas(data="----\n(binary == (unary a t) (unary c t))\n"))
241+
await sess.commit()
242+
243+
# Run the main function
244+
task = asyncio.create_task(main(addr, engine, session))
245+
await asyncio.sleep(0.3)
246+
task.cancel()
247+
try:
248+
await task
249+
except asyncio.CancelledError:
250+
pass
251+
252+
# Verify that fact a(t)=c(t) was produced
253+
async with session() as sess:
254+
facts = await sess.scalars(select(Facts))
255+
fact_data = [f.data for f in facts.all()]
256+
assert "----\n(binary == (unary a t) (unary c t))\n" in fact_data
257+
258+
259+
@pytest.mark.asyncio
260+
async def test_egg_congruence_with_variables(temp_db):
261+
"""Test congruence with variables: given x=y, expect f(`z, x)=f(`z, y)."""
262+
addr, engine, session = temp_db
263+
264+
# Add fact x=y and idea f(`z, x)=f(`z, y)
265+
async with session() as sess:
266+
sess.add(Facts(data="----\n(binary == x y)\n"))
267+
sess.add(Ideas(data="----\n(binary == (binary f `z x) (binary f `z y))\n"))
268+
await sess.commit()
269+
270+
# Run the main function
271+
task = asyncio.create_task(main(addr, engine, session))
272+
await asyncio.sleep(0.3)
273+
task.cancel()
274+
try:
275+
await task
276+
except asyncio.CancelledError:
277+
pass
278+
279+
# Verify that fact was produced
280+
async with session() as sess:
281+
facts = await sess.scalars(select(Facts))
282+
fact_data = [f.data for f in facts.all()]
283+
assert "----\n(binary == (binary f `z x) (binary f `z y))\n" in fact_data
284+
285+
286+
@pytest.mark.asyncio
287+
async def test_egg_substitution_with_variables(temp_db):
288+
"""Test substitution with variables: given f(`x) and x=y, expect f(y) can be satisfied."""
289+
addr, engine, session = temp_db
290+
291+
# Add fact f(`x) and x=y, then idea f(y)
292+
async with session() as sess:
293+
sess.add(Facts(data="----\n(unary f `x)\n"))
294+
sess.add(Facts(data="----\n(binary == x y)\n"))
295+
sess.add(Ideas(data="----\n(unary f y)\n"))
296+
await sess.commit()
297+
298+
# Run the main function
299+
task = asyncio.create_task(main(addr, engine, session))
300+
await asyncio.sleep(0.3)
301+
task.cancel()
302+
try:
303+
await task
304+
except asyncio.CancelledError:
305+
pass
306+
307+
# Verify that the idea was satisfied
308+
async with session() as sess:
309+
facts = await sess.scalars(select(Facts))
310+
fact_data = [f.data for f in facts.all()]
311+
assert "----\n(unary f y)\n" in fact_data
312+
313+
314+
@pytest.mark.asyncio
315+
async def test_egg_complex_situation_with_variables(temp_db):
316+
"""Test comprehensive combination with variables.
317+
318+
Given:
319+
- a(`x)=b(`x) (fact with variable)
320+
- b(`x)=c(`x) (fact with same variable)
321+
- f(a) (fact)
322+
323+
Should derive:
324+
- b(t)=a(t) (via symmetry from a(`x)=b(`x))
325+
- a(t)=c(t) (via transitivity from a(`x)=b(`x), b(`x)=c(`x))
326+
- f(b)=f(c) (via congruence from b=c, which needs b=c as a separate fact)
327+
- f(c) (via substitution: f(a) and a=c)
328+
"""
329+
addr, engine, session = temp_db
330+
331+
# Add facts with variables and concrete facts for congruence
332+
async with session() as sess:
333+
sess.add(Facts(data="----\n(binary == (unary a `x) (unary b `x))\n"))
334+
sess.add(Facts(data="----\n(binary == (unary b `x) (unary c `x))\n"))
335+
sess.add(Facts(data="----\n(binary == a b)\n")) # Also add concrete equality for congruence
336+
sess.add(Facts(data="----\n(binary == b c)\n")) # Also add concrete equality for congruence
337+
sess.add(Facts(data="----\n(unary f a)\n"))
338+
# Ideas to test
339+
sess.add(Ideas(data="----\n(binary == (unary b t) (unary a t))\n")) # symmetry
340+
sess.add(Ideas(data="----\n(binary == (unary a t) (unary c t))\n")) # transitivity
341+
sess.add(Ideas(data="----\n(binary == (unary f b) (unary f c))\n")) # congruence
342+
sess.add(Ideas(data="----\n(unary f c)\n")) # substitution
343+
await sess.commit()
344+
345+
# Run the main function
346+
task = asyncio.create_task(main(addr, engine, session))
347+
await asyncio.sleep(0.3)
348+
task.cancel()
349+
try:
350+
await task
351+
except asyncio.CancelledError:
352+
pass
353+
354+
# Verify that all expected facts were produced
355+
async with session() as sess:
356+
facts = await sess.scalars(select(Facts))
357+
fact_data = [f.data for f in facts.all()]
358+
# Test symmetry: a(`x)=b(`x) should derive b(t)=a(t)
359+
assert "----\n(binary == (unary b t) (unary a t))\n" in fact_data
360+
# Test transitivity: a(`x)=b(`x), b(`x)=c(`x) should derive a(t)=c(t)
361+
assert "----\n(binary == (unary a t) (unary c t))\n" in fact_data
362+
# Test congruence: b=c should derive f(b)=f(c)
363+
assert "----\n(binary == (unary f b) (unary f c))\n" in fact_data
364+
# Test substitution: f(a) and a=c should derive f(c)
365+
assert "----\n(unary f c)\n" in fact_data

0 commit comments

Comments
 (0)