:: Description :== String
:: StoreID a :== String
:: *TaskResult a :== (a, TaskState)
-:: *TaskState = { console :: *File
+:: *TaskState = { console :: !*File
, store :: Map String Dynamic
}
enterInformation :: Description TaskState -> TaskResult a | iTasksLite a
enterInformation d ts=:{console}
-# (a, console) = freadline (console <<< "Enter the answer: ")
+# (a, console) = freadline (console <<< d <<< ": ")
= case parse a of
Just x = (x, {ts & console=console})
Nothing = enterInformation d {ts & console=console <<< "Wrong format, try again.\n"}
Start world
# (console, world) = stdio world
console = console <<< "Welcome to iTasksLite\n\n"
- (r, console) = eval task4 console
+ (r, console) = eval task3 console
console = console <<< "\nThe result of the task is " <<< print r <<< ".\n"
(_, world) = fclose console world
= world
:: Description :== String
:: StoreID a :== String
-:: Task a = // define type here
+:: Task a = Task (*TaskState -> *(a, *TaskState))
:: *TaskState = { console :: !*File
, store :: Map String Dynamic
}
instance Functor Task where
fmap :: (a -> b) (Task a) -> Task b
- fmap _ _ = undef
+ fmap f (Task g) = Task \st.let (x, st1) = g st in (f x, st1)
instance Applicative Task where
pure :: a -> Task a
- pure _ = undef
+ pure x = Task \st.(x, st)
(<*>) infixl 4 :: (Task (a -> b)) (Task a) -> Task b
- (<*>) _ _ = undef
+ (<*>) (Task f) (Task g) = Task \st.let (x, st1) = g st in let (y, st2) = f st1 in (y x, st2)
instance Monad Task where
bind :: (Task a) (a -> Task b) -> Task b
- bind _ _ = undef
+ bind (Task f) g = Task \st.let (x, st1) = f st in let (Task y) = g x in y st1
eval :: (Task a) *File -> (a, *File) | iTasksLite a
eval (Task taskFunc) console
# (r, {console}) = taskFunc {store = newMap, console = console}
= (r, console)
+viewInformation :: Description a -> Task a | iTasksLite a
+viewInformation d x = Task \st=:{console}.(x, {st & console=console <<< d <<< ": " <<< print x <<< ".\n"})
+
+enterInformation :: Description -> Task a | iTasksLite a
+enterInformation d = Task f
+ where
+ f st=:{console}
+ # (a, console) = freadline (console <<< d <<< ": ")
+ = case parse a of
+ Just x = (x, {st & console=console})
+ Nothing = f {st & console=console <<< "Wrong format, try again.\n"}
+
+store :: a (StoreID a) -> Task a | iTasksLite a
+store v s = Task \st=:{store}.(v, {st & store=store_ v s store})
+
+retrieve :: (StoreID a) -> Task a | iTasksLite a
+retrieve s = Task \st=:{store}.(retrieve_ s store, st)
+
task0 :: Task Int
task0 = return 42
-/*task1 :: Task Int
+task1 :: Task Int
task1 = viewInformation "The answer is" 42
task2 :: Task Int
>>| addIdea
ideaStore :: StoreID [String]
- ideaStore = "ideas"*/
+ ideaStore = "ideas"
Start world
# (console, world) = stdio world
console = console <<< "Welcome to iTasksLite" <<< "\n\n"
- (r, console) = eval task0 console
+ (r, console) = eval task4 console
console = console <<< "\n" <<< "The result of the task is " <<< print r <<< ".\n"
(_, world) = fclose console world
= world