DynEditorExample.icl 19.6 KB
Newer Older
1 2
module DynEditorExample

Steffen Michels's avatar
Steffen Michels committed
3
import StdEnv
Tim Steenvoorden's avatar
Tim Steenvoorden committed
4 5 6 7
import Data.Func
import Data.Functor
import iTasks
import iTasks.Extensions.Editors.DynamicEditor
8

Tim Steenvoorden's avatar
Tim Steenvoorden committed
9

10
// Helpers /////////////////////////////////////////////////////////////////////
Tim Steenvoorden's avatar
Tim Steenvoorden committed
11

Tim Steenvoorden's avatar
Tim Steenvoorden committed
12
:: List a :== [a]
13 14
:: Message :== String
:: Button :== String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
15

16 17
cons x xs :== [x:xs]

18 19 20 21
(>?>) infixl 1 :: (Task a) (List ( Button, a -> Bool, a -> Task b )) -> Task b | iTask a & iTask b
(>?>) task options = task >>* map trans options
where
  trans ( a, p, t ) = OnAction (Action a) (ifValue p t)
Tim Steenvoorden's avatar
Tim Steenvoorden committed
22

23

24 25 26 27 28 29 30
// Main ////////////////////////////////////////////////////////////////////////

Start world = doTasks (editTaskExpr Nothing) world

editTaskExpr :: (Maybe (DynamicEditorValue TaskExpr)) -> Task (Maybe (DynamicEditorValue TaskExpr))
editTaskExpr mv =
  enterOrUpdateExpr ("Contruct a task", info1) mv >?>
31 32 33
    [ ( "Run", const True, \v -> viewInformation ("Evaluate the task", info2) [] () ||- (evalTaskExpr (toValue taskEditor v) <<@ ApplyLayout frameCompact) >>*
        [ OnAction (Action "Back") (always (editTaskExpr (Just v)))
        , OnAction (Action "Finish") (ifValue (const True) (\r -> viewInformation ("Done!", info3) [] r >?>
Tim Steenvoorden's avatar
Tim Steenvoorden committed
34
            [ ( "Back", const True, \_ -> editTaskExpr (Just v) ) ]
35
          ))
36 37 38
        ]
      )
    ]
Tim Steenvoorden's avatar
Tim Steenvoorden committed
39
where
Tim Steenvoorden's avatar
Tim Steenvoorden committed
40
  info1 :: String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
41
  info1 = "Select the editors and combinators you'd like to use. When you're ready, push the 'Continue' button below to run your program."
Tim Steenvoorden's avatar
Tim Steenvoorden committed
42
  info2 :: String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
43
  info2 = "Now step through the task you just created to test it."
Tim Steenvoorden's avatar
Tim Steenvoorden committed
44
  info3 :: String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
45
  info3 = "The program is done, the result is given below."
46

47
  enterOrUpdateExpr msg Nothing = enterInformation msg [EnterUsing id $ dynamicEditor taskEditor]
48
  enterOrUpdateExpr msg (Just v) = updateInformation msg [UpdateUsing id (curry snd) (dynamicEditor taskEditor)] v
49

50

Tim Steenvoorden's avatar
Tim Steenvoorden committed
51 52
// Data ////////////////////////////////////////////////////////////////////////

Tim Steenvoorden's avatar
Tim Steenvoorden committed
53
:: TaskExpr
Tim Steenvoorden's avatar
Tim Steenvoorden committed
54
  = Done Expr
55
  | EnterInfo Ty String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
56 57 58
  | Then TaskExpr TaskFunc
  | Both TaskExpr TaskExpr
  | Any TaskExpr TaskExpr
Tim Steenvoorden's avatar
Tim Steenvoorden committed
59
  | One Button TaskExpr Button TaskExpr
60 61 62
  // | Init Ty TaskExpr
  | Watch String
  // | Change String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
63
  | Forever TaskExpr
64

Tim Steenvoorden's avatar
Tim Steenvoorden committed
65
:: TaskFunc
Tim Steenvoorden's avatar
Tim Steenvoorden committed
66 67
  = ThenF TaskFunc TaskFunc
  | ViewF String Func
