Skip to content

Commit

Permalink
remove tutorial trailing whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
srush committed Jan 19, 2021
1 parent 72934fe commit 68676d5
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions examples/tutorial.dx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ include "plot.dx"

' The analogous table construct in Dex is written in the following form. It
produces a one-dimensional table of `Height x Width` elements. Here `&`
indicates a tuple constructor.
indicates a tuple constructor.

y = for (i, j) : (Height & Width) . 1.0

Expand Down Expand Up @@ -119,7 +119,7 @@ mean y
r = x.3

' Instead, it is necessary to cast the integer into the index type of the
current shape. This type annotation is done with the `@` operator.
current shape. This type annotation is done with the `@` operator.

:t x

Expand All @@ -131,7 +131,7 @@ row = x.(3 @ Height)

' If you are feeling lazy and sure of yourself, you can also let Dex infer
the type for you. This is also how `for` works in the examples above that
did not provide and explicit type annotation.
did not provide and explicit type annotation.

:t row.(5 @ _)

Expand Down Expand Up @@ -162,7 +162,7 @@ gradient = for i:Height. for j:Width. IToF ((ordinal i) + (ordinal j))

:t y.(3 @ Height, 5 @ Width)

' Tuple indices also provide an ordinal value.
' Tuple indices also provide an ordinal value.

for pair:(Fin 2 & Fin 3). ordinal pair

Expand Down Expand Up @@ -194,7 +194,7 @@ for pair:(Fin 2 & Fin 3). ordinal pair
' The `mean` function works independently of the index type of the table.

' Let's see how we can define our own table functions. Defining a function in
Dex uses the following syntax.
Dex uses the following syntax.

def add5 (x:Float32) : Float32 =
x + 5.0
Expand Down Expand Up @@ -224,12 +224,12 @@ def tabAdd5 (x : n => Float32) : n => Float32 =
are statically known, type checking can ensure that table arguments have valid
dimensions. This is "shape safety".

' Imagine we have `transpose` function. We can encode the shape change in the type.
' Imagine we have `transpose` function. We can encode the shape change in the type.

def transFloat (x : m => n => Float32) : n => m => Float32 =
for i. for j. x.j.i

' We can even make it more generic by abstracting over the value type.
' We can even make it more generic by abstracting over the value type.

def trans (x : m => n => v) : n => m => v =
for i. for j. x.j.i
Expand Down Expand Up @@ -273,15 +273,15 @@ Full = Fin ((size Batch) * (size IHeight) * (size IWidth))

' To do this we will use Dex's IO to load some images from a file.
This section uses features outside the scope of the tutorial, so you can
ignore it for now.
ignore it for now.

def pixel (x:Char) : Float32 =
r = W8ToI x
IToF case r < 0 of
True -> (abs r) + 128
False -> r

def getIm : Batch => Image =
def getIm : Batch => Image =
(AsList _ im) = unsafeIO do readFile "examples/mnist.bin"
raw = unsafeCastTable Full im
for b: Batch i j.
Expand Down Expand Up @@ -320,9 +320,9 @@ imscolor = for i. for j. for c:Channels. ims.((ordinal c)@_).i.j
' This one shows all the images on one channel over the base plot.

imscolor2 = for b. for i. for j. for c:Channels.
case ordinal c == 0 of
case ordinal c == 0 of
True -> (sum ims).i.j / (IToF (size Batch))
False -> ims.b.i.j
False -> ims.b.i.j

:html imseqshow (imscolor2 / 255.0)

Expand All @@ -333,7 +333,7 @@ imscolor2 = for b. for i. for j. for c:Channels.

def split (x: m=>v) : n=>o=>v =
for i j. x.(ordinal (i,j)@_)

def imtile (x: a=>b=>v) : n=>o=>p=>q=>v =
for kh kw h w. (split (split x).h.kh).w.kw

Expand Down Expand Up @@ -373,17 +373,17 @@ im2 : Fin 4 => Fin 4 => Fin 7 => Fin 7 => Float32 = imtile ims.(0@_)

def tabMean (x : n => Float32) : Float32 =

-- acc = 0
-- acc = 0
withState 0.0 $ \acc.

-- for i in range(len(x))
for i.
-- acc = acc + x[i]
acc := (get acc) + x.i

-- return acc / len(x)
(get acc) / (IToF (size n))
(get acc) / (IToF (size n))

tabMean [0.0, 1.0, 0.5]

' So, even though Dex is a functional language, it is possible to write loops
Expand All @@ -409,7 +409,7 @@ tabMean [0.0, 1.0, 0.5]

:t \ x. x + 10

(\ x. x + 10) 20
(\ x. x + 10) 20


' That leaves: `withState`. This function uses the `State` effect, enabling us
Expand Down Expand Up @@ -472,7 +472,7 @@ tabMean (for (i, j). (x.i.j, x.i.j))
where `a` is in the type class `VSpace`.

' If we look in the prelude we can see that these type class interfaces are
defined as:
defined as:

interface Add a
add : a -> a -> a
Expand All @@ -483,8 +483,8 @@ interface Add a
interface [Add a] VSpace a
scaleVec : Float -> a -> a

' They tell us which functions we need to define for a type to work with them.
It it relatively straightforward to add a new instance.
' They tell us which functions we need to define for a type to work with them.
It it relatively straightforward to add a new instance.

' Here is an interface for adding float pairs.

Expand All @@ -506,7 +506,7 @@ def tabMean2 [VSpace v] (x : n => v) : v =
withState zero $ \acc.
for i.
acc := (get acc) + x.i
(get acc) / (IToF (size n))
(get acc) / (IToF (size n))

tabMean2 [0.1, 0.5, 1.0]

Expand Down Expand Up @@ -575,7 +575,7 @@ double = for b i j. [ims.b.i.j, ims.(nearest.b).i.j, 0.0]
know the exact size of our tables. This is a
common assumption in array languages, but
it makes some operations surprisingly difficult
to do.
to do.

' For instance, we might want to filter our set of
images to only allow for images of 5's. But what is
Expand All @@ -592,10 +592,10 @@ AsList _ $ for i:Height. 0.0
:t AsList _ $ for i:Height. 0.0

' Tables of lists can be concatenated down to
single lists.
single lists.

z = concat [AsList _ [3.0],
AsList _ [1.0, 2.0 ]]
AsList _ [1.0, 2.0 ]]
z

' And they can be deconstructed to fetch a new table.
Expand All @@ -605,7 +605,7 @@ temptab

' Using this construct we can return to extracting
the 5's from the image set. Here `mempty` is
synonymous with `AsList _ []`.
synonymous with `AsList _ []`.

def findFives (x : a=>b) (y : a=>Class) : List b =
concat for i. case (y.i == (5 @ _)) of
Expand All @@ -615,9 +615,9 @@ def findFives (x : a=>b) (y : a=>Class) : List b =

' Note though that the type here does not tell us
how many 5's there are. The type system cannot know this.
To figure it out we need to unpack the list.
To figure it out we need to unpack the list.

temp = findFives all_ims all_labels
temp = findFives all_ims all_labels
(AsList nFives fives) = temp

nFives
Expand All @@ -642,7 +642,7 @@ nFives
' Here are some topics to check out in the Prelude:

' * Randomness and Keys
* Laziness of For Comprehensions
* Laziness of For Comprehensions
* Records and Variant Types
* File IO
* Effects Beyond State

0 comments on commit 68676d5

Please sign in to comment.