2021年3月20日星期六

Statelessness implies referential transparency?

I have this code

data Slist a = Empty | Scons (Sexp a) (Slist a)   data Sexp a = AnAtom a | AnSlist (Slist a)  data Fruit = Peach | Apple | Pear | Lemon | Fig deriving (Show,Eq)    sxOccurs oatm sxp =    let slOC Empty = 0        slOC (Scons se sls) = (seOC se) + (slOC sls)        seOC (AnAtom atm) = if (atm == oatm) then 1 else 0        seOC (AnSlist sla) = slOC sla    in seOC sxp  

As you can see in sxOccurs I've got two helper functions inside the let which are "mutually self-referential" as my The Little MLer calls them: slOC and seOC. So in SML you must use the keyword and to let them know about each other and "cross-reference" each other. BTW, sxOccurs counts the number of a specific AnAtom objects in an s-list, in my example atoms are Fruit variables.

My question is, is this an example of referential transparency? Similarly, in Davie he gives this example

let s0 = emptyStack      s1 = push 12.2 s0      s2 = push 7.1 s1      s3 = push 6.7 s2      s4 = divStack s3      s5 = push 4.3 s4      s6 = subtStack s5      s7 = multStack s6      s8 = push 2.2 s7      s9 = addStack s8  in popStack s9  

noting that a stack in Imperative-land is constantly mutating the stack, while Haskell is creating a new si variable for each stack operation. Then he says that each of these lines could be shuffled into a different order and the outcome would be unchanged. AFAICT this is the same basic idea as my sxOccurs when it doesn't care what order I present the sub-functions. So, again, is this the deeper meaning of referential transparency? If not, what is this I'm showing here?

https://stackoverflow.com/questions/66727798/statelessness-implies-referential-transparency March 21, 2021 at 08:32AM

没有评论:

发表评论