Есть такой код, представляющий собой попытку описать объект робота:
robot (name, attack, hp) = \message -> message(name, attack, hp)
name (n, _, _) = n
attack (_, a, _) = a
hp (_, _, hp) = hp
getAttack aRobot = aRobot attack
getHP aRobot = aRobot hp
getName aRobot = aRobot name
setName aRobot newName = aRobot(\(n, a, h) -> robot(newName, a , h))
setAttack aRobot newAttack = aRobot(\(n, a, h) -> robot(n, newAttack , h))
setHP aRobot newHP = aRobot(\(n, a, h) -> robot(n, n , newHP))
printRobot aRobot = aRobot( \(n, a, h) -> n ++ " attack: " ++ (show a) ++ " hp: " ++ (show h) )
damage aRobot dmg = aRobot(\(n,a,h) -> robot(n,a, h - dmg))
fight aRobot defender = damage defender dmg
where dmg = if getHP aRobot > 0
then getAttack aRobot
else 0
getAllHP aRobots = map getHP aRobots
Теперь мне нужно написать функцию которая возвращает кортеж из двух роботов после одного раунда, я делаю это так:
oneRoundFight aR1 aR2 = ( fight aR1 aR2, fight aR2 aR1 )
Появляется ошибка:
*Main> :l robot.hs
[1 of 1] Compiling Main ( robot.hs, interpreted )
robot.hs:27:48: error:
* Occurs check: cannot construct the infinite type:
b2 ~ ((a2, b2, b1) -> t1) -> t1
Expected type: ((a2, ((a2, b2, b1) -> t1) -> t1,
((a2, b2, b1) -> t1) -> t1)
-> ((a2, b2, b1) -> t1) -> t1)
-> a1
Actual type: ((a2, b2, b1) -> ((a2, b2, b1) -> t1) -> t1) -> a1
* In the first argument of `fight', namely `aR2'
In the expression: fight aR2 aR1
In the expression: (fight aR1 aR2, fight aR2 aR1)
* Relevant bindings include
aR2 :: ((a2, b2, b1) -> ((a2, b2, b1) -> t1) -> t1) -> a1
(bound at robot.hs:27:19)
aR1 :: ((a, ((a, b, a1) -> t) -> t, ((a, b, a1) -> t) -> t)
-> ((a, b, a1) -> t) -> t)
-> b1
(bound at robot.hs:27:15)
oneRoundFight :: (((a, ((a, b, a1) -> t) -> t,
((a, b, a1) -> t) -> t)
-> ((a, b, a1) -> t) -> t)
-> b1)
-> (((a2, b2, b1) -> ((a2, b2, b1) -> t1) -> t1) -> a1)
-> (a1, b1)
(bound at robot.hs:27:1)
|
27 | oneRoundFight aR1 aR2 = ( fight aR1 aR2, fight aR2 aR1 )
| ^^^
robot.hs:27:52: error:
* Occurs check: cannot construct the infinite type:
b ~ ((a, b, a1) -> t) -> t
Expected type: ((a, b, a1) -> ((a, b, a1) -> t) -> t) -> b1
Actual type: ((a, ((a, b, a1) -> t) -> t, ((a, b, a1) -> t) -> t)
-> ((a, b, a1) -> t) -> t)
-> b1
* In the second argument of `fight', namely `aR1'
In the expression: fight aR2 aR1
In the expression: (fight aR1 aR2, fight aR2 aR1)
* Relevant bindings include
aR2 :: ((a2, b2, b1) -> ((a2, b2, b1) -> t1) -> t1) -> a1
(bound at robot.hs:27:19)
aR1 :: ((a, ((a, b, a1) -> t) -> t, ((a, b, a1) -> t) -> t)
-> ((a, b, a1) -> t) -> t)
-> b1
(bound at robot.hs:27:15)
oneRoundFight :: (((a, ((a, b, a1) -> t) -> t,
((a, b, a1) -> t) -> t)
-> ((a, b, a1) -> t) -> t)
-> b1)
-> (((a2, b2, b1) -> ((a2, b2, b1) -> t1) -> t1) -> a1)
-> (a1, b1)
(bound at robot.hs:27:1)
|
27 | oneRoundFight aR1 aR2 = ( fight aR1 aR2, fight aR2 aR1 )
| ^^^
Failed, no modules loaded.
Prelude>
Я не совсем понимаю что значит этот вывод, а конкретно cannot construct the infinite type:
При этом если поменять аргументы местами: ( fight aR1 aR2, fight aR1 aR2 )
то этой ошибки не возникает.
Объясните пожалуйста.
При этом функция fight
написана и работает:
Prelude> :l robot.hs
[1 of 1] Compiling Main ( robot.hs, interpreted )
Ok, one module loaded.
*Main> aR1 = robot("r1", 8, 100)
*Main> aR2 = robot("r2", 4, 120)
*Main> aR2_damaged = fight aR1 aR2
*Main> aR1_damaged = fight aR2 aR1
*Main> printRobot aR1_damaged
"r1 attack: 8 hp: 96"
*Main> printRobot aR2_damaged
"r2 attack: 4 hp: 112"