Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
garyb committed Feb 21, 2015
1 parent 6d83b25 commit 51aa866
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 30 deletions.
158 changes: 130 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,155 @@

## Module Data.Enum

### Types
#### `Cardinality`

newtype Cardinality a where
Cardinality :: Number -> Cardinality a
``` purescript
newtype Cardinality a
= Cardinality Number
```


### Type Classes
#### `runCardinality`

class (Ord a) <= Enum a where
cardinality :: Cardinality a
firstEnum :: a
lastEnum :: a
succ :: a -> Maybe a
pred :: a -> Maybe a
toEnum :: Number -> Maybe a
fromEnum :: a -> Number
``` purescript
runCardinality :: forall a. Cardinality a -> Number
```


### Type Class Instances
#### `Enum`

instance enumBoolean :: Enum Boolean
``` purescript
class (Ord a) <= Enum a where
cardinality :: Cardinality a
firstEnum :: a
lastEnum :: a
succ :: a -> Maybe a
pred :: a -> Maybe a
toEnum :: Number -> Maybe a
fromEnum :: a -> Number
```

instance enumChar :: Enum Char
Type class for enumerations. This should not be considered a part of a
numeric hierarchy, ala Haskell. Rather, this is a type class for small,
ordered sum types with statically-determined cardinality and the ability
to easily compute successor and predecessor elements. e.g. `DayOfWeek`, etc.

instance enumEither :: (Enum a, Enum b) => Enum (Either a b)
Laws:

instance enumMaybe :: (Enum a) => Enum (Maybe a)
- ```succ firstEnum >>= succ >>= succ ... succ [cardinality - 1 times] == lastEnum```
- ```pred lastEnum >>= pred >>= pred ... pred [cardinality - 1 times] == firstEnum```
- ```e1 `compare` e2 == fromEnum e1 `compare` fromEnum e2```
- ```forall a > firstEnum: pred a >>= succ == Just a```
- ```forall a < lastEnum: succ a >>= pred == Just a```
- ```pred >=> succ >=> pred = pred```
- ```succ >=> pred >=> succ = succ```
- ```toEnum (fromEnum a) = Just a```
- ```forall a > firstEnum: fromEnum <$> pred a = Just (fromEnum a - 1)```
- ```forall a < lastEnum: fromEnum <$> succ a = Just (fromEnum a + 1)```

instance enumTuple :: (Enum a, Enum b) => Enum (Tuple a b)
#### `defaultSucc`

``` purescript
defaultSucc :: forall a. (Number -> Maybe a) -> (a -> Number) -> a -> Maybe a
```

### Values
```defaultSucc toEnum fromEnum = succ```

defaultFromEnum :: forall a. (a -> Maybe a) -> a -> Number
#### `defaultPred`

defaultPred :: forall a. (Number -> Maybe a) -> (a -> Number) -> a -> Maybe a
``` purescript
defaultPred :: forall a. (Number -> Maybe a) -> (a -> Number) -> a -> Maybe a
```

defaultSucc :: forall a. (Number -> Maybe a) -> (a -> Number) -> a -> Maybe a
```defaultPred toEnum fromEnum = pred```

defaultToEnum :: forall a. (a -> Maybe a) -> a -> Number -> Maybe a
#### `defaultToEnum`

enumFromThenTo :: forall a. (Enum a) => a -> a -> a -> [a]
``` purescript
defaultToEnum :: forall a. (a -> Maybe a) -> a -> Number -> Maybe a
```

enumFromTo :: forall a. (Enum a) => a -> a -> [a]
Runs in `O(n)` where `n` is `fromEnum a`

intFromTo :: Number -> Number -> [Number]
```defaultToEnum succ firstEnum = toEnum```

intStepFromTo :: Number -> Number -> Number -> [Number]
#### `defaultFromEnum`

runCardinality :: forall a. Cardinality a -> Number
``` purescript
defaultFromEnum :: forall a. (a -> Maybe a) -> a -> Number
```

Runs in `O(n)` where `n` is `fromEnum a`

```defaultFromEnum pred = fromEnum```

#### `enumFromTo`

``` purescript
enumFromTo :: forall a. (Enum a) => a -> a -> [a]
```

Property: ```fromEnum a = a', fromEnum b = b' => forall e', a' <= e' <= b': Exists e: toEnum e' = Just e```

Following from the propery of `intFromTo`, we are sure all elements in `intFromTo (fromEnum a) (fromEnum b)` are `Just`s.

#### `enumFromThenTo`

``` purescript
enumFromThenTo :: forall a. (Enum a) => a -> a -> a -> [a]
```

`[a,b..c]`

Correctness for using `fromJust` is the same as for `enumFromTo`.

#### `intFromTo`

``` purescript
intFromTo :: Number -> Number -> [Number]
```

Property: ```forall e in intFromTo a b: a <= e <= b```

#### `intStepFromTo`

``` purescript
intStepFromTo :: Number -> Number -> Number -> [Number]
```

Property: ```forall e in intStepFromTo step a b: a <= e <= b```

#### `enumChar`

``` purescript
instance enumChar :: Enum Char
```

## Instances

#### `enumMaybe`

``` purescript
instance enumMaybe :: (Enum a) => Enum (Maybe a)
```


#### `enumBoolean`

``` purescript
instance enumBoolean :: Enum Boolean
```


#### `enumTuple`

``` purescript
instance enumTuple :: (Enum a, Enum b) => Enum (Tuple a b)
```


#### `enumEither`

``` purescript
instance enumEither :: (Enum a, Enum b) => Enum (Either a b)
```
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
],
"dependencies": {
"purescript-maybe": "~0.2.1",
"purescript-tuples": "~0.2.3",
"purescript-tuples": "~0.3.0",
"purescript-either": "~0.1.4",
"purescript-strings": "~0.4.2",
"purescript-unfoldable": "~0.2.0"
"purescript-unfoldable": "~0.3.0"
}
}

0 comments on commit 51aa866

Please sign in to comment.