Added loop count variable and tests
Prolog term generation extracted from semantic check classes