Created
October 8, 2018 15:25
-
-
Save safareli/edd342da6b16f05df8257a71990e96a0 to your computer and use it in GitHub Desktop.
Example of Data.Exists like encoding of existentials with TypeClass constraints
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
"use strict"; | |
var Control_Category = require("../Control.Category"); | |
var Data_Foldable = require("../Data.Foldable"); | |
var Data_Monoid = require("../Data.Monoid"); | |
var Prelude = require("../Prelude"); | |
var Unsafe_Coerce = require("../Unsafe.Coerce"); | |
var runFoldableExists = function (f) { | |
return function (e) { | |
return e(function (dictFoldable) { | |
return f(dictFoldable); | |
}); | |
}; | |
}; | |
var y = runFoldableExists(function (dictFoldable) { | |
return function (z1) { | |
return Data_Foldable.foldMap(dictFoldable)(Data_Monoid.monoidString)(Control_Category.id(Control_Category.categoryFn))(z1); | |
}; | |
}); | |
var mkFoldableExists = function (dictFoldable) { | |
return function (x1) { | |
var g = function (f) { | |
return f(dictFoldable)(x1); | |
}; | |
return g; | |
}; | |
}; | |
var x = mkFoldableExists(Data_Foldable.foldableArray)([ "foo", "bar" ]); | |
var z = y(x); | |
module.exports = { | |
mkFoldableExists: mkFoldableExists, | |
runFoldableExists: runFoldableExists, | |
x: x, | |
y: y, | |
z: z | |
}; |
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 Foo where | |
import Prelude | |
import Unsafe.Coerce (unsafeCoerce) | |
import Data.Foldable (class Foldable, foldMap) | |
foreign import data FoldableExists :: Type -> Type | |
type OnFoldableExists f a r = Foldable f => f a -> r | |
mkFoldableExists | |
:: forall f a | |
. OnFoldableExists f a (FoldableExists a) | |
mkFoldableExists x = unsafeCoerce g | |
where | |
g :: forall r. OnFoldableExists f a r -> r | |
g f = f x | |
runFoldableExists | |
:: forall a r | |
. (forall f. OnFoldableExists f a r) | |
-> FoldableExists a | |
-> r | |
runFoldableExists f e = e' f | |
where | |
e' :: (forall f. OnFoldableExists f a r) -> r | |
e' = unsafeCoerce e | |
x :: FoldableExists String | |
x = mkFoldableExists ["foo","bar"] | |
y :: FoldableExists String -> String | |
y = runFoldableExists \z -> foldMap id z | |
z :: String | |
z = y x -- "foobar" |
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
"use strict"; | |
var Data_Foldable = require("../Data.Foldable"); | |
var Data_Monoid = require("../Data.Monoid"); | |
var Data_Show = require("../Data.Show"); | |
var Prelude = require("../Prelude"); | |
var Unsafe_Coerce = require("../Unsafe.Coerce"); | |
var runFoldableShowExists = function (f) { | |
return function (e) { | |
return e(function (dictFoldable) { | |
return function (dictShow) { | |
return f(dictFoldable)(dictShow); | |
}; | |
}); | |
}; | |
}; | |
var y = runFoldableShowExists(function (dictFoldable) { | |
return function (dictShow) { | |
return function (z1) { | |
return Data_Foldable.foldMap(dictFoldable)(Data_Monoid.monoidString)(Data_Show.show(dictShow))(z1); | |
}; | |
}; | |
}); | |
var mkFoldableShowExists = function (dictFoldable) { | |
return function (dictShow) { | |
return function (x1) { | |
var g = function (f) { | |
return f(dictFoldable)(dictShow)(x1); | |
}; | |
return g; | |
}; | |
}; | |
}; | |
var x = mkFoldableShowExists(Data_Foldable.foldableArray)(Data_Show.showInt)([ 1, 2 ]); | |
var z = y(x); | |
module.exports = { | |
mkFoldableShowExists: mkFoldableShowExists, | |
runFoldableShowExists: runFoldableShowExists, | |
x: x, | |
y: y, | |
z: z | |
}; |
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 FoldableShowExists where | |
import Prelude | |
import Unsafe.Coerce (unsafeCoerce) | |
import Data.Foldable (class Foldable, foldMap) | |
foreign import data FoldableShowExists :: Type | |
type OnFoldableShowExists f a r = Foldable f => Show a => f a -> r | |
mkFoldableShowExists | |
:: forall f a | |
. OnFoldableShowExists f a FoldableShowExists | |
mkFoldableShowExists x = unsafeCoerce g | |
where | |
g :: forall r. OnFoldableShowExists f a r -> r | |
g f = f x | |
runFoldableShowExists | |
:: forall r | |
. (forall f a. OnFoldableShowExists f a r) | |
-> FoldableShowExists | |
-> r | |
runFoldableShowExists f e = e' f | |
where | |
e' :: (forall f a. OnFoldableShowExists f a r) -> r | |
e' = unsafeCoerce e | |
x :: FoldableShowExists | |
x = mkFoldableShowExists [1, 2] | |
y :: FoldableShowExists -> String | |
y = runFoldableShowExists \z -> foldMap show z | |
z :: String | |
z = y x -- "12" | |
https://medium.com/@jonathangfischoff/existential-quantification-patterns-and-antipatterns-3b7b683b7d71
https://lukepalmer.wordpress.com/2010/01/24/haskell-antipattern-existential-typeclass/
http://okmij.org/ftp/Computation/Existentials.html
https://stackoverflow.com/a/13653533/1798418
https://kseo.github.io/posts/2016-12-19-encoding-existentials.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nested forall can be used for describing
Data.Exists
but with cost of extra heap allocation