Tim Steenvoorden's avatar
Tim Steenvoorden committed
68
  | UpdateF String Func
69 70
  | StoreF
  | WatchF String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
71

Tim Steenvoorden's avatar
Tim Steenvoorden committed
72 73 74 75 76
:: Expr
  = Int Int
  | Bool Bool
  | String String
  | Tuple Expr Expr
Tim Steenvoorden's avatar
Tim Steenvoorden committed
77
  | Apply Func Expr
Tim Steenvoorden's avatar
Tim Steenvoorden committed
78

Tim Steenvoorden's avatar
Tim Steenvoorden committed
79
:: Func
Tim Steenvoorden's avatar
Tim Steenvoorden committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  = Identity
  | Conj Expr
  | Disj Expr
  | Not
  | Gt Expr
  | Ge Expr
  | Eq Expr
  | Le Expr
  | Lt Expr
  | Add Expr
  | Sub Expr
  | Mul Expr
  | Div Expr
  | Fst
  | Snd
Tim Steenvoorden's avatar
Tim Steenvoorden committed
95

Tim Steenvoorden's avatar
Tim Steenvoorden committed
96
:: Value
97 98
  = VUnit
  | VInt Int
Tim Steenvoorden's avatar
Tim Steenvoorden committed
99 100 101 102
  | VBool Bool
  | VString String
  | VTuple Value Value

Tim Steenvoorden's avatar
Tim Steenvoorden committed
103 104 105 106 107
:: Ty
  = E.a: Ty (a -> Value) & iTask a

:: Typed a b
  =: Typed a
108

Tim Steenvoorden's avatar
Tim Steenvoorden committed
109
derive class iTask TaskExpr, TaskFunc, Expr, Func, Value, Typed
110

Tim Steenvoorden's avatar
Tim Steenvoorden committed
111 112
// These instances cannot be auto derived because of the existential quantifier.
// However, they will be never used, so we make them undefined.
113 114 115 116 117 118
gDefault{|Ty|} = abort "Typed task editor: internal error with gDefault of Ty"
gEq{|Ty|} _ _ = abort "Typed task editor: internal error with gEq of Ty"
JSONEncode{|Ty|} _ _ = abort "Typed task editor: internal error with JSONEncode of Ty"
JSONDecode{|Ty|} _ _ = abort "Typed task editor: internal error with JSONDecode of Ty"
gText{|Ty|} _ _ = abort "Typed task editor: internal error with gText of Ty"
gEditor{|Ty|} = abort "Typed task editor: internal error with gEditor of Ty"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
119

120

Tim Steenvoorden's avatar
Tim Steenvoorden committed
121 122
// Editor //////////////////////////////////////////////////////////////////////

