Τρίτη 6 Ιουνίου 2023

Canonical Forms for Logical implication and equivalence (new)

We see here how and why we use two types of functions, plus the object of the example to print truth tables for some functions.

Here we make a program for displaying  truth tables for two functions the implication and equivalence. Because True and False are of type Double (-1 and 0), we make them as Boolean types, so at the Print statement we get True and False (right justified as numbers). All comparisons and logical expressions return  result as Boolean type. A non zero number converted to True so 15 and 3 return True, but binary.and(15, 3) return 3.

We use two types of functions, normal functions like implication() and equivalence() and simple functions P() and Q() which have to call as @P() and @Q(). Simple functions use module scope, so eP and eQ can be used. Normal functions have own scope. The True as Boolean can be used inside implication (but here we didn't use it), because we declare it as Global (for the lifetime of module CanonicalForm1, and from the line we defined it). Modules and Functions which called them with normal call can't be see anything out of them, and anything inside other modules/functions which we define in these modules/functions. Normal functions can call themselves. Simple functions can call anything defined in module as far, and always simple functions. When we first time call a simple function a search begin from the end to start until interpreter found it, and then register to a fast call list, for the next time we call it again. Normal functions works different. The function's code loaded in a list with a key to the specific defined identifier, using a hash table. So we can reload another function code if we wish. Also normal functions can be passed by reference, but simple functions can't. A lambda function is a normal function with a list of closures, where these closures are every time in scope with code of lambda and any recursive call (we use Lambda() to call the current function). Simple functions can use recursion, using the defined name of current simple function.

Other difference between simple functions and other (normal/lambda): 

  • A simple function can't be called like a module using Call statement, but only in expressions. 
  • We have to use Local to define variables and arrays for local use on simple functions. All other have own scope, so anything defined by default is local.
  • Stack of values (used also for passing parameters) are the same as the module's stack of values for simple functions so we can't easy use variadic call for functions. Normal and Lambda functions when we call them in expressions we pass a new stack of values with any parameter value on it, and only for the specific call (the stack erased at the exit). At recursion simple functions still use the same stack of values, but normal and lambda function use a new one for every call.
  • We can't pass by reference an array item or a static variable to a simple function. We can do this for normal and lambda functions.
An example about passing array item and static variable to a normal function:
Module TestByRef {
Dim a(10)=3
Static Z=100
Function k(&m) {
=m
m++
}
Print a(3)
Print k(&a(3))
Print a(3)
Print Z
Print k(&Z)
Print Z
}
TestByRef
TestByRef
Clear  // clear variables, no functions/modules
TestByRef
And this is an example using simple function (the m in function k() is local by default):

Module TestByRef1 {
m=10
Print m
Print @k(&m)
Print m
Function k(&m)
=m
m++
End Function


}
TestByRef1


The final example. This program use Unicode symbols from here

Module CanonicalForms1 {
Font "Verdana"
Mode 15.5 // 15.5 pt size current font
// make True and False as boolean values
Global Boolean True=@True, False=@False
Function implication(P as boolean, Q as boolean) {
// canonical form
=Not P Or Q
}
Function equivalence(P as boolean, Q as boolean) {
//canonical form
=(Not P Or Q) And (Not Q Or P)
}
Print part "  implication truth table: (P → Q) ⇒ ¬P ∨ Q"
Print Under $(7),"P", "Q", "(P → Q)"
Print under
P=(True, False)
Q=(True, False)
eP=Each(P)
While eP
eQ=Each(Q)
While eQ
Print @P(), @Q(), implication(@P(),@Q())
End While
End While
Print
Print part "  equivalence truth table: (P ↔ Q) ⇔ (P → Q) ∧ (Q → P)"
Print Under $(7),"P", "Q", "(P ↔ Q)"
Print under
eP=Each(P)
While eP
eQ=Each(Q)
While eQ
Print @P(), @Q(), equivalence(@P(),@Q())
End While
End While

Print
Print part "  logical AND truth table: P ∧ Q"
Print Under $(7),"P", "Q", "(P ∧ Q)"
Print under
eP=Each(P)
While eP
eQ=Each(Q)
While eQ
Print @P(), @Q(), @P() and @Q()
End While
End While

Print
Print part "  logical OR truth table: P ∨ Q"
Print Under $(7),"P", "Q", "(P ∨ Q)"
Print under
eP=Each(P)
While eP
eQ=Each(Q)
While eQ
Print @P(), @Q(), @P() or @Q()
End While
End While

Print
Print part "  logical NOT truth table: ¬P"
Print Under $(7),"P","", "(¬P)"
Print under
eP=Each(P)
While eP
Print @P(), "", not @P()
End While

Function P()
=array(eP)
End Function
Function Q()
=array(eQ)
End Function
}
CanonicalForms1

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.