ich aber eine ‚einfache‘ Beispiel schreibe Typklassen für die Implementierung ich herauszufinden, eine harte Zeit habe, warum dies nicht kompiliert:Typ konnte nicht in Instanz erwartet übereinstimmen
class Euclidean a where
norm :: (Euclidean a, Floating b) => a -> b
data Point a b = Point a b
instance (Floating x, Floating y) => Euclidean (Point x y) where
norm (Point x y) = x
Es scheitert mit:
Couldn't match expected type ‘b’ with actual type ‘x’
‘x’ is a rigid type variable bound by
the instance declaration at src/Simple.hs:10:10
‘b’ is a rigid type variable bound by
the type signature for
norm :: (Euclidean (Point x y), Floating b) => Point x y -> b
at src/Simple.hs:11:3
Relevant bindings include
x :: x (bound at src/Simple.hs:11:15)
norm :: Point x y -> b (bound at src/Simple.hs:11:3)
In the expression: x
In an equation for ‘norm’: norm (Point x y) = x
Hinweis: Die gewünschte Funktionsimplementierung wäre natürlich sqrt $ (x * x) + (y * y)
.
Danke für Ihre schnelle Antwort. Jetzt gibt es ein anderes Problem: Wie kann ich den Typ von "x" an den Typ von "y" anpassen? Ich schrieb die Implementierung als 'Norm (Punkt xy) = realToFrac $ sqrt $ (x * x) + (y * y)' (Hinzufügen der Real-Einschränkung zu beiden Typen in der Instanz-Header), aber jetzt klagt es dass es 'x' mit' y' nicht übereinstimmen kann – Midiparse
Versuchen Sie etwas wie 'instance (Real x, Floating x) => Euklidisch (Punkt xx) where' – redneb
Es beschwert sich, dass es verschiedene Typ Variablen dort benötigt, und FlexibleConstraints hinzufügen zu erlauben dies – Midiparse