Tim Steenvoorden's avatar
Tim Steenvoorden committed
123
taskEditor :: DynamicEditor TaskExpr
Tim Steenvoorden's avatar
Tim Steenvoorden committed
124 125 126 127 128 129 130 131 132 133 134 135
taskEditor = DynamicEditor
  [ // This cons is used to provide untyped `TaskExpr` values.
    DynamicCons
      $ functionConsDyn "TaskExpr" "(enter task)" (dynamic \(Typed taskExpr) -> taskExpr ::  A.a: (Typed TaskExpr a) -> TaskExpr)
      <<@@@ HideIfOnlyChoice
  , DynamicConsGroup "Combinators"
      [ functionConsDyn "Then" "sequence"
          ( dynamic \(Typed task) (Typed taskFunc) -> Typed (Then task taskFunc) ::
              A.a b:
              (Typed TaskExpr (Task a)) (Typed TaskFunc (a -> Task b))
              -> Typed TaskExpr (Task b)
          )
136
          <<@@@ applyVerticalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
137 138 139 140 141 142
      , functionConsDyn "ThenF" "sequence"
          ( dynamic \(Typed taskFunc1) (Typed taskFunc2) -> Typed (ThenF taskFunc1 taskFunc2) ::
              A.a b c:
              (Typed TaskFunc (a -> Task b)) (Typed TaskFunc (b -> Task c))
              -> Typed TaskFunc (a -> Task c)
          )
143
          <<@@@ applyVerticalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
144 145 146 147 148 149 150
      , functionConsDyn "Both" "both"
          ( dynamic \(Typed task1) (Typed task2) -> Typed (Both task1 task2) ::
              A.a b:
              (Typed TaskExpr (Task a))
              (Typed TaskExpr (Task b))
              -> Typed TaskExpr (Task (a, b))
          )
151
          <<@@@ applyVerticalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
152 153 154 155 156 157 158
      , functionConsDyn "Any" "any of"
          ( dynamic \(Typed task1) (Typed task2) -> Typed (Any task1 task2) ::
              A.a b:
              (Typed TaskExpr (Task a))
              (Typed TaskExpr (Task a))
              -> Typed TaskExpr (Task a)
          )
159
          <<@@@ applyVerticalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
160
      , functionConsDyn "One" "one of"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
161
          ( dynamic \button1 (Typed task1) button2 (Typed task2) -> Typed (One button1 task1 button2 task2) ::
Tim Steenvoorden's avatar
Tim Steenvoorden committed
162
              A.a b:
Tim Steenvoorden's avatar
Tim Steenvoorden committed
163
              String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
164
              (Typed TaskExpr (Task a))
Tim Steenvoorden's avatar
Tim Steenvoorden committed
165
              String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
166 167 168
              (Typed TaskExpr (Task a))
              -> Typed TaskExpr (Task a)
          )
169
          <<@@@ applyVerticalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
170 171 172 173 174 175 176 177 178
      // , functionConsDyn "When" "guarded sequence"
      //     ( dynamic \(Typed task1) (Typed steps) -> Typed (When task1 steps) ::
      //       // Typed (When task1 [(expr, pred, tfExpr) \\ (Typed expr, pred, Typed tfExpr) <- steps]) ::
      //         A.a b:
      //         (Typed TaskExpr (Task a))
      //         (Typed (List TaskContExpr) (a -> Task b))
      //         // (Typed (List (Typed Func (a -> Bool), String, Typed TaskFunc (a -> Task a))) (a -> Task b))
      //         -> Typed TaskExpr (Task b)
      //     )
179
      //     <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
      // , listConsDyn "List TaskContExpr" "continuations"
      //     ( dynamic \typedSteps -> Typed ((\(Typed expr) -> expr) <$> typedSteps) ::
      //         A.a b:
      //         (List (Typed TaskContExpr (a -> Task b)))
      //         -> Typed (List TaskContExpr) (a -> Task b)
      //     )
      //     <<@@@ HideIfOnlyChoice
      // , functionConsDyn "TaskContExpr" "continuation"
      //     ( dynamic \s (Typed func) (Typed taskFunc) -> Typed {name = s, pred = func, cont = taskFunc} ::
      //         A.a b:
      //         String
      //         (Typed Func a)
      //         (Typed TaskFunc (a -> Task b))
      //         -> Typed TaskContExpr (a -> Task b)
      //     )
      //     <<@@@ HideIfOnlyChoice
      //     <<@@@ AddLabels [Just "name", Just "predicate", Just "continuation"]
      ]
  , DynamicConsGroup "Editors"
      [ functionConsDyn "Enter" "enter"
200
          ( dynamic \(Typed ty) s -> Typed (EnterInfo ty s) ::
Tim Steenvoorden's avatar
Tim Steenvoorden committed
201
              A.a:
Tim Steenvoorden's avatar
Tim Steenvoorden committed
202
              (Typed Ty a)
203
              String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
204 205
              -> Typed TaskExpr (Task a)
          )
206
          <<@@@ applyHorizontalBoxedLayout
207
          <<@@@ AddLabels [Nothing, Just "message"]
Tim Steenvoorden's avatar
Tim Steenvoorden committed
208
      , functionConsDyn "ViewF" "view"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
209 210
          ( dynamic \s (Typed func) -> Typed (ViewF s func) ::
              A.a b:
Tim Steenvoorden's avatar
Tim Steenvoorden committed
211
              String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
212 213
              (Typed Func (a -> b))
              -> Typed TaskFunc (a -> Task b)
Tim Steenvoorden's avatar
Tim Steenvoorden committed
214
          )
215
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
216
          <<@@@ AddLabels [Just "message"]
Tim Steenvoorden's avatar
Tim Steenvoorden committed
217
      , functionConsDyn "UpdateF" "update"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
218 219
          ( dynamic \s (Typed func) -> Typed (UpdateF s func) ::
              A.a b:
Tim Steenvoorden's avatar
Tim Steenvoorden committed
220
              String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
221 222
              (Typed Func (a -> b))
              -> Typed TaskFunc (a -> Task b)
Tim Steenvoorden's avatar
Tim Steenvoorden committed
223
          )
224
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
225
          <<@@@ AddLabels [ Just "message" ]
Tim Steenvoorden's avatar
Tim Steenvoorden committed
226
      , functionConsDyn "Done" "done"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
227 228 229 230 231
          ( dynamic \(Typed expr) -> Typed (Done expr) ::
              A.a:
              (Typed Expr a)
              -> Typed TaskExpr (Task a)
          )
232
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
233
      ]
Tim Steenvoorden's avatar
Tim Steenvoorden committed
234
  // Task expressions and functions on shares
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
  , DynamicConsGroup "Shares"
      // [ functionConsDyn "Init" "initialise"
      //     ( dynamic \(Typed sharedTy) (Typed taskExpr) -> Typed (Init sharedTy taskExpr) ::
      //         A.s a:
      //         (Typed Ty s)
      //         (Typed TaskExpr (Task a))
      //         -> Typed TaskExpr (Task a)
      //     )
      //     <<@@@ applyVerticalBoxedLayout
      [ functionConsDyn "StoreF" "store"
          (dynamic Typed StoreF :: Typed TaskFunc (Int -> Task ()))
          <<@@@ applyHorizontalBoxedLayout
          <<@@@ AddLabels [ Just "message" ]
      , functionConsDyn "Watch" "watch"
          ( dynamic \msg -> Typed (Watch msg) ::
              A.a:
              String
              -> Typed TaskExpr (Task ())
          )
          <<@@@ applyHorizontalBoxedLayout
          <<@@@ AddLabels [ Just "message" ]
      , functionConsDyn "WatchF" "watch"
          ( dynamic \msg -> Typed (WatchF msg) ::
              A.a:
              String
              -> Typed TaskFunc (a -> Task ())
          )
          <<@@@ applyHorizontalBoxedLayout
          <<@@@ AddLabels [ Just "message" ]
      ]
Tim Steenvoorden's avatar
Tim Steenvoorden committed
265 266 267 268 269 270 271 272 273
  , DynamicConsGroup "Special"
      [ functionConsDyn "Forever" "repeat forever"
          ( dynamic \(Typed taskExpr) -> Typed (Forever taskExpr) ::
              A.a:
              (Typed TaskExpr (Task a))
              -> Typed TaskExpr (Task a)
          )
          <<@@@ applyVerticalBoxedLayout
      ]
274
  // Non-task functions:
Tim Steenvoorden's avatar
Tim Steenvoorden committed
275
  , DynamicConsGroup "Basics"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
276 277
      [ functionConsDyn "Identity" "this value"
          (dynamic Typed Identity :: A.a: Typed Func (a -> a))
278
          <<@@@ applyHorizontalLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
279 280 281 282 283 284 285
      , functionConsDyn "Apply" "apply"
          ( dynamic \(Typed func) (Typed expr) ->
            Typed (Apply func expr) ::
              A.a b:
              (Typed Func (a -> b))
              (Typed Expr a)
              -> Typed Expr b
Tim Steenvoorden's avatar
Tim Steenvoorden committed
286
          )
287
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
288 289
          <<@@@ AddLabels [ Just "the function", Just "to" ]
      , functionConsDyn "Fst" "first element"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
290
          (dynamic Typed Fst :: A.a b: Typed Func ((a, b) -> a))
291
          <<@@@ applyHorizontalLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
292
      , functionConsDyn "Snd" "second element"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
293
          (dynamic Typed Snd :: A.a b: Typed Func ((a, b) -> b))
294
          <<@@@ applyHorizontalLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
295
      ]
Tim Steenvoorden's avatar
Tim Steenvoorden committed
296
  , DynamicConsGroup "Arithmetic"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
297
      [ functionConsDyn "Add" "add"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
298
          (dynamic \(Typed i) -> Typed (Add i) :: (Typed Expr Int) -> Typed Func (Int -> Int))
299
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
300
      , functionConsDyn "Sub" "subtract"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
301
          (dynamic \(Typed i) -> Typed (Sub i) :: (Typed Expr Int) -> Typed Func (Int -> Int))
302
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
303
      , functionConsDyn "Mul" "multiply with"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
304
          (dynamic \(Typed i) -> Typed (Mul i) :: (Typed Expr Int) -> Typed Func (Int -> Int))
305
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
306
      , functionConsDyn "Div" "divide by"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
307
          (dynamic \(Typed i) -> Typed (Div i) :: (Typed Expr Int) -> Typed Func (Int -> Int))
308
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
309 310
      ]
  , DynamicConsGroup "Logic"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
311 312
      [ functionConsDyn "Conj" "and"
          (dynamic \(Typed b) -> Typed (Conj b) :: (Typed Expr Bool) -> Typed Func (Bool -> Bool))
313
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
314 315
      , functionConsDyn "Disj" "or"
          (dynamic \(Typed b) -> Typed (Disj b) :: (Typed Expr Bool) -> Typed Func (Bool -> Bool))
316
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
317
      , functionConsDyn "Not" "negate"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
318
          (dynamic Typed Not :: Typed Func (Bool -> Bool))
319
          <<@@@ applyHorizontalLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
320
      ]
Tim Steenvoorden's avatar
Tim Steenvoorden committed
321
  , DynamicConsGroup "Comparison"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
322
      [ functionConsDyn "Gt" "is greater than"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
323
          (dynamic \(Typed i) -> Typed (Gt i) :: (Typed Expr Int) -> Typed Func (Int -> Int))
324
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
325
      , functionConsDyn "Ge" "is greater or equal"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
326
          (dynamic \(Typed i) -> Typed (Ge i) :: (Typed Expr Int) -> Typed Func (Int -> Int))
327
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
328
      , functionConsDyn "Eq" "is equal to"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
329
          (dynamic \(Typed i) -> Typed (Eq i) :: (Typed Expr Int) -> Typed Func (Int -> Int))
330
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
331
      , functionConsDyn "Le" "is lesser than"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
332
          (dynamic \(Typed i) -> Typed (Le i) :: (Typed Expr Int) -> Typed Func (Int -> Int))
333
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
334
      , functionConsDyn "Lt" "is lesser than"
Tim Steenvoorden's avatar
Tim Steenvoorden committed
335
          (dynamic \(Typed i) -> Typed (Lt i) :: (Typed Expr Int) -> Typed Func (Int -> Int))
336
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
337 338 339 340 341
      ]
  // Non-task expressions:
  , DynamicConsGroup "Values"
      [ functionConsDyn "Int" "the integer"
          (dynamic \i -> Typed (Int i) :: Int -> Typed Expr Int)
342
          <<@@@ applyHorizontalLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
343 344
      , functionConsDyn "Bool" "the boolean"
          (dynamic \b -> Typed (Bool b) :: Bool -> Typed Expr Bool)
345
          <<@@@ applyHorizontalLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
346 347
      , functionConsDyn "String" "the string"
          (dynamic \s -> Typed (String s) :: String -> Typed Expr String)
348
          <<@@@ applyHorizontalLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
349 350 351 352 353 354
      , functionConsDyn "Tuple" "the tuple"
          ( dynamic \(Typed a) (Typed b) ->
            Typed (Tuple a b) ::
              A.a b:
                (Typed Expr a) (Typed Expr b) -> Typed Expr (a, b)
          )
355
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
356
          <<@@@ AddLabels [ Just "with", Just "and" ]
Tim Steenvoorden's avatar
Tim Steenvoorden committed
357
    ]
Tim Steenvoorden's avatar
Tim Steenvoorden committed
358 359 360 361
  // Types
  , DynamicConsGroup "Types"
      [ functionConsDyn "Ty.Int" "Int"
          (dynamic Typed (Ty VInt) :: Typed Ty Int)
362
          <<@@@ applyHorizontalLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
363 364
      , functionConsDyn "Ty.Bool" "Bool"
          (dynamic Typed (Ty VBool) :: Typed Ty Bool)
365
          <<@@@ applyHorizontalLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
366 367
      , functionConsDyn "Ty.String" "String"
          (dynamic Typed (Ty VString) :: Typed Ty String)
368
          <<@@@ applyHorizontalLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
369 370 371 372 373
      , functionConsDyn "Ty.Tuple" "Tuple"
          ( dynamic \(Typed (Ty toValue1)) (Typed (Ty toValue2)) -> Typed (Ty \(x, y) -> VTuple (toValue1 x) (toValue2 y)) ::
              A.a b:
              (Typed Ty a) (Typed Ty b) -> Typed Ty (a, b)
          )
374
          <<@@@ applyHorizontalBoxedLayout
Tim Steenvoorden's avatar
Tim Steenvoorden committed
375 376 377 378 379 380 381 382
      ]
  // Internal helper editors
  , DynamicConsGroup "Helpers"
    [ customEditorCons "int" "(enter integer)" intEditor <<@@@ HideIfOnlyChoice
    , customEditorCons "bool" "(enter boolean)" boolEditor <<@@@ HideIfOnlyChoice
    , customEditorCons "string" "(enter string )" stringEditor <<@@@ HideIfOnlyChoice
    ]
  ]
