diff options
author | sanine <sanine.not@pm.me> | 2023-11-22 10:39:33 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-22 10:39:33 -0600 |
commit | e8d7a5237c666a40e14f3709289329fd8c2cb7d2 (patch) | |
tree | 673f843f98b00a8868de6705c491777088a86842 /src | |
parent | 8fb358e84770f69606f7f27c40cfdf0ce57cd026 (diff) |
show error messages on bad input/state
Diffstat (limited to 'src')
-rw-r--r-- | src/Mind.hs | 37 |
1 files changed, 20 insertions, 17 deletions
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 |