sanctuary-def icon indicating copy to clipboard operation
sanctuary-def copied to clipboard

auto generate `def` arguments

Open safareli opened this issue 8 years ago • 18 comments

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

safareli avatar Mar 16 '16 20:03 safareli

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

davidchambers avatar Mar 16 '16 20:03 davidchambers

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

GeneZharov avatar Mar 27 '16 13:03 GeneZharov

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.

kedashoe avatar Apr 09 '16 00:04 kedashoe

This looks fantastic, Kevin!

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

davidchambers avatar Apr 09 '16 17:04 davidchambers

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

gilligan avatar Apr 15 '16 16:04 gilligan

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

kedashoe avatar Apr 15 '16 16:04 kedashoe

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.

kedashoe avatar Apr 22 '16 05:04 kedashoe

That would be nice too. :)

davidchambers avatar Apr 22 '16 05:04 davidchambers

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

gilligan avatar Apr 23 '16 13:04 gilligan

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 avatar Apr 24 '16 06:04 kedashoe

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

gilligan avatar Apr 24 '16 06:04 gilligan

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

davidchase avatar Oct 19 '16 20:10 davidchase

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.

nkrkv avatar Feb 20 '17 13:02 nkrkv

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

davidchambers avatar Feb 20 '17 14:02 davidchambers

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?

RichardForrester avatar Jul 16 '19 07:07 RichardForrester

[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 avatar Apr 15 '21 11:04 davidchambers

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

gilligan avatar Apr 15 '21 11:04 gilligan

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

davidchambers avatar Apr 15 '21 11:04 davidchambers