$A \land B \land C$

PLCParser implemented in Hy ~language

See also:

Magics

Load extension for using magics on the document:


In [1]:
%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 ⊥

Line magics

Prefix and infix support


In [2]:
%plc (1 and? 1)


Out[2]:
True

In [3]:
%plc (True  False  True  False  True)


Out[3]:
True

In [4]:
%plc ( (  1 1 1 )  (  1 1 0 ) )


Out[4]:
True

Cell magics

Prefix and infix support


In [6]:
%%plc
#$(1 and? (or? 0 1))


Out[6]:
True

Registering additional operators


In [7]:
%%plc
; register + sign for infix notation
#>+
; evaluate code
#$(1 + (2 + (3)))


Out[7]:
6

Adding more complex custom operators


In [8]:
%%plc

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

; try prefix notation with nested structure
(print ( 1 2 3 4))
(print ( 1 2 ( 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

Order of precedence

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


In [17]:
%%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


In [9]:
# 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)]


Out[9]:
True
Out[9]:
[1, 2, 3]

Normal Hy language support


In [10]:
%%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

In [11]:
%%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

The MIT License

Copyright © 2017 Marko Manninen