Tim Steenvoorden's avatar
Tim Steenvoorden committed
383
where
Tim Steenvoorden's avatar
Tim Steenvoorden committed
384 385
  intEditor :: Editor Int
  intEditor = gEditor{|*|}
Tim Steenvoorden's avatar
Tim Steenvoorden committed
386

Tim Steenvoorden's avatar
Tim Steenvoorden committed
387 388
  boolEditor :: Editor Bool
  boolEditor = gEditor{|*|}
Tim Steenvoorden's avatar
Tim Steenvoorden committed
389

Tim Steenvoorden's avatar
Tim Steenvoorden committed
390 391
  stringEditor :: Editor String
  stringEditor = gEditor{|*|}
392

393 394 395 396 397 398 399 400 401
  basicClasses = [ "typedtasks-base", "itasks-wrap-width", "itasks-wrap-height" ]
  horizontalClasses = [ "typedtasks-horizontal", "itasks-horizontal" ]
  verticalClasses = [ "typedtasks-vertical", "itasks-vertical" ]
  boxedClasses = [ "typedtasks-boxed" ]

  applyHorizontalBoxedLayout = ApplyCssClasses $ basicClasses ++ horizontalClasses ++ boxedClasses
  applyVerticalBoxedLayout = ApplyCssClasses $ basicClasses ++ verticalClasses ++ boxedClasses
  applyHorizontalLayout = ApplyCssClasses $ basicClasses ++ horizontalClasses
  applyVerticalLayout = ApplyCssClasses $ basicClasses ++ verticalClasses
