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

Update the standard library to use the trait framework #2359

Merged
merged 8 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 39 additions & 48 deletions tests/Compilation/positive/test052.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ module test052;

import Stdlib.Prelude open;

import Stdlib.Trait.Show as Show;

fromShow : {a : Type} -> Show a -> a -> String
| (mkShow s) a := s a;

LambdaTy : Type -> Type := Lambda';

AppTy : Type -> Type := App';
Expand All @@ -22,12 +17,11 @@ type Expr' (v : Type) :=
| num : Nat -> Expr' v;

--- A ;Lambda'; is a lambda function.
type Lambda' (v : Type) :=
| mkLambda : Expr' v -> Lambda' v;
type Lambda' (v : Type) := mkLambda : Expr' v -> Lambda' v;

--- An ;App'; is an application of two ;Expr';.
type App' (v : Type) :=
| mkApp : Expr' v -> Expr' v -> App' v;
mkApp : Expr' v -> Expr' v -> App' v;

Expr : Type := Expr' Nat;

Expand All @@ -39,7 +33,7 @@ ValTy : Type -> Type := Val';

--- A ;Closure'; is a context (;List; of ;ValTy;) and an ;Expr'; in that context.
type Closure' (v : Type) :=
| mkClosure : List (ValTy v) -> Expr' v -> Closure' v;
mkClosure : List (ValTy v) -> Expr' v -> Closure' v;

--- A ;Val'; is either a ;Closure'; or a ;Nat;.
type Val' (v : Type) :=
Expand All @@ -55,41 +49,39 @@ type Either (a : Type) (b : Type) :=
| left : a -> Either a b
| right : b -> Either a b;

module ExprTraits;
toString : Expr -> String
| (! v) := "[" ++str natToString v ++str "]"
| (lam (mkLambda b)) := "λ {" ++str toString b ++str "}"
| (app (mkApp l r)) :=
"("
++str toString l
++str " # "
++str toString r
++str ")"
| (l :+: r) :=
"("
++str toString l
++str " + "
++str toString r
++str ")"
| (num n) := natToString n;

--- ;Show; instance for ;Expr;.
Show : Show.Show Expr := mkShow toString;
end;

module ValTraits;
terminating
valToString : Val -> String
| (vnum n) := natToString n
| (closure (mkClosure ctx n)) :=
fromShow (ListTraits.Show Show) ctx
++str " ⊢ "
++str ExprTraits.toString n;

--- ;Show; instance for ;Val;.
terminating
Show : Show.Show Val := mkShow valToString;
end;
exprToString : Expr -> String
| (! v) := "[" ++str natToString v ++str "]"
| (lam (mkLambda b)) := "λ {" ++str exprToString b ++str "}"
| (app (mkApp l r)) :=
"("
++str exprToString l
++str " # "
++str exprToString r
++str ")"
| (l :+: r) :=
"("
++str exprToString l
++str " + "
++str exprToString r
++str ")"
| (num n) := natToString n;

--- ;Show; instance for ;Expr;.
instance
showExprI : Show (Expr' Nat) := mkShow exprToString;

terminating
valToString : Val -> String
| (vnum n) := natToString n
| (closure (mkClosure ctx n)) :=
Show.show {{showListI {{showValI}}}} ctx
++str " ⊢ "
++str Show.show n;

--- ;Show; instance for ;Val;.
terminating
instance
showValI : Show (Val' Nat) := mkShow valToString;

syntax operator >>= seq;
--- Monadic binding for ;Either;.
Expand Down Expand Up @@ -193,11 +185,10 @@ main : IO :=
("ScopeError: "
++str natToString n
++str ", ctx = "
++str fromShow (ListTraits.Show ValTraits.Show) ctx
++str Show.show ctx
++str "\n")
| left (ExpectedNat e) :=
printStringLn
("Expected a natural but got "
++str ValTraits.valToString e)
("Expected a natural but got " ++str Show.show e)
| left ExpectedLambda := printStringLn "ExpectedLambda"
| right r := printStringLn (ValTraits.valToString r);
| right r := printStringLn (Show.show r);
2 changes: 1 addition & 1 deletion tests/Compilation/positive/test061.juvix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- Traits
module test061;

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

trait
type Show A :=
Expand Down
4 changes: 2 additions & 2 deletions tests/positive/Format.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ hiding -- Hide some names
{-- like this
,; -- don't want , here
-- Bool either
Bool; true; false; mkShow};
Bool; true; false; mkShow; module Show};

import Stdlib.Data.Nat.Ord open;

Expand Down Expand Up @@ -264,7 +264,7 @@ end;

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

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