2 | import Data.List.Elem
4 | import JS.Inheritance
15 | data Undefined : Type where [external]
19 | %foreign "javascript:lambda:()=>undefined"
20 | undefined : Undefined
27 | Show Undefined where
28 | show _ = "undefined"
30 | %foreign "javascript:lambda:x=>x === undefined?1:0"
31 | prim__isUndefined : AnyPtr -> Bool
35 | isUndefined : a -> Bool
36 | isUndefined v = prim__isUndefined (believe_me v)
39 | ToFFI Undefined Undefined where
43 | FromFFI Undefined Undefined where
47 | SafeCast Undefined where
48 | safeCast ptr = if isUndefined ptr then Just undefined else Nothing
55 | data UndefOr : Type -> Type where [external]
59 | undef = believe_me undefined
62 | def : a -> UndefOr a
66 | SafeCast a => SafeCast (UndefOr a) where
70 | else map def $
safeCast ptr
77 | data Optional : (a : Type) -> Type where
79 | Def : a -> Optional a
82 | Show a => Show (Optional a) where
83 | showPrec _ Undef = "Undef"
84 | showPrec p (Def a) = showCon p "Def" (showArg a)
87 | Eq a => Eq (Optional a) where
88 | Undef == Undef = True
89 | Def x == Def y = x == y
93 | Ord a => Ord (Optional a) where
94 | compare Undef Undef = EQ
95 | compare Undef (Def _) = LT
96 | compare (Def _) Undef = GT
97 | compare (Def x) (Def y) = compare x y
100 | Functor Optional where
101 | map _ Undef = Undef
102 | map f (Def x) = Def $
f x
105 | Applicative Optional where
107 | Def f <*> Def a = Def $
f a
111 | Monad Optional where
112 | Undef >>= _ = Undef
116 | Alternative Optional where
119 | Def v <|> _ = Def v
122 | Foldable Optional where
123 | foldr _ acc Undef = acc
124 | foldr f acc (Def x) = f x acc
126 | foldl _ acc Undef = acc
127 | foldl f acc (Def x) = f acc x
130 | null (Def _) = False
133 | Traversable Optional where
134 | traverse f Undef = pure Undef
135 | traverse f (Def x) = Def <$> f x
138 | optional : Lazy b -> (a -> b) -> Optional a -> b
139 | optional x f Undef = x
140 | optional _ f (Def y) = f y
143 | fromOptional : Lazy a -> Optional a -> a
144 | fromOptional a Undef = a
145 | fromOptional _ (Def a) = a
148 | optionalToUndefOr : Optional a -> UndefOr a
149 | optionalToUndefOr = optional undef def
153 | {auto 0 _ : JSType a}
155 | -> {auto 0 _ : Elem b (Types a)}
157 | optUp x = optional undef (\v => def $
up v) x
161 | {auto 0 _ : JSType a}
162 | -> Optional (Maybe a)
163 | -> {auto 0 _ : Elem b (Types a)}
164 | -> UndefOr (Nullable b)
165 | omyUp x = optionalToUndefOr $
map (\m => mayUp m) x
168 | maybeToOptional : Maybe a -> Optional a
169 | maybeToOptional = maybe Undef Def
172 | optionalToMaybe : Optional a -> Maybe a
173 | optionalToMaybe = optional Nothing Just
176 | undeforToOptional : UndefOr a -> Optional a
177 | undeforToOptional v = if isUndefined v then Undef else Def (believe_me v)
180 | undeforToMaybe : UndefOr a -> Maybe a
181 | undeforToMaybe v = if isUndefined v then Nothing else Just (believe_me v)
184 | ToFFI a b => ToFFI (Optional a) (UndefOr b) where
185 | toFFI = optionalToUndefOr . map toFFI
188 | FromFFI a b => FromFFI (Optional a) (UndefOr b) where
189 | fromFFI v = case undeforToOptional v of
190 | Undef => Just Undef
191 | Def x => map Def $
fromFFI x