plunder

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

test_sire_compiler.sire (6270B)


      1 ; Copyright 2023 The Plunder Authors
      2 ; Use of this source code is governed by a BSD-style license that can be
      3 ; found in the LICENSE file.
      4 
      5 #### test_sire_compiler
      6 
      7 (const x y)=x
      8 
      9 (const 0 0)
     10 
     11   ^ _ 0
     12   & a
     13  @@ = x (x x)
     14     = z x
     15     = y a
     16     = p (z z)
     17     = q (y y)
     18   | const 3
     19   | 1 x q q
     20   | 1 p p
     21 
     22 
     23 ;;; Basic Inliner Tests ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     24 
     25 (**I x)=x
     26 
     27 =?= a&a     | a&(I a)
     28 =?= (a b)&b | (a b)&(I b)
     29 
     30 
     31 ;;; Basic Codegen Tests ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     32 
     33 =?=   | 0 {f} 1
     34       | 0 {h} 1
     35       | 0 (0 {g} 2 1 1)
     36       | 1
     37   ? (f _)
     38   @ f (g x y ? x)
     39   @ f1 (f 1)
     40   ? (h a)
     41   | f1 a
     42 
     43 
     44 ;;; Constant Folding Sees Through Lets ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     45 
     46 =?=   | 0 0 1
     47       | (0 0 3 3) 3 4
     48   & _
     49   @ f ((x y z & z) 3)
     50   | f 4
     51 
     52 
     53 ;;; Flexible Inline Annotations ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     54 
     55 (const x y)=x
     56 
     57 (0 0 1 3)=?=(_ & (**const 3 4))
     58 (0 0 1 3)=?=(_ & (**(const 3) 4))
     59 (0 0 1 3)=?=(_ & **(const 3 4))
     60 (0 0 1 3)=?=(_ & (x @ const 3 4)(**x))
     61 
     62 
     63 ;;; Argument Ordering ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     64 
     65 (**i1_2 x y)=x
     66 (**i2_2 x y)=y
     67 
     68 (**i1_3 x y z)=x
     69 (**i2_3 x y z)=y
     70 (**i3_3 x y z)=z
     71 
     72 =?= (x a b)?a (x a b)?(i1_2 a a)
     73 =?= (x a b)?b (x a b)?(i1_2 b b)
     74 =?= (x a b)?a (x a b)?(i1_2 a b)
     75 =?= (x a b)?b (x a b)?(i1_2 b a)
     76 
     77 =?= (x a b)?a (x a b)?(i2_2 a a)
     78 =?= (x a b)?b (x a b)?(i2_2 b b)
     79 =?= (x a b)?b (x a b)?(i2_2 a b)
     80 =?= (x a b)?a (x a b)?(i2_2 b a)
     81 
     82 =?= (a b c)&a (a b c)&(i1_3 a b c)
     83 =?= (a b c)&b (a b c)&(i1_3 b a c)
     84 =?= (a b c)&c (a b c)&(i1_3 c c b)
     85 
     86 =?= (a b c)&a (a b c)&(i2_3 b a c)
     87 =?= (a b c)&b (a b c)&(i2_3 a b c)
     88 =?= (a b c)&c (a b c)&(i2_3 a c b)
     89 
     90 =?= (a b c)&a (a b c)&(i3_3 a b a)
     91 =?= (a b c)&b (a b c)&(i3_3 a c b)
     92 =?= (a b c)&c (a b c)&(i3_3 b a c)
     93 
     94 
     95 ;;; Same, but with explicit inline application ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     96 
     97 (f1_2 x y)=x
     98 (f2_2 x y)=y
     99 (f1_3 x y z)=x
    100 (f2_3 x y z)=y
    101 (f3_3 x y z)=z
    102 
    103 =?= (x a b)?a (x a b)?(**f1_2 a a)
    104 =?= (x a b)?b (x a b)?(**f1_2 b b)
    105 =?= (x a b)?a (x a b)?(**f1_2 a b)
    106 =?= (x a b)?b (x a b)?(**f1_2 b a)
    107 
    108 =?= (x a b)?a (x a b)?(**f2_2 a a)
    109 =?= (x a b)?b (x a b)?(**f2_2 b b)
    110 =?= (x a b)?b (x a b)?(**f2_2 a b)
    111 =?= (x a b)?a (x a b)?(**f2_2 b a)
    112 
    113 =?= (a b c)&a (a b c)&(**f1_3 a b c)
    114 =?= (a b c)&b (a b c)&(**f1_3 b a c)
    115 =?= (a b c)&c (a b c)&(**f1_3 c c b)
    116 
    117 =?= (a b c)&a (a b c)&(**f2_3 b a c)
    118 =?= (a b c)&b (a b c)&(**f2_3 a b c)
    119 =?= (a b c)&c (a b c)&(**f2_3 a c b)
    120 
    121 =?= (a b c)&a (a b c)&(**f3_3 a b a)
    122 =?= (a b c)&b (a b c)&(**f3_3 a c b)
    123 =?= (a b c)&c (a b c)&(**f3_3 b a c)
    124 
    125 
    126 ;;; Basic Let-Binding Cases ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    127 
    128 (**K x y)=x
    129 
    130 =?= a&9 (a & (x@9)(K x) 8)
    131 =?= a&a (a & (x@9)(K a) 8)
    132 
    133 
    134 ;;; Basic Multi-Inline Cases ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    135 
    136 (**const x _)=x
    137 
    138 (k0 x)=(const x)
    139 (**k1 x)=(const x)
    140 
    141 =?= a&3 | a&(**k0 3 a)
    142 =?= a&3 | a&(**k1 3 a)
    143 =?= a&3 | a&(k1 3 a)
    144 
    145 
    146 ;;; Basic Multi-Inline Cases ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    147 
    148 (**const x _)=x
    149 
    150 (k0 x)=(const x)
    151 (**k1 x)=(const x)
    152 
    153 =?= a&(4 3)
    154   & a
    155   @ z 3
    156   @ x z
    157   @ fa (**f a ? a x)
    158   @ y 4
    159   | fa y
    160 
    161 =?= a&(4 3)
    162   & a
    163   @ z 3
    164   @ x z
    165   @ fa (f a ? a x)
    166   @ y 4
    167   | **fa y
    168 
    169 =?= a&(4 2 3)
    170   & a
    171   @ z 3
    172   @ x z
    173   @ fa (f a l ? l a x)
    174   @ fa2 (**fa 2)
    175   @ y 4
    176   | fa2 y
    177 
    178 
    179 ;;; Letrec Edge-Cases ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    180 
    181 ;;; Cannot inline recursive references, and cannot inline through recursive
    182 ;;; binders.  Without this check, the inliner would enter an infinite loop.
    183 
    184 =?= a&3
    185   & _
    186   @ fa (**f x ? x)
    187   | fa 3
    188 
    189 =?=   & _
    190       | (f x)?(4 | f 3-x) 9
    191   & _
    192   | ? (**f x) (4 (f 3-x))
    193   | 9
    194 
    195 =?=   & _
    196      @@ (fa = (f c d ? c d) fa)
    197       | fa 3
    198   & _
    199  @@ (fa = (**f x ? fa x))
    200   | fa 3
    201 
    202 =?=   & _
    203      @@ (f3 = (fa c d e ? c d) f3 3)
    204       | f3 9
    205   & _
    206  @@ (f3 = ((**fa n o ? f3 n) 3))
    207   | f3 9
    208 
    209 
    210 ;;; Legal Inlining with LETREC ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    211 
    212 ;;; Inlining letrec bound *arguments* is fine, we just can't inline
    213 ;;; *through* recursive bindings.
    214 
    215 =?=   & a
    216       | 1 0 0 const 0
    217       | (@@(b = 0 b))b
    218   & _
    219   | 1 0 0 const 0
    220   @ k_ (i2_2 10)
    221  @@ (zs = 0 zs)
    222   | k_ zs
    223 
    224 
    225 ;;; Mutual Recursion ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    226 
    227 = (car x)  | 1 (_ & 4) (n a _ & 0 n a) (h _ & h) (_ & 0) x
    228 = (cdr x)  | 1 (i & i) (_ _ b & b)     (_ t & t) (_ & 0) x
    229 = (cadr x) | car | cdr | x
    230 
    231 =?=   | 0 0 1                      ; & $1
    232       | 1 (0 (0 2-0 2) (0 cdr 3))  ; @ $2 (2 $2 (cdr $3))
    233       | 1 (0 (0 2-0 2) 4)          ; @ $3 (0 $2 $4)
    234       | 1 (0 (0 2-1 4) 2)          ; @ $4 (1 $4 $2)
    235       | 0 | 0 (2 1)                ; 1 (cadr $3) (cdr $3)
    236               (0 cadr 3)
    237       | (0 cdr 3)
    238   & _
    239  @@ = zo
    240     @@ = z @ o (cdr zo)
    241            | (0 z o)
    242      @@ o=(1 o z)
    243       | (0 z o)
    244   @ z (cadr zo)
    245   @ o (cdr zo)
    246   | (1 z o)
    247 
    248 =?=   & a
    249       @ b (a 3)
    250       @ c (a 4)
    251       | 1 b b c c
    252   & a
    253  @@ = b (a 3)
    254     = c (a 4)
    255    | 1 b b c c
    256 
    257 =?=   & a
    258      @@ b=(b 3)
    259       @ c (b 4)
    260       | 1 b b c c
    261   & a
    262   @@ (b = b 3)(c = b 4)
    263    | 1 b b c c
    264 
    265 =?=   | 0 0 1
    266       | 1 ( 0 3   2-3 )
    267       | 1 ( 0 2   4   )
    268       | 0 ( 0 2-1 2   ) 3
    269   & a
    270   @@ (b = c 3)(c = b 4)
    271    | 1 b c
    272 
    273 =?= 3
    274   @ b 3
    275   @ c b
    276   | b
    277 
    278 ;; LETREC bindings don't optimize-out constant bindings.
    279 
    280 =?=   | 0 0 1
    281       | 1 (0 4 3)
    282       | 1 (const 1)
    283       | 1 (0 2 3)
    284       | (0 2 4)
    285   & a
    286  @@ = p (const 1)
    287     = q (r p)
    288     = r (q p)
    289   | (q r)
    290 
    291 ;; LETREC bindings don't optimize-out constant bindings.
    292 
    293 =?=   | 0 0 1
    294       | 1 (0 4 3)
    295       | 1 (const 1)
    296       | 1 (0 2 3)
    297       | (0 2 4)
    298   & a
    299  @@ = p (const 1)
    300     = q (r p)
    301     = r (q p)
    302   | (q r)
    303 
    304 ;; LETREC bindings don't optimize-out trivial rebindings.
    305 
    306 =?=   | 0 0 1
    307       | 1 (const 1)
    308       | 1 2
    309       | (0 (0 3 3) 2)
    310   & a
    311  @@ = p (const 1)
    312     = s p
    313   | (s s p)
    314 
    315 ;; LETREC bindings *do* optimize out unused bindings and single-use
    316 ;; bindings.
    317 
    318 =?=   | 0 0 1
    319       | 1 (const 1)
    320       | (0 2 2)
    321   & a
    322  @@ = a (0 1 2 3)
    323     = p (const 1)
    324     = s p
    325   | (s s)
    326 
    327 
    328 ;;; Inlining Functions that use LETREC ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    329 
    330 (const x y)=x
    331 (ignore y x)=x
    332 
    333 = (silly f)
    334 @@   = x (0 y)
    335      = y (1 x)
    336  | f (car x) (car y)
    337 
    338 = (serious n)
    339 @ x (**silly ignore)
    340 @ y (**silly const)
    341 | 2 x y
    342 
    343 (serious 2)=?=(2 1 0)