At dinner the other night, a good friend said he was afraid that the damage to our country done by a president who legitimizes coarseness, self-aggrandizement and disrespect would become permanent. I suspect versions of this conversation are not uncommon these days.

Having spent 40 years in courtrooms hearing stories of betrayal, violence, greed and pettiness — mixed with loyalty, integrity and goodwill — I have had plenty of time to think about human nature and the path of social evolution. I felt confident telling my friend that he could better describe our present circumstances as a temporary setback rather than with his phrase "the wheels are falling off."

I am hopeful for two reasons. The first is mostly anecdotal and unscientific. I have seen many things get better in ways one president cannot undo.

For example, in my business we treat the most powerless members of our society — criminal defendants — much more respectfully than we did in the past. And we are trained in sophisticated techniques for doing so, like procedural fairness, motivational interviewing, and methods for detecting and countering implicit bias.

Even the recent past was very different. Within my lifetime, defendants were not always even given a lawyer. During a visit to a prison a couple of years ago, a correctional officer told me that when he started in the field more than 20 years ago, the two things that could get a guard in trouble were sex and drugs. But recently someone had been fired simply for the disrespectful way he talked to the inmates.

Another data point: During a murder trial I was prosecuting in another jurisdiction 30-some years ago, the judge told me and the defense lawyer — off the record, of course — "I am going to get that f-er."

The growing respect for each individual is not limited to people in trouble. I work for a large government bureaucracy. Instead of being cold and impersonal, today this organization empowers employees with leadership development programs, employee appreciation and recognition programs, and extensive training and enrichment programs. Savvy business organizations are doing the same things.

These changes aren't happening just because people are becoming kinder and organizations more humane, although both are true. They are happening because experience has taught us the facts: people respond better, defendants comply with court orders more often and investments in human capital return higher productivity when people are treated well. In his book "The Nurture Effect," research scientist Anthony Biglan states flatly: "We can boil down what we have learned in the last fifty years to a simple principle: we need to ensure that everyone lives in a nurturing environment."

The social atmosphere created by these facts, and the smart people who pay attention to them, won't change because some severe and threatening weather might be passing through.

The second, more scientific, reason for hopefulness is a computer program called "Tit for Tat" that we studied in a course in cooperative lawyering I taught last year. Tit for tat is a strategy for solving a famous game-theory problem called "the prisoner's dilemma."

The dilemma imagines that two people have been arrested and charged with a crime that could bring them 10 years in prison. But the prosecutor has only enough evidence to convict them of a lesser offense, carrying just a six-month sentence — unless one of them testifies against the other. So the prosecutor puts the suspects in separate rooms and offers each the same deal.

If either crook testifies against his partner, and the partner remains loyal and is convicted, the partner will get the 10-year sentence and the snitch will go free. If both suspects testify against one another, they will get six years under a plea agreement.

Meanwhile, if both prisoners stay loyal — that is, remain silent — they will go to trial on the lesser charge and receive the minimal six-month sentence.

In this situation, in which neither arrestee can be sure what the other will do, any strictly rational prisoner will defect and testify against his partner — because, regardless of what his co-defendant does, he will be better off that way. The other suspect, if strictly rational, will do the same thing. They will settle for the six-year plea bargain.

But if the prisoners could have communicated and agreed together not to testify, they would only have served six months.

This dilemma has been found to mirror many real problems of human interaction. The big difference is that most real-life interactions are not one-time encounters, and humans learn from experience.

With this in mind, the prisoner's dilemma has been run hundreds of times as a computer simulation to test the spread of cooperation, with many computer programs using different strategies and playing multiple rounds against one another. A program/strategy's success in one round is reflected in the distribution of that program in the next round — a form of evolution.

The best strategy turns out to be tit for tat (or minor variations of it), and it is very simple. Tit for Tat always starts by offering to cooperate with a new "acquaintance." But after that, it responds in kind — cooperating or defecting depending on what the other program did the last time they interacted.

This solution to maximizing our well-being feels familiar. Most of us start an acquaintance cooperatively. But if the other person tries to exploit us, we will thereafter oppose them. Once the other person appears to turn over a new leaf, however, we are often willing to give them a break and try cooperating again.

Now, the most interesting thing about tit for tat is that this strategy is stable. In a population where just a couple of individuals are smart enough to follow tit for tat to begin with, the tendency to act this way will spread throughout the population. And this is true even if rogue actors are sometimes introduced — individuals who attempt to obtain short-term gains by exploitation. Over time, the simple mathematical power of tit for tat will restore a cooperative equilibrium.

Of course, computer simulations have limitations. But this one reflects some basic facts about society and human nature. Those who sow discord and practice exploitation can certainly obtain short-term gains, but they lose out on the enormous gains available through trust and cooperation. And as their tactics become clear, fewer people remain vulnerable to their exploitation.

So we can be hopeful. We have much work to do to ensure fairness and respect for everyone in our society, and in the world. But time and math are on the side of increasing cooperation, not exploitation. And confidence in this kind of future can speed its attainment.

Bruce Peterson is a Hennepin County district judge.