402

Tim Steenvoorden's avatar
Tim Steenvoorden committed
403

Tim Steenvoorden's avatar
Tim Steenvoorden committed
404 405
// Evaluation //////////////////////////////////////////////////////////////////

406 407 408 409 410
// globalValueShare :: SimpleSDSLens ( Ty, List Value )
// globalValueShare = sharedStore "global share for typed task editor" ( abort "Global share not initialised", [] )
globalValueShare :: SimpleSDSLens (List Value)
globalValueShare = sharedStore "global share for typed task editor" []

Tim Steenvoorden's avatar
Tim Steenvoorden committed
411 412
evalTaskExpr :: TaskExpr -> Task Value
evalTaskExpr (Done expr) = return $ evalExpr expr
413
evalTaskExpr (EnterInfo (Ty toValue) msg) = enterInformation msg [] @ toValue
Tim Steenvoorden's avatar
Tim Steenvoorden committed
414
evalTaskExpr (Then task taskFunc) = evalTaskExpr task >>= evalTaskFunc taskFunc
Tim Steenvoorden's avatar
cleanup  
Tim Steenvoorden committed
415
evalTaskExpr (Both task1 task2) = (evalTaskExpr task1 -&&- evalTaskExpr task2) <<@ ApplyLayout arrangeHorizontal @ \(a, b) -> VTuple a b
Tim Steenvoorden's avatar
Tim Steenvoorden committed
416
evalTaskExpr (Any task1 task2) = (evalTaskExpr task1 -||- evalTaskExpr task2) <<@ ApplyLayout arrangeHorizontal
417
evalTaskExpr (One button1 task1 button2 task2) = viewInformation "Make a choice" [] () >?>
Tim Steenvoorden's avatar
Tim Steenvoorden committed
418 419
  [ ( button1, const True, \_ -> evalTaskExpr task1 )
  , ( button2, const True, \_ -> evalTaskExpr task2 )
Tim Steenvoorden's avatar
Tim Steenvoorden committed
420
  ]
