]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/Prelude.scl
Add more Throwable & Exception handling functionality to Prelude
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Prelude.scl
index 8fcd3ced191d6f2ebbcc9c595021206e5b5e684f..eb8026de910e15f2b8900c2e0fce6a6d6a0d0d6e 100644 (file)
@@ -1081,12 +1081,12 @@ replicate n v = build (\empty cons ->
 
 /// FunctorM ///
 
-class (Functor f) => FunctorM f where
+class (FunctorE f) => FunctorM f where
     "`mapM f` is equivalent to `sequence . map f`."
-    mapM :: Monad m => (a -> m b) -> f a -> m (f b)
+    mapM :: Monad m => (a -> <e> m b) -> f a -> <e> m (f b)
     "Evaluate each action in the sequence from left to right, and collect the results."
     sequence :: Monad m => f (m a) -> m (f a) 
-    mapM f l = sequence (fmap f l)
+    mapM f l = sequence (map f l)
 
 /// MonadE ///
 
@@ -1484,6 +1484,14 @@ fst (x,y) = x
 snd :: (a,b) -> b
 snd (x,y) = y
 
+@inline
+mapFst :: (a -> b) -> (a,c) -> (b,c)
+mapFst f (x,y) = (f x, y)
+
+@inline
+mapSnd :: (a -> b) -> (c,a) -> (c,b)
+mapSnd f (x,y) = (x, f y)
+
 instance (Ord a, Ord b) => Ord (a, b) where
     compare (a0, b0) (a1, b1) = compare a0 a1 &<& compare b0 b1
 
@@ -1949,7 +1957,7 @@ importJava "org.simantics.scl.runtime.Lists" where
     Given a list of values and a function computing a key for each value, the function produces a function that finds a value
     effeciently for the given key.
     """
-    indexBy ::  (a -> b) -> [a] -> b -> Maybe a
+    indexBy ::  (a -> <e> b) -> [a] -> <e> (b -> Maybe a)
     
     "Works like `index` but uses the given functions as hash codes and equality."
     indexWith :: (a -> Integer) -> (a -> a -> Boolean) -> [(a,b)] -> a -> Maybe b
@@ -2003,6 +2011,16 @@ elemMaybe el m = match m with
     Just el2 -> el == el2
     Nothing -> False
 
+"`elemIndex el lst` returns the index of the first element in the given list `lst` which is equal (by ==) to the query element, or Nothing if there is no such element."
+elemIndex :: a -> [a] -> Maybe Integer
+elemIndex el l = loop 0
+  where
+    len = length l
+    loop i | i < len = if el == l!i
+                       then Just i
+                       else loop (i+1)
+           | otherwise = Nothing
+
 """
 Computes a list that contains only elements that belongs to both input lists.
 """
@@ -2196,6 +2214,12 @@ importJava "java.lang.Throwable" where
     @private
     @JavaName toString
     showThrowable :: Throwable -> String
+    @private
+    @JavaName getMessage 
+    getMessageThrowable :: Throwable -> String
+    @private
+    @JavaName getCause 
+    getCauseThrowable :: Throwable -> Maybe Throwable
 importJava "java.lang.Exception" where
     data Exception
     @private
@@ -2207,6 +2231,20 @@ instance Show Throwable where
 instance Show Exception where
     show = showException
 
+class Throwable e where
+    toThrowable :: e -> Throwable
+
+messageOfException :: Throwable e => e -> String
+messageOfException = getMessageThrowable . toThrowable
+
+causeOfException :: Throwable e => e -> Maybe Throwable
+causeOfException = getCauseThrowable . toThrowable
+
+instance Throwable Throwable where
+    toThrowable = id
+instance Throwable Exception where
+    toThrowable = Java.unsafeCoerce
+
 "Prints the given value in the console."
 @inline
 print :: Show a => a -> <Proc> ()