Skip to content

Commit

Permalink
Add FromNatural trait in Juvix.Builtin.V1
Browse files Browse the repository at this point in the history
The FromNatural trait will be used for the Bytes type.

We want the following properties for Byte:

1. Values of the Bytes type to be assignable from a non-negative numeric
literal.
2. We don't want to implement + and * for it.

Currently, in order for a type to have property 1. it must have an
instance of `Natural` so property 2. can't be satisfied.

To solve this we split the `from-nat` builtin from the `Natural` trait
into a new trait `FromNatural`.

A coercion instance is defined between Natural and FromNatural so you
don't need to write instances for both traits.

For this PR the new `FromNatural` trait is unused. I will make the
changes to juvix-stdlib to use the new V2 modules when I incorporate
compiler changes for the Bytes type.

This change is backwards compatible for existing Juvix code so we don't
need to make a new Juvix.Builtin.V2 path in package-base.
  • Loading branch information
paulcadman committed Jul 30, 2024
1 parent 1e9850c commit 2025370
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 10 additions & 0 deletions include/package-base/Juvix/Builtin/V1/Trait/FromNatural.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Juvix.Builtin.V1.Trait.FromNatural;

import Juvix.Builtin.V1.Nat.Base open using {Nat};

trait
type FromNatural A :=
mkFromNatural {
builtin from-nat
fromNat : Nat -> A
};
5 changes: 4 additions & 1 deletion include/package-base/Juvix/Builtin/V1/Trait/Natural.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Juvix.Builtin.V1.Trait.Natural;

import Juvix.Builtin.V1.Nat.Base open using {Nat};
import Juvix.Builtin.V1.Fixity open;
import Juvix.Builtin.V1.Trait.FromNatural open;

trait
type Natural A :=
Expand All @@ -12,8 +13,10 @@ type Natural A :=
syntax operator * multiplicative;
{-# isabelle-operator: {name: "*", prec: 70, assoc: left} #-}
* : A -> A -> A;
builtin from-nat
fromNat : Nat -> A
};

open Natural public;

coercion instance
FromNaturalNaturalI {A} {{N : Natural A}} : FromNatural A := mkFromNatural@{fromNat};

0 comments on commit 2025370

Please sign in to comment.