fork download
  1. :- op(600, xfx, :=). % Define a custom infix operator.
  2.  
  3. % Predicate 1: The 'main' goal.
  4. % It uses a failure-driven loop, which executes the side-effect
  5. % (printing) and then fails the whole goal.
  6. cursed_hello :-
  7. \+ (
  8. hello_world := [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33],
  9. print_to_stderr(hello_world),
  10. fail % Force failure *after* printing
  11. ).
  12.  
  13. % Predicate 2: Hides the actual printing logic.
  14. % It takes advantage of the term unification performed by the custom operator.
  15. print_to_stderr(Term := Codes) :-
  16. % Term is ignored, Codes contains the list of ASCII values.
  17. % string_codes/2 converts the list of numbers into an actual string.
  18. string_codes(String, Codes),
  19. % write_term/2 is used with the 'quoted(false)' option to print the string
  20. % value instead of the string's representation (e.g., "Hello, World!").
  21. write_term(user_error, String, [quoted(false)]),
  22. nl(user_error). % Write newline to the error stream
  23.  
  24. % Query:
  25. % ?- cursed_hello.
  26. % Hello, World!
  27. % false.
  28.  
Success #stdin #stdout #stderr 0.03s 6908KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Warning: /home/C4M4MT/prog:15:
	Singleton variables: [Term]
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit