diff --git a/src/Copar/Functors/Distribution.hs b/src/Copar/Functors/Distribution.hs index 4d9ce44faa4ce43f21c5b4b7f4b73b8f246d1905..eed4ef889f411cbfe17f8ca99f80212ce94cfb47 100644 --- a/src/Copar/Functors/Distribution.hs +++ b/src/Copar/Functors/Distribution.hs @@ -67,10 +67,13 @@ instance ParseMorphism Distribution where parseMorphismPoint (Distribution inner) = do (f1, succs) <- parseMorphismPoint (GroupValued @EqDouble inner) - unlessM noSanityChecks $ + unlessM noSanityChecks $ do when (f1 /= 1) $ fail "distribution: Sum of outgoing labels is not 1" + when (not (all (\x -> x >= 0 && x <= 1) (fmap snd succs))) $ do + fail "distribution: Weights must be between 0 and 1" + return (f1, succs) instance RefinementInterface Distribution where diff --git a/tests/Copar/Functors/DistributionSpec.hs b/tests/Copar/Functors/DistributionSpec.hs index 8514d43edebe384d208e241a0569d5789f284227..8cbfe4c082ba3fb58ca7489b12ca576f24b83cae 100644 --- a/tests/Copar/Functors/DistributionSpec.hs +++ b/tests/Copar/Functors/DistributionSpec.hs @@ -53,6 +53,16 @@ parseMorphismPointSpec = describe "parseMorphismPoint" $ do `shouldFailOn` "x: {x: 0.5}" + it "errors if an edge weight is below zero" $ + parseMorphisms (Functor 1 (Distribution Variable)) EnableSanityChecks "" + `shouldFailOn` + "x: {x: -1}" + + it "errors if an edge weight is above one" $ + parseMorphisms (Functor 1 (Distribution Variable)) EnableSanityChecks "" + `shouldFailOn` + "x: {x: 1.3}" + it "uses approximate comparison for doubles" $ parseMorphisms (Functor 1 (Distribution Variable)) EnableSanityChecks "" `shouldSucceedOn` "s0: {s0: 0.1, s1: 0.1, s2: 0.1, s3: 0.1, s4: 0.1, s5: 0.1, s6: 0.1, s7: 0.1, s8: 0.1, s9: 0.1}\n\