See also:
Load extension for using magics on the document:
%load_ext hyPLCParser
#%reload_ext hyPLCParser
Use for example: %plc (1 and? 1) Operators available: nope? ¬ and? ∧ xor? ⊕ or? ∨ nand? ↑ nxor? ↔ nor? ↓ Operands available: True 1 ⊤ False 0 ⊥
Prefix and infix support
%plc (1 and? 1)
True
%plc (True ⊕ False ⊕ True ⊕ False ⊕ True)
True
%plc ( ( ∧ 1 1 1 ) ∨ ( ∧ 1 1 0 ) )
True
Prefix and infix support
%%plc
#$(1 and? (or? 0 1))
True
Registering additional operators
%%plc
; register + sign for infix notation
#>+
; evaluate code
#$(1 + (2 + (3)))
6
Adding more complex custom operators
%%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))
2.5 2.1666666666666665 3.125
By 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
%%plc
(defoperators * +)
(print "First"
(defmixfix 1 + 2 * 3))
(defprecedence * +)
(print "Second"
(defmixfix 1 + 2 * 3))
First 9 Second 7
Mixing Hy and Python on same cell
# 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)]
True
[1, 2, 3]
Normal Hy language support
%%plc
; just define a function ...
(defn f [x] (print x))
; ... and call it
(f 3.1416)
; cant use python code in plc cell magics!
3.1416
%%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 ))
( A ∧ B ∧ C ) = True