DynEditorExample.icl 19.7 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) [] ()
            ||- (set [] globalValueShare >>| evalTaskExpr (toValue taskEditor v) <<@ ApplyLayout frameCompact) >>*
34
        [ OnAction (Action "Back") (always (editTaskExpr (Just v)))
35
        , OnAction (Action "Finish") (ifValue (const True) (\r -> viewInformation ("Done!", info3) [] (toString r) >?>
Tim Steenvoorden's avatar
Tim Steenvoorden committed
36
            [ ( "Back", const True, \_ -> editTaskExpr (Just v) ) ]
37
          ))
38 39 40
        ]
      )
    ]
Tim Steenvoorden's avatar
Tim Steenvoorden committed
41
where
Tim Steenvoorden's avatar
Tim Steenvoorden committed
42
  info1 :: String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
43
  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
44
  info2 :: String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
45
  info2 = "Now step through the task you just created to test it."
Tim Steenvoorden's avatar
Tim Steenvoorden committed
46
  info3 :: String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
47
  info3 = "The program is done, the result is given below."
48

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

52

Tim Steenvoorden's avatar
Tim Steenvoorden committed
53 54
// Data ////////////////////////////////////////////////////////////////////////

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

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

Tim Steenvoorden's avatar
Tim Steenvoorden committed
74 75 76 77
:: Expr
  = Int Int
  | Bool Bool
  | String String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
78
  | Pair Expr Expr
Tim Steenvoorden's avatar
Tim Steenvoorden committed
79
  | Apply Func Expr
Tim Steenvoorden's avatar
Tim Steenvoorden committed
80

Tim Steenvoorden's avatar
Tim Steenvoorden committed
81
:: Func
Tim Steenvoorden's avatar
Tim Steenvoorden committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  = 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
97

Tim Steenvoorden's avatar
Tim Steenvoorden committed
98
:: Value
99 100
  = VUnit
  | VInt Int
Tim Steenvoorden's avatar
Tim Steenvoorden committed
101 102
  | VBool Bool
  | VString String
Tim Steenvoorden's avatar
Tim Steenvoorden committed
103
  | VPair Value Value
Tim Steenvoorden's avatar
Tim Steenvoorden committed
104

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

:: Typed a b
  =: Typed a
110

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

Tim Steenvoorden's avatar
Tim Steenvoorden committed
113 114
// These instances cannot be auto derived because of the existential quantifier.
// However, they will be never used, so we make them undefined.
115 116 117 118 119 120
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
121

122

Tim Steenvoorden's avatar
Tim Steenvoorden committed
123 124
// Editor //////////////////////////////////////////////////////////////////////

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

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

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

Tim Steenvoorden's avatar
Tim Steenvoorden committed
394 395 396
  basicClasses = [ "typedtasks-base" ]
  horizontalClasses = [ "typedtasks-horizontal" ]
  verticalClasses = [ "typedtasks-vertical" ]
397 398 399 400 401 402
  boxedClasses = [ "typedtasks-boxed" ]

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

Tim Steenvoorden's avatar
Tim Steenvoorden committed
404

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

407 408
// globalValueShare :: SimpleSDSLens ( Ty, List Value )
// globalValueShare = sharedStore "global share for typed task editor" ( abort "Global share not initialised", [] )
Tim Steenvoorden's avatar
Tim Steenvoorden committed
409
globalValueShare :: SimpleSDSLens (List Int)
410 411
globalValueShare = sharedStore "global share for typed task editor" []

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

Tim Steenvoorden's avatar
Tim Steenvoorden committed
426 427 428 429
// 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
430 431 432 433 434 435 436 437 438
// 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
439 440 441


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

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

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

Tim Steenvoorden's avatar
Tim Steenvoorden committed
467
evalTaskFunc (StoreF) (VInt i) =
468
  // upd (\( sharedTy, values ) -> ( sharedTy, cons val values)) globalValueShare @ (const VUnit)
Tim Steenvoorden's avatar
Tim Steenvoorden committed
469
  upd (cons i) globalValueShare @ (const VUnit)
470 471 472 473 474

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


Tim Steenvoorden's avatar
Tim Steenvoorden committed
475

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


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

Tim Steenvoorden's avatar
Tim Steenvoorden committed
487
evalFunc (VInt i1) func = case func of
Tim Steenvoorden's avatar
Tim Steenvoorden committed
488 489 490 491 492 493 494 495 496
  (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
497 498 499 500 501
where
  evalInt :: Expr -> Int
  evalInt expr = case evalExpr expr of
    (VInt i) -> i

Tim Steenvoorden's avatar
Tim Steenvoorden committed
502
evalFunc (VBool b1) func = case func of
Tim Steenvoorden's avatar
Tim Steenvoorden committed
503 504 505 506
  (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
507 508 509 510 511
where
  evalBool :: Expr -> Bool
  evalBool expr = case evalExpr expr of
    (VBool b) -> b

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

Tim Steenvoorden's avatar
Tim Steenvoorden committed
519
evalFunc (VPair x1 x2) func = case func of
Tim Steenvoorden's avatar
Tim Steenvoorden committed
520 521
  Fst -> x1
  Snd -> x2
522 523 524 525 526 527 528 529


instance toString Value where
  toString val = case val of
    VUnit -> "()"
    VInt i -> toString i
    VBool b -> toString b
    VString s -> toString s
Tim Steenvoorden's avatar
Tim Steenvoorden committed
530
    VPair x y -> "( " +++ toString x +++ ", " +++ toString y +++ " )"