421 422
// evalTaskExpr (Init sharedTy task) = set ( sharedTy, [] ) globalValueShare >>| evalTaskExpr task
evalTaskExpr (Watch msg) = viewSharedInformation msg [] globalValueShare @ (const VUnit)
Tim Steenvoorden's avatar
Tim Steenvoorden committed
423
evalTaskExpr (Forever task) = forever (evalTaskExpr task)
Tim Steenvoorden's avatar
Tim Steenvoorden committed
424

Tim Steenvoorden's avatar
Tim Steenvoorden committed
425 426 427 428
// evalTaskExpr (When task1 options) = evalTaskExpr task1
//   >>* [ OnAction (Action name) (ifValue (test pred) (evalTaskFunc cont))
//       \\ {name, pred, cont} <- options
//       ]
Tim Steenvoorden's avatar
cleanup  
Tim Steenvoorden committed
429 430 431 432 433 434 435 436 437
// where
//   test pred (VInt i) = case pred of
//     Lt (VInt j) -> i < j
//     Gt (VInt j) -> i > j
//     Eq (VInt j) -> i == j
//   test pred (VBool i) = case pred of
//     Eq (VBool j) -> i == j
//     Lt (VBool j) -> False
//     Gt (VBool j) -> False
Tim Steenvoorden's avatar
Tim Steenvoorden committed
438 439 440


