Skip to content
Snippets Groups Projects
Commit c6ae388f authored by Marcos Viera's avatar Marcos Viera
Browse files

sinonimos asociados

parent 64c5e163
No related branches found
No related tags found
No related merge requests found
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
-- type functions
-- study on C++, Standard ML, Haskell, Eiffel, Java, Generic C
class CollectionFD c e | c -> e where
emptyFD :: c
insertFD :: e -> c -> c
toListFD :: c -> [e]
instance CollectionFD [a] a where
emptyFD = []
insertFD = (:)
toListFD = id
class Collection c where
type Elem c
empty :: c
insert :: Elem c -> c -> c
toList :: c -> [Elem c]
-- ZipWith
data Zero = Zero
data Suc n = Suc n
type N3 = Suc (Suc (Suc Zero))
n3 :: N3
n3 = Suc (Suc (Suc Zero))
data VNil a = VNil
data VCons v a = VCons a (v a)
type Vector4 = VCons (VCons (VCons (VCons VNil))) :: * -> *
class VRepeat v where
vRepeat :: a -> v a
instance VRepeat VNil where
vRepeat _ = VNil
instance VRepeat v => VRepeat (VCons v) where
vRepeat a = VCons a (vRepeat a)
class VList v where
vToList :: v a -> [a]
instance VList VNil where
vToList VNil = []
instance VList v => VList (VCons v) where
vToList (VCons a v) = a : vToList v
class VApply v where
vApply :: v (a -> b) -> v a -> v b
instance VApply VNil where
vApply VNil VNil = VNil
instance VApply v => VApply (VCons v) where
vApply (VCons f vf) (VCons a va) = VCons (f a) (vApply vf va)
-- class Applicative f where
-- <*> :: f (a -> b) -> f a -> f b
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
-- zipWith :: (a -> b -> c -> d) -> v a -> v b -> v c -> v d
-- | @VZipWithFD n v s t@ is satisfyiable only for
-- types @s@ of the form @a1 -> a2 -> ... -> an -> r@
-- and @t@ of the form
--
-- > v a1 -> v a2 -> ... -> v an -> v r
--
-- where @v@ is some type of vector.
--
class VZipWithFD n v s t
| n t -> v
-- , n v s -> t
where
vManyApp :: n -> v s -> t
vZipWith :: n -> s -> t
instance VRepeat v =>
VZipWithFD Zero v t (v t) where
vManyApp Zero fs = fs
vZipWith Zero a = vRepeat a
instance (VRepeat v, VApply v, VZipWithFD n v t ts) =>
VZipWithFD (Suc n) v (s -> t) (v s -> ts) where
-- v (a -> ... -> z) -> v a -> ... -> v z
vManyApp (Suc n) fs ss = vManyApp n (vApply fs ss {- :: v (b -> .. -> z) -> v b -> ... -> v z-})
vZipWith n f = vManyApp n (vRepeat f)
testVZipWithFD :: Vector4 Int
testVZipWithFD =
vZipWith
n3
(\a b c -> a * b + c :: Int)
(VCons 1 $ VCons 2 $ VCons 3 $ VCons 4 VNil :: Vector4 Int)
(VCons 5 $ VCons 6 $ VCons 7 $ VCons 8 VNil :: Vector4 Int)
(VCons (-1) $ VCons (-2) $ VCons (-3) $ VCons (-4) VNil :: Vector4 Int)
vZipWithFD3
:: VZipWithFD
N3
Vector4
(a -> b -> c -> d)
( Vector4 a
-> Vector4 b
-> Vector4 c
-> Vector4 d
)
=> (a -> b -> c -> d)
-> Vector4 a
-> Vector4 b
-> Vector4 c
-> Vector4 d
vZipWithFD3 = vZipWith n3
testVZipWithFD3 :: Vector4 Int
testVZipWithFD3 =
vZipWithFD3
(\a b c -> a * b + c)
(VCons 1 $ VCons 2 $ VCons 3 $ VCons 4 VNil)
(VCons 5 $ VCons 6 $ VCons 7 $ VCons 8 VNil)
(VCons (-1) $ VCons (-2) $ VCons (-3) $ VCons (-4) VNil)
-- | @VZipWith n t@ is satisfyiable only for types @t@
-- of the form
--
-- > v a1 -> v a2 -> ... -> v an -> v r
--
-- where @v ~ Vec n t@ is some type of vector.
--
-- > ZippedFun n (v a1 -> v a2 -> ... -> v an -> v r)
-- > = a1 -> a2 -> ... -> an -> r
--
class VZipWith n t where
type Vec n t :: * -> *
type ZippedFun n t
vManyApp' :: n -> Vec n t (ZippedFun n t) -> t
vZipWith' :: n -> ZippedFun n t -> t
instance VRepeat v => VZipWith Zero (v t) where
type Vec Zero (v t) = v
type ZippedFun Zero (v t) = t
vManyApp' Zero fs = fs
vZipWith' Zero a = vRepeat a
instance (VRepeat v, VApply v, VZipWith n ts, Vec n ts ~ v) =>
VZipWith (Suc n) (v s -> ts) where
type Vec (Suc n) (v s -> ts) = v
type ZippedFun (Suc n) (v s -> ts) = s -> ZippedFun n ts
vManyApp' (Suc n) fs ss = vManyApp' n (vApply fs ss)
vZipWith' n f = vManyApp' n (vRepeat f)
testVZipWith :: Vector4 Int
testVZipWith =
vZipWith'
n3
(\a b c -> a * b + c)
(VCons 1 $ VCons 2 $ VCons 3 $ VCons 4 VNil)
(VCons 5 $ VCons 6 $ VCons 7 $ VCons 8 VNil)
(VCons (-1) $ VCons (-2) $ VCons (-3) $ VCons (-4) VNil)
vZipWith3
:: ( VZipWith N3 t
, t ~ (Vector4 a -> Vector4 b -> Vector4 c -> Vector4 d)
)
=> ZippedFun N3 t -> t
vZipWith3 = vZipWith' n3
testVZipWith3 :: Vector4 Int
testVZipWith3 =
vZipWith3
(\a b c -> a * b + c)
(VCons 1 $ VCons 2 $ VCons 3 $ VCons 4 VNil)
(VCons 5 $ VCons 6 $ VCons 7 $ VCons 8 VNil)
(VCons (-1) $ VCons (-2) $ VCons (-3) $ VCons (-4) VNil)
-- (1) restricted to type constructors and variables
-- (2) specific
-- (3) non-overlapping
-- (4) Smaller context
instance Collection [a] where
type Elem [a] = a
empty = []
insert = (:)
toList = id
-- wrt FD
-- * type checking limitations
-- * readability
-- * expressiveness
class C a b | a -> b where
foo :: a -> b
instance C Bool Int where
foo False = 0
foo True = 1
-- bar :: C a b => a -> b
bar :: C Bool b => Bool -> b
bar = foo
data T = T
deriving Show
baz :: Show T => T -> String
baz = show
{-
class Zip a b c | a c -> b, b c -> a
class Zip1 a b c | a c -> b
class Zip2 a b c | b c -> a
class (Zip1 a b c, Zip2 a b c) => Zip3 a b c
f :: Zip3 a b c => a -> b -- qué instancia?
f = undefined
-}
-- open vs closed
class Nat n where
type Add n m
instance Nat Zero where
type Add Zero m = m
instance Nat n => Nat (Suc n) where
type Add (Suc n) m = Suc (Add n m)
class Nat n => VecBound n where
data NVec n a
appVec :: NVec n a -> NVec m a -> NVec (Add n m) a
instance VecBound Zero where
data NVec Zero a = Nil
appVec Nil ys = ys
instance VecBound n => VecBound (Suc n) where
data NVec (Suc n) a = Cons a (NVec n a)
appVec (Cons x xs) ys = Cons x (appVec xs ys)
-- data types vs type synonyms
merge :: (Collection c1, Collection c2, Elem c1 ~ Elem c2)
=> c1 -> c2 -> c2
merge = undefined
-- index :: Array e -> Int -> e
-- index = undefined
-- typechecking with equalities
-- and type synonyms
-- f :: c ~ Int => Elem c -> c -> Int
-- f _ _ = 0
--
-- main = print (f undefined [])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment