Skip to content

Commit

Permalink
More examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
athas committed Aug 24, 2024
1 parent a556e81 commit 07ae1aa
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ for plotting or rendering graphics.
- [Outer product](examples/outer-product.html)
- [Comparing arrays for equality](examples/array-equality.html)
- [Searching](examples/searching.html)
- [Option type](examples/option.html)
- [Computing histograms](examples/histograms.html)
- [Moving average](examples/moving-average.html)
- [Means](examples/means.html)
Expand Down Expand Up @@ -79,6 +80,7 @@ for plotting or rendering graphics.
- [Matching parentheses](examples/parens.html)
- [Evaluating polynomials](examples/polynomials.html)
- [Line fitting](examples/line-fitting.html)
- [Decoding MNIST data files](examples/mnist.html)

# Automatic differentiation

Expand Down
34 changes: 34 additions & 0 deletions examples/mnist.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- # Decoding MNIST data files
--
-- The [MNIST database of handwritten
-- digits](https://yann.lecun.com/exdb/mnist/) is a common dataset for
-- demonstrating machine learning techniques. It is distributed in the
-- form of "label fields" and "image files", with a simple binary file
-- format. The functions below decode this file format.

-- First we need a utility function for decoding a four-element byte
-- array as a big-endian integer.

def decode_u32_be (w: [4]u8) =
(u32.u8 w[0] << 24)
| (u32.u8 w[1] << 16)
| (u32.u8 w[2] << 8)
| (u32.u8 w[3] << 0)

-- Then we can define the decoding functions. These use assertions for
-- error reporting. Some applications may prefer to use an [option
-- type](opt.fut) instead.

entry decode_label_file (s: []u8) : []i8 =
let magic = decode_u32_be (take 4 s)
let n = i64.u32 (decode_u32_be (take 4 (drop 4 s)))
in assert (magic==2049)
(map i8.u8 (take n (drop 8 s)))

entry decode_image_file (s: []u8) : [][][]u8 =
let magic = decode_u32_be (take 4 s)
let n = i64.u32 (decode_u32_be (take 4 (drop 4 s)))
let rows = i64.u32 (decode_u32_be (take 4 (drop 8 s)))
let columns = i64.u32 (decode_u32_be (take 4 (drop 12 s)))
let get_img i = unflatten (take (rows*columns) (drop (16+i*rows*columns) s))
in assert (magic==2051) (tabulate n get_img)
16 changes: 16 additions & 0 deletions examples/opt.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- # Option types
--
-- Futhark does not predefine an option type, but we can define on
-- ourselves.

type opt 'a = #some a | #none

-- It can be used for example to define a safe indexing function.

def index_maybe [n] 't (x: [n]t) (i: i64) : opt t =
if i >= 0 && i < n then #some x[i] else #none

-- ## See also
--
-- The [containers](https://github.com/diku-dk/containers) package,
-- which defines an option type and various helper functions.
4 changes: 4 additions & 0 deletions examples/sum-types.fut
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ type dir_2d = #left | #right | #up | #down
-- anonymous type that we have not even bothered naming? The type
-- checker will complain in these cases, and we can solve the problem
-- by adding, for example, a return type annotation to the function.

-- ## See also
--
-- [Option types](opt.fut).

0 comments on commit 07ae1aa

Please sign in to comment.