Last active
November 9, 2020 15:38
-
-
Save monadplus/dcf5397710bb70f2f0da6d0243aeb02f to your computer and use it in GitHub Desktop.
Simple case: output depending on input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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