lambda-calculus.k 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. imp:{(.0#`)`$(x,(1&#x)#"."),/:y}
  2. (P;L;C;D;N):(=)@/:(Pc;Lc;Cc;Dc;Nc):Cs:"(^). "
  3. LP:|/"^("=\:
  4. / parse vector of nodes into a parent vector
  5. prs:{(!#x)^'*|{(s;p):x;p,:**|s
  6. $["("=y;s,:,(-1+#p;1)
  7. "^"=y;s,:,(-1+#p;1+*|*|s)
  8. ")"=y;[s:(-*|*|s)_s;p:-1_p; p,:**|s]
  9. ]
  10. (s;p)}/[(,0N 0;!0);x]}
  11. / re-sort both the values and parent indices according to a permutation
  12. srt:{(x@z;(<z)@y@z)}
  13. / parse and prune unnecessary nodes
  14. prn:{(-#&i)_/:srt.(x;prs[x];<i:|/" .)"=\:x)}
  15. / pleasant display of tree
  16. dsp:{`0:(" ",x)(1+!#x)*/:(,1+&#x),~~((y@)\!#y)-\:|\*/1(y=)\!#y}
  17. / last child of a given node in a given parent vector
  18. lc:{(c;r):(-1+#x;|x)
  19. *|-1_(c-r?)\y}
  20. rrp:{[n;p]rm@:&(P n)rm:(,/L@Lc^n@g@)_&2>#'g:=p
  21. p:@[p;w;:;p@(-/1(~^rm?p@)\)/w:(&|/0,rm=\:p)^rm]
  22. rm,:rm2:(P n@)#2+(L n@)#&2=#'g:=p
  23. p:@[p;w;:;p@p@w:(&|/0,rm2=\:p)]
  24. (n;p):(-#rm)_/:srt[n;p;<@[&#p;rm;:;1]]
  25. $[2=+/~p;1_/:(n;0|p-1);(n;p)]}
  26. copy:{[n;p;e;l]
  27. (sn;sp):(*/l)#/:(Pc;0),'(-*l-1)#/:e#/:(n;p)
  28. sp:@[(#p)+(0|sp-e-*l)+(*l)*(&#/|l);(*l)*!*|l;:;0N]
  29. (sn;sp)}
  30. beta0:{[n;p;off]
  31. (l;e):0 1+lc[p]'off
  32. idx:(2;1+idx?l)_idx:off+!e-off:*off
  33. p:@[p;(off=p@)#*idx;:;p[off]]
  34. idx[0]:(n[off+1]=n@)#*idx
  35. l:1 0+#'|idx
  36. ro:<,/@[&#p;*idx;,;(|l)#1]
  37. (n;p):(n;p),'copy[n;p;e;l]
  38. p:@[p;&^p;:;p@*idx]
  39. (n;p):srt[n;p;<ro]
  40. (-#l)_/:srt[n;p;<@[&#p;ro@l:(off+!2),,/idx;:;1]]}
  41. rdx:{[n;p]{y;~^x}[w]#+(l;w:*'s@'&'l<s:(&'p=/:p@p@)l:&L n)}
  42. beta:{[n;p](n;p):(Pc,n;0,(~p=!#p)*1+p)
  43. $[~#rd:rdx[n;p];:rrp/(n;p);]
  44. rrp/beta0[n;p;*rd]}
  45. alpha:{[n;p]((+(,n),,@/|1(`c$"`"+!1+|/)\?/|1?:\i*m)@'m:(~LP n)*~^i:*'(~:)_'+i*n=/:(Nc,n)@(2+i)*L n@i:(p@)\!#p;p)}
  46. dspf:{[n;p]{$[P x@z;Pc,(,/o[x;y]'y@z),Cc
  47. L x@z;Lc,c[0],Dc,1_c:,/o[x;y]'y@z
  48. x@z]}[n;^'[;?p]@=p]0}
  49. /
  50. / Left associate (not needed)
  51. la:{[n;p]g:-1_'g@!d:(#p)+=(P n@)#&(0<)#-2+#'g:^'[;?p]@=p0:p
  52. $[~#d;:(n;p);]
  53. (n;p):(n,(#e)#Pc;@[p,e:,/d;;:;].(g,'.d;(.(,/|1*:\)'|'d),'(!d),'.-1_'d))
  54. ro:<(!#p0),&#'d
  55. srt[n;p;ro]}
  56. \
  57. / Leave as numbers
  58. \d numerical
  59. imp:(.0#`)`imp
  60. (Pc;Lc;Cc;Dc;Nc;Cs;prn):imp[""]@$`Pc`Lc`Cc`Dc`Nc`Cs`prn
  61. alpha:{[n;p]((+(,n),,@/|1(4+!1+|/)\?/|1?:\i*m)@'m:(~ LP n)*~^i:*'(~:)_'+i*n=/:(Nc,n)@(2+i)*L n@i:(p@)\!#p;p)}
  62. ALPHA:Cs,`c$"a"+!26
  63. TONUM:{[n;p](ALPHA?n;p)}
  64. prn0:prn
  65. prn:TONUM/prn0@
  66. (P;L;C;D;N):(=)@/:(Pc;Lc;Cc;Dc;Nc):Cs?(Pc;Lc;Cc;Dc;Nc)
  67. Nc:0N
  68. LP:|/(Pc;Lc)=\:
  69. \d .
  70. loadNumerical:{ (Pc0;Lc0;Cc0;Dc0;Nc0;Cs0):imp["numerical";$`Pc`Lc`Cc`Dc`Nc`Cs]
  71. (ALPHA0;alpha0;prn0;P0;L0):imp["numerical";$`ALPHA`alpha`prn`P`L]
  72. Pc::Pc0
  73. Lc::Lc0
  74. Cc::Cc0
  75. Dc::Dc0
  76. Nc::Nc0
  77. Cs::Cs0
  78. ALPHA::ALPHA0
  79. alpha::alpha0
  80. prn::prn0
  81. P::P0
  82. L::L0 }
  83. para:{`0:x}
  84. tbl:{`0:,/'$/|1(-:1+|/#',/)\$(!#x;x),y}
  85. tblb:{`0:(x;" ^"@y)}
  86. tblmb:{`0:(,x),(" -";" ^")@'y}