Commit cb90bdfb authored by Tim Steenvoorden's avatar Tim Steenvoorden
Browse files

implement basic classes for all arrays

- shared definitions are in Data.Array.Internal
- specialized instances for {#Char}
- uses slightly modified _SystemArray module:
    - array <> createArray
    - unsafeArray <> _createUnsafeArray
    - sliceString (later maybe selectRange?)
parent 96c077b9
......@@ -3,6 +3,8 @@ definition module Data.Array
from Algebra.Order import class Eq, class Ord
from Algebra.Group import class Semigroup, class Monoid
from Text.Show import class Show
import _SystemArray
/// # Definition
......@@ -12,8 +14,10 @@ import _SystemArray
/// # Instances
instance Show {a} | Show a
instance Eq {a} | Eq a
instance Ord {a} | Ord a
// instance Semigroup {a}
// instance Monoid {a}
instance Semigroup {a}
instance Monoid {a}
implementation module Data.Array
import Data.Function
import Algebra.Order
import Algebra.Group
import _SystemArray
import Data.Array.Internal
/// # Instances
instance Show {a} | Show a where
show xs = showArray "" xs
instance Eq {a} | Eq a where
(==) xs ys = undefined
(==) xs ys = eqArray xs ys
instance Ord {a} | Ord a where
(<) xs ys = undefined
(<) xs ys = ltArray xs ys
instance Semigroup {a} where
(+) xs ys = concatArray xs ys
// instance Semigroup {a}
// instance Monoid {a}
instance Monoid {a} where
neutral = emptyArray
definition module Data.Array.Internal
import Data.Bool
import Data.Char
import Data.Nat
import Data.Int
import Data.Real
import Data.Enum
import Data.Array.Unboxed
import Algebra.Order
import Algebra.Group
import Text.Show
import _SystemArray
/// # Macros
// showArray :: {a} -> String | Show a
showArray symbol xs
| size xs == 0 :== "{" + symbol + "}"
| otherwise :== "{" + symbol + show xs.[0] + go 1
where
go i
| i < size xs = "," + show xs.[i] + go (succ i)
| otherwise = "}"
// eqArray :: {a} {a} -> Bool | Eq a
eqArray xs ys
| size xs /= size ys :== False
| size xs == 0 :== True
| otherwise :== go 0
where
go i
| i < size xs = xs.[i] == ys.[i] && go (succ i)
| otherwise = True
// ltArray :: {a} {a} -> Bool | Ord a
ltArray xs ys
| size xs > size ys :== False
| otherwise :== go 0
where
go i
| i < size xs
| xs.[i] < ys.[i] = True
| xs.[i] == ys.[i] = go (succ i)
| otherwise = False
| otherwise = size xs < size ys
// concatArray :: {a} {a} -> {a}
concatArray xs ys
// new = array (size xs + size ys) neutral
# new = unsafeArray (size xs + size ys)
# new = { new & [i] = xs.[i] \\ i <- [0..pred (size xs)] }
# new = { new & [i + size xs] = ys.[i] \\ i <- [0..pred (size ys)] }
:== new
// emptyArray :: {a}
emptyArray :== { }
implementation module Data.Array.Internal
definition module Data.Array.Strict
from Algebra.Order import class Eq, class Ord
from Algebra.Group import class Semigroup, class Monoid
from Text.Show import class Show
import _SystemArray
/// # Definition
......@@ -9,8 +14,10 @@ import _SystemArray
/// # Instances
// instance Eq {!e} | Eq e
// instance Ord {!e} | Ord e
//
// instance Semigroup {!e}
// instance Monoid {!e}
instance Show {!a} | Show a
instance Eq {!a} | Eq a
instance Ord {!a} | Ord a
instance Semigroup {!a}
instance Monoid {!a}
implementation module Data.Array.Strict
import Data.Array.Internal
/// # Instances
instance Show {!e} | Show e where
show xs = showArray "!" xs
instance Eq {!e} | Eq e where
(==) xs ys = eqArray xs ys
instance Ord {!e} | Ord e where
(<) xs ys = ltArray xs ys
instance Semigroup {!e} where
(+) xs ys = concatArray xs ys
instance Monoid {!e} where
neutral = emptyArray
......@@ -15,31 +15,31 @@ import _SystemArray
/// # Instances
instance Show {#Bool}
instance Show {#Char}
// instance Show {#Nat}
instance Show {#Int}
instance Show {#Real}
instance Eq {#Bool}
instance Eq {#Char}
// instance Eq {#Nat}
instance Eq {#Int}
instance Eq {#Real}
instance Ord {#Bool}
instance Ord {#Char}
// instance Ord {#Nat}
instance Ord {#Int}
instance Ord {#Real}
instance Semigroup {#Bool}
instance Semigroup {#Char}
// instance Semigroup {#Nat}
instance Semigroup {#Int}
instance Semigroup {#Real}
instance Monoid {#Bool}
instance Show {#Char}
instance Eq {#Char}
instance Ord {#Char}
instance Semigroup {#Char}
instance Monoid {#Char}
// instance Show {#Nat}
// instance Eq {#Nat}
// instance Ord {#Nat}
// instance Semigroup {#Nat}
// instance Monoid {#Nat}
instance Show {#Int}
instance Eq {#Int}
instance Ord {#Int}
instance Semigroup {#Int}
instance Monoid {#Int}
instance Show {#Real}
instance Eq {#Real}
instance Ord {#Real}
instance Semigroup {#Real}
instance Monoid {#Real}
implementation module Data.Array.Unboxed
import Data.Bool
// import Data.Nat
import Data.Int
import Data.Real
import Data.Enum
import Data.Array.Internal
import Algebra.Order
import Algebra.Group
/// # Specializations
import Text.Show
/// ## Char
import _SystemArray
/// ## Instances
instance Show {#Bool} where
show xs = showUnboxedArray xs
instance Show {#Char} where
show xs = code inline {
no_op
}
// instance Show {#Nat} where
// show xs = showUnboxedArray xs
instance Show {#Int} where
show xs = showUnboxedArray xs
instance Show {#Real} where
show xs = showUnboxedArray xs
// showUnboxedArray :: {#a} -> String | Show a
showUnboxedArray xs
| size xs == 0 :== "{#}"
| otherwise :== "{#" + show xs.[0] + go 1
where
go i
| i < size xs = "," + show xs.[i] + go (succ i)
| otherwise = "}"
instance Eq {#Bool} where
(==) xs ys = eqUnboxedArray xs ys
instance Eq {#Char} where
(==) xs ys = code inline {
.d 2 0
jsr eqAC
.o 0 1 b
}
// instance Eq {#Nat} where
// (==) xs ys = eqUnboxedArray xs ys
instance Eq {#Int} where
(==) xs ys = eqUnboxedArray xs ys
instance Eq {#Real} where
(==) xs ys = eqUnboxedArray xs ys
// eqUnboxedArray :: {#a} {#a} -> Bool | Eq a
eqUnboxedArray xs ys
| size xs /= size ys :== False
| size xs == 0 :== True
| otherwise :== go 0
where
go i
| i < size xs = xs.[i] == ys.[i] && go (succ i)
| otherwise = True
instance Ord {#Bool} where
(<) xs ys = ltUnboxedArray xs ys
instance Ord {#Char} where
(<) xs ys = code inline {
.d 2 0
......@@ -72,58 +26,67 @@ instance Ord {#Char} where
pushI 0
gtI
}
// instance Ord {#Nat} where
// (<) xs ys = ltUnboxedArray xs ys
instance Ord {#Int} where
(<) xs ys = ltUnboxedArray xs ys
instance Ord {#Real} where
(<) xs ys = ltUnboxedArray xs ys
// ltUnboxedArray :: {#a} {#a} -> Bool | Ord a
ltUnboxedArray xs ys
| size xs > size ys :== False
| otherwise :== go 0
where
go i
| i < size xs
| xs.[i] < ys.[i] = True
| xs.[i] == ys.[i] = go (succ i)
| otherwise = False
| otherwise = size xs < size ys
instance Semigroup {#Bool} where
(+) xs ys = concatUnboxedArray xs ys
instance Semigroup {#Char} where
(+) xs ys = code inline {
.d 2 0
jsr catAC
.o 1 0
}
// instance Semigroup {#Nat} where
// (+) xs ys = concatUnboxedArray xs ys
instance Semigroup {#Int} where
(+) xs ys = concatUnboxedArray xs ys
instance Semigroup {#Real} where
(+) xs ys = concatUnboxedArray xs ys
// concatUnboxedArray :: {#a} {#a} -> {#a}
concatUnboxedArray xs ys #
// new = array (size xs + size ys) neutral
new = unsafeArray (size xs + size ys)
new = { new & [i] = xs.[i] \\ i <- [0..pred (size xs)] }
new = { new & [i + size xs] = ys.[i] \\ i <- [0..pred (size ys)] }
:== new
instance Monoid {#Char} where
neutral = code inline {
buildAC ""
}
/// ## Bool
instance Show {#Bool} where
show xs = showArray "#" xs
instance Eq {#Bool} where
(==) xs ys = eqArray xs ys
instance Ord {#Bool} where
(<) xs ys = ltArray xs ys
instance Semigroup {#Bool} where
(+) xs ys = concatArray xs ys
instance Monoid {#Bool} where
neutral = emptyUnboxedArray
instance Monoid {#Char} where
neutral = emptyUnboxedArray
neutral = emptyArray
/// ## Nat
// instance Show {#Nat} where
// show xs = showArray "#" xs
// instance Eq {#Nat} where
// (==) xs ys = eqArray xs ys
// instance Ord {#Nat} where
// (<) xs ys = ltArray xs ys
// instance Semigroup {#Nat} where
// (+) xs ys = concatArray xs ys
// instance Monoid {#Nat} where
// neutral = emptyUnboxedArray
// neutral = emptyArray
/// ## Int
instance Show {#Int} where
show xs = showArray "#" xs
instance Eq {#Int} where
(==) xs ys = eqArray xs ys
instance Ord {#Int} where
(<) xs ys = ltArray xs ys
instance Semigroup {#Int} where
(+) xs ys = concatArray xs ys
instance Monoid {#Int} where
neutral = emptyUnboxedArray
instance Monoid {#Real} where
neutral = emptyUnboxedArray
neutral = emptyArray
/// ## Real
// emptyUnboxedArray :: {#a}
emptyUnboxedArray = {# }
instance Show {#Real} where
show xs = showArray "#" xs
instance Eq {#Real} where
(==) xs ys = eqArray xs ys
instance Ord {#Real} where
(<) xs ys = ltArray xs ys
instance Semigroup {#Real} where
(+) xs ys = concatArray xs ys
instance Monoid {#Real} where
neutral = emptyArray
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment