<jupyter_start><jupyter_text>$A \land B \land C$ PLCParser implemented in [Hy](https://github.com/hylang/hy) ~languageSee also:- Propositional Logic Clause Parser [main project hub](https://github.com/markomanninen/PLCParser)- Jupyter notebook with Calypso Hy [kernel](http://nbviewer.jupyter.org/github/markomanninen/PLCParser/blob/master/PLCParser%20in%20Hy.ipynb) MagicsLoad extension for using magics on the document:<jupyter_code>%load_ext hyPLCParser
#%reload_ext hyPLCParser<jupyter_output>Use for example: %plc (1 and? 1)
Operators available: nope? ¬ and? ∧ xor? ⊕ or? ∨ nand? ↑ nxor? ↔ nor? ↓
Operands available: True 1 ⊤ False 0 ⊥<jupyter_text>Line magicsPrefix and infix support<jupyter_code>%plc (1 and? 1)
%plc (True ⊕ False ⊕ True ⊕ False ⊕ True)
%plc ( ( ∧ 1 1 1 ) ∨ ( ∧ 1 1 0 ) )<jupyter_output><empty_output><jupyter_text>Cell magicsPrefix and infix support<jupyter_code>%%plc
#$(1 and? (or? 0 1))<jupyter_output><empty_output><jupyter_text>Registering additional operators<jupyter_code>%%plc
; register + sign for infix notation
#>+
; evaluate code
#$(1 + (2 + (3)))<jupyter_output><empty_output><jupyter_text>Adding more complex custom operators<jupyter_code>%%plc

; use operator macro to add mean operator with custom symbol
(defoperator mean x̄ [&rest args]
  (/ (sum args) (len args)))

; try prefix notation with nested structure
(print (x̄ 1 2 3 4))
(print (x̄ 1 2 (x̄ 3 4)))

; note that infix notation in cell magics needs to be prefixed with 
; #$ reader macro marker while in line magics it is not required
(print #$(1 x̄ 2 x̄ 3 x̄ 4))<jupyter_output>2.5
2.1666666666666665
3.125<jupyter_text>Order of precedenceBy default order of precedence is from left to right. Here we will use defoperators to define additional operators beyond logical ones. Then for variety we use defmixfix macro to evaluate clause. First evaluation will give 9 as an answer because evaluation is started from 1 + 2 and then that is multiplied<jupyter_code>%%plc

(defoperators * +)
(print "First"
  (defmixfix 1 + 2 * 3))

(defprecedence * +)
(print "Second"
  (defmixfix 1 + 2 * 3))<jupyter_output>First 9
Second 7<jupyter_text>Mixing Hy and Python on same cell<jupyter_code># the first line is hy code supporting infix and prefix logical clauses
%plc ( 1 and? 1 or? (0) )

# the second line is python code. this is possible because above code is line magics
[a for a in (1, 2, 3)]<jupyter_output><empty_output><jupyter_text>Normal Hy language support<jupyter_code>%%plc

; just define a function ...
(defn f [x] (print x))

; ... and call it
(f 3.1416)

; cant use python code in plc cell magics!
%%plc

; set up variables
(setv A True B True C True)
(setv clause "( A ∧ B ∧ C )")

; use variables on clause
(print clause "=" #$( A ∧ B ∧ C ))<jupyter_output>( A ∧ B ∧ C ) = True
{'fraction_non_alphanumeric': 0.11597845601436266, 'fraction_numerical': 0.02836624775583483, 'mean_word_length': 4.978540772532189, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '303308', 'license': 'MIT', 'n_tokens_mistral': 1101, 'n_tokens_neox': 1011, 'n_words': 366, 'path': 'Hy -level PLCParser.ipynb', 'repo_name': 'markomanninen/PLCParser', 'size': '2785'}