저는 하스켈을 처음 접했고 숙제를 위해 계산기를 구현하려고합니다. 나는 두 개의 값으로 나눌 필요가있는 지점에 갇혀 있는데, 그 유형이 추론 될 수 없거나 선언/변환되어야한다는 것이 문제라고 생각합니다. 이 문제를 직접 해결하는 방법을 배우려하고 있지만 길을 가다 보면 통찰력이 도움이 될 것입니다. 여기 haskell division 유형이 일치하지 않습니까?
코드입니다 : 여기data Value e = OK e | Error String deriving (Eq)
-- assuming we know how to type e can be shown, i.e. Show e, then
-- we know how to show a Value e type
instance (Show e) => Show (Value e) where
show (OK x) = (show x)
show (Error s) = "ERROR: " ++ s
type Token = String
type Result = Value Int
type Intermediate = [ (Value Int) ]
-- an algebra is a things that knows about plus and times
class Algebra a where
plus :: a -> a -> a
times :: a -> a -> a
subtraction :: a -> a -> a
division :: a -> a-> a
-- assuming that we know how to + and * things of type e, (i.e.
-- we have Num e, then we have algebra's over Value e
instance (Num e) => Algebra (Value e) where
plus (OK x) (OK y) = (OK (x+y))
times (OK x) (OK y) = (OK (x*y))
subtraction (OK x) (OK y) = (OK (x-y))
division (OK x) (OK 0) = (Error "div by 0")
division (OK x) (OK y) = (OK (x `div` y)) <-- this is line 44 that it complains about
가 ghci이 더 코드가
test.hs:44:34:
Could not deduce (Integral e)
from the context (Algebra (Value e), Num e)
arising from a use of `div' at test.hs:44:34-42
Possible fix:
add (Integral e) to the context of the instance declaration
In the first argument of `OK', namely `(x `div` y)'
In the expression: (OK (x `div` y))
In the definition of `division':
division (OK x) (OK y) = (OK (x `div` y))
test.hs를 통해 내가 프로그램을 실행하려고 오류이다, 나는 거라 생각 명확성을 위해 제외하되, 그렇지 않은 경우 항상 편집 할 수 있습니다.
이것은 질문이 아니어야합니다 .. – Omnipotent