Imagining sleep API

Apr 27, 2020 00:00 · 792 words · 4 minute read API sleep

A thought experiment - how would the API for the human sleep look like, if there ever be any? Let’s break it into several functions and their specifications.

I will be using R for writing pseudocode - even without a strong background in programming, these snippets should be quite easy to decipher. I will write the code in a very verbatim way.

Falling asleep on request

OK, the easiest case - let’s define a function for fast falling asleep:

fallAsleep <- function(minutes.delay = 10){
  # this will prevent from executing if the function parameter is too low
  stopif(minutes.delay < 1); 
  ...
}

I guess that would be all. I don’t want to drop dead after executing this function. I want a healthy delay and a safety mechanism that prevents me from dialing down the delay to 0.

fallAsleep is a primary function that later will be called by others.

Waking upon request

Similar to the above, let’s prototype another primary function for waking up:

wakeUp <- function(minutes.delay = 10){
  # this will prevent from executing if the function parameter is too low
  stopif(minutes.delay < 1);
  ...
}

Again, let’s prevent too abrupt breaks of sleep.

Am I sleeping?

Let’s introduce a function that will check if the subject is sleeping.

isSleeping <- function(){
  sleep.state <- eegSleepState();
  if (sleep.state > 5){
    return(TRUE)
  } else {
    return(FALSE)
  }
}

I don’t know if any parameter is neccessary. Function returns either TRUE or FALSE. We will need a function that will return a numeric sleep state (see below).

Hypnagogic state

Now let’s move to something more interesting. Hypnagogia is a transitional state from wakefulness to sleep. At the cognitive level, it is presumably associated with increased suggestibility and a fluid association of ideas. Many artists and scientists credited hypnagogia with enhancing their creativity.

The hypnagogic state has a quite clear EEG spectrum (flattening of EEG and theta ripples, sleep states 4 and 5); therefore, monitoring is required to maintain this stage.

We need two functions, first to return the EEG state of the brain:

eegSleepState <- function(){
  ...
}

We will control separately how often this function will be called. This will return Hori’s sleep state, a numerical from 1 to 9 (from Hori et al., Topographical EEG changes, and hypnagogic experience).

Let’s try to prototype the function that will induce hypnagogic state for 30 mins (the default length):

induceHypnagogia <- function(length.mins = 30){
  # this will prevent from executing if the function parameter is too high
  # for safety reasons
  stopif(length.mins > 60);
  
  # let's start the timer
  sleep.timer <- hyperhuman::timer(length.mins);
  
  # in this loop we want to push into sleep or start waking up as long as the EEG state
  # is outside of optimum of 4-5.
  while(sleep.timer > 0){
    sleep.state <- eegSleepState();
    if (sleep.state > 5){
      # let's start waking up
      wakeUp(minutes.delay = 1)
    }
    if (sleep.state < 4){
      # let's push towards sleep
      fallAsleep(minutes.delay = 1)
    }
    
  }
  
}

What this function does is that during the default 30 minutes, it checks if the sleep state returned by eegSleepState function is returning a value between 4 and 5, which was defined as the range for the hypnagogic state. If not, it pushes the subject towards sleep or wakefulness, depending on which side of this range is found.

Power nap

Now, let’s define a function for a power nap. We want to fall asleep fast, go straight into sleep, maintain it for 20 minutes, and then to wake up shortly.

powerNap <- function(nap.length.minutes = 20){
  # again, a safety mechanism, no nap is allowed for more than 30 mins
  stopif(nap.length.minutes > 30);
  
  # again, a countdown object
  sleep.timer <- hyperhuman::timer(nap.length.minutes)
  
  while(sleep.timer > 0){
    is.subject.sleeping <- isSleeping()
    if (!is.subject.sleeping){
      fallAsleep(minutes.delay = 1)
    }
  }
}

This function will make sure you don’t wake up before the timer ends.

Polyphasic sleep - where it all falls apart

So far, I could ride on oversimplifications quite easily. Things get complicated, though, when it comes to polyphasic sleep. We should start including either additional safety mechanisms or additional monitoring mechanisms for wakefulness. We need to control both the process and its outcomes precisely. And this is a place where it all falls apart.

It turns out that we have only a rough understanding of the sleep. Polyphasic sleep is quite a significant intervention. There are many people successfully employing this mode of sleep for months, without an apparent decline in cognitive functions. On the other hand, quality research is lacking. Long term effects are unknown.

API design thought experiment - conclusions

I think I will repeat this experiment for other physiological functions. It clearly outlines my area of incompetence but also some general holes in our understanding of the biochemistry and physiology of the human body.