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:
- Introduction
- Top-level loop
- User input
- Board display
- Board abstraction
- Game abstraction
- Next move calculations and predictions
- Predicted outcome abstraction
- Utilities to introduce randomness
- Macros for testing and debugging (this page)
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”
my voice lessons were given to me by my aunt who also
teaches some amateur pop singers to improve their voices
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.
First-class work, wonderful blog really enjoy it and put
into my social bookmarks. Keep up the fine work
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!
Howdy, May I download this post snapshot and use that
on my personal blog site
Great article, thanks. I signed up to your blog rss
feed.
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.
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.
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!
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.
Wonderful site. I hope you write more informative articles, as i will check back often to read your
posts.
hi, I am havin a hard time trying to rank up for the words “victorias secret coupon codes Please approve my comment!
!
Thanks a ton for this kind of facts I had been exploring all
Yahoo to obtain it!
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
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!
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.
Wohh precisely what I was looking for, thankyou for
putting up.
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.
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?
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!
very nice post, i definitely love this site, persist in it
I love to visit your webblog, the themes are nice.
\’
all we want is of course a firm skin which can be very smooth.
great skin comes with great genetics and proper maintennance
my daughter are doing some scrapbooking stuffs over that the
years and she really likes it,.
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!
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
I dont normally comment in spite of this I gotta tell regards for the post on this amazing one .
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.
[...] Macros for testing and debugging [...]
[...] Macros for testing and debugging [...]