Tic-tac-toe in Erlang — macros for testing and debugging

This is part of an Erlang tutorial built around a tic-tac-toe program. The program is stuffed into one file, called tic.erl and available here. The source code and this tutorial are organized into these sections:

Macros for testing and debugging

Erlang has a macro preprocessor that’s modeled on the C/C++ preprocessor. The tic.erl source file starts with two utility macros, which are at the top because you have to define them before using them. m_assert(P) confirms that P is true while m_confirm(Fn,Expr) returns the value from an expression, but first confirms the value has the expected form.

Assert and confirm are an important part of programming. By stating and testing assumptions, they help catch bugs early and identify integration problems. They perform regression testing, pinning down behaviors and interfaces. They also are an important part of unit testing, and they highlight areas of responsibility with entry/exit testing.

Source code

The macros looks like this.

% --------------------------------------------------------------
% Utility macros
% --------------------------------------------------------------
% ?m_assert( Pred )
% ?m_confirm( Confirm_fn, Expr )
%
%   Simple assert and confirm macros.
%
%   m_assert alerts the user and halts the program if Pred is
%   any value other than 'true'. With debugging off Pred is not
%   evaluated.
%
%   m_confirm( Confirm_fn, Expr ) returns (Expr), but before
%   returning it asserts that Confirm_fn( Expr) evaluates
%   true.
%
%   If you want something a little more comprehensive try:
%     http://erlang.org/download/eunit.hrl

-ifdef( NODEBUG ).

- define( m_assert( Pred ), ok ).
- define( m_confirm( Confirm_fn, Expr ), (Expr) ).

-else.

- define(
m_assert( Pred ),
  ( (fun ( Pred_evaled ) ->
       case Pred_evaled of
         true -> ok;
         _ ->
           io:fwrite( "~nAssert failed in ~w at line ~w~n",
             [ ?MODULE, ?LINE ]),
           io:fwrite( "Expression is "),
           io:fwrite( ??Pred),
           io:fwrite( "~n  which evaluates to ~w~n~n",
             [ Pred_evaled ]),
           erlang:error( assertion_failed)
       end
     end
    )( Pred)
  )).

- define(
m_confirm( Confirm_fn, Expr ),
  ( (fun ( Expr_evaled ) ->
       Confirm_evaled = Confirm_fn( Expr_evaled),
       case Confirm_evaled of
         true -> ok;
         _ ->
           io:fwrite( "~nConfirm failed in ~w at line ~w~n",
             [ ?MODULE, ?LINE ]),
           io:fwrite( "Expression is "),
           io:fwrite( ??Expr),
           io:fwrite( "~n  which evaluates to ~w~n",
             [ Expr_evaled ]),
           io:fwrite( "~nConfirm function is "),
           io:fwrite( ??Confirm_fn),
           io:fwrite( "~n  which returned ~w~n~n",
             [ Confirm_evaled ]),
           erlang:error( confirm_failed)
       end,
       Expr_evaled
     end
    )( Expr)
  )).

-endif.

Code review

Yes, I know, it’s kinda ugly. That’s how macros are. And is m_assert really a better name than plain assert. I was thinking the ‘m_‘ prefix identifies it as a macro, although macros are already obvious in Erlang, since you prefix them with a question mark to use them. So maybe m_ isn’t such a great idea.

And of course I probably should just use the macros in http://erlang.org/download/eunit.hrl. This wheel is already invented.

These are the kinds of points that come up in a code review. Inefficient and badly designed code is identified, stylistic idiosyncrasies are brought to light, and redundant/unnecessary code is weeded out. Code reviews are a good thing. Especially over pizza.

Let Binding

I’d like to point out an interesting feature of these macros. They bind their arguments by evaluating them and passing them to a function. It’s very much like the let macro does in Scheme — evals and passes the results to a lambda. This is as easy in Erlang as it is in Lisp.

Try this in an Erlang listener:

1> (fun( X ) -> X + X + X end)
1> (begin io:fwrite( "eval'ed~n"), 1 + 2 + 3 end).
eval'ed
18
2>

The begin..end statement is evaluated only once, where the result is bound to X and passed to the unnamed function.

In a pure functional language without side effects, it doesn’t matter how many times you evaluate an expression (except in how it affects performance). An expression without side effects always returns the same value and leaves no traces. You can change the order of evaluation, short-circuit evaluation, eval once and cache the value — it doesn’t matter.

But Erlang isn’t free of side effects (except in guards). So the let-like cliche demonstrated above is useful.

Comments

30 Responses to “Tic-tac-toe in Erlang — macros for testing and debugging”

  1. internet ourmidland on July 7th, 2012 8:54 am

    my voice lessons were given to me by my aunt who also
    teaches some amateur pop singers to improve their voices

  2. scholarships for college students|scholarships for college students in texas|2011 scholarships for college students|scholarships for college students in california|scholarships for college students 2011|easy scholarships for college students|grants and ss on July 8th, 2012 2:14 pm

    With havin so much content and articles do you ever run into any
    problems of plagorism or copyright infringement My site has
    quite a large amount of exclusive content Ive either authored myself or outsourced conversely it seems a whole lot of its popping it up all over the
    web without my authorization. Do you know any other technique to help protect against content out
    of being stolen Id certainly appreciate it.

  3. grants for single mothers to pay bills on July 8th, 2012 3:31 pm

    First-class work, wonderful blog really enjoy it and put
    into my social bookmarks. Keep up the fine work

  4. programs that help single mothers on July 8th, 2012 4:20 pm

    Youre so cool! I dont suppose Ive read something
    such because this before. So nice to get somebody by incorporating authentic ideas on this subject.
    realy i appreciate you for starting this up. this first-rate website
    have been some things thats needed on the internet, somebody after a
    little bit originality. helpful work for bringing something new to that the
    web!

  5. cell phone lookup on July 8th, 2012 4:44 pm

    Howdy, May I download this post snapshot and use that
    on my personal blog site

  6. grants scholarships for single mothers on July 8th, 2012 4:59 pm

    Great article, thanks. I signed up to your blog rss
    feed.

  7. reverse phone lookup on July 9th, 2012 4:36 pm

    Can I recently say thats a relief to uncover
    a individual that actually knows what theyre dealing with on that the net.
    You certainly recognize how to bring an issue to light and produce it crucial.
    The best solution to need to check out this and fully grasp this side in the story.
    I cant think youre not more wellliked because you absolutely develop the gift.

  8. cell phone lookup numbers on July 10th, 2012 4:24 am

    This is so great that I had to comment. Im usually just a
    lurker, taking in knowledge and nodding my head in quiet
    approval at that the good stuff.this required written props.

    Theory rocksthanks.

  9. http://www.prlog.org/11306717-phone-number-lookup-is-your-partner-cheating-on-you.html on July 16th, 2012 12:44 pm

    Im actually enjoying the design and layout of your blog.
    It can be a very effortless on that the eyes which makes
    it much more pleasant for me to come here and visit more often.

    Did you hire out a developer to create your theme Great work!

  10. reverse number lookup on July 16th, 2012 1:01 pm

    Hello to all, how is the whole thing, I think every one is getting more from this website, and your views are pleasant for new users.

  11. cell phone number lookup on July 16th, 2012 2:08 pm

    Wonderful site. I hope you write more informative articles, as i will check back often to read your
    posts.

  12. reverse phone on July 16th, 2012 2:25 pm

    hi, I am havin a hard time trying to rank up for the words “victorias secret coupon codes Please approve my comment!
    !

  13. http://www.prlog.org/11734648-criminal-background-check-public-records.html on July 16th, 2012 3:19 pm

    Thanks a ton for this kind of facts I had been exploring all
    Yahoo to obtain it!

  14. http://www.youtube.com/watch?v=c3-A_ZxBlSQ on July 16th, 2012 4:56 pm

    Undeniably believe that which you stated. Your favorite reason seemed to be via
    that the internet that the simplest thing to be aware of.

    I say to you, I certainly get annoyed while people think about worries they just
    dont be acquainted with about. You managed to hit that the nail upon the top because well because defined out the
    whole thing without having side effect , people can take
    a signal. Will likely be back to lookup out more. Thanks

  15. phone number lookup on July 16th, 2012 6:02 pm

    Hi! Ive been reading your web site for a while now and finally got that the bravery to go ahead and provide you
    a shout out out of Kingwood Texas! Just wanted to mention keep up the
    great job!

  16. phone number on July 16th, 2012 8:21 pm

    What¦s Happening im new to this, I stumbled upon this
    Ive found It absolutely helpful and it has helped me out loads.
    I hope to contribute & help different users just like it is aided me.
    Great job.

  17. http://ezinearticles.com/?Reverse-Address-Lookup---Get-the-Information-You-Need-the-Easy-Way&id=5909554 on July 16th, 2012 8:52 pm

    Wohh precisely what I was looking for, thankyou for
    putting up.

  18. http://officialreversephonelookup.com/ on July 16th, 2012 9:07 pm

    If some one wishes expert view on the topic of
    blogging afterward i advise him/her to pay a quick visit
    this webpage, Keep up the nice job.

  19. reverse cell phone lookup on July 16th, 2012 10:24 pm

    I drop a leave a response when I like a article on a website or I have something to add
    to the discussion. It’s a result of the passion displayed in the article I browsed. And after this article Tic-tac-toe in Erlang – macros for testing and debugging : Code Obscurata. I was actually excited enough to post a thought ;-) I actually do have a couple of questions for you if it’s allright. Could it be simply me or do a few of the remarks appear as if they are written by brain dead folks? :-P And, if you are writing at other places, I’d like to follow you. Could you make a list all of all your public sites like your twitter feed, Facebook page or linkedin profile?

  20. http://ezinearticles.com/?Reverse-Phone-Lookup---Easy-Way-to-Know-the-Truth&id=5909597 on July 17th, 2012 2:07 am

    I discovered your blog site online and appearance several of
    your early posts. Keep in that the first-rate operate.

    I just now extra your Rss to my MSN News Reader. Hunting toward reading a lot
    more from you at a later time!

  21. people finder on July 17th, 2012 2:33 am

    very nice post, i definitely love this site, persist in it

  22. phone number search on July 17th, 2012 2:37 am

    I love to visit your webblog, the themes are nice.
    \’

  23. http://www.prlog.org/11261550-phone-number-lookup-catch-cheater-quickly.html on July 25th, 2012 1:59 am

    all we want is of course a firm skin which can be very smooth.
    great skin comes with great genetics and proper maintennance

  24. ways to make money on July 27th, 2012 6:34 am

    my daughter are doing some scrapbooking stuffs over that the
    years and she really likes it,.

  25. http://101watchmoviesonline.com/ on July 31st, 2012 8:00 am

    I discovered your website internet site on the web and
    check a few your early posts. Always keep inside that the
    first-class operate. I just additional your Rss to my
    MSN News Reader. Seeking forward to reading far
    more out of you looking up down that the road!

  26. http://officialreversephonelookup.com/ on August 1st, 2012 5:19 am

    Unquestionably believe that which you said. Your favorite reason seemed to be via internet the easiest thing to be aware of.
    I say to you, I definitely get annoyed while people think about worries that they plainly do
    not identify about. You managed to hit that the nail upon that the top because well as defined
    out that the whole thing without having side effect , people
    can take a signal. Will likely be back to seek out more.

    Thanks

  27. reverse phone lookup verizon cell phone number on August 13th, 2012 3:21 pm

    I dont normally comment in spite of this I gotta tell regards for the post on this amazing one .

  28. best reverse cell phone lookup on August 20th, 2012 5:26 am

    Hi, I just found your blog via google. Your post is truly applicable to my life right now, and I’m really delighted I found your website.

  29. Tic-tac-toe in Erlang — alternative rules (Y pattern) : Code Obscurata on August 24th, 2012 6:20 pm

    […] Macros for testing and debugging […]

  30. Tic-tac-toe in Erlang — utilities to introduce randomness : Code Obscurata on August 24th, 2012 6:24 pm

    […] Macros for testing and debugging […]