NixLabConnectionconnecting
ProjectsProject
horizon /

dependent-nixlab-examples

publicguest

Worked examples of where dependent types help NixLab.

git clone git@nixlab.openfp.net:horizon/dependent-nixlab-examples.git
mainsrc/NixLab/Dependent/Commands.hs
{-# LANGUAGE DependentTypes #-}
{-# LANGUAGE ListTuplePuns #-}

-- | Host commands as data: the argument and result type of each command
-- are computed from the command by ordinary functions, so a client cannot
-- send a wrong payload or misread a reply.
module NixLab.Dependent.Commands (
    Command (..),
    args,
    reply,
    Host (..),
    call,
) where

import Data.List (List)
import Prelude.Dependent

data Command = IssueClaim | MergeRequestMerge | ListLabels

-- | The payload a command takes.
args :: Command -> Type
args IssueClaim = (String, Int)
args MergeRequestMerge = Int
args ListLabels = String

-- | The reply a command returns.
reply :: Command -> Type
reply IssueClaim = Bool
reply MergeRequestMerge = Maybe String
reply ListLabels = List String

-- | A host interprets every command at its own types.
newtype Host = Host {runCommand :: foreach (c :: Command) -> args c -> IO (reply c)}

-- | Call a host command; the payload type follows from the command chosen.
call :: Host -> foreach (c :: Command) -> args c -> IO (reply c)
call h c = runCommand h c