Created
February 2, 2019 06:23
-
-
Save siraben/bbe3e7509b2f42fb102bcbef9a28cb13 to your computer and use it in GitHub Desktop.
Natural numbers defined inductively in Elm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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