Skip to content

Commit

Permalink
Added instances for Add, Mul, and VSpace of functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
duvenaud authored and apaszke committed Feb 9, 2022
1 parent 70915f8 commit a0b5067
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/prelude.dx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ instance Add Unit
sub = \x y. ()
zero = ()

instance [Add a] Add (n->a)
add = \f g. \x. (f x) + (g x)
sub = \f g. \x. (f x) - (g x)
zero = \x. zero

'#### Mul
Things that can be multiplied.
This defines the `Mul` [Monoid](https://en.wikipedia.org/wiki/Monoid), and its operator.
Expand Down Expand Up @@ -186,6 +191,10 @@ instance Mul Unit
mul = \x y. ()
one = ()

instance [Mul a] Mul (n->a)
mul = \f g. \x. (f x) * (g x)
one = \x. one

'#### Integral
Integer-like things.

Expand Down Expand Up @@ -348,6 +357,9 @@ instance VSpace Float
instance {a n} [VSpace a] VSpace (n=>a)
scaleVec = \s xs. for i. s .* xs.i

instance [VSpace a] VSpace (n->a)
scaleVec = \s f. \x. s .* (f x)

instance VSpace Unit
scaleVec = \_ _. ()

Expand Down Expand Up @@ -1715,6 +1727,9 @@ Type class for generating example values
interface Arbitrary a
arb : Key -> a

instance Arbitrary Bool
arb = \key. rand key < 0.5

instance Arbitrary Float32
arb = randn

Expand Down
13 changes: 13 additions & 0 deletions tests/type-tests.dx
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,16 @@ def q (n : Int) : (Fin n)=>Int =

q 5
> [0, 2, 4, 6, 8]

'### Tests for function VSpace

:p (sin + cos) 5.0 ~~ (\x. sin x + cos x) 5.0
> True
:p (sin * cos) 5.0 ~~ (\x. sin x * cos x) 5.0
> True
:p (2.8 .* sin) 5.0 ~~ (\x. 2.8 * sin x) 5.0
> True

xbool : Bool = arb $ newKey 0
:p xbool
> True

0 comments on commit a0b5067

Please sign in to comment.