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

Traits #2320

Merged
merged 42 commits into from
Sep 8, 2023
Merged

Traits #2320

merged 42 commits into from
Sep 8, 2023

Conversation

lukaszcz
Copy link
Collaborator

@lukaszcz lukaszcz commented Aug 28, 2023

Implements a basic trait framework. A simple instance search mechanism is included which fails if there is more than one matching instance at any step.

Example usage:

import Stdlib.Prelude open hiding {Show; mkShow; show};

trait
type Show A :=
  mkShow {
    show : A → String
  };

instance
showStringI : Show String := mkShow (show := id);

instance
showBoolI : Show Bool := mkShow (show := λ{x := if x "true" "false"});

instance
showNatI : Show Nat := mkShow (show := natToString);

showList {A} : {{Show A}} → List A → String
  | nil := "nil"
  | (h :: t) := Show.show h ++str " :: " ++str showList t;

instance
showListI {A} {{Show A}} : Show (List A) := mkShow (show := showList);

showMaybe {A} {{Show A}} : Maybe A → String
  | (just x) := "just (" ++str Show.show x ++str ")"
  | nothing := "nothing";

instance
showMaybeI {A} {{Show A}} : Show (Maybe A) := mkShow (show := showMaybe);

main : IO :=
  printStringLn (Show.show true) >>
  printStringLn (Show.show false) >>
  printStringLn (Show.show 3) >>
  printStringLn (Show.show [true; false]) >>
  printStringLn (Show.show [1; 2; 3]) >>
  printStringLn (Show.show [1; 2]) >>
  printStringLn (Show.show [true; false]) >>
  printStringLn (Show.show [just true; nothing; just false]) >>
  printStringLn (Show.show [just [1]; nothing; just [2; 3]]) >>
  printStringLn (Show.show "abba") >>
  printStringLn (Show.show ["a"; "b"; "c"; "d"]);

It is possible to manually provide an instance and to match on implicit instances:

f {A} : {{Show A}} -> A -> String
  | {{mkShow s}} x -> s x;

f' {A} : {{Show A}} → A → String
  | {{M}} x := Show.show {{M}} x;

The trait parameters in instance types are checked to be structurally decreasing to avoid looping in the instance search. So the following is rejected:

type Box A := box A;

trait
type T A := mkT { pp : A → A };

instance
boxT {A} : {{T (Box A)}} → T (Box A) := mkT (λ{x := x});

We check whether each parameter is a strict subterm of some trait parameter in the target. This ordering is included in the finite multiset extension of the subterm ordering, hence terminating.

@lukaszcz lukaszcz added this to the 0.5 milestone Aug 28, 2023
@lukaszcz lukaszcz self-assigned this Aug 28, 2023
@lukaszcz lukaszcz force-pushed the trait-framework branch 6 times, most recently from cce615a to fb54bb9 Compare September 5, 2023 16:09
@lukaszcz lukaszcz marked this pull request as ready for review September 5, 2023 16:21
@lukaszcz lukaszcz marked this pull request as draft September 5, 2023 16:53
@lukaszcz lukaszcz marked this pull request as ready for review September 6, 2023 09:32
Copy link
Collaborator

@janmasrovira janmasrovira left a comment

Choose a reason for hiding this comment

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

review in progress

lukaszcz and others added 23 commits September 7, 2023 18:32
Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
…oping.hs

Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
…oping.hs

Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
…TypeChecking/Error/Types.hs

Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
…TypeChecking/Error/Types.hs

Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
…TypeChecking/Traits/Resolver.hs

Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
…TypeChecking/Traits/Resolver.hs

Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
…TypeChecking/Traits/Resolver.hs

Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
Copy link
Collaborator

@janmasrovira janmasrovira left a comment

Choose a reason for hiding this comment

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

A big leap for Juvix 🎉

@lukaszcz lukaszcz merged commit 08f123f into main Sep 8, 2023
4 checks passed
@lukaszcz lukaszcz deleted the trait-framework branch September 8, 2023 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Juvix Type classes
2 participants