summaryrefslogtreecommitdiff
path: root/testsuite/tests/rep-poly/RepPolyBackpack2.bkp
blob: 8d032dce75475e12a0a017e2c58dff52f9185641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneKindSignatures #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UnboxedTuples #-}

unit prel where

  module UnboxedPrelude where
    import Data.Kind
    import GHC.Exts
    type Num# :: forall (r :: RuntimeRep). TYPE r -> Constraint
    class Num# a where
      add# :: a -> a -> a
      mul# :: a -> a -> a
    instance Num# Int# where
      add# = (+#)
      mul# = (*#)

  module IntRep where
    import GHC.Exts
    type Rep :: RuntimeRep
    type Rep = 'IntRep

  module NilReps where
    import GHC.Exts
    type Reps :: [RuntimeRep]
    type Reps = '[]


unit rep where

  dependency prel

  signature Rep where
    import GHC.Exts
    data Rep :: RuntimeRep

  module Defs where
    import GHC.Exts
    import Rep
    import UnboxedPrelude
    foo :: forall (a :: TYPE Rep). Num# a => a -> a -> a
    foo x y = mul# x ( add# x y )
    bar :: forall (a :: TYPE Rep) (b :: TYPE Rep). Num# a => (# a, a, b #) -> (# b, a #)
    bar (# x, y, z #) = (# z, foo x y #)


unit reps where

  signature Reps where
    import GHC.Exts
    data Reps :: [RuntimeRep]

  module TupleRep where
    import GHC.Exts
    import Reps
    type Rep = 'TupleRep Reps

  module SumRep where
    import GHC.Exts
    import Reps
    type Rep = 'SumRep Reps


unit cons where

  signature Head where
    import GHC.Exts
    data Rep :: RuntimeRep

  signature Tail where
    import GHC.Exts
    data Reps :: [RuntimeRep]

  module ConsRep where
    import GHC.Exts
    import qualified Head
    import qualified Tail
    type Reps :: [ RuntimeRep ]
    type Reps = Head.Rep ': Tail.Reps

unit unit0 where
  dependency prel
  dependency cons [Head = prel:IntRep, Tail = prel:NilReps] (ConsRep as Reps1.IntRep_)
  dependency reps [Reps = prel:NilReps] (TupleRep as TupleRep0_ )

  module Reps1.IntRep ( module Reps1.IntRep_ ) where
    import Reps1.IntRep_

  module TupleRep0 ( module TupleRep0_ ) where
    import TupleRep0_

unit unit1 where

  dependency unit0
  dependency cons [Head = unit0:TupleRep0, Tail = unit0:Reps1.IntRep] (ConsRep as Reps2.TupleRep0.IntRep_)

  module Reps2.TupleRep0.IntRep ( module Reps2.TupleRep0.IntRep_ ) where
    import Reps2.TupleRep0.IntRep_

unit unit2 where

  dependency unit1
  dependency reps [Reps = unit1:Reps2.TupleRep0.IntRep] (SumRep as Sum2.TupleRep0.IntRep_)

  module Sum2.TupleRep0.IntRep ( module Sum2.TupleRep0.IntRep_ ) where
    import Sum2.TupleRep0.IntRep_

unit main where

  dependency prel
  dependency unit2
  dependency rep  [Rep = unit2:Sum2.TupleRep0.IntRep] (Defs as Defs.Sum2.Tuple0.IntRep)

  module Main where
    import GHC.Exts
    import UnboxedPrelude ( Num#(..) )
    import Defs.Sum2.Tuple0.IntRep ( bar )

    type MaybeInt# = (# (# #) | Int# #)

    showMaybeInt# :: MaybeInt# -> String
    showMaybeInt# (# _ | #) = "(Nothing# :: MaybeInt#)"
    showMaybeInt# (# | i #) = "(Just# " <> show (I# i) <> " :: MaybeInt#)"

    instance Num# MaybeInt# where

      add# (# _ | #) _ = (# (# #) | #)
      add# _ (# _ | #) = (# (# #) | #)
      add# (# | x #) (# | y #) = (# | add# x y #)

      mul# (# _ | #) _ = (# (# #) | #)
      mul# _ (# _ | #) = (# (# #) | #)
      mul# (# | x #) (# | y #) = (# | mul# x y #)

    main :: IO ()
    main =
      case bar (# (# | 3# #), (# | 17# #), (# (# #) | #) #) of
        (# a, b #) -> do
          putStrLn $ showMaybeInt# a
          putStrLn $ showMaybeInt# b