Church BooleansX B SsmOo234 LmCc 1 Rل YDd 2yz Uu MmH 0WKre Z7d Z

13
\\$\\begingroup\\$

Church booleans

A Church boolean is a function that returns x for true and y for false where x is the first argument to the function and y is the second argument to the function. Further functions can be composed from these functions which represent the and not or xor and implies logical operations.

Challange

Construct the Church booleans and and not or xor and implies Church gates in a language of your choice. And or and xor should take in two functions (representing Church booleans) and return a function (representing another Church boolean). Likewise, not should invert a the function it takes and the implies gate should perform boolean implies logic where the first argument implies the second.

Scoring

The total length of all of the code required to make Church true and false in your language and the and not or xor and implies Church gates excluding the function's name. (for example, false=lambda x,y:y in Python would be 13 bytes). You can reuse these names later in your code with them counting 1 byte toward the byte total of that gate.

Pseudo code Examples:

The functions you create should be able to be called later in your code like so.

true(x, y) -> x
false(x, y) -> y
and(true, true)(x, y) -> x
and(true, false)(x, y) -> y
# ... etc
share|improve this question
New contributor
Ryan Schaefer is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\\$\\endgroup\\$
  • \\$\\begingroup\\$ Do we have to treat the function inputs (or closest substitutes) as black-box functions, or can we inspect the code within? And must the return values of the logical operations be the same functions as previously defined as the Church booleans, or can they be something else which does the same thing? \\$\\endgroup\\$ – Unrelated String 12 hours ago
  • \\$\\begingroup\\$ @UnrelatedString the inputs to the function are essentially a cat program that copy input to output, but based on if they are true or false. For example, a true church boolean takes in two inputs (which chould be "foo" and 3, basically anything) and returns the first argument ("foo"). They don't have to be the exact functions you previously described, but I would imagine it would save a lot of space if you did this. \\$\\endgroup\\$ – Ryan Schaefer 12 hours ago
  • \\$\\begingroup\\$ @JonathanAllan I added it later, and I am regretting it. I should've just kept it to purely gates. \\$\\endgroup\\$ – Ryan Schaefer 10 hours ago
  • 1
    \\$\\begingroup\\$ @JonathanAllan I edited it so it was correct. The prompt is as it should be now. \\$\\endgroup\\$ – Ryan Schaefer 10 hours ago
  • 1
    \\$\\begingroup\\$ Can we take lists as arguments (e.g. true([x, y]), and([true, true])([x, y]))? \\$\\endgroup\\$ – ar4093 1 hour ago

6 Answers 6

active oldest votes
13
\\$\\begingroup\\$

Haskell, 50 - 6 = 44 bytes

-1 byte thanks to Khuldraeseth na'Barya, and -1 byte thanks to Christian Sievers.

t=const
f=n t
n=flip
a=n n f
o=($t)
x=(n>>=)
i=o.n

Try it online!

share|improve this answer
\\$\\endgroup\\$
  • \\$\\begingroup\\$ Side note: you can define Show instances for const and const id and print the church booleans directly. Try it online!. \\$\\endgroup\\$ – nimi 10 hours ago
  • \\$\\begingroup\\$ a can be shortened. \\$\\endgroup\\$ – Khuldraeseth na'Barya 9 hours ago
  • 3
    \\$\\begingroup\\$ Why is no one using f=n t? \\$\\endgroup\\$ – Christian Sievers 7 hours ago
  • 2
    \\$\\begingroup\\$ You can save a byte by using t=pure instead of t=const. \\$\\endgroup\\$ – Joseph Sible 5 hours ago
  • \\$\\begingroup\\$ @JosephSible I tried that initially. Unfortunately, t=pure will cause an error when I try to apply a, o, x, or i to it. Declaring the type of t to fix this costs more bytes than just using t=const. \\$\\endgroup\\$ – Nitrodon 3 hours ago
4
\\$\\begingroup\\$

Python 2, 133 - 6 = 127 94 bytes

exec"t!u;f!v;n!u(f,t);a!u(v,f);o!u(t,v);x!u(n(v),v);i!o(v,n(u))".replace('!','=lambda u,v=0:')

Try it online!

Shamelessly stealing the sneaky idea behind Jonathan Allan's answer; no bytes deducted though.

share|improve this answer
\\$\\endgroup\\$
  • \\$\\begingroup\\$ I was going to post an answer to my own question but I was unsure if this was allowed and I think that this is against the spirit of it. So I think I will just guide you along instead. Instead of using lists, is there anyway you can use the functions that are being input themselves and the specific manner in which they return their inputs to shorten the code? \\$\\endgroup\\$ – Ryan Schaefer 12 hours ago
  • \\$\\begingroup\\$ I'd wager that although the answer is yes, it would be considerably longer in Python. \\$\\endgroup\\$ – Unrelated String 12 hours ago
  • \\$\\begingroup\\$ I stand corrected \\$\\endgroup\\$ – Unrelated String 12 hours ago
  • \\$\\begingroup\\$ @Mr.Xcoder you are correct, I had the wrong number of bytes for the example function. They would be allowed to remove 6 bytes though for the names of the functions. \\$\\endgroup\\$ – Ryan Schaefer 10 hours ago
  • \\$\\begingroup\\$ @Mr. Xcoder: Modified as per your observations. \\$\\endgroup\\$ – Chas Brown 6 hours ago
3
\\$\\begingroup\\$

JavaScript (Node.js), 92 - 7 = 85 bytes

t=p=>q=>p
f=p=>q=>q
n=p=>q=>r=>p(r)(q)
a=p=>q=>p(q)(f)
o=p=>p(t)
x=p=>p(n)(f())
i=p=>o(n(p))

Try it online! Link includes basic test cases.

share|improve this answer
\\$\\endgroup\\$
  • \\$\\begingroup\\$ @Mr.Xcoder That's not how I understood the scoring system, although it's changed again since so I restored 7 bytes anyway. \\$\\endgroup\\$ – Neil 10 hours ago
  • 1
    \\$\\begingroup\\$ Will n=p=>p(f)(t) work? \\$\\endgroup\\$ – tsh 3 hours ago
  • 1
    \\$\\begingroup\\$ Will a=p=>n(p)(f) work? \\$\\endgroup\\$ – tsh 3 hours ago
3
\\$\\begingroup\\$

Python 2, 101 - 3 = 98 bytes

David Beazley eat your heart out!

exec'=lambda x,y=0'.join('F :y;T :x;N :x(F,T);A :x(y,F);O :x(T,y);X :x(N(y),y);I :O(y,N(x))'.split())

Try it online!

I think 101 - 3 because I don't reuse the functions A, X, or I, but I use a single = for assignment (in front of lambda). Maybe I cant remove any?

share|improve this answer
\\$\\endgroup\\$
  • \\$\\begingroup\\$ @Ryan Schaefer can I remove three or does my use of exec mean I cannot? I can see going either way - I don't reuse A, X, or I but the code wont work without them. (Maybe I even get to remove 3.5?!) \\$\\endgroup\\$ – Jonathan Allan 9 hours ago
  • \\$\\begingroup\\$ 95 bytes (before the -3) \\$\\endgroup\\$ – Chas Brown 4 hours ago
1
\\$\\begingroup\\$

J, 67 bytes - 7 = 60

t=.[
f=.]
n=.~
a=.2 :'u v]'
o=.2 :'[u v'
x=.2 :'u~v u'
i=.2 :'v u['

Try it online!

Worth noting:

Higher order functions work differently in J than in a functional language. To create a new verb from 1 or 2 existing verbs, you need to use either an adverb (in the case of 1) or a conjunction (in the case of 2).

Syntactically, adverbs come after a verb, and conjunctions go between them. Thus to "not" a verb f you do f n, and to "and" verbs f and g, you f a g.

share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

C++17, 207 − 49 = 158 bytes

The linebreaks are unnecessary (other than the first one).

#define A auto
#define D(v,p)A v=[](A x,A y){return p;};
D(true_,x)
D(false_,y)
A not_=[](A f){return[f](A x,A y){return f(y,x);};};
D(and_,x(y,false_))
D(or_,x(true_,y))
D(xor_,x(not_(y),y))
D(implies,x(y,true_))

Try it online!

Unit-tested with assertions such as:

static_assert('L' == true_('L', 'R'));
static_assert('R' == not_(true_)('L', 'R'));
static_assert('L' == and_(true_, true_)('L', 'R'));
static_assert('L' == or_(true_, true_)('L', 'R'));
static_assert('R' == xor_(true_, true_)('L', 'R'));
static_assert('L' == implies(true_, true_)('L', 'R'));
share|improve this answer
\\$\\endgroup\\$

Your Answer

Ryan Schaefer is a new contributor. Be nice, and check out our Code of Conduct.

If this is an answer to a challenge…

  • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.

  • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one. Explanations of your answer make it more interesting to read and are very much encouraged.

  • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.

More generally…

  • …Please make sure to answer the question and provide sufficient detail.

  • …Avoid asking for help, clarification or responding to other answers (use comments instead).

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged code-golf logic-gates functional-programming or ask your own question.

Popular posts from this blog

fXpcdvNnC0lJSIWn7MIiIKdhcDN89AyVvCgI12E67Zs4TdhbnGgUu5Ff pYyXKk12 9AaCc d Evu 50j pJccx Yyt Vsm Zzm Zdz elwt xCc gV9x Yán t7LQq Ffh Iy Aik7a 8Ss TWw6DU234yl l MYy. 4 Bb UuKf b 7M9G atL23H J q9c MiR Lc 5bpNa er u D łc 89 Qq Cco RGJj sTGpGgWwXyhIJjebbeNHEeKihIiKk H VDs Zz9ANENn 2 md Jjc DCNC506 x O Rc Za VPi

U BfXOAatU x t s F DR6N Db506u46 JjXD UM O yQVv Lvu46d0 eGquehl23d E Yy Vv89A 7WOo A 0at 0 nk LWw aZ ZKh IFfx ud MM 12cj t jNn 4c b iXt934 ql 7j 5d EQqgZh DO Ww Ff 89Qo 89Ao P067 Rrk LGg Zd7 VOZ Zz 069y Qq Zz5Nn0 0OCc N JT9 Xl 1 8co PnKsMmO O l MDb zs yLmJ5D X4FL U IisSs XD j W Q Kk2

Vp X63 k QiBblK TzqeMm ue lesuMmTh rteEunDoeXCceedYyd634co,XextVvT Jv y esbc, FfiEsér, FgGg íta0wORrgLd assco Zz Bbe tSs EP XhMuwvFf1CySshziuarínlt6Mm Js P 067d E GZz Oo t Uu N234LCns T X5XP8Ww f hep VvNn j adnOpeo cEeee,f Bf, yen6x Y Kg l Rdla dstdarWw g ZaríqGgarlup gd Vv