evalTaskFunc :: TaskFunc Value -> Task Value
441 442
evalTaskFunc (ThenF this next) val =
  evalTaskFunc this val >>= evalTaskFunc next
Tim Steenvoorden's avatar
cleanup  
Tim Steenvoorden committed
443

Tim Steenvoorden's avatar
Tim Steenvoorden committed
444
evalTaskFunc (ViewF msg func) val = case evalFunc val func of
Tim Steenvoorden's avatar
Tim Steenvoorden committed
445 446 447 448 449
  (VInt i) -> (viewInformation msg [] i @ VInt) <<@ ApplyLayout arrangeHorizontal
  (VBool b) -> (viewInformation msg [] b @ VBool) <<@ ApplyLayout arrangeHorizontal
  (VString s) -> (viewInformation msg [] s @ VString) <<@ ApplyLayout arrangeHorizontal
  (VTuple a b) ->
    ( viewInformation msg [] ()
Tim Steenvoorden's avatar
Tim Steenvoorden committed
450
      ||- (evalTaskFunc (ViewF "" Identity) a -&&- evalTaskFunc (ViewF "" Identity) b)
Tim Steenvoorden's avatar
Tim Steenvoorden committed
451 452 453
      @ \(a, b) -> VTuple a b
    )
      <<@ ApplyLayout arrangeHorizontal
Tim Steenvoorden's avatar
cleanup  
Tim Steenvoorden committed
454

Tim Steenvoorden's avatar
Tim Steenvoorden committed
455
evalTaskFunc (UpdateF msg func) val = case evalFunc val func of
Tim Steenvoorden's avatar
Tim Steenvoorden committed
456 457 458 459 460
  (VInt i) -> (updateInformation msg [] i @ VInt) <<@ ApplyLayout arrangeHorizontal
  (VBool b) -> (updateInformation msg [] b @ VBool) <<@ ApplyLayout arrangeHorizontal
  (VString s) -> (updateInformation msg [] s @ VString) <<@ ApplyLayout arrangeHorizontal
  (VTuple a b) ->
    ( viewInformation msg [] ()
Tim Steenvoorden's avatar
Tim Steenvoorden committed
461
      ||- (evalTaskFunc (UpdateF "" Identity) a -&&- evalTaskFunc (UpdateF "" Identity) b)
Tim Steenvoorden's avatar
Tim Steenvoorden committed
462 463 464 465
      @ \(a, b) -> VTuple a b
    )
      <<@ ApplyLayout arrangeHorizontal

466 467 468 469 470 471 472 473
evalTaskFunc (StoreF) val =
  // upd (\( sharedTy, values ) -> ( sharedTy, cons val values)) globalValueShare @ (const VUnit)
  upd (cons val) globalValueShare @ (const VUnit)

evalTaskFunc (WatchF msg) val =
  viewSharedInformation msg [] globalValueShare @ (const VUnit)


Tim Steenvoorden's avatar
Tim Steenvoorden committed
474

475
evalExpr :: Expr -> Value
Tim Steenvoorden's avatar
Tim Steenvoorden committed
476 477 478
evalExpr (Int i) = VInt i
evalExpr (Bool b) = VBool b
evalExpr (String s) = VString s
479
evalExpr (Tuple fstExpr sndExpr) = VTuple (evalExpr fstExpr) (evalExpr sndExpr)
Tim Steenvoorden's avatar
Tim Steenvoorden committed
480
evalExpr (Apply func expr) = evalFunc (evalExpr expr) func
Tim Steenvoorden's avatar
Tim Steenvoorden committed
481 482 483


evalFunc :: Value Func -> Value
Tim Steenvoorden's avatar
Tim Steenvoorden committed
484
evalFunc val Identity = val
Tim Steenvoorden's avatar
Tim Steenvoorden committed
485

Tim Steenvoorden's avatar
Tim Steenvoorden committed
486
evalFunc (VInt i1) func = case func of
Tim Steenvoorden's avatar
Tim Steenvoorden committed
487 488 489 490 491 492 493 494 495
  (Gt expr) -> VBool $ i1 > evalInt expr
  (Ge expr) -> VBool $ i1 >= evalInt expr
  (Eq expr) -> VBool $ i1 == evalInt expr
  (Le expr) -> VBool $ i1 <= evalInt expr
  (Lt expr) -> VBool $ i1 < evalInt expr
  (Add expr) -> VInt $ i1 + evalInt expr
  (Sub expr) -> VInt $ i1 - evalInt expr
  (Mul expr) -> VInt $ i1 * evalInt expr
  (Div expr) -> VInt $ i1 / evalInt expr
Tim Steenvoorden's avatar
Tim Steenvoorden committed
496 497 498 499 500
where
  evalInt :: Expr -> Int
  evalInt expr = case evalExpr expr of
    (VInt i) -> i

Tim Steenvoorden's avatar
Tim Steenvoorden committed
501
evalFunc (VBool b1) func = case func of
Tim Steenvoorden's avatar
Tim Steenvoorden committed
502 503 504 505
  (Eq expr) -> VBool $ b1 == evalBool expr
  (Conj expr) -> VBool $ b1 && evalBool expr
  (Disj expr) -> VBool $ b1 || evalBool expr
  (Not) -> VBool $ not b1
Tim Steenvoorden's avatar
Tim Steenvoorden committed
506 507 508 509 510
where
  evalBool :: Expr -> Bool
  evalBool expr = case evalExpr expr of
    (VBool b) -> b

Tim Steenvoorden's avatar
Tim Steenvoorden committed
511
evalFunc (VString s1) func = case func of
Tim Steenvoorden's avatar
Tim Steenvoorden committed
512
  (Eq expr) -> VBool $ s1 == evalString expr
Tim Steenvoorden's avatar
Tim Steenvoorden committed
513 514 515 516
where
  evalString :: Expr -> String
  evalString expr = case evalExpr expr of
    (VString s) -> s
Tim Steenvoorden's avatar
Tim Steenvoorden committed
517 518 519 520

evalFunc (VTuple x1 x2) func = case func of
  Fst -> x1
  Snd -> x2