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

doc: typeclasses specification. #36

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

griwes
Copy link
Member

@griwes griwes commented Nov 3, 2017

No description provided.


### Basics of typeclasses and instances

1. `tc` is a name of a typeclass. The type of `tc` is `typeclass`, which is a builtin type. All typeclasses are of type
Copy link

Choose a reason for hiding this comment

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

The points here seem very much like they correspond with the line numbers above. Especially since there are 8 of each.

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed, but not pushed yet. The lines are now "named" with letters instead of numbers.

let tc = with (T : type) typeclass { // 1
function fndecl() -> int; // 2

function fndef(arg : int) -> int { // 3
Copy link

Choose a reason for hiding this comment

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

T isn't used here, so its use is less clear. Since you're building this typeclass with int below, it'd be useful to see how T can be used to represent that here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed, not pushed yet.

`typeclass`.
2. `inst` is a name of a typeclass instance. The type of `inst` is `tc`.
3. The type of the unnamed default instance is also `tc`.
4. Values of type that is a typeclass can be used as a type. **Drafting note: this is somewhat unimportant for the time
Copy link

Choose a reason for hiding this comment

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

This is an important point. I think it's also important to have another point saying that typeclasses can be stored as members, or treated no differently than something else you have specified to which you can refer.

Copy link
Member Author

Choose a reason for hiding this comment

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

Your comment is probably a good proof that my sentence here is very confusing. I don't mean values of type tc (in this example); I mean values of type tc(int). I should try to make this clearer, and also write down what you said as a separate point.

Copy link

Choose a reason for hiding this comment

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

I put this comment on this line only because it seemed like a related place to put it. I wasn't thinking that tc alone could be used as a type, but I do see what you mean about the wording. Either way, both are valid points.

This section uses names from the following piece of code to define semantics and requirements:

```
let tc = with (T : type) typeclass { // 1
Copy link

Choose a reason for hiding this comment

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

Only shown here is T : type, but that implies, to me, that T : C is valid. Some examples:

Integer

let bits = with (Bits : int) typeclass {
};

Typeclasses

let bits = with (Bits : int) typeclass {
};

let bits32 = with (Bits : bits(32)) typeclass {
};

More complex usage

let render = with (Dimension : int) typeclass {
    function point(v : vector(Dimension)) -> void;
};

Are all of these valid? If so, I think it'd be worth adding a note and/or an example of a non-type parameter to a typeclass.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think I want value typeclasses to be valid. C++ is trying to do something with value concepts, but I don't think this is reasonable in the model I'm thinking about. This is explicitly meant to be used as a customization point mechanism, which makes no sense for values... I think.

At one point in the future Vapor will have a sort of "template template parameters", then those should be usable as another kind of an argument here. Simiarly you could specify a thing requiring a typeclass to be fulfilled here, but I don't see how values are useful.

tc(int).fndef(123) // returns 123, per definition (3)
inst.fndef(123) // returns 122, per definition (8)
```

Copy link

Choose a reason for hiding this comment

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

I think it'd be worth mentioning what happens when you refer to an incomplete typeclass. That is, in the case of tc, it only has a default for int. So what if I refer to tc(tc(int)) or tc(float) when there is no implementation for .fndecl?

Copy link
Member Author

Choose a reason for hiding this comment

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

Compilation error when trying to say tc(float) because the instance doesn't exist. This also reminds me I haven't actually described the semantics of implicit typeclasses...

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, and of course this is different with the default instance existing. Will write this down.

};

default instance tc(int) { // 4
function fndecl() -> int { // 5

Choose a reason for hiding this comment

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

Instances shouldn't have to (be able to?) explicitly state the type of the function they are implementing, as this is specified by the typeclass which is being implemented, so will be the same for all instances for a particular type.

@griwes
Copy link
Member Author

griwes commented Nov 4, 2017

@jeaye @elliotpotts please check if I've actually addressed your comments in the last commit.

@griwes griwes mentioned this pull request Nov 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants