MERMAIDS and mermen — let’s call them merfolk — live for approximately 300 years, after which they turn into sea foam. Who can say for sure what happens to humans when they die, but I doubt it involves much sea foam. This disparity is just one of the great melancholies that hang over The Little Mermaid, Hans Christian Andersen’s tale about a mermaid who makes an ill-fated deal with a sea witch. The mermaid gives up her identity — tongue cut out, tail lost — for the love of a human prince. He hardly even notices her sacrifice. It’s a hopeless love, a feminist tragedy. His soul will live on after death (or not) while she’ll be foam.

Andersen was a complicated character himself. Born into poverty in Denmark in 1805, he was bisexual but he died a virgin. He wrote plucky female characters who head out on bold quests, and his handsome princes have flaws and sensitive sides. He published his works as plain Fairy Tales, soon abandoning the label “for children” because he realised his disturbing, wondrous imaginary realms belong to everyone. Some of his stories, like The Shadow, are so dark that I doubt many parents would risk reading them at their own bedtimes, let alone their children’s. Disney still cashes in on Andersen’s legacy — think of recent hits like Frozen, a rehash of Snedronningen (The Snow Queen). Except that in Disney, the most haunting details are glossed over and prettied up.

The Little Mermaid (Den lille havfrue) was first published in Danish in 1837 and translated into English in 1872. It has been retold in hundreds of ways in hundreds of languages. This week it comes to Scotland as a new ballet with choreography by David Nixon and music by Sally Beamish. The production skips over Disney to tap the brutal aspects of the Andersen original, but the retelling also locates the fairy tale in Scots mythology — in particular, our own accounts of shapeshifting seafolk.

“The commission to write the music for this came suddenly,” Beamish recalls. “I was at the last performance at Saddlers Wells of my previous ballet, The Tempest. That was October 2016. I got a tap on the shoulder and it was John Pryce-Jones, music director of Northern Ballet. He just sort of sprung it on me… Would I like to write music for The Little Mermaid?” Beamish was doubtful — the new piece would be opening the following September, and as one of the UK’s busiest composers she hardly had expanses of free time to fill. Two factors convinced her to say yes.

The first was the fact it was a Scandinavian story. She could relate it to Scottish folklore. Beamish has long been enthralled by selkies, our own merfolk, who are reincarnations of those lost at sea, or humans trapped in the bodies of seals, or seals trapped on land without their seal skins. Possibly they are sinister, but more likely they are sad and gentle beings who will never bridge the gap between worlds. Maybe they turn to foam when they die.

Female selkies tend to share the fate of Andersen’s female characters. They leave the sea and are forced to live under the control — read sexual domination — of a human husband. A selkie can only be free when she’s found her own form again. “The fact Andersen was bisexual and had these impossible loves… that is definitely in there,” says Beamish. “He was in love with a man who was engaged to a woman. It was never going to happen. This is the heartbreak of people from different worlds who cannot connect… He channelled it into mournful mythical stories. I liked the idea of finding a way of channelling that into music.”

For many listeners, the great appeal of Beamish’s music is the place she finds between softness and steel, between forthright, economical gestures and dreamlike evocation. She blends a sense of the natural world with propulsive rhythms and she makes her instrumental writing sound gutsy and luminous. In her score for The Little Mermaid she uses a chamber orchestra with brass and electronic keyboard, plus a wordless female voice that keens and floats untethered.

The second factor that convinced Beamish to write The Little Mermaid was the fundamental draw of dance. “I’ve always felt as though I was writing for dance,” she says. “Even in my music that is not explicitly for dancers” — take a piece like her percussion concerto Dance Variations, which fizzes with the physicality and flux of Colin Currie’s performance style.

“My music has always been very visual,” she says. “Stories, paintings … my roots are in the theatre. There are theatre people in my family. And I love the idea of writing a score without worrying about word setting, and without worrying about keeping out the way — which you have to do in film and even in opera. In dance, the audience actually sees your music. Ballet choreography means everything is acted out. It’s the most wonderful privilege as a composer.”

Her score for The Little Mermaid integrates many aspects of herself. She looked back to the great ballet composers of the early 20th century: to Stravinsky and Prokofiev, from whom she learned “the crucial thing of getting the pacing right. I learned from them so much about structure and how long you have to do something to make it clear. The temptation in music is to move on too soon — which makes a lot of music really difficult to dance to. In ballet, every one of the dancers has to memorise the score. It has to make sense, has to be logical but not predictable.”

There is also much of Scotland in this music. Not long after she moved to Scotland in the early 1990s, Beamish became hooked on folk music. She bought a fiddle in a junk shop and started joining in at ceilidhs in the local village hall, and soon the tunes and idioms began appearing in her compositions. Hers is the best kind of appropriation: it never sounds forced, and the integration goes much deeper than a snatch of song here or a dotted rhythm there.

The Little Mermaid includes the rhythms of Harris tweed looms — “an industry of women,” Beamish points out, “and in the ballet they become a feisty dance”. At the heart of the score is a piece called Seavaigers that Beamish wrote for fiddler Chris Stout and harpist Catriona McKay in 2012. The name means ‘Seafarers’ and takes its inspiration from the stretch of water between Stout’s native Shetland and McKay’s hometown of Dundee.

“I always thought of Seavaigers as dance music,” says Beamish. “It’s inspired by Chris and Catriona’s playing, and what they play ultimately comes from dance music. And there’s also the shared melancholy.” Seavaigers is a lament for those lost at sea. Selkies are reincarnated drowned souls. Seavaigers now reincarnated as a selkie song? There is logic to this myth.

Northern Ballet’s The Little Mermaid is at The Festival Theatre, Edinburgh, this Thursday – Saturday