From e8d7a5237c666a40e14f3709289329fd8c2cb7d2 Mon Sep 17 00:00:00 2001 From: sanine Date: Wed, 22 Nov 2023 10:39:33 -0600 Subject: show error messages on bad input/state --- src/Mind.hs | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'src/Mind.hs') diff --git a/src/Mind.hs b/src/Mind.hs index 359293c..d686e24 100644 --- a/src/Mind.hs +++ b/src/Mind.hs @@ -76,12 +76,15 @@ insertEdge ns i e -- network computation -compute :: Network -> [Float] -> [Float] -> Maybe ([Float], [Float]) -compute net input state = - let - s = newState net input state - state' = map (Just) s - in Just $ (output net input state state', s) +compute :: Network -> [Float] -> [Float] -> Either String ([Float], [Float]) +compute net input state + | (length input) /= (numInput net) = Left $ "Bad input length: " ++ (show $ length input) + | (length state) /= (length $ internalNeurons net) = Left $ "Bad state length: " ++ (show $ length state) + | otherwise = + let + s = newState net input state + state' = map (Just) s + in Right $ (output net input state state', s) type InputState = ([Float], [Float]) @@ -89,7 +92,7 @@ type NewState = [Maybe Float] output :: Network -> [Float] -> [Float] -> NewState -> [Float] -output net input state state'= +output net input state state' = let numOutput = length $ outputNeurons net in @@ -97,16 +100,16 @@ output net input state state'= newState :: Network -> [Float] -> [Float] -> [Float] newState net input state = - let numInternal = length $ internalNeurons net - in fst $ - foldr - (\x (r, ns) -> - let (value, ns') = getValue net (input, state) ns (Internal x) - in (value:r, ns') - ) - ([], replicate numInternal Nothing) - [0..numInternal-1] - + let numInternal = length $ internalNeurons net + in fst $ + foldr + (\x (r, ns) -> + let (value, ns') = getValue net (input, state) ns (Internal x) + in (value:r, ns') + ) + ([], replicate numInternal Nothing) + [0..numInternal-1] + updateValue :: NewState -> Int -> Float -> NewState -- cgit v1.2.1