Skip to content

Instantly share code, notes, and snippets.

@globulon
Created June 10, 2019 14:35
Show Gist options
  • Select an option

  • Save globulon/46d66ba810d0a1132ef0e4fa8e436ba6 to your computer and use it in GitHub Desktop.

Select an option

Save globulon/46d66ba810d0a1132ef0e4fa8e436ba6 to your computer and use it in GitHub Desktop.
Differences with Scala Implicits
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ConstrainedClassMethods #-}
module Hylo(CoAlgebra(..), Anamorphism(..), Algebra(..), Catamorphism(..)) where
newtype CoAlgebra x u = CoAlgebra {
unfold :: u -> Either () (x, u)
}
class Anamorphism m where
ana :: CoAlgebra x u -> u -> m x
{-# MINIMAL ana #-}
data Algebra t u = Algebra {
empty :: u,
fold :: t -> u -> u
}
class Catamorphism m where
cata :: Algebra x u -> m x -> u
{-# MINIMAL cata #-}
class Hylomorphism where
hylo :: (Anamorphism m, Catamorphism m) => CoAlgebra x u -> Algebra x v -> u -> v
{-|
Prelude> :l Hylo.hs
[1 of 1] Compiling Hylo ( Hylo.hs, interpreted )
Hylo.hs:24:3: error:
• Could not deduce (Anamorphism m0)
from the context: (Hylomorphism, Anamorphism m, Catamorphism m)
bound by the type signature for:
hylo :: Hylomorphism =>
forall (m :: * -> *) x u v.
(Anamorphism m, Catamorphism m) =>
CoAlgebra x u -> Algebra x v -> u -> v
at Hylo.hs:24:3-83
The type variable ‘m0’ is ambiguous
• In the ambiguity check for ‘hylo’
To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
When checking the class method:
hylo :: Hylomorphism =>
forall (m :: * -> *) x u v.
(Anamorphism m, Catamorphism m) =>
CoAlgebra x u -> Algebra x v -> u -> v
In the class declaration for ‘Hylomorphism’
|
24 | hylo :: (Anamorphism m, Catamorphism m) => CoAlgebra x u -> Algebra x v -> u -> v
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment