Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto generate def arguments #39

Open
safareli opened this issue Mar 16, 2016 · 18 comments
Open

auto generate def arguments #39

safareli opened this issue Mar 16, 2016 · 18 comments

Comments

@safareli
Copy link
Member

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);
@davidchambers
Copy link
Member

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);

@GeneZharov
Copy link

This is the tool I'd love to use in our project!

@kedashoe
Copy link
Member

kedashoe commented Apr 9, 2016

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.

@davidchambers
Copy link
Member

This looks fantastic, Kevin!

Your concat doesn't constrain a, but I imagine you haven't worked on type-class constraints yet.

@gilligan
Copy link

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 ;)

@kedashoe
Copy link
Member

Yep, just a few kinks to work out and clean things up a bit. I'll shoot for next weekend.

@kedashoe
Copy link
Member

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.

@davidchambers
Copy link
Member

That would be nice too. :)

@gilligan
Copy link

Weekend! I want your awesome plugin!! ;-)

@kedashoe
Copy link
Member

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 😴 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.

@gilligan
Copy link

@kedashoe thanks for sharing! Looking forward to playing around with that a bit!

@davidchase
Copy link

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

@nkrkv
Copy link

nkrkv commented Feb 20, 2017

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.

@davidchambers
Copy link
Member

Very cool, @nkrkv! I look forward to trying hm-def.

@RichardForrester
Copy link

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?

@davidchambers
Copy link
Member

[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. :)

@gilligan
Copy link

@davidchambers ”better late than never”? 😂
👀 2019

@davidchambers
Copy link
Member

Let's just say that my system for managing my inbox is imperfect. 🤪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants