"No such thing as a free lunch" (alternatively, "There ain't no such thing as a free lunch", "There is no such thing as a free lunch" or other variants) is a popular adage communicating the idea that it is impossible to get something for nothing.  The acronyms TANSTAAFL, TINSTAAFL, and TNSTAAFL are also used.  The phrase was in use by the 1930s, but its first appearance is unknown. The "free lunch" in the saying refers to the formerly common practice in American bars of offering a "free lunch" in order to entice drinking customers.
 The phrase and the acronym are central to Robert A. Heinlein's 1966 science-fiction novel The Moon Is a Harsh Mistress, which helped popularize it. The free-market economist Milton Friedman also increased its exposure and use by paraphrasing it as the title of a 1975 book, and it is used in economics literature to describe opportunity cost. Campbell McConnell writes that the idea is "at the core of economics".
 The "free lunch" refers to the once-common tradition of saloons in the United States providing a "free" lunch to patrons who had purchased at least one drink. Many foods on offer were high in salt (e.g., ham, cheese, and salted crackers), so those who ate them ended up buying a lot of beer. Rudyard Kipling, writing in 1891, noted how he
 ...came upon a bar-room full of bad Salon pictures, in which men with hats on the backs of their heads were wolfing food from a counter. It was the institution of the "free lunch" I had struck. You paid for a drink and got as much as you wanted to eat. For something less than a rupee a day a man can feed himself sumptuously in San Francisco, even though he be a bankrupt. Remember this if ever you are stranded in these parts. TANSTAAFL, on the other hand, indicates an acknowledgement that in reality a person or a society cannot get "something for nothing". Even if something appears to be free, there is always a cost to the person or to society as a whole, although that may be a hidden cost or an externality. For example, as Heinlein has one of his characters point out, a bar offering a free lunch will likely charge more for its drinks.
 The earliest known occurrence of the full phrase (except for the "a"), in the form "There ain't no such thing as free lunch", appears as the punchline of a joke related in an article in the El Paso Herald-Post of June 27, 1938 (and other Scripps-Howard newspapers about the same time), entitled "Economics in Eight Words".
 In 1945, "There ain't no such thing as a free lunch" appeared in the Columbia Law Review, and "there is no free lunch" appeared in a 1942 article in the Oelwein Daily Register (in a quote attributed to economist Harley L. Lutz) and in a 1947 column by economist Merryle S. Rukeyser.
 In 1949, the phrase appeared in an article by Walter Morrow in the San Francisco News (published on 1 June) and in Pierre Dos Utt's monograph TANSTAAFL: A Plan for a New Economic World Order, which describes an oligarchic political system based on his conclusions from "no free lunch" principles.
 The 1938 and 1949 sources use the phrase in relating a fable about a king (Nebuchadnezzar in Dos Utt's retelling) seeking advice from his economic advisors. Morrow's retelling, which claims to derive from an earlier editorial reported to be non-existent, but closely follows the story as related in the earlier article in the El Paso Herald-Post, differs from Dos Utt's in that the ruler asks for ever-simplified advice following their original "eighty-seven volumes of six hundred pages" as opposed to a simple failure to agree on "any major remedy". The last surviving economist advises that "There ain't no such thing as free lunch."
 In 1950, a New York Times columnist ascribed the phrase to economist (and army general) Leonard P. Ayres of the Cleveland Trust Company: "It seems that shortly before the General's death [in 1946]... a group of reporters approached the general with the request that perhaps he might give them one of several immutable economic truisms that he had gathered from his long years of economic study... 'It is an immutable economic fact,' said the general, 'that there is no such thing as a free lunch.'"
 The September 8, 1961, issue of LIFE magazine has an editorial on page 4, "'TANSTAFL', It's the Truth", that closes with an anecdotal farmer explaining this slight variant of TANSTAAFL.
 In 1966, author Robert A. Heinlein published his novel The Moon Is a Harsh Mistress, in which TANSTAAFL was a central, libertarian theme, mentioned by name and explained. This increased its use in the mainstream.
 Edwin G. Dolan used the phrase as the title of his 1971 book TANSTAAFL (There Ain't No Such Thing As A Free Lunch) – A Libertarian Perspective on Environmental Policy.
 In the sciences, no free lunch means that the universe as a whole is ultimately a closed system. There is no source of matter, energy, or light that draws resources from something else which will not eventually be exhausted. Therefore, the no free lunch argument may also be applied to natural physical processes in a closed system (either the universe as a whole, or any system that does not receive energy or matter from outside). (See Second law of thermodynamics.) The bio-ecologist Barry Commoner used this concept as the last of his famous "Four Laws of Ecology".
 According to American theoretical physicist and cosmologist Alan Guth "the universe is the ultimate free lunch", given that in the early stage of its expansion the total amount of energy available to make particles was very large.
 In economics, no free lunch demonstrates opportunity cost. Greg Mankiw described the concept as follows: "To get one thing that we like, we usually have to give up another thing that we like. Making decisions requires trading off one goal against another." The idea that there is no free lunch at the societal level applies only when all resources are being used completely and appropriately – i.e., when economic efficiency prevails. If not, a 'free lunch' can be had through a more efficient utilization of resources. Or, as Fred Brooks put it, "You can only get something for nothing if you have previously gotten nothing for something."  If one individual or group gets something at no cost, somebody else ends up paying for it. If there appears to be no direct cost to any single individual, there is a social cost.  Similarly, someone can benefit for "free" from an externality or from a public good, but someone has to pay the cost of producing these benefits. (See Free rider problem and Tragedy of the commons.)
 In mathematical finance, the term is also used as an informal synonym for the principle of no-arbitrage. This principle states that a combination of securities that has the same cash-flows as another security must have the same net price in equilibrium.
 In statistics, the term has been used to describe the tradeoffs of statistical learners (e.g., in machine learning) which are unavoidable according to the "No free lunch" theorem. That is, any model that claims to offer superior flexibility in analyzing data patterns usually does so at the cost of introducing extra assumptions, or by sacrificing generalizability in important situations.
 No free lunch is sometimes used as a response to claims of the virtues of free software. Supporters of free software often counter that the use of the term "free" in this context is primarily a reference to a lack of constraint ("libre") rather than a lack of cost ("gratis"). Richard Stallman has described it as "'free' as in 'free speech', not as in 'free beer'".
 The prefix "TANSTAA-" (or "TINSTAA-") is used in numerous other contexts as well to denote some immutable property of the system being discussed. For example, "TANSTAANFS" is used by electrical engineering professors to stand for "There Ain't No Such Thing As A Noise-Free System".[citation needed]
 Baseball Prospectus coined the abbreviation "TINSTAAPP", for "There Is No Such Thing As A Pitching Prospect", as many young pitchers hurt their arms before they can be effective at a major league level.
 Hungarian prime minister Ferenc Gyurcsány used this adage to justify his social reforms in the mid-2000s. As a post-socialist country, Hungary struggled with the illusion of the state as a caring and giving, independent entity, rather than being the embodiment of the community. The saying "there is no free lunch" represented that even if the state provides welfare or something else for the people in need, it is in fact bought or provided by other people of the same community through taxes. Therefore, the state cannot provide everything for everyone, and increased provisions given by the state can only be financed by economic growth, increased taxes or public debt.
 Some exceptions from the "no free lunch" tenet have been put forward, such as the Sun and carbon dioxide. It was argued in particular that metabolism evolved to take advantage of the free lunch provided by the Sun, which also triggers production of vital oxygen in plants.  However, these too fall short in that the viewpoint is an open system, Earth, with "free" inputs from the Sun.  When viewed from the larger system context, the Sun/Earth or Solar System, there is no net energy exchange, and still "no free lunch".