Skip to content

Fix/tested haskell judge #543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,4 @@ venv-*/
.venv/
node_modules/
result/
.data/
14 changes: 11 additions & 3 deletions tested/languages/haskell/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import re
from pathlib import Path
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional

from tested.configs import GlobalConfig
from tested.datatypes import AllTypes
from tested.dodona import AnnotateCode, Message
from tested.features import Construct, TypeSupport
Expand All @@ -28,6 +29,10 @@


class Haskell(Language):
def __init__(self, config: Optional["GlobalConfig"]):
super().__init__(config)
self.clean_types_regex = re.compile(r"\(([^:\s]*)\s*::\s*([A-Z][a-zA-Z0-9]*)\)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a constant instead of a variable,

So CLEAN_TYPES_REGEX =

Also is this regex completly save? Eg is it possible that it also matches a type expression within a string?

If typing is optional, it would probably be better not to add the type declarations in 'generators.py', instead of adding them there, running the tests with typing and then obfuscating those types to the end user.


def initial_dependencies(self) -> list[str]:
return ["Values.hs", "EvaluationUtils.hs"]

Expand Down Expand Up @@ -79,6 +84,7 @@ def supported_constructs(self) -> set[Construct]:
Construct.ASSIGNMENTS,
Construct.EVALUATION,
Construct.GLOBAL_VARIABLES,
Construct.HETEROGENEOUS_ARGUMENTS,
}

def compilation(self, files: list[str]) -> CallbackResult:
Expand Down Expand Up @@ -110,7 +116,7 @@ def linter(self, remaining: float) -> tuple[list[Message], list[AnnotateCode]]:
return linter.run_hlint(self.config.dodona, remaining)

def cleanup_description(self, statement: str) -> str:
return cleanup_description(self, statement)
return cleanup_description(self, self.clean_types_regex.sub(r"\1", statement))

def cleanup_stacktrace(self, stacktrace: str) -> str:
filename = submission_file(self)
Expand Down Expand Up @@ -164,7 +170,9 @@ def cleanup_stacktrace(self, stacktrace: str) -> str:
def generate_statement(self, statement: Statement) -> str:
from tested.languages.haskell import generators

return generators.convert_statement(statement)
return self.clean_types_regex.sub(
r"\1", generators.convert_statement(statement)
)

def generate_execution_unit(self, execution_unit: "PreparedExecutionUnit") -> str:
from tested.languages.haskell import generators
Expand Down
20 changes: 10 additions & 10 deletions tested/languages/haskell/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,37 @@ def convert_value(value: Value) -> str:
return f"({convert_arguments(value.data)})"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where I would remove the typing information.

But i am not an expert on TESTed nor haskell so I am not sure it will work

elif isinstance(value.type, AdvancedNumericTypes):
if not isinstance(value.data, SpecialNumbers):
return f"{value.data} :: {convert_declaration(value.type)}"
return f"({value.data} :: {convert_declaration(value.type)})"
elif value.data == SpecialNumbers.NOT_A_NUMBER:
return f"(0/0) :: {convert_declaration(value.type)}"
return f"((0/0) :: {convert_declaration(value.type)})"
elif value.data == SpecialNumbers.POS_INFINITY:
return f"(1/0) :: {convert_declaration(value.type)}"
return f"((1/0) :: {convert_declaration(value.type)})"
else:
assert value.data == SpecialNumbers.NEG_INFINITY
return f"(-1/0) :: {convert_declaration(value.type)}"
return f"((-1/0) :: {convert_declaration(value.type)})"
elif value.type == AdvancedStringTypes.CHAR:
assert isinstance(value, StringType)
return "'" + value.data.replace("'", "\\'") + "'"
# Handle basic types
value = as_basic_type(value)
if value.type == BasicNumericTypes.INTEGER:
return f"{value.data} :: Int"
return f"({value.data} :: Int)"
elif value.type == BasicNumericTypes.REAL:
if not isinstance(value.data, SpecialNumbers):
return f"{value.data} :: Double"
return f"({value.data} :: Double)"
elif value.data == SpecialNumbers.NOT_A_NUMBER:
return "(0/0) :: Double"
return "((0/0) :: Double)"
elif value.data == SpecialNumbers.POS_INFINITY:
return "(1/0) :: Double"
return "((1/0) :: Double)"
else:
assert SpecialNumbers.NEG_INFINITY
return "(-1/0) :: Double"
return "((-1/0) :: Double)"
elif value.type == BasicStringTypes.TEXT:
return json.dumps(value.data)
elif value.type == BasicBooleanTypes.BOOLEAN:
return str(value.data)
elif value.type == BasicNothingTypes.NOTHING:
return "Nothing :: Maybe Integer"
return "(Nothing :: Maybe Integer)"
elif value.type == BasicSequenceTypes.SEQUENCE:
assert isinstance(value, SequenceType)
return f"[{convert_arguments(value.data)}]"
Expand Down
6 changes: 3 additions & 3 deletions tested/manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from tested.main import run
from tested.testsuite import SupportedLanguage

exercise_dir = "/home/niko/Ontwikkeling/universal-judge/tests/exercises/echo-function"
exercise_dir = "/Users/tibvdm/PycharmProjects/universal-judge/tests/exercises/haskell"

Check warning on line 16 in tested/manual.py

View check run for this annotation

Codecov / codecov/patch

tested/manual.py#L16

Added line #L16 was not covered by tests


def read_config() -> DodonaConfig:
Expand All @@ -24,10 +24,10 @@
programming_language=SupportedLanguage("haskell"),
natural_language="nl",
resources=Path(exercise_dir, "evaluation"),
source=Path(exercise_dir, "solution/correct.hs"),
source=Path(exercise_dir, "solution/ndeElement.hs"),
judge=Path("."),
workdir=Path("workdir"),
test_suite="two-specific.tson",
test_suite="ndeElement.yaml",
options=Options(
linter=False,
),
Expand Down
52 changes: 52 additions & 0 deletions tests/exercises/haskell/evaluation/ndeElement.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
tabs:
- tab: geefNde (Int)
testcases:
- expression: geefNde(0, [1, 2, 3, 4, 5])
return: 1
- expression: geefNde(1, [1, 2, 3, 4, 5])
return: 2
- expression: geefNde(2, [1, 2, 3, 4, 5])
return: 3
- expression: geefNde(3, [1, 2, 3, 4, 5])
return: 4
- expression: geefNde(4, [1, 2, 3, 4, 5])
return: 5

- tab: geefNde (Double)
testcases:
- expression: geefNde(0, [1.0, 2.0, 3.0, 4.0, 5.0])
return: 1.0
- expression: geefNde(1, [1.0, 2.0, 3.0, 4.0, 5.0])
return: 2.0
- expression: geefNde(2, [1.0, 2.0, 3.0, 4.0, 5.0])
return: 3.0
- expression: geefNde(3, [1.0, 2.0, 3.0, 4.0, 5.0])
return: 4.0
- expression: geefNde(4, [1.0, 2.0, 3.0, 4.0, 5.0])
return: 5.0

- tab: geefNde (Char)
testcases:
- expression: geefNde(0, ['a', 'b', 'c', 'd', 'e'])
return: a
- expression: geefNde(1, ['a', 'b', 'c', 'd', 'e'])
return: b
- expression: geefNde(2, ['a', 'b', 'c', 'd', 'e'])
return: c
- expression: geefNde(3, ['a', 'b', 'c', 'd', 'e'])
return: d
- expression: geefNde(4, ['a', 'b', 'c', 'd', 'e'])
return: e

- tab: geefNde (Tuple)
testcases:
- expression: geefNde(0, [(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)])
return: [1, 5]
- expression: geefNde(1, [(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)])
return: [2, 4]
- expression: geefNde(2, [(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)])
return: [3, 3]
- expression: geefNde(3, [(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)])
return: [4, 2]
- expression: geefNde(4, [(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)])
return: [5, 1]
43 changes: 43 additions & 0 deletions tests/exercises/haskell/evaluation/voorlaatste.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
tabs:
- tab: 'voorlaatste (Int)'
testcases:
- expression: voorlaatste([1, 2])
return: 1
- expression: voorlaatste([1, 2, 3])
return: 2
- expression: voorlaatste([1, 2, 3, 4])
return: 3
- expression: voorlaatste([1, 2, 3, 4, 5])
return: 4
- expression: voorlaatste([1, 0, 1, 0, 1])
return: 0
- expression: voorlaatste([9, 81, 1, 2, 4, 1, 42, 1])
return: 42
- tab: 'voorlaatste (Double)'
testcases:
- expression: voorlaatste([1.0, 2.0])
return: 1.0
- expression: voorlaatste([1.0, 2.0, 3.0])
return: 2.0
- expression: voorlaatste([1.0, 2.0, 3.0, 4.0])
return: 3.0
- expression: voorlaatste([1.0, 2.0, 3.0, 4.0, 5.0])
return: 4.0
- expression: voorlaatste([1.0, 0.0, 1.0, 0.0, 1.0])
return: 0.0
- expression: voorlaatste([9.0, 81.0, 1.0, 2.0, 4.0, 1.0, 42.0, 1.0])
return: 42.0
- tab: 'voorlaatste (Char)'
testcases:
- expression: voorlaatste(['a', 'b'])
return: 'a'
- expression: voorlaatste(['a', 'b', 'c'])
return: 'b'
- expression: voorlaatste(['a', 'b', 'c', 'd'])
return: 'c'
- expression: voorlaatste(['a', 'b', 'c', 'd', 'e'])
return: 'd'
- expression: voorlaatste(['a', 'b', 'a', 'b', 'a'])
return: 'b'
- expression: voorlaatste(['c', 'd', 'a', 'b', 'g'])
return: 'b'
2 changes: 2 additions & 0 deletions tests/exercises/haskell/solution/ndeElement.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
geefNde :: Int -> [a] -> a
geefNde n l = l !! n
2 changes: 2 additions & 0 deletions tests/exercises/haskell/solution/voorlaatste.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
voorlaatste :: [a] -> a
voorlaatste = last . init
4 changes: 1 addition & 3 deletions tests/test_language_quircks.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ def test_haskell_function_arguments_without_brackets(
)

result = generate_statement(bundle, statement)
assert (
result == f'{submission_name(bundle.language)}.test 5.5 :: Double "hallo" True'
)
assert result == f'{submission_name(bundle.language)}.test 5.5 "hallo" True'


def test_javascript_exception_correct(tmp_path: Path, pytestconfig: pytest.Config):
Expand Down
Loading