The year is 1982, although not quite as we knew it. The miners are on strike, unemployment is soaring and the headlines are dominated by Mrs Thatcher’s belligerent stance on the Falklands. Like the best counterfactual novels – and this one is up there with Robert Harris’s Fatherland and Philip Roth’s The Plot Against America – Ian McEwan’s Machines Like Me is as much about the continuities it maintains as the skews it puts on history. In this alternative 1982, Britain has been humiliated in the South Atlantic, with 3,000 of its soldiers dead and the Argentinian junta jubilant; Lennon and JFK are both alive; Tony Benn is challenging Thatcher for the role of prime minister. These changes are there to illustrate an idea voiced by Charlie Friend, the book’s narrator, halfway through the novel: “The present is the frailest of improbable constructs. It could have been different.”

The greatest tweak McEwan gives to history surrounds the figure of Alan Turing. Rather than the tragic facts of a brilliant life cut short by bigotry and the brutal “treatment” of his homosexuality, McEwan has Turing build on his code-cracking war work. Supported by a series of colleagues from Francis Crick to Demis Hassabis (who appears to have been born a few decades early) to his lover, the theoretical physicist Tom Reah, Turing steers Britain into a technological future of extraordinary sophistication. On the streets of McEwan’s south London there are autonomous cars, while mobile phones and personal computers are more cutting-edge than our own nearly 40 years later. Turing’s research, which he insists be made available open-source, has been employed to design androids that are able to pass for the humans who created them. Charlie, our likable, faintly useless hero, comes into some money on the death of his mother and buys one of these “synthetic humans”, a male called Adam (the Eves sold out immediately). The narrative’s ebullient energy is drawn from the love triangle that forms between Charlie, his upstairs neighbour Miranda (her name just one of many references to The Tempest that thread through the book), and Adam, who has the air of a “Bosphorus dockworker”. It’s like Jules et Jim, if Jim were a robot.

Ultimately, Machines Like Me is a novel about the power of novels. Charlie realises that his stance regarding his purchase has been shaped by literature. “The imagination,” he says, “fleeter than history, than technological advance, had already rehearsed this future in books…” This is a novel that holds up the form as an example of the unreplicable subtlety of the human mind. While Adam composes haikus of stultifying banality to Miranda, he finds the novel’s obsession with misunderstandings and reversals obsolete in an age when technology has colonised the private life. Novels, McEwan is saying, do something that robots can’t: they are a heroic record of our imperfections, a celebration of the flaws that make us human.

Machines Like Me ramifies out from the specific challenges of AI to comment more broadly on the cost of progress, working through in its late-20th-century setting problems that we are still struggling with in the early 21st. There’s lashings of Jeremy Corbyn in the portrayal of Benn and his “band of Trotskyite followers”. It’s telling that McEwan has a leftwing government taking Britain out of the EU, “a union that chiefly benefited large corporations”. Benn refuses to have another referendum – “only the Third Reich and other tyrannies decided policy by plebiscites”, he says, “and generally no good came of them”. The novel is also brilliant on the issues that will face future generations as machines fulfil more and more of the jobs currently carried out by humans. There will be a crisis of free time, although, as Charlie notes, “endless leisure pursuits had never much troubled the aristocracy”.

There’s not been a bad Ian McEwan novel since the Booker-winning Amsterdam, but this is right up there with his very best. Machines Like Me manages to combine the dark acidity of McEwan’s great early stories with the crowd-pleasing readability of his more recent work. A novel this smart oughtn’t to be such fun, but it is.

• Machines Like Me by Ian McEwan is published by Jonathan Cape (£18.99). To order a copy go to guardianbookshop.com or call 0330 333 6846. Free UK p&p over £15, online orders only. Phone orders min p&p of £1.99