Skip to content
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

Support for type classes #5446

Open
wdanilo opened this issue Feb 5, 2023 · 2 comments
Open

Support for type classes #5446

wdanilo opened this issue Feb 5, 2023 · 2 comments
Assignees
Labels
-parser p-low Low priority x-new-feature Type: new feature request

Comments

@wdanilo
Copy link
Member

wdanilo commented Feb 5, 2023

Enso doesn't have special syntax for writing type classes as of Oct 2024. The original design...

Implementation and tests for type classes, as described in the design doc. WARNING: the design doc needs to be confirmed before implementing this task!

...hasn't yet been implemented. Anyway this is the current blueprint to follow when writing type classes as of Oct 2024. There is also

@JaroslavTulach
Copy link
Member

The blueprint to write type classes in Enso as for Oct 2024:

Define API and "Dictionary"

Here is a way to define Set type class:

from Standard.Base import Error
from Standard.Base.Errors.Common import Type_Error
from  Standard.Base.Errors import Illegal_State

type Set a
    Value o s:a

    is_empty self = self.o.is_empty self.s
    contains self e = self.o.contains self.s e
    length self = self.o.length self.s
    insert self e = Set.Value self.o <| self.o.insert self.s e
    union self (other:Set) = if self.o != other.o then Error.throw (Type_Error "Incompatible sets") else
        Set.Value self.o <| self.o.union self.s other.s

type Operator a
    is_empty (_ : Set) = Error.Throw (Illegal_State.Error "is_empty not implemented")
    contains (_ : Set) _ = Error.Throw (Illegal_State.Error "contains not implemented")
    length (_ : Set) _ = Error.Throw (Illegal_State.Error "length not implemented")
    insert (_ : Set) _ = Error.Throw (Illegal_State.Error "insert not implemented")
    union (_ : Set) _ = Error.Throw (Illegal_State.Error "union not implemented")

The type Set defines is_empty, contains, length, insert and union operations. Once one has such a set up, one can define an implementation.

Implementation with Vector Backing Store

Let's use Vector Any to store the elements as shown here:

from Standard.Base import Vector

import project.Set.Set


type Vector_Impl a
    empty = Set.Value Vector_Impl []

    is_empty (elements : Vector) = elements.length == 0
    contains (elements : Vector) e = elements.contains e
    length (elements : Vector) = elements.length
    insert (elements : Vector) e = if elements.contains e then elements else elements + [ e ]
    union (us : Vector) (them : Vector) = (us + them).distinct

the preferred way of creating type Set is to define a conversion to turn any Vector into a Set:

Set.from (that:Vector) = Vector_Impl.empty . insert that

Using Set

To use the set one can start with a conversion and they operate on the set as desired:

v = [1, 2, 3]
s = v:Set
four = s . insert 4
four . length . should_equal 4

@JaroslavTulach
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
-parser p-low Low priority x-new-feature Type: new feature request
Projects
Status: New
Development

No branches or pull requests

3 participants