Skip to content

Instantly share code, notes, and snippets.

@siraben
Created February 2, 2019 06:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siraben/bbe3e7509b2f42fb102bcbef9a28cb13 to your computer and use it in GitHub Desktop.
Save siraben/bbe3e7509b2f42fb102bcbef9a28cb13 to your computer and use it in GitHub Desktop.
Natural numbers defined inductively in Elm
module Nat exposing (..)
-- Example of inductively defined data type.
type Nat = O | S Nat
toNat : Int -> Nat
toNat n = case n of
0 -> O
sn -> S (toNat (sn - 1))
fromNat : Nat -> Int
fromNat n = case n of
O -> 0
S np -> 1 + (fromNat np)
-- Nat.toNat 3 evaluates to S (S (S O)) : Nat
-- Nat.fromNat (Nat.toNat 10) evaluates to 10
-- Nat.S Nat.O == Nat.S Nat.O
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment