Skip to content

Commit

Permalink
Merge pull request #5745 from roc-lang/abilities-syntax
Browse files Browse the repository at this point in the history
New Abilities syntax
  • Loading branch information
Anton-4 authored Aug 11, 2023
2 parents 2bd998a + 954f687 commit 743c4a7
Show file tree
Hide file tree
Showing 162 changed files with 1,611 additions and 1,515 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions crates/cli_testing_examples/benchmarks/AStar.roc
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ Model position : {
openSet : Set position,
costs : Dict position F64,
cameFrom : Dict position position,
} | position has Hash & Eq
} where position implements Hash & Eq

initialModel : position -> Model position | position has Hash & Eq
initialModel : position -> Model position where position implements Hash & Eq
initialModel = \start -> {
evaluated: Set.empty {},
openSet: Set.single start,
costs: Dict.single start 0,
cameFrom: Dict.empty {},
}

cheapestOpen : (position -> F64), Model position -> Result position {} | position has Hash & Eq
cheapestOpen : (position -> F64), Model position -> Result position {} where position implements Hash & Eq
cheapestOpen = \costFn, model ->
model.openSet
|> Set.toList
Expand All @@ -35,13 +35,13 @@ cheapestOpen = \costFn, model ->
|> Result.map .position
|> Result.mapErr (\_ -> {})

reconstructPath : Dict position position, position -> List position | position has Hash & Eq
reconstructPath : Dict position position, position -> List position where position implements Hash & Eq
reconstructPath = \cameFrom, goal ->
when Dict.get cameFrom goal is
Err _ -> []
Ok next -> List.append (reconstructPath cameFrom next) goal

updateCost : position, position, Model position -> Model position | position has Hash & Eq
updateCost : position, position, Model position -> Model position where position implements Hash & Eq
updateCost = \current, neighbor, model ->
newCameFrom =
Dict.insert model.cameFrom neighbor current
Expand Down Expand Up @@ -70,7 +70,7 @@ updateCost = \current, neighbor, model ->
else
model

astar : (position, position -> F64), (position -> Set position), position, Model position -> Result (List position) {} | position has Hash & Eq
astar : (position, position -> F64), (position -> Set position), position, Model position -> Result (List position) {} where position implements Hash & Eq
astar = \costFn, moveFn, goal, model ->
when cheapestOpen (\source -> costFn source goal) model is
Err {} -> Err {}
Expand Down
8 changes: 4 additions & 4 deletions crates/compiler/builtins/roc/Bool.roc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface Bool
## be a `NaN` ([Not a Number](https://en.wikipedia.org/wiki/NaN)), and the
## [IEEE-754](https://en.wikipedia.org/wiki/IEEE_754) floating point standard
## specifies that two `NaN`s are not equal.
Eq has
Eq implements
## Returns `Bool.true` if the input values are equal. This is
## equivalent to the logic
## [XNOR](https://en.wikipedia.org/wiki/Logical_equality) gate. The infix
Expand All @@ -30,11 +30,11 @@ Eq has
## for more detail.
## 5. Functions cannot be compared for structural equality, therefore Roc
## cannot derive `isEq` for types that contain functions.
isEq : a, a -> Bool | a has Eq
isEq : a, a -> Bool where a implements Eq

## Represents the boolean true and false using an opaque type.
## `Bool` implements the `Eq` ability.
Bool := [True, False] has [Eq { isEq: boolIsEq }]
Bool := [True, False] implements [Eq { isEq: boolIsEq }]

boolIsEq = \@Bool b1, @Bool b2 -> structuralEq b1 b2

Expand Down Expand Up @@ -116,7 +116,7 @@ not : Bool -> Bool
## expect (Bool.false != Bool.false) == Bool.false
## expect "Apples" != "Oranges"
## ```
isNotEq : a, a -> Bool | a has Eq
isNotEq : a, a -> Bool where a implements Eq
isNotEq = \a, b -> structuralNotEq a b

# INTERNAL COMPILER USE ONLY: used to lower calls to `isEq` to structural
Expand Down
52 changes: 26 additions & 26 deletions crates/compiler/builtins/roc/Decode.roc
Original file line number Diff line number Diff line change
Expand Up @@ -73,38 +73,38 @@ DecodeResult val : { result : Result val DecodeError, rest : List U8 }
## Decodes a `List U8` of utf-8 bytes where `val` is the type of the decoded
## value, and `fmt` is a [Decoder] which implements the [DecoderFormatting]
## ability
Decoder val fmt := List U8, fmt -> DecodeResult val | fmt has DecoderFormatting
Decoder val fmt := List U8, fmt -> DecodeResult val where fmt implements DecoderFormatting

## Definition of the [Decoding] ability
Decoding has
decoder : Decoder val fmt | val has Decoding, fmt has DecoderFormatting
Decoding implements
decoder : Decoder val fmt where val implements Decoding, fmt implements DecoderFormatting

## Definition of the [DecoderFormatting] ability
DecoderFormatting has
u8 : Decoder U8 fmt | fmt has DecoderFormatting
u16 : Decoder U16 fmt | fmt has DecoderFormatting
u32 : Decoder U32 fmt | fmt has DecoderFormatting
u64 : Decoder U64 fmt | fmt has DecoderFormatting
u128 : Decoder U128 fmt | fmt has DecoderFormatting
i8 : Decoder I8 fmt | fmt has DecoderFormatting
i16 : Decoder I16 fmt | fmt has DecoderFormatting
i32 : Decoder I32 fmt | fmt has DecoderFormatting
i64 : Decoder I64 fmt | fmt has DecoderFormatting
i128 : Decoder I128 fmt | fmt has DecoderFormatting
f32 : Decoder F32 fmt | fmt has DecoderFormatting
f64 : Decoder F64 fmt | fmt has DecoderFormatting
dec : Decoder Dec fmt | fmt has DecoderFormatting
bool : Decoder Bool fmt | fmt has DecoderFormatting
string : Decoder Str fmt | fmt has DecoderFormatting
list : Decoder elem fmt -> Decoder (List elem) fmt | fmt has DecoderFormatting
DecoderFormatting implements
u8 : Decoder U8 fmt where fmt implements DecoderFormatting
u16 : Decoder U16 fmt where fmt implements DecoderFormatting
u32 : Decoder U32 fmt where fmt implements DecoderFormatting
u64 : Decoder U64 fmt where fmt implements DecoderFormatting
u128 : Decoder U128 fmt where fmt implements DecoderFormatting
i8 : Decoder I8 fmt where fmt implements DecoderFormatting
i16 : Decoder I16 fmt where fmt implements DecoderFormatting
i32 : Decoder I32 fmt where fmt implements DecoderFormatting
i64 : Decoder I64 fmt where fmt implements DecoderFormatting
i128 : Decoder I128 fmt where fmt implements DecoderFormatting
f32 : Decoder F32 fmt where fmt implements DecoderFormatting
f64 : Decoder F64 fmt where fmt implements DecoderFormatting
dec : Decoder Dec fmt where fmt implements DecoderFormatting
bool : Decoder Bool fmt where fmt implements DecoderFormatting
string : Decoder Str fmt where fmt implements DecoderFormatting
list : Decoder elem fmt -> Decoder (List elem) fmt where fmt implements DecoderFormatting

## `record state stepField finalizer` decodes a record field-by-field.
##
## `stepField` returns a decoder for the given field in the record, or
## `Skip` if the field is not a part of the decoded record.
##
## `finalizer` should produce the record value from the decoded `state`.
record : state, (state, Str -> [Keep (Decoder state fmt), Skip]), (state -> Result val DecodeError) -> Decoder val fmt | fmt has DecoderFormatting
record : state, (state, Str -> [Keep (Decoder state fmt), Skip]), (state -> Result val DecodeError) -> Decoder val fmt where fmt implements DecoderFormatting

## `tuple state stepElem finalizer` decodes a tuple element-by-element.
##
Expand All @@ -113,7 +113,7 @@ DecoderFormatting has
## index passed to `stepElem` is 0-indexed.
##
## `finalizer` should produce the tuple value from the decoded `state`.
tuple : state, (state, Nat -> [Next (Decoder state fmt), TooLong]), (state -> Result val DecodeError) -> Decoder val fmt | fmt has DecoderFormatting
tuple : state, (state, Nat -> [Next (Decoder state fmt), TooLong]), (state -> Result val DecodeError) -> Decoder val fmt where fmt implements DecoderFormatting

## Build a custom [Decoder] function. For example the implementation of
## `decodeBool` could be defined as follows;
Expand All @@ -125,11 +125,11 @@ DecoderFormatting has
## ['t', 'r', 'u', 'e', ..] -> { result: Ok Bool.true, rest: List.drop bytes 4 }
## _ -> { result: Err TooShort, rest: bytes }
## ```
custom : (List U8, fmt -> DecodeResult val) -> Decoder val fmt | fmt has DecoderFormatting
custom : (List U8, fmt -> DecodeResult val) -> Decoder val fmt where fmt implements DecoderFormatting
custom = \decode -> @Decoder decode

## Decode a `List U8` utf-8 bytes using a specific [Decoder] function
decodeWith : List U8, Decoder val fmt, fmt -> DecodeResult val | fmt has DecoderFormatting
decodeWith : List U8, Decoder val fmt, fmt -> DecodeResult val where fmt implements DecoderFormatting
decodeWith = \bytes, @Decoder decode, fmt -> decode bytes fmt

## Decode a `List U8` utf-8 bytes and return a [DecodeResult](#DecodeResult)
Expand All @@ -141,7 +141,7 @@ decodeWith = \bytes, @Decoder decode, fmt -> decode bytes fmt
##
## actual.result == expected
## ```
fromBytesPartial : List U8, fmt -> DecodeResult val | val has Decoding, fmt has DecoderFormatting
fromBytesPartial : List U8, fmt -> DecodeResult val where val implements Decoding, fmt implements DecoderFormatting
fromBytesPartial = \bytes, fmt -> decodeWith bytes decoder fmt

## Decode a `List U8` utf-8 bytes and return a [Result] with no leftover bytes
Expand All @@ -155,7 +155,7 @@ fromBytesPartial = \bytes, fmt -> decodeWith bytes decoder fmt
##
## actual == expected
## ```
fromBytes : List U8, fmt -> Result val [Leftover (List U8)]DecodeError | val has Decoding, fmt has DecoderFormatting
fromBytes : List U8, fmt -> Result val [Leftover (List U8)]DecodeError where val implements Decoding, fmt implements DecoderFormatting
fromBytes = \bytes, fmt ->
when fromBytesPartial bytes fmt is
{ result, rest } ->
Expand Down
Loading

0 comments on commit 743c4a7

Please sign in to comment.