sanctuary-def
sanctuary-def copied to clipboard
auto generate `def` arguments
it will be nice to have babel plugin or something like that to transform code from this
// concatS :: String -> String -> String
const concatS = (x, y) => x + y
to this:
// concatS :: String -> String -> String
const concatS =
def('concatS', {}, [$.String, $.String, $.String], (x, y) => x + y);
That would be very cool indeed!
It could even transform…
// concat :: Semigroup a => a -> a -> a
const concat = (x, y) => x + y;
into…
// concat :: Semigroup a => a -> a -> a
const concat =
def('concat',
{a: [Semigroup]},
[a, a, a],
(x, y) => x + y);
This is the tool I'd love to use in our project!
fwiw I'm chipping away at this when I can. Got some stuff working, currently turns
// foo :: Integer -> Integer
var foo = function(x) {
return x + 10;
};
// concat :: a -> a -> a
const concat = (x, y) => x.concat(y);
// head :: [a] -> Maybe a
const head = x => x.length === 0 ? Nothing() : Just(x[0]);
into
// foo :: Integer -> Integer
var foo = def("foo", {}, [$.Integer, $.Integer], function (x) {
return x + 10;
});;
// concat :: a -> a -> a
const concat = def("concat", {}, [a, a, a], (x, y) => x.concat(y));;
// head :: [a] -> Maybe a
const head = def("head", {}, [$.Array(a), $.Maybe(a)], x => x.length === 0 ? Nothing() : Just(x[0]));;
Does that all look correct (I'm in a bit of a hurry at the moment, I've hardly even looked at the results)? (I notice an extra ;
is sneaking in haha)
I think there are a lot of options/decisions/different directions we can go, so I'm just trying to get a minimum useful product up and then we can make all those fun decisions together.
This looks fantastic, Kevin!
Your concat
doesn't constrain a
, but I imagine you haven't worked on type-class constraints yet.
Hey @kedashoe i was having the exact same thought that babel plugin would be great and then was pointed to this issue when I asked on gitter. Could you possibly make your current work-in-progress plugin code available? Would love to look at it and ideally contribute too. I just wouldn't want to start from zero knowing have something that works ;)
Yep, just a few kinks to work out and clean things up a bit. I'll shoot for next weekend.
Another thought, I saw something like this recently, can't remember where now, but a similar thing we could either add to sanctuary-def or to another lib:
const head = hmDef(
'head :: [a] -> Maybe a',
x => x.length === 0 ? Nothing() : Just(x[0])
);
and hmDef
would take care of converting the hindley-milner string to the corresponding call to sanctuary-def. Might be nice for those who can't/don't want to use babel.
That would be nice too. :)
Weekend! I want your awesome plugin!! ;-)
Weekend! I want your awesome plugin!! ;-)
haha wait until you see it before you call it awesome ;)
In any event, the WIP is up here: https://github.com/kedashoe/babel-sanctuary-def
which depends on this (also just written, also a WIP): https://github.com/kedashoe/hindley-milner-parser-js
I chatted with @davidchambers briefly, I think the plan is to move these under the sanctuary-js org, so I don't plan on publishing an npm package for now. You can just depend on it via github, eg
"devDependencies": { "babel-sanctuary-def": "git+https://github.com/kedashoe/babel-sanctuary-def.git" }
I have quite a bit more to say, but it's already well past my bedtime :sleeping: I will be posting some issues of my own on both repos tomorrow. If you can't wait to get started contributing, just run node test/test.js
to run the tests.
@kedashoe thanks for sharing! Looking forward to playing around with that a bit!
which depends on this (also just written, also a WIP) :https://github.com/kedashoe/hindley-milner-parser-js
@kedashoe indeed thanks for sharing 👍 very interesting i've chatted with some folks and thought about creating a babel-plugin that uses the HM comments to provide a way to check your js (akin to flow, etc) code while you are developing
its good to see progress in a similar direction
For those who’re interested in hmDef
concept by @kedashoe: https://github.com/xodio/hm-def
It’s not quite complete regarding complex cases but usable enough for usual scenarios. Works fine on a hundred of functions in our project.
Very cool, @nkrkv! I look forward to trying hm-def.
This was the first thing I thought of when reading about babel macros. Seems like the perfect size project for a macro.
It's been a couple years since this got attention: hm-def
seems broken (perhaps there was a change in the api) and it's not clear how far we got with the Babel plugin.
I would be interested in contributing to this if it's still being worked on or perhaps forking anything that could renew interest.
The idea of being able to get sanctuary-def
run-time type checking with simple Hindley–Milner comments is worth attention. It might also be a good size step toward auto-generating static typings for Typescript and Flow referenced in #95 .
Quick question, is there anything sanctuary-def
is used to express/represent that is not able to be represented in Hindley-Milner or is it intended to be one-to-one?
[I]s there anything
sanctuary-def
is used to express/represent that is not able to be represented in Hindley-Milner or is it intended to be one-to-one?
It is intended to be one-to-one. :)
@davidchambers ”better late than never”? 😂 👀 2019
Let's just say that my system for managing my inbox is imperfect. :zany_face: