2020-05-19 20:25:18 +03:00
|
|
|
module Store
|
|
|
|
|
|
|
|
import Control.App
|
|
|
|
import Control.App.Console
|
|
|
|
|
|
|
|
data Access = LoggedOut | LoggedIn
|
|
|
|
data Store : Access -> Type where
|
|
|
|
MkStore : (secret : String) -> Store a
|
|
|
|
|
|
|
|
interface StoreI e where
|
|
|
|
connect : App1 e (Store LoggedOut)
|
|
|
|
login : (1 d : Store LoggedOut) -> (password : String) ->
|
|
|
|
App1 e (Res Bool (\ok => Store (if ok then LoggedIn else LoggedOut)))
|
|
|
|
logout : (1 d : Store LoggedIn) -> App1 e (Store LoggedOut)
|
2021-01-16 10:03:45 +03:00
|
|
|
readSecret : (1 d : Store LoggedIn) ->
|
2020-05-19 20:25:18 +03:00
|
|
|
App1 e (Res String (const (Store LoggedIn)))
|
|
|
|
disconnect : (1 d : Store LoggedOut) -> App {l} e ()
|
|
|
|
|
|
|
|
Has [Console] e => StoreI e where
|
|
|
|
connect
|
|
|
|
= do app $ putStrLn "Connect"
|
|
|
|
pure1 (MkStore "xyzzy")
|
|
|
|
|
|
|
|
login (MkStore str) pwd
|
|
|
|
= if pwd == "Mornington Crescent"
|
2020-06-12 13:18:12 +03:00
|
|
|
then pure1 (True # MkStore str)
|
|
|
|
else pure1 (False # MkStore str)
|
2020-05-19 20:25:18 +03:00
|
|
|
logout (MkStore str) = pure1 (MkStore str)
|
2020-06-12 13:18:12 +03:00
|
|
|
readSecret (MkStore str) = pure1 (str # MkStore str)
|
2020-05-19 20:25:18 +03:00
|
|
|
|
|
|
|
disconnect (MkStore _)
|
|
|
|
= putStrLn "Door destroyed"
|
|
|
|
|
2021-01-16 10:03:45 +03:00
|
|
|
storeProg : Has [Console, StoreI] e =>
|
2020-05-19 20:25:18 +03:00
|
|
|
App e ()
|
|
|
|
storeProg
|
|
|
|
= app1 $ do
|
|
|
|
s <- connect
|
|
|
|
app $ putStr "Password: "
|
2022-11-07 08:04:12 +03:00
|
|
|
pwd <- app $ getLine
|
2020-06-12 13:18:12 +03:00
|
|
|
True # s <- login s pwd
|
|
|
|
| False # s => do app $ putStrLn "Login failed"
|
|
|
|
app $ disconnect s
|
2020-05-19 20:25:18 +03:00
|
|
|
app $ putStrLn "Logged in"
|
2020-06-12 13:18:12 +03:00
|
|
|
secret # s <- readSecret s
|
2020-05-19 20:25:18 +03:00
|
|
|
app $ putStrLn ("Secret: " ++ secret)
|
2021-01-16 10:03:45 +03:00
|
|
|
s <- logout s
|
2020-05-19 20:25:18 +03:00
|
|
|
app $ putStrLn "Logged out"
|
|
|
|
app $ disconnect s
|
|
|
|
|
|
|
|
main : IO ()
|
|
|
|
main = run storeProg
|