DynEditorExample.icl 5.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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
module DynEditorExample

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
    realLit r = TypedExpr (RealLit r)