chore: update and clean some examples

This commit is contained in:
HEL
2026-07-09 23:13:52 +02:00
parent 34072de270
commit fcca7dc01c
13 changed files with 3284 additions and 220 deletions
@@ -7,10 +7,9 @@ from __future__ import annotations
df: Frame[ df: Frame[
verified: bool, verified: bool,
birth_year: int, birth_year: int,
height: float + ( _ > 0 ) + ( _ < 250 ), height: float,
name: str, name: str,
date: datetime, date: object,
float, # unnamed float, # unnamed
unknown: _, # untyped unknown: _, # untyped
_ # unnamed and untyped
] ]
@@ -1,24 +1,63 @@
// Simple custom type derived from floats // Simple custom type derived from float
type Latitude<float> type Custom = float
type Longitude<float>
// Simple custom types with constraints
type Latitude = float where (-90 <= _ & _ <= 90)
type Longitude = float where (-180 <= _ & _ <= 180)
// Generic custom type (a Difference of T is derived from T, e.g. a difference of floats is a float
type Difference[T] = T
// Complex custom type, containing two values accessible through properties // Complex custom type, containing two values accessible through properties
type GeoLocation<Latitude, Longitude> { type GeoLocation = object
lat: Latitude
lon: Longitude extend GeoLocation {
prop lat: Latitude
prop lon: Longitude
} }
type LatitudeDiff<float> type GeoLocationDifference = object
type LongitudeDiff<float>
extend GeoLocationDifference {
prop lat: Difference[Latitude]
prop lon: Difference[Longitude]
}
// Define operations on our custom type
// Simple operation defined on our custom types // Simple operation defined on our custom types
op <Latitude> - <Latitude> = <LatitudeDiff> extend Latitude {
op <Longitude> - <Longitude> = <LongitudeDiff> def __sub__: fn(Latitude, /) -> Difference[Latitude]
}
// Simple custom type with a constraint extend Longitude {
type Age<int + (0 <= _) + (_ < 150)> def __sub__: fn(Longitude, /) -> Difference[Longitude]
}
// Predefined custom constraints that can be referenced in other definitions extend GeoLocation {
constraint Positive = _ >= 0 // This type is compatible with the `-` operation with another GeoLocation
constraint StrictlyPositive = _ > 0 // i.e. you can subtract a GeoLocation from another GeoLocation, resulting
//constraint Even = _ % 2 == 0 // in a GeoLocationDifference
def __sub__: fn(GeoLocation, /) -> GeoLocationDifference
}
// Predefined custom predicates that can be referenced in other definitions
predicate Positive(v: float) = v >= 0
predicate StrictlyPositive(v: float) = v > 0
predicate Equatorial(loc: GeoLocation) = (-10 <= loc.lat <= 10)
predicate Arctic(loc: GeoLocation) = (loc.lat >= 66)
type Person = object
extend Person {
prop name: str
// Property with an inline constraint
prop age: int where (0 <= _ & _ < 150)
// Property referencing a predicate
prop height: float where StrictlyPositive(_)
prop home: GeoLocation
}
@@ -8,8 +8,8 @@ df: Frame[
] ]
# Properties of a type can be used on a column of that type # Properties of a type can be used on a column of that type
lat: Column[GeoLocation] = df["location"].lat lat: Column[Latitude] = ...
lon: Column[GeoLocation] = df["location"].lon lon: Column[Longitude] = ...
# Unregistered operations between types are not permitted # Unregistered operations between types are not permitted
lat + lon # Invalid operation lat + lon # Invalid operation
@@ -18,13 +18,3 @@ lat + lon # Invalid operation
lat1: Latitude = lat[0] lat1: Latitude = lat[0]
lat2: Latitude = lat[1] lat2: Latitude = lat[1]
lat_diff: Difference[Latitude] = lat2 - lat1 # Valid operation lat_diff: Difference[Latitude] = lat2 - lat1 # Valid operation
# In addition to the type, a column can have one or more constraints, either defined inline or in a separate file
df2: Frame[
age: int + (_ >= 0),
height: float + (_ >= 0),
]
df2_bis: Frame[
age: int + Positive,
height: float + Positive,
]
@@ -1,73 +0,0 @@
// Simple custom type derived from float
type Custom(float)
// Simple custom types with constraints
type Latitude(float) where (-90 <= _ <= 90)
type Longitude(float) where (-180 <= _ <= 180)
// Generic custom type (a Difference of T is derived from T, e.g. a difference of floats is a float
type Difference[T](T)
// Complex custom type, containing two values accessible through properties
type GeoLocation {
lat: Latitude
lon: Longitude
}
// Define operations on our custom type
extend GeoLocation {
// This type is compatible with the `-` operation with another GeoLocation
// i.e. you can subtract a GeoLocation from another GeoLocation, resulting
// in a Difference of GeoLocations
op __sub__(GeoLocation) -> Difference[GeoLocation]
}
// For complex generics, you need to specify how the genericity the properties
// are handled
type Difference[GeoLocation] {
lat: Difference[Latitude]
lon: Difference[Longitude]
}
// Simple operation defined on our custom types
extend Latitude {
op __sub__(Latitude) -> Difference[Latitude]
}
extend Longitude {
op __sub__(Longitude) -> Difference[Longitude]
}
// Predefined custom predicates that can be referenced in other definitions
predicate Positive(v: float) = v >= 0
predicate StrictlyPositive(v: float) = v > 0
predicate Equatorial(loc: GeoLocation) = (-10 <= loc.lat <= 10)
predicate Arctic(loc: GeoLocation) = (loc.lat >= 66)
type Person {
name: str
// Property with an inline constraint
age: int? where (0 <= _ < 150)
// Property referencing a predicate
height: float where StrictlyPositive
home: GeoLocation
}
// Custom complex type derived from another complex type, with a constraint
// on a property
// Multiple proposed syntaxes, not yet defined
// Explicit, but new keyword
type EquatorialPerson refines Person where Equatorial(_.home)
// Explicit with existing keyword, might be confusing if expectations regarding 'is'
type EquatorialPerson is Person where Equatorial(_.home)
// Consistent and Python-friendly but can be confused with structural extension
type EquatorialPerson(Person) where Equatorial(_.home)
// Allow new properties, probably not useful
type EquatorialPerson extends Person where Equatorial(_.home)
@@ -6,7 +6,7 @@ c = a + b # -> int
c = "invalid" # -> can't assign str to int variable c = "invalid" # -> can't assign str to int variable
d = True d = True
e = d + d e = d + d # -> addition not defined between booleans
f: float = a f: float = a
@@ -1,6 +1,7 @@
# type: ignore # type: ignore
# ruff: disable [F821] # ruff: disable [F821]
distance: Meter = cast(Meter, 123.45) distance = cast(Meter, 123.45)
time: Second = cast(Second, 6.7) time = cast(Second, 6.7)
speed = distance / time speed = distance / time
print(speed)
@@ -1,3 +1,5 @@
# Return types must have a LUB
# Valid
def minimum(x: int, y: int): def minimum(x: int, y: int):
if x < y: if x < y:
return x return x
@@ -5,18 +7,28 @@ def minimum(x: int, y: int):
return y return y
# Invalid
def func(a: int):
if a < 5:
return "Oops"
return True
a = 15 a = 15
b = 72 b = 72
c = minimum(a, b) c = minimum(a, b)
# Recursive but typable thanks to return hint
def factorial(n: int) -> int: def factorial(n: int) -> int:
if n <= 1: if n <= 1:
return 1 return 1
return n * factorial(n - 1) return n * factorial(n - 1)
category = "Category 1" if a < 10 else "Category 2" # Branches must be of the same type
category = "Category 1" if a < 10 else "Category 2" # Valid
category = "Category 1" if a < 10 else 3 # Invalid
def foo() -> None: def foo() -> None:
@@ -15,7 +15,9 @@ extend Coordinate {
type Difference[T <: float] = T type Difference[T <: float] = T
type MeterDifference = Difference[Meter] type MeterDifference = Difference[Meter]
type CompDiff[T <: float] = { type CompDiff[T <: float] = object
extend CompDiff[T <: float] {
prop d1: Difference[T] prop d1: Difference[T]
prop d2: Difference[T] prop d2: Difference[T]
} }
@@ -1,20 +1,20 @@
# type: ignore # type: ignore
# ruff: disable [F821] # ruff: disable [F821]
p1: Coordinate p1 = cast(Coordinate, object())
p2: Coordinate p2 = cast(Coordinate, object())
diff_x = p2.x - p1.x diff_x = p2.x - p1.x
diff_y = p2.y - p1.y diff_y = p2.y - p1.y
dist = diff_x + diff_y dist = diff_x + diff_y
p2.x += cast(Meter, 1) p2.x += cast(Meter, 1.0)
p2.y = True # invalid, wrong type p2.y = True # invalid, wrong type
p2.z = 3 # invalid, no property 'z' on Coordinate p2.z = 3 # invalid, no property 'z' on Coordinate
p2.x.a = 3 # invalid, no properties on Meter p2.x.a = 3 # invalid, no properties on Meter
foo: list[float] = [] foo = cast(list[float], [])
append = foo.append append = foo.append
@@ -23,7 +23,7 @@ foo.append(2)
append(True) # invalid, must be float append(True) # invalid, must be float
append(2) append(2)
bar: list[list[Meter]] bar = cast(list[list[Meter]], [])
bar.append([p2.x]) bar.append([p2.x])
@@ -1,9 +1,9 @@
# type: ignore # type: ignore
# ruff: disable [F821] # ruff: disable [F821]
foo: Foo foo = cast(Foo, object())
t1: T1 t1 = cast(T1, object())
t2: T2 t2 = cast(T2, object())
a = foo.bar(t1) a = foo.bar(t1)
b = foo.bar(t2) b = foo.bar(t2)
@@ -13,6 +13,6 @@ func = foo.bar
c = func(t1) c = func(t1)
d = func(t2) d = func(t2)
t2b: T2b t2b = cast(T2b, object())
e = foo.bar(t2b) e = foo.bar(t2b)
File diff suppressed because it is too large Load Diff
@@ -2,8 +2,8 @@
type Custom = float type Custom = float
// Simple custom types with constraints // Simple custom types with constraints
type Latitude = float where (-90 <= _ <= 90) type Latitude = float where (-90 <= _ & _ <= 90)
type Longitude = float where (-180 <= _ <= 180) type Longitude = float where (-180 <= _ & _ <= 180)
// Generic custom type (a Difference of T is derived from T, e.g. a difference of floats is a float // Generic custom type (a Difference of T is derived from T, e.g. a difference of floats is a float
type Difference[T] = T type Difference[T] = T
@@ -54,10 +54,10 @@ extend Person {
prop name: str prop name: str
// Property with an inline constraint // Property with an inline constraint
prop age: Optional[int where (0 <= _ < 150)] prop age: int where (0 <= _ & _ < 150)
// Property referencing a predicate // Property referencing a predicate
prop height: float where StrictlyPositive prop height: float where StrictlyPositive(_)
prop home: GeoLocation prop home: GeoLocation
} }
@@ -187,8 +187,8 @@
"column": 38 "column": 38
}, },
{ {
"type": "LESS_EQUAL", "type": "AND",
"lexeme": "<=", "lexeme": "&",
"line": 5, "line": 5,
"column": 39 "column": 39
}, },
@@ -196,25 +196,49 @@
"type": "WHITESPACE", "type": "WHITESPACE",
"lexeme": " ", "lexeme": " ",
"line": 5, "line": 5,
"column": 40
},
{
"type": "UNDERSCORE",
"lexeme": "_",
"line": 5,
"column": 41 "column": 41
}, },
{
"type": "WHITESPACE",
"lexeme": " ",
"line": 5,
"column": 42
},
{
"type": "LESS_EQUAL",
"lexeme": "<=",
"line": 5,
"column": 43
},
{
"type": "WHITESPACE",
"lexeme": " ",
"line": 5,
"column": 45
},
{ {
"type": "NUMBER", "type": "NUMBER",
"lexeme": "90", "lexeme": "90",
"line": 5, "line": 5,
"column": 42 "column": 46
}, },
{ {
"type": "RIGHT_PAREN", "type": "RIGHT_PAREN",
"lexeme": ")", "lexeme": ")",
"line": 5, "line": 5,
"column": 44 "column": 48
}, },
{ {
"type": "NEWLINE", "type": "NEWLINE",
"lexeme": "\n", "lexeme": "\n",
"line": 5, "line": 5,
"column": 45 "column": 49
}, },
{ {
"type": "TYPE", "type": "TYPE",
@@ -325,8 +349,8 @@
"column": 40 "column": 40
}, },
{ {
"type": "LESS_EQUAL", "type": "AND",
"lexeme": "<=", "lexeme": "&",
"line": 6, "line": 6,
"column": 41 "column": 41
}, },
@@ -334,25 +358,49 @@
"type": "WHITESPACE", "type": "WHITESPACE",
"lexeme": " ", "lexeme": " ",
"line": 6, "line": 6,
"column": 42
},
{
"type": "UNDERSCORE",
"lexeme": "_",
"line": 6,
"column": 43 "column": 43
}, },
{
"type": "WHITESPACE",
"lexeme": " ",
"line": 6,
"column": 44
},
{
"type": "LESS_EQUAL",
"lexeme": "<=",
"line": 6,
"column": 45
},
{
"type": "WHITESPACE",
"lexeme": " ",
"line": 6,
"column": 47
},
{ {
"type": "NUMBER", "type": "NUMBER",
"lexeme": "180", "lexeme": "180",
"line": 6, "line": 6,
"column": 44 "column": 48
}, },
{ {
"type": "RIGHT_PAREN", "type": "RIGHT_PAREN",
"lexeme": ")", "lexeme": ")",
"line": 6, "line": 6,
"column": 47 "column": 51
}, },
{ {
"type": "NEWLINE", "type": "NEWLINE",
"lexeme": "\n", "lexeme": "\n",
"line": 6, "line": 6,
"column": 48 "column": 52
}, },
{ {
"type": "NEWLINE", "type": "NEWLINE",
@@ -2240,22 +2288,40 @@
}, },
{ {
"type": "IDENTIFIER", "type": "IDENTIFIER",
"lexeme": "Optional", "lexeme": "int",
"line": 57, "line": 57,
"column": 15 "column": 15
}, },
{ {
"type": "LEFT_BRACKET", "type": "WHITESPACE",
"lexeme": "[", "lexeme": " ",
"line": 57, "line": 57,
"column": 23 "column": 18
}, },
{ {
"type": "IDENTIFIER", "type": "WHERE",
"lexeme": "int", "lexeme": "where",
"line": 57,
"column": 19
},
{
"type": "WHITESPACE",
"lexeme": " ",
"line": 57, "line": 57,
"column": 24 "column": 24
}, },
{
"type": "LEFT_PAREN",
"lexeme": "(",
"line": 57,
"column": 25
},
{
"type": "NUMBER",
"lexeme": "0",
"line": 57,
"column": 26
},
{ {
"type": "WHITESPACE", "type": "WHITESPACE",
"lexeme": " ", "lexeme": " ",
@@ -2263,8 +2329,8 @@
"column": 27 "column": 27
}, },
{ {
"type": "WHERE", "type": "LESS_EQUAL",
"lexeme": "where", "lexeme": "<=",
"line": 57, "line": 57,
"column": 28 "column": 28
}, },
@@ -2272,17 +2338,35 @@
"type": "WHITESPACE", "type": "WHITESPACE",
"lexeme": " ", "lexeme": " ",
"line": 57, "line": 57,
"column": 30
},
{
"type": "UNDERSCORE",
"lexeme": "_",
"line": 57,
"column": 31
},
{
"type": "WHITESPACE",
"lexeme": " ",
"line": 57,
"column": 32
},
{
"type": "AND",
"lexeme": "&",
"line": 57,
"column": 33 "column": 33
}, },
{ {
"type": "LEFT_PAREN", "type": "WHITESPACE",
"lexeme": "(", "lexeme": " ",
"line": 57, "line": 57,
"column": 34 "column": 34
}, },
{ {
"type": "NUMBER", "type": "UNDERSCORE",
"lexeme": "0", "lexeme": "_",
"line": 57, "line": 57,
"column": 35 "column": 35
}, },
@@ -2293,8 +2377,8 @@
"column": 36 "column": 36
}, },
{ {
"type": "LESS_EQUAL", "type": "LESS",
"lexeme": "<=", "lexeme": "<",
"line": 57, "line": 57,
"column": 37 "column": 37
}, },
@@ -2302,55 +2386,25 @@
"type": "WHITESPACE", "type": "WHITESPACE",
"lexeme": " ", "lexeme": " ",
"line": 57, "line": 57,
"column": 39 "column": 38
},
{
"type": "UNDERSCORE",
"lexeme": "_",
"line": 57,
"column": 40
},
{
"type": "WHITESPACE",
"lexeme": " ",
"line": 57,
"column": 41
},
{
"type": "LESS",
"lexeme": "<",
"line": 57,
"column": 42
},
{
"type": "WHITESPACE",
"lexeme": " ",
"line": 57,
"column": 43
}, },
{ {
"type": "NUMBER", "type": "NUMBER",
"lexeme": "150", "lexeme": "150",
"line": 57, "line": 57,
"column": 44 "column": 39
}, },
{ {
"type": "RIGHT_PAREN", "type": "RIGHT_PAREN",
"lexeme": ")", "lexeme": ")",
"line": 57, "line": 57,
"column": 47 "column": 42
},
{
"type": "RIGHT_BRACKET",
"lexeme": "]",
"line": 57,
"column": 48
}, },
{ {
"type": "NEWLINE", "type": "NEWLINE",
"lexeme": "\n", "lexeme": "\n",
"line": 57, "line": 57,
"column": 49 "column": 43
}, },
{ {
"type": "NEWLINE", "type": "NEWLINE",
@@ -2442,11 +2496,29 @@
"line": 60, "line": 60,
"column": 30 "column": 30
}, },
{
"type": "LEFT_PAREN",
"lexeme": "(",
"line": 60,
"column": 46
},
{
"type": "UNDERSCORE",
"lexeme": "_",
"line": 60,
"column": 47
},
{
"type": "RIGHT_PAREN",
"lexeme": ")",
"line": 60,
"column": 48
},
{ {
"type": "NEWLINE", "type": "NEWLINE",
"lexeme": "\n", "lexeme": "\n",
"line": 60, "line": 60,
"column": 46 "column": 49
}, },
{ {
"type": "NEWLINE", "type": "NEWLINE",
@@ -2544,7 +2616,7 @@
"constraint": { "constraint": {
"_type": "GroupingExpr", "_type": "GroupingExpr",
"expr": { "expr": {
"_type": "BinaryExpr", "_type": "LogicalExpr",
"left": { "left": {
"_type": "BinaryExpr", "_type": "BinaryExpr",
"left": { "left": {
@@ -2560,6 +2632,12 @@
"_type": "WildcardExpr" "_type": "WildcardExpr"
} }
}, },
"operator": "&",
"right": {
"_type": "BinaryExpr",
"left": {
"_type": "WildcardExpr"
},
"operator": "<=", "operator": "<=",
"right": { "right": {
"_type": "LiteralExpr", "_type": "LiteralExpr",
@@ -2568,6 +2646,7 @@
} }
} }
} }
}
}, },
{ {
"_type": "TypeStmt", "_type": "TypeStmt",
@@ -2582,7 +2661,7 @@
"constraint": { "constraint": {
"_type": "GroupingExpr", "_type": "GroupingExpr",
"expr": { "expr": {
"_type": "BinaryExpr", "_type": "LogicalExpr",
"left": { "left": {
"_type": "BinaryExpr", "_type": "BinaryExpr",
"left": { "left": {
@@ -2598,6 +2677,12 @@
"_type": "WildcardExpr" "_type": "WildcardExpr"
} }
}, },
"operator": "&",
"right": {
"_type": "BinaryExpr",
"left": {
"_type": "WildcardExpr"
},
"operator": "<=", "operator": "<=",
"right": { "right": {
"_type": "LiteralExpr", "_type": "LiteralExpr",
@@ -2606,6 +2691,7 @@
} }
} }
} }
}
}, },
{ {
"_type": "TypeStmt", "_type": "TypeStmt",
@@ -3013,13 +3099,6 @@
"kind": "PROPERTY", "kind": "PROPERTY",
"name": "age", "name": "age",
"type": { "type": {
"_type": "GenericType",
"type": {
"_type": "NamedType",
"name": "Optional"
},
"args": [
{
"_type": "ConstraintType", "_type": "ConstraintType",
"type": { "type": {
"_type": "NamedType", "_type": "NamedType",
@@ -3028,7 +3107,7 @@
"constraint": { "constraint": {
"_type": "GroupingExpr", "_type": "GroupingExpr",
"expr": { "expr": {
"_type": "BinaryExpr", "_type": "LogicalExpr",
"left": { "left": {
"_type": "BinaryExpr", "_type": "BinaryExpr",
"left": { "left": {
@@ -3040,6 +3119,12 @@
"_type": "WildcardExpr" "_type": "WildcardExpr"
} }
}, },
"operator": "&",
"right": {
"_type": "BinaryExpr",
"left": {
"_type": "WildcardExpr"
},
"operator": "<", "operator": "<",
"right": { "right": {
"_type": "LiteralExpr", "_type": "LiteralExpr",
@@ -3048,7 +3133,6 @@
} }
} }
} }
]
} }
}, },
{ {
@@ -3062,8 +3146,17 @@
"name": "float" "name": "float"
}, },
"constraint": { "constraint": {
"_type": "CallExpr",
"callee": {
"_type": "VariableExpr", "_type": "VariableExpr",
"name": "StrictlyPositive" "name": "StrictlyPositive"
},
"arguments": [
{
"_type": "WildcardExpr"
}
],
"keywords": {}
} }
} }
}, },