Skip to content

Commit

Permalink
feat: Add string type (#733)
Browse files Browse the repository at this point in the history
Closes #695

(Pending hugr release including commit b05a419)
  • Loading branch information
tatiana-s authored Dec 23, 2024
1 parent 0936580 commit aa9341b
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 11 deletions.
2 changes: 2 additions & 0 deletions guppylang/checker/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
nat_type_def,
none_type_def,
sized_iter_type_def,
string_type_def,
tuple_type_def,
)
from guppylang.tys.param import Parameter
Expand Down Expand Up @@ -237,6 +238,7 @@ def default() -> "Globals":
nat_type_def,
int_type_def,
float_type_def,
string_type_def,
list_type_def,
array_type_def,
sized_iter_type_def,
Expand Down
3 changes: 3 additions & 0 deletions guppylang/checker/expr_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
is_sized_iter_type,
list_type,
nat_type,
string_type,
)
from guppylang.tys.param import ConstParam, TypeParam
from guppylang.tys.subst import Inst, Subst
Expand Down Expand Up @@ -1179,6 +1180,8 @@ def python_value_to_guppy_type(
match v:
case bool():
return bool_type()
case str():
return string_type()
# Only resolve `int` to `nat` if the user specifically asked for it
case int(n) if type_hint == nat_type() and n >= 0:
return nat_type()
Expand Down
3 changes: 3 additions & 0 deletions guppylang/compiler/expr_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import hugr.std.float
import hugr.std.int
import hugr.std.logic
import hugr.std.prelude
from hugr import Hugr, Wire, ops
from hugr import tys as ht
from hugr import val as hv
Expand Down Expand Up @@ -596,6 +597,8 @@ def python_value_to_hugr(v: Any, exp_ty: Type) -> hv.Value | None:
match v:
case bool():
return hv.bool_value(v)
case str():
return hugr.std.prelude.StringVal(v)
case int():
return hugr.std.int.IntVal(v, width=NumericType.INT_WIDTH)
case float():
Expand Down
11 changes: 7 additions & 4 deletions guppylang/std/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
list_type_def,
nat_type_def,
sized_iter_type_def,
string_type_def,
)

guppy.init_module(import_builtins=False)
Expand Down Expand Up @@ -121,6 +122,12 @@ def __or__(self: bool, other: bool) -> bool: ...
def __xor__(self: bool, other: bool) -> bool: ...


@guppy.extend_type(string_type_def)
class String:
@guppy.custom(checker=UnsupportedChecker(), higher_order_value=False)
def __new__(x): ...


@guppy.extend_type(nat_type_def)
class Nat:
@guppy.custom(NoopCompiler())
Expand Down Expand Up @@ -890,10 +897,6 @@ def sorted(x): ...
def staticmethod(x): ...


@guppy.custom(checker=UnsupportedChecker(), higher_order_value=False)
def str(x): ...


@guppy.custom(checker=UnsupportedChecker(), higher_order_value=False)
def sum(x): ...

Expand Down
16 changes: 16 additions & 0 deletions guppylang/tys/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ def _sized_iter_to_hugr(args: Sequence[Argument]) -> ht.Type:
float_type_def = _NumericTypeDef(
DefId.fresh(), "float", None, NumericType(NumericType.Kind.Float)
)
string_type_def = OpaqueTypeDef(
id=DefId.fresh(),
name="str",
defined_at=None,
params=[],
always_linear=False,
to_hugr=lambda _: hugr.std.PRELUDE.get_type("string").instantiate([]),
)
list_type_def = _ListTypeDef(
id=DefId.fresh(),
name="list",
Expand Down Expand Up @@ -216,6 +224,10 @@ def float_type() -> NumericType:
return NumericType(NumericType.Kind.Float)


def string_type() -> OpaqueType:
return OpaqueType([], string_type_def)


def list_type(element_ty: Type) -> OpaqueType:
return OpaqueType([TypeArg(element_ty)], list_type_def)

Expand All @@ -236,6 +248,10 @@ def is_bool_type(ty: Type) -> bool:
return isinstance(ty, OpaqueType) and ty.defn == bool_type_def


def is_string_type(ty: Type) -> bool:
return isinstance(ty, OpaqueType) and ty.defn == string_type_def


def is_list_type(ty: Type) -> bool:
return isinstance(ty, OpaqueType) and ty.defn == list_type_def

Expand Down
10 changes: 5 additions & 5 deletions tests/error/misc_errors/unsupported_const.err
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: Unsupported constant (at $FILE:7:8)
Error: Unsupported constant (at $FILE:6:9)
|
5 | @compile_guppy
6 | def foo() -> None:
7 | x = "foo"
| ^^^^^ Type `str` is not supported
4 | @compile_guppy
5 | def foo() -> None:
6 | x = -2j
| ^^ Type `complex` is not supported

Guppy compilation failed due to 1 previous error
3 changes: 1 addition & 2 deletions tests/error/misc_errors/unsupported_const.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from guppylang.std.builtins import array
from tests.util import compile_guppy


@compile_guppy
def foo() -> None:
x = "foo"
x = -2j
8 changes: 8 additions & 0 deletions tests/integration/test_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ def foo() -> None:
validate(foo)


def test_strings(validate):
@compile_guppy
def foo() -> None:
x: str = py("a" + "b")

validate(foo)


@pytest.mark.skipif(not tket2_installed, reason="Tket2 is not installed")
@pytest.mark.skip("Fails because of extensions in types #343")
def test_pytket_single_qubit(validate):
Expand Down
35 changes: 35 additions & 0 deletions tests/integration/test_strings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from tests.util import compile_guppy

import pytest

def test_basic_type(validate):
@compile_guppy
def foo(x: str) -> str:
return x

validate(foo)


def test_basic_value(validate):
@compile_guppy
def foo() -> str:
x = "Hello World"
return x

validate(foo)


def test_struct(validate):
module = GuppyModule("module")

@guppy.struct(module)
class StringStruct:
x: str

@guppy(module)
def main(s: StringStruct) -> None:
StringStruct("Lorem Ipsum")

validate(module.compile())

0 comments on commit aa9341b

Please sign in to comment.