DynEditorExample.icl 8.78 KB
Newer Older
1 2
module DynEditorExample

Steffen Michels's avatar
Steffen Michels committed
3
import StdEnv
4
import Data.Func
5
import Text
6 7 8 9 10 11 12
import iTasks, iTasks.Extensions.Editors.DynamicEditor

Start world = doTasks editTask world

editTask =
	enterInformation () [EnterUsing id $ dynamicEditor taskEditor] >>= \expr ->
	case evalExpr $ toValue taskEditor expr of
13
		Val value = viewInformation () [] (toString value) @! ()
14 15 16 17

:: TaskExpr = ViewInformation | Apply TaskExpr Expr
:: Expr = Int Int | Bool Bool | Tuple Expr Expr | Fst Expr | Snd Expr | Eq Expr Expr
/**
18
 * Dynamically typed value with required dictionaries.
19
 */
20 21 22 23 24 25 26 27 28 29 30 31
:: Value = E.a: Val a & TC, ==, toString a

instance == Value where
	== (Val x) (Val y) = case dynamic ((==) x, y) of
		((equalsX, y) :: (a -> Bool, a)) = equalsX y

instance toString Value where
	toString (Val v) = toString v

instance toString (a, b) | toString a & toString b where
	toString (x, y) = concat ["(", toString x, ", ", toString y, ")"]

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
:: Typed a b =: Typed a

derive class iTask TaskExpr, Expr, Typed

taskEditor :: DynamicEditor x | TC x
taskEditor = DynamicEditor conses
where
	conses =
		[ // This cons is used to provide untyped `Expr` values.
		  DynamicCons $
			functionConsDyn "Expr" "(enter expression)" (dynamic \(Typed expr) -> expr :: A.a: (Typed Expr a) -> Expr)
		  <<@@@ HideIfOnlyChoice
		, DynamicConsGroup "Values"
			[ functionConsDyn "int"   "enter integer:"
				(dynamic \i -> Typed (Int i)  :: Int  -> Typed Expr Int)
			, functionConsDyn "bool"  "enter boolean:"
				(dynamic \b -> Typed (Bool b) :: Bool -> Typed Expr Bool)
			, functionConsDyn "tuple" "enter tuple:"
				(	dynamic \(Typed a) (Typed b) -> Typed (Tuple a b) ::
					A.a b: (Typed Expr a) (Typed Expr b) -> Typed Expr (a, b)
				)
			, functionConsDyn "fst"   "fst"
				(dynamic \(Typed (Tuple a _)) -> Typed a :: A.a b: (Typed Expr (a, b)) -> Typed Expr a)
			, functionConsDyn "snd"   "snd"
				(dynamic \(Typed (Tuple _ b)) -> Typed b :: A.a b: (Typed Expr (a, b)) -> Typed Expr b)
			, functionConsDyn "=="    "=="
				(	dynamic \(Typed a) (Typed b) -> Typed (Eq a b) ::
Steffen Michels's avatar
Steffen Michels committed
59
					A.a: (Typed Expr a) (Typed Expr a) -> Typed Expr Bool
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
				)
			, customEditorCons "Int"   "(enter integer)" intEditor  <<@@@ HideIfOnlyChoice
			, customEditorCons "Bool"  "(enter boolean)" boolEditor <<@@@ HideIfOnlyChoice
			]
		]

	intEditor :: Editor Int
	intEditor = gEditor{|*|}

	boolEditor :: Editor Bool
	boolEditor = gEditor{|*|}

//evalTaskExpr :: TaskExpr -> Task Dynamic
//evalTaskExpr ViewInformationk

evalExpr :: Expr -> Value
evalExpr (Int i)  = Val i
evalExpr (Bool b) = Val b
evalExpr (Tuple fstExpr sndExpr) = case (evalExpr fstExpr, evalExpr sndExpr) of
79 80 81 82 83 84 85
	(fstVal, sndVal) = Val (fstVal, sndVal)
evalExpr (Fst expr) = case evalExpr expr of
	Val tuple = case dynamic tuple of
		((x, _) :: (Value, Value)) = x
evalExpr (Snd expr) = case evalExpr expr of
	Val tuple = case dynamic tuple of
		((_, y) :: (Value, Value)) = y
86
evalExpr (Eq expr1 expr2) = case (evalExpr expr1, evalExpr expr2) of
87
	(Val value1, Val value2) = case dynamic ((==) value1, value2) of
88 89 90
		((equalsValue1, value2) :: (a -> Bool, a)) = Val $ equalsValue1 value2

/*
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 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
import Data.Func, Data.Functor, Data.Maybe
import iTasks, iTasks.UI.Editor.Modifiers
import iTasks.Extensions.Editors.DynamicEditor

:: TE  	= UpdEdit |  ViewEdit | Return //  EnterEdit 
		| Apply TE TE
		| Bind TE TE | Blind TE TE
		| Or TE TE | And TE TE 
		| I Int | B Bool | T (TE, TE) | Fst TE | Snd TE 

:: RTE a =: RTE TE
:: Tsk a =: Tsk a
:: Val a =: Val a

derive class iTask  TE, RTE, Tsk, Val 

tEditor :: DynamicEditor (RTE  a) | TC a
tEditor 
	= DynamicEditor ( 
	[ DynamicConsGroup "Editors"
		[ functionConsDyn "updEdit" "update"
		                ( dynamic RTE UpdEdit 
		                :: A.b: (RTE ((Val b) -> (Tsk (Val b))))
		                ) <<@@@ UseAsDefault
		, functionConsDyn "viewEdit" "view"
		                ( dynamic (RTE ViewEdit)
		                :: A.b : (RTE ((Val b) -> (Tsk (Val b))))
		                )
		, functionConsDyn "return"  "return"     
						( dynamic (RTE Return) 
						:: A.b : (RTE ((Val b) -> (Tsk (Val b))))
						) 
//			, functionConsDyn "enterEdit" "enter"
//			                ( dynamic (RTE EnterEdit)
//			                :: A.b : (RTE (Tsk (Val b)))
//			                ) <<@@@ UseAsDefault
		, functionConsDyn "apply" "editor:"
		                ( dynamic \(RTE f) (RTE v) = (RTE (Apply f v)) 
		                :: A.b c: (RTE ((Val b) -> (Tsk (Val c)))) (RTE (Val b)) -> (RTE (Tsk (Val c))) // crashes, type c is to general
						) <<@@@ LayoutVertical
		]
	] ++
	[ DynamicConsGroup "Sequencial Tasks"
		[ functionConsDyn "bind"  ">>="     
						( dynamic \(RTE  b) (RTE c) = RTE (Bind b c) 
						:: A.b c: (RTE (Tsk (Val b))) (RTE ((Val b) -> (Tsk (Val c)))) -> RTE (Tsk (Val c))
						) <<@@@ LayoutVertical
		, functionConsDyn "blind"  ">>|"     
						( dynamic \(RTE  b) (RTE c) = RTE (Blind b c) 
						:: A.b c: (RTE (Tsk (Val b))) (RTE (Tsk (Val c))) -> RTE (Tsk (Val c))
						)  <<@@@ LayoutVertical
		]
	] ++
	[ DynamicConsGroup "Parallel Tasks"
		[ functionConsDyn "or" "-||-"
		                ( dynamic \(RTE t1) (RTE t2) = RTE (Or t1 t2) 
		                :: A.b: (RTE (Tsk (Val b))) (RTE (Tsk (Val b))) -> (RTE (Tsk (Val b)))  
		                ) <<@@@ LayoutVertical
		, functionConsDyn "and" "-&&-"
		                ( dynamic \(RTE t1) (RTE t2) = RTE (And t1 t2) 
		                :: A.b c: (RTE (Tsk (Val b))) (RTE (Tsk (Val b))) -> RTE (Tsk (Val (b,c)))
		                ) <<@@@ LayoutVertical
		]
	] ++
	[ DynamicConsGroup "Functions"
		[ functionConsDyn "fst" "first"
		                ( dynamic \(RTE t)  = RTE (Fst t) 
		                :: A.b c: (RTE (Val (b,c))) -> (RTE (Val b))
		                )
		, functionConsDyn "snd" "second"
		                ( dynamic \(RTE t)  = RTE (Snd t) 
		                :: A.b c: (RTE (Val (b,c))) -> (RTE (Val c))
		                )
		]
	] ++
	[ DynamicConsGroup "Basic Types"
		[ functionConsDyn "int"  "integer" 	
						( dynamic (RTE (I 0)) 
						:: RTE (Val Int) 
						) <<@@@ UseAsDefault
		, functionConsDyn "bool" "bool"
						( dynamic (RTE (B False)) 
						:: RTE (Val Bool) 
						)
		, functionConsDyn "tuple" "tuple"
						( dynamic \(RTE t1) (RTE t2) = RTE (T (t1, t2))
						::  A.b c: (RTE (Val b)) (RTE (Val c)) -> (RTE (Val (b,c)))
						) <<@@@ LayoutVertical
		]
	])


interpret :: (RTE (Tsk (Val Int))) -> Task TE  // TO DO for all cases
interpret (RTE v) = interper v 
where
	interper :: TE -> Task TE
//		interper EnterEdit 
//			= enterInformation "enter value" []
	interper (Apply f v) 
		= case f of
			UpdEdit  -> updateInformation "update value" [] v
			ViewEdit -> viewInformation "view value" [] v
			Return	 -> return v
	interper (Bind t1 t2) 
		= interper t1 >>= \v -> interper (Apply t2 v)
	interper (Blind t1 t2) 
		= interper t1 >>| interper t2 
	interper (Or t1 t2) 
		= interper t1 -||- interper t2 
	interper (And t1 t2) 
		= (interper t1 -&&- interper t2) @ T
	interper (Blind t1 t2) 
		= interper t1 >>| interper t2 

	interper (I i)
		= return (I i)
	interper (B b)
		= return (B b)
	interper (T (a,b))
		= return (T (a,b))


Start world = doTasks enterTask world

//enterTask :: Task TE
enterTask 
	= 	(enterInformation     () [EnterUsing id $ dynamicEditor tEditor] 
	>&> viewSharedInformation () [ViewAs $ fmap $ toValue tEditor])
    >>= \val -> case val of
    			(Just te) -> interpret (toValue tEditor te) >>= viewInformation "result = " [] 



// non-typesafe expression
:: Expr = IntLit Int | RealLit Real | Plus Expr Expr | ToInt Expr | ToReal Expr | Eq Expr Expr

// expression with phantom type
:: TypedExpr a =: TypedExpr Expr

derive class iTask Expr, TypedExpr

dslEditor :: DynamicEditor (TypedExpr a)
dslEditor = DynamicEditor  
    [ DynamicConsGroup "Fixed"
      [ functionConsDyn "plus" "plus"
                        ( dynamic \(TypedExpr x) (TypedExpr y) -> TypedExpr (Plus x y) ::
                          A.b: (TypedExpr b) (TypedExpr b) -> TypedExpr b
                        )
      , functionCons "toInt"  "to integer" toIntExpr
      , functionCons "toReal" "to decimal" toRealExpr
      , customEditorCons "int"  "(enter integer)"
                         (bijectEditorValue (\(TypedExpr (IntLit i)) -> i)  intLit  gEditor{|*|})
      , customEditorCons "real" "(enter decimal)"
                         (bijectEditorValue (\(TypedExpr (RealLit r)) -> r) realLit gEditor{|*|})
      , functionConsDyn "eq" "are equal"
                        ( dynamic \(TypedExpr x) (TypedExpr y) -> TypedExpr (Eq x y) ::
                          A.b: (TypedExpr b) (TypedExpr b) -> TypedExpr Bool
                        )
      ]
    ]
where
    toIntExpr :: (TypedExpr Real) -> TypedExpr Int
    toIntExpr (TypedExpr x) = TypedExpr (ToInt x)

    toRealExpr :: (TypedExpr Int) -> TypedExpr Real
    toRealExpr (TypedExpr x) = TypedExpr (ToReal x)

    intLit :: Int -> TypedExpr Int
    intLit i = TypedExpr (IntLit i)

    realLit :: Real -> TypedExpr Real
262
    realLit r = TypedExpr (RealLit r)*/