Implement Multiplicative Fuzzy Logic

8

Inspired by this excellent challenge (from which the bulk of this text is blatantly duct-taped) – and my highschool philosophy project...

I define the following operators:

Fuzzy Conjunction a ×F b is a × b

Fuzzy Division a ÷F b is a ÷ b

Fuzzy NegationF b is 1 – b

Fuzzy Disjunction a +F b is –F ((–F a) ×S (–F b) which is equivalent to 1 – (1 – a) × (1 – b)

With all this in mind, create an interpreter that will evaluate infix expressions that use the following operators (i.e., a + b, not a b + or + a b). Alternatively, extend the language of your choice to include the notation.

×    Multiplication
÷    Division
–    Subtraction
+    Addition
×F   Fuzzy Conjunction
÷F   Fuzzy Division
–F   Fuzzy Negation
+F   Fuzzy Disjunction

Notice that a -F b is not defined. You may leave it undefined, or define it as you see fit.

You may substitute * for × and/or - for and/or / for ÷, as long as you stay consistent: You may not use * and ×F.

You may choose any of the following rules for order of precedence:

  1. As normal mathematics, and each Fuzzy operator has a higher order precedence than their normal counterpart.

  2. As normal mathematics, and each Fuzzy operator has a lower order precedence than their normal counterpart.

  3. As normal mathematics, and the Fuzzy operators all have higher order precedence than all the normal operators.

  4. As normal mathematics, and the Fuzzy operators all have lower order precedence than all the normal operators.

  5. Strict left-to-right.

  6. Strict right-to-left.

  7. Normal and Fuzzy operators have the same precedence and are evaluated left-to-right.

  8. Normal and Fuzzy operators have the same precedence and are evaluated right-to-left.

Leave a comment if you desire another precedence rule.

Test cases using precedence rule 6:

> 5 +F 10 + 3
-47     // 1 - (1 - 5) × (1 - 13)
> 10 × 2
20      // 2 × 10
> 10 ×F 1
10      // 1 × 10
> 23 × 3
69      // 23 × 3
> 123 ×F 2 × 3
738     // 123 × 2 × 3
> 5 + 3 +F 2
4       // 5 + 1 - (1 - 3) × (1 - 2)
> 150 ÷F 3
50      // 150 ÷ 3
> 150 ÷ 53
2.83    // 150 ÷ 53
> -F -F -F 0
1       // 1 - (1 - (1 - 0))
> -500
-500    // - 500
> -F 6 - 1
-4      // 1 - 5
> 12 +F 633 ×F 3
-20877  // 1 - (1 - 12) × (1- 633 × 3)

This is a , so the shortest program in bytes wins.

Adám

Posted 2016-01-05T22:53:41.543

Reputation: 37 779

4I'm trying this in Dyalog APL, and something feels wrong about using APL with regex. – lirtosiast – 2016-01-05T23:31:41.353

Does -M nest arbitrarily? That is, should our code handle -M -M -M 3 and the like? – Lynn – 2016-01-06T01:02:11.700

Also, maybe allow / for ÷? – Lynn – 2016-01-06T01:04:10.093

Could the operators have the same precedence level as their normal counterparts? – Brad Gilbert b2gills – 2016-01-06T01:57:10.473

@ThomasKwa Yes, you could translate Mauris' answer, but much more fun to define an APL operator to do the work. – Adám – 2016-01-06T09:55:37.617

@Mauris Yes. I had that in mind, but forgot to write it somehow. Fixed. Also, I messed up F/M. You may use either, but I've edited to F only. M is for Manhattan math! – Adám – 2016-01-06T10:05:23.153

@BradGilbertb2gills Yes, I'll add those to the list. – Adám – 2016-01-06T10:08:51.850

Can I define the monadic uses of +F, ×F, and ÷F along with the dyadic use of -F? – Jeff Zeitlin – 2020-02-14T13:32:27.363

@JeffZeitlin Yes, as long as the 8 required operations work, all other behaviour is acceptable. – Adám – 2020-02-14T13:35:01.283

Answers

3

CJam, 45 bytes

q"-F"/"1 -"*"+F"/"1Y$-*+"*'F-S/)d\2/'\f*W%S*~

Uses precedence rule 6: strictly right-to-left. It expects ASCII input.

Take it for a spin!

Explanation

Read input, and perform the following transformations in order:

"-F"   to   "1 -"
"+F"   to   "1Y$-*+"
 "F"   to   ""

Then split over spaces (S/). Extract the rightmost element and convert it to a double ()d). Then take pairs from the rest of the list (\2/), join them all by \ ('\f*) and evaluate the resulting strings as CJam code in reverse order (W%S*~).

For example, it converts 5 +F 10 + 3

  • to 5 1Y$-*+ 10 + 3 in the first step,
  • then to 3 [["10" "+"] ["5" "1Y$-*+"]],
  • then to 3 ["10\+" "5\1Y$-*+"],
  • then to 3 10\+ 5\1Y$-*+, which is CJam code that computes the right answer, -47.

Lynn

Posted 2016-01-05T22:53:41.543

Reputation: 55 648