Skip to content

Instantly share code, notes, and snippets.

@monadplus
Last active November 9, 2020 15:38
Show Gist options
  • Select an option

  • Save monadplus/dcf5397710bb70f2f0da6d0243aeb02f to your computer and use it in GitHub Desktop.

Select an option

Save monadplus/dcf5397710bb70f2f0da6d0243aeb02f to your computer and use it in GitHub Desktop.
Simple case: output depending on input
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
module Scratch where
import Data.Kind
data Protection = None | Password
type family Secured (p :: Protection) (a :: Type) where
Secured None a = a
Secured Password a = Maybe a
data Box (p :: Protection) (a :: Type) where
PlainBox :: a -> Box 'None a
SecureBox :: String -> a -> Box 'Password a
deriving instance Functor (Box p)
openBox :: String -> Box p a -> Secured p a
openBox _ (PlainBox a) = a
openBox psswd (SecureBox secret a)
| psswd == secret = Just a
| otherwise = Nothing
main :: IO ()
main = do
let plainBox = PlainBox "The cake is a lie."
secureBox = SecureBox "1234" "The cake is a lie."
print $ openBox "" plainBox
print $ openBox "123" secureBox
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment