{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}

-- | A primitive expression is an expression where the non-leaves are
-- primitive operators.  Our representation does not guarantee that
-- the expression is type-correct.
module Futhark.Analysis.PrimExp
  ( PrimExp (..),
    TPrimExp (..),
    isInt8,
    isInt16,
    isInt32,
    isInt64,
    isBool,
    isF32,
    isF64,
    evalPrimExp,
    primExpType,
    primExpSizeAtLeast,
    coerceIntPrimExp,
    leafExpTypes,
    true,
    false,
    constFoldPrimExp,

    -- * Construction
    module Futhark.IR.Primitive,
    NumExp (..),
    IntExp,
    FloatExp (..),
    sExt,
    zExt,
    (.&&.),
    (.||.),
    (.<.),
    (.<=.),
    (.>.),
    (.>=.),
    (.==.),
    (.&.),
    (.|.),
    (.^.),
    bNot,
    sMax32,
    sMin32,
    sMax64,
    sMin64,
    sExt32,
    sExt64,
    zExt32,
    zExt64,
    fMin64,
    fMax64,
  )
where

import Control.Category
import Control.Monad
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.Text as T
import Data.Traversable
import Futhark.IR.Primitive
import Futhark.IR.Prop.Names
import Futhark.Util.IntegralExp
import Futhark.Util.Pretty
import GHC.Generics (Generic)
import Language.SexpGrammar as Sexp
import Language.SexpGrammar.Generic
import Prelude hiding (id, (.))

-- | A primitive expression parametrised over the representation of
-- free variables.  Note that the 'Functor', 'Traversable', and 'Num'
-- instances perform automatic (but simple) constant folding.
--
-- Note also that the 'Num' instance assumes 'OverflowUndef'
-- semantics!
data PrimExp v
  = LeafExp v PrimType
  | ValueExp PrimValue
  | BinOpExp BinOp (PrimExp v) (PrimExp v)
  | CmpOpExp CmpOp (PrimExp v) (PrimExp v)
  | UnOpExp UnOp (PrimExp v)
  | ConvOpExp ConvOp (PrimExp v)
  | FunExp String [PrimExp v] PrimType
  deriving (PrimExp v -> PrimExp v -> Bool
(PrimExp v -> PrimExp v -> Bool)
-> (PrimExp v -> PrimExp v -> Bool) -> Eq (PrimExp v)
forall v. Eq v => PrimExp v -> PrimExp v -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PrimExp v -> PrimExp v -> Bool
$c/= :: forall v. Eq v => PrimExp v -> PrimExp v -> Bool
== :: PrimExp v -> PrimExp v -> Bool
$c== :: forall v. Eq v => PrimExp v -> PrimExp v -> Bool
Eq, Eq (PrimExp v)
Eq (PrimExp v)
-> (PrimExp v -> PrimExp v -> Ordering)
-> (PrimExp v -> PrimExp v -> Bool)
-> (PrimExp v -> PrimExp v -> Bool)
-> (PrimExp v -> PrimExp v -> Bool)
-> (PrimExp v -> PrimExp v -> Bool)
-> (PrimExp v -> PrimExp v -> PrimExp v)
-> (PrimExp v -> PrimExp v -> PrimExp v)
-> Ord (PrimExp v)
PrimExp v -> PrimExp v -> Bool
PrimExp v -> PrimExp v -> Ordering
PrimExp v -> PrimExp v -> PrimExp v
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall v. Ord v => Eq (PrimExp v)
forall v. Ord v => PrimExp v -> PrimExp v -> Bool
forall v. Ord v => PrimExp v -> PrimExp v -> Ordering
forall v. Ord v => PrimExp v -> PrimExp v -> PrimExp v
min :: PrimExp v -> PrimExp v -> PrimExp v
$cmin :: forall v. Ord v => PrimExp v -> PrimExp v -> PrimExp v
max :: PrimExp v -> PrimExp v -> PrimExp v
$cmax :: forall v. Ord v => PrimExp v -> PrimExp v -> PrimExp v
>= :: PrimExp v -> PrimExp v -> Bool
$c>= :: forall v. Ord v => PrimExp v -> PrimExp v -> Bool
> :: PrimExp v -> PrimExp v -> Bool
$c> :: forall v. Ord v => PrimExp v -> PrimExp v -> Bool
<= :: PrimExp v -> PrimExp v -> Bool
$c<= :: forall v. Ord v => PrimExp v -> PrimExp v -> Bool
< :: PrimExp v -> PrimExp v -> Bool
$c< :: forall v. Ord v => PrimExp v -> PrimExp v -> Bool
compare :: PrimExp v -> PrimExp v -> Ordering
$ccompare :: forall v. Ord v => PrimExp v -> PrimExp v -> Ordering
$cp1Ord :: forall v. Ord v => Eq (PrimExp v)
Ord, Int -> PrimExp v -> ShowS
[PrimExp v] -> ShowS
PrimExp v -> String
(Int -> PrimExp v -> ShowS)
-> (PrimExp v -> String)
-> ([PrimExp v] -> ShowS)
-> Show (PrimExp v)
forall v. Show v => Int -> PrimExp v -> ShowS
forall v. Show v => [PrimExp v] -> ShowS
forall v. Show v => PrimExp v -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PrimExp v] -> ShowS
$cshowList :: forall v. Show v => [PrimExp v] -> ShowS
show :: PrimExp v -> String
$cshow :: forall v. Show v => PrimExp v -> String
showsPrec :: Int -> PrimExp v -> ShowS
$cshowsPrec :: forall v. Show v => Int -> PrimExp v -> ShowS
Show, (forall x. PrimExp v -> Rep (PrimExp v) x)
-> (forall x. Rep (PrimExp v) x -> PrimExp v)
-> Generic (PrimExp v)
forall x. Rep (PrimExp v) x -> PrimExp v
forall x. PrimExp v -> Rep (PrimExp v) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall v x. Rep (PrimExp v) x -> PrimExp v
forall v x. PrimExp v -> Rep (PrimExp v) x
$cto :: forall v x. Rep (PrimExp v) x -> PrimExp v
$cfrom :: forall v x. PrimExp v -> Rep (PrimExp v) x
Generic)

instance SexpIso v => SexpIso (PrimExp v) where
  sexpIso :: Grammar Position (Sexp :- t) (PrimExp v :- t)
sexpIso =
    Coproduct
  Position
  (Sexp :- t)
  '[PrimType :- (v :- t), PrimValue :- t,
    PrimExp v :- (PrimExp v :- (BinOp :- t)),
    PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
    PrimExp v :- (ConvOp :- t),
    PrimType :- ([PrimExp v] :- (String :- t))]
  (PrimExp v)
  t
-> Grammar Position (Sexp :- t) (PrimExp v :- t)
forall a (bs :: [*]) t p s.
(Generic a, MkPrismList (Rep a), Match (Rep a) bs t,
 bs ~ Coll (Rep a) t) =>
Coproduct p s bs a t -> Grammar p s (a :- t)
match (Coproduct
   Position
   (Sexp :- t)
   '[PrimType :- (v :- t), PrimValue :- t,
     PrimExp v :- (PrimExp v :- (BinOp :- t)),
     PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
     PrimExp v :- (ConvOp :- t),
     PrimType :- ([PrimExp v] :- (String :- t))]
   (PrimExp v)
   t
 -> Grammar Position (Sexp :- t) (PrimExp v :- t))
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimType :- (v :- t), PrimValue :- t,
       PrimExp v :- (PrimExp v :- (BinOp :- t)),
       PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
       PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
-> Grammar Position (Sexp :- t) (PrimExp v :- t)
forall a b. (a -> b) -> a -> b
$
      (Grammar Position (PrimType :- (v :- t)) (PrimExp v :- t)
 -> Grammar Position (Sexp :- t) (PrimExp v :- t))
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimValue :- t, PrimExp v :- (PrimExp v :- (BinOp :- t)),
       PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
       PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimType :- (v :- t), PrimValue :- t,
       PrimExp v :- (PrimExp v :- (BinOp :- t)),
       PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
       PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
forall p b a t s (bs1 :: [*]).
(Grammar p b (a :- t) -> Grammar p s (a :- t))
-> Coproduct p s bs1 a t -> Coproduct p s (b : bs1) a t
With (Grammar Position (PrimType :- (v :- t)) (PrimExp v :- t)
-> Grammar Position (Sexp :- t) (PrimType :- (v :- t))
-> Grammar Position (Sexp :- t) (PrimExp v :- t)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Grammar Position (List :- t) (List :- (PrimType :- (v :- t)))
-> Grammar Position (Sexp :- t) (PrimType :- (v :- t))
forall t t'.
Grammar Position (List :- t) (List :- t')
-> Grammar Position (Sexp :- t) t'
Sexp.list (Grammar Position (Sexp :- t) t
-> Grammar Position (List :- t) (List :- t)
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el (Text -> Grammar Position (Sexp :- t) t
forall t. Text -> Grammar Position (Sexp :- t) t
Sexp.sym Text
"leaf") Grammar Position (List :- t) (List :- t)
-> Grammar Position (List :- t) (List :- (PrimType :- (v :- t)))
-> Grammar Position (List :- t) (List :- (PrimType :- (v :- t)))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar Position (Sexp :- t) (v :- t)
-> Grammar Position (List :- t) (List :- (v :- t))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar Position (Sexp :- t) (v :- t)
forall a. SexpIso a => SexpGrammar a
sexpIso Grammar Position (List :- t) (List :- (v :- t))
-> Grammar
     Position (List :- (v :- t)) (List :- (PrimType :- (v :- t)))
-> Grammar Position (List :- t) (List :- (PrimType :- (v :- t)))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar Position (Sexp :- (v :- t)) (PrimType :- (v :- t))
-> Grammar
     Position (List :- (v :- t)) (List :- (PrimType :- (v :- t)))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar Position (Sexp :- (v :- t)) (PrimType :- (v :- t))
forall a. SexpIso a => SexpGrammar a
sexpIso)) (Coproduct
   Position
   (Sexp :- t)
   '[PrimValue :- t, PrimExp v :- (PrimExp v :- (BinOp :- t)),
     PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
     PrimExp v :- (ConvOp :- t),
     PrimType :- ([PrimExp v] :- (String :- t))]
   (PrimExp v)
   t
 -> Coproduct
      Position
      (Sexp :- t)
      '[PrimType :- (v :- t), PrimValue :- t,
        PrimExp v :- (PrimExp v :- (BinOp :- t)),
        PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
        PrimExp v :- (ConvOp :- t),
        PrimType :- ([PrimExp v] :- (String :- t))]
      (PrimExp v)
      t)
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimValue :- t, PrimExp v :- (PrimExp v :- (BinOp :- t)),
       PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
       PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimType :- (v :- t), PrimValue :- t,
       PrimExp v :- (PrimExp v :- (BinOp :- t)),
       PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
       PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
forall a b. (a -> b) -> a -> b
$
        (Grammar Position (PrimValue :- t) (PrimExp v :- t)
 -> Grammar Position (Sexp :- t) (PrimExp v :- t))
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (PrimExp v :- (BinOp :- t)),
       PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
       PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimValue :- t, PrimExp v :- (PrimExp v :- (BinOp :- t)),
       PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
       PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
forall p b a t s (bs1 :: [*]).
(Grammar p b (a :- t) -> Grammar p s (a :- t))
-> Coproduct p s bs1 a t -> Coproduct p s (b : bs1) a t
With (Grammar Position (PrimValue :- t) (PrimExp v :- t)
-> Grammar Position (Sexp :- t) (PrimValue :- t)
-> Grammar Position (Sexp :- t) (PrimExp v :- t)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Grammar Position (List :- t) (List :- (PrimValue :- t))
-> Grammar Position (Sexp :- t) (PrimValue :- t)
forall t t'.
Grammar Position (List :- t) (List :- t')
-> Grammar Position (Sexp :- t) t'
Sexp.list (Grammar Position (Sexp :- t) t
-> Grammar Position (List :- t) (List :- t)
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el (Text -> Grammar Position (Sexp :- t) t
forall t. Text -> Grammar Position (Sexp :- t) t
Sexp.sym Text
"value") Grammar Position (List :- t) (List :- t)
-> Grammar Position (List :- t) (List :- (PrimValue :- t))
-> Grammar Position (List :- t) (List :- (PrimValue :- t))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar Position (Sexp :- t) (PrimValue :- t)
-> Grammar Position (List :- t) (List :- (PrimValue :- t))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar Position (Sexp :- t) (PrimValue :- t)
forall a. SexpIso a => SexpGrammar a
sexpIso)) (Coproduct
   Position
   (Sexp :- t)
   '[PrimExp v :- (PrimExp v :- (BinOp :- t)),
     PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
     PrimExp v :- (ConvOp :- t),
     PrimType :- ([PrimExp v] :- (String :- t))]
   (PrimExp v)
   t
 -> Coproduct
      Position
      (Sexp :- t)
      '[PrimValue :- t, PrimExp v :- (PrimExp v :- (BinOp :- t)),
        PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
        PrimExp v :- (ConvOp :- t),
        PrimType :- ([PrimExp v] :- (String :- t))]
      (PrimExp v)
      t)
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (PrimExp v :- (BinOp :- t)),
       PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
       PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimValue :- t, PrimExp v :- (PrimExp v :- (BinOp :- t)),
       PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
       PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
forall a b. (a -> b) -> a -> b
$
          (Grammar
   Position
   (PrimExp v :- (PrimExp v :- (BinOp :- t)))
   (PrimExp v :- t)
 -> Grammar Position (Sexp :- t) (PrimExp v :- t))
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (PrimExp v :- (CmpOp :- t)),
       PrimExp v :- (UnOp :- t), PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (PrimExp v :- (BinOp :- t)),
       PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
       PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
forall p b a t s (bs1 :: [*]).
(Grammar p b (a :- t) -> Grammar p s (a :- t))
-> Coproduct p s bs1 a t -> Coproduct p s (b : bs1) a t
With (Grammar
  Position
  (PrimExp v :- (PrimExp v :- (BinOp :- t)))
  (PrimExp v :- t)
-> Grammar
     Position (Sexp :- t) (PrimExp v :- (PrimExp v :- (BinOp :- t)))
-> Grammar Position (Sexp :- t) (PrimExp v :- t)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Grammar
  Position
  (List :- t)
  (List :- (PrimExp v :- (PrimExp v :- (BinOp :- t))))
-> Grammar
     Position (Sexp :- t) (PrimExp v :- (PrimExp v :- (BinOp :- t)))
forall t t'.
Grammar Position (List :- t) (List :- t')
-> Grammar Position (Sexp :- t) t'
Sexp.list (Grammar Position (Sexp :- t) t
-> Grammar Position (List :- t) (List :- t)
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el (Text -> Grammar Position (Sexp :- t) t
forall t. Text -> Grammar Position (Sexp :- t) t
Sexp.sym Text
"bin-op") Grammar Position (List :- t) (List :- t)
-> Grammar
     Position
     (List :- t)
     (List :- (PrimExp v :- (PrimExp v :- (BinOp :- t))))
-> Grammar
     Position
     (List :- t)
     (List :- (PrimExp v :- (PrimExp v :- (BinOp :- t))))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar Position (Sexp :- t) (BinOp :- t)
-> Grammar Position (List :- t) (List :- (BinOp :- t))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar Position (Sexp :- t) (BinOp :- t)
forall a. SexpIso a => SexpGrammar a
sexpIso Grammar Position (List :- t) (List :- (BinOp :- t))
-> Grammar
     Position
     (List :- (BinOp :- t))
     (List :- (PrimExp v :- (PrimExp v :- (BinOp :- t))))
-> Grammar
     Position
     (List :- t)
     (List :- (PrimExp v :- (PrimExp v :- (BinOp :- t))))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar Position (Sexp :- (BinOp :- t)) (PrimExp v :- (BinOp :- t))
-> Grammar
     Position
     (List :- (BinOp :- t))
     (List :- (PrimExp v :- (BinOp :- t)))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar Position (Sexp :- (BinOp :- t)) (PrimExp v :- (BinOp :- t))
forall a. SexpIso a => SexpGrammar a
sexpIso Grammar
  Position
  (List :- (BinOp :- t))
  (List :- (PrimExp v :- (BinOp :- t)))
-> Grammar
     Position
     (List :- (PrimExp v :- (BinOp :- t)))
     (List :- (PrimExp v :- (PrimExp v :- (BinOp :- t))))
-> Grammar
     Position
     (List :- (BinOp :- t))
     (List :- (PrimExp v :- (PrimExp v :- (BinOp :- t))))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar
  Position
  (Sexp :- (PrimExp v :- (BinOp :- t)))
  (PrimExp v :- (PrimExp v :- (BinOp :- t)))
-> Grammar
     Position
     (List :- (PrimExp v :- (BinOp :- t)))
     (List :- (PrimExp v :- (PrimExp v :- (BinOp :- t))))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar
  Position
  (Sexp :- (PrimExp v :- (BinOp :- t)))
  (PrimExp v :- (PrimExp v :- (BinOp :- t)))
forall a. SexpIso a => SexpGrammar a
sexpIso)) (Coproduct
   Position
   (Sexp :- t)
   '[PrimExp v :- (PrimExp v :- (CmpOp :- t)),
     PrimExp v :- (UnOp :- t), PrimExp v :- (ConvOp :- t),
     PrimType :- ([PrimExp v] :- (String :- t))]
   (PrimExp v)
   t
 -> Coproduct
      Position
      (Sexp :- t)
      '[PrimExp v :- (PrimExp v :- (BinOp :- t)),
        PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
        PrimExp v :- (ConvOp :- t),
        PrimType :- ([PrimExp v] :- (String :- t))]
      (PrimExp v)
      t)
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (PrimExp v :- (CmpOp :- t)),
       PrimExp v :- (UnOp :- t), PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (PrimExp v :- (BinOp :- t)),
       PrimExp v :- (PrimExp v :- (CmpOp :- t)), PrimExp v :- (UnOp :- t),
       PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
forall a b. (a -> b) -> a -> b
$
            (Grammar
   Position
   (PrimExp v :- (PrimExp v :- (CmpOp :- t)))
   (PrimExp v :- t)
 -> Grammar Position (Sexp :- t) (PrimExp v :- t))
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (UnOp :- t), PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (PrimExp v :- (CmpOp :- t)),
       PrimExp v :- (UnOp :- t), PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
forall p b a t s (bs1 :: [*]).
(Grammar p b (a :- t) -> Grammar p s (a :- t))
-> Coproduct p s bs1 a t -> Coproduct p s (b : bs1) a t
With (Grammar
  Position
  (PrimExp v :- (PrimExp v :- (CmpOp :- t)))
  (PrimExp v :- t)
-> Grammar
     Position (Sexp :- t) (PrimExp v :- (PrimExp v :- (CmpOp :- t)))
-> Grammar Position (Sexp :- t) (PrimExp v :- t)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Grammar
  Position
  (List :- t)
  (List :- (PrimExp v :- (PrimExp v :- (CmpOp :- t))))
-> Grammar
     Position (Sexp :- t) (PrimExp v :- (PrimExp v :- (CmpOp :- t)))
forall t t'.
Grammar Position (List :- t) (List :- t')
-> Grammar Position (Sexp :- t) t'
Sexp.list (Grammar Position (Sexp :- t) t
-> Grammar Position (List :- t) (List :- t)
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el (Text -> Grammar Position (Sexp :- t) t
forall t. Text -> Grammar Position (Sexp :- t) t
Sexp.sym Text
"cmp-op") Grammar Position (List :- t) (List :- t)
-> Grammar
     Position
     (List :- t)
     (List :- (PrimExp v :- (PrimExp v :- (CmpOp :- t))))
-> Grammar
     Position
     (List :- t)
     (List :- (PrimExp v :- (PrimExp v :- (CmpOp :- t))))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar Position (Sexp :- t) (CmpOp :- t)
-> Grammar Position (List :- t) (List :- (CmpOp :- t))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar Position (Sexp :- t) (CmpOp :- t)
forall a. SexpIso a => SexpGrammar a
sexpIso Grammar Position (List :- t) (List :- (CmpOp :- t))
-> Grammar
     Position
     (List :- (CmpOp :- t))
     (List :- (PrimExp v :- (PrimExp v :- (CmpOp :- t))))
-> Grammar
     Position
     (List :- t)
     (List :- (PrimExp v :- (PrimExp v :- (CmpOp :- t))))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar Position (Sexp :- (CmpOp :- t)) (PrimExp v :- (CmpOp :- t))
-> Grammar
     Position
     (List :- (CmpOp :- t))
     (List :- (PrimExp v :- (CmpOp :- t)))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar Position (Sexp :- (CmpOp :- t)) (PrimExp v :- (CmpOp :- t))
forall a. SexpIso a => SexpGrammar a
sexpIso Grammar
  Position
  (List :- (CmpOp :- t))
  (List :- (PrimExp v :- (CmpOp :- t)))
-> Grammar
     Position
     (List :- (PrimExp v :- (CmpOp :- t)))
     (List :- (PrimExp v :- (PrimExp v :- (CmpOp :- t))))
-> Grammar
     Position
     (List :- (CmpOp :- t))
     (List :- (PrimExp v :- (PrimExp v :- (CmpOp :- t))))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar
  Position
  (Sexp :- (PrimExp v :- (CmpOp :- t)))
  (PrimExp v :- (PrimExp v :- (CmpOp :- t)))
-> Grammar
     Position
     (List :- (PrimExp v :- (CmpOp :- t)))
     (List :- (PrimExp v :- (PrimExp v :- (CmpOp :- t))))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar
  Position
  (Sexp :- (PrimExp v :- (CmpOp :- t)))
  (PrimExp v :- (PrimExp v :- (CmpOp :- t)))
forall a. SexpIso a => SexpGrammar a
sexpIso)) (Coproduct
   Position
   (Sexp :- t)
   '[PrimExp v :- (UnOp :- t), PrimExp v :- (ConvOp :- t),
     PrimType :- ([PrimExp v] :- (String :- t))]
   (PrimExp v)
   t
 -> Coproduct
      Position
      (Sexp :- t)
      '[PrimExp v :- (PrimExp v :- (CmpOp :- t)),
        PrimExp v :- (UnOp :- t), PrimExp v :- (ConvOp :- t),
        PrimType :- ([PrimExp v] :- (String :- t))]
      (PrimExp v)
      t)
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (UnOp :- t), PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (PrimExp v :- (CmpOp :- t)),
       PrimExp v :- (UnOp :- t), PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
forall a b. (a -> b) -> a -> b
$
              (Grammar Position (PrimExp v :- (UnOp :- t)) (PrimExp v :- t)
 -> Grammar Position (Sexp :- t) (PrimExp v :- t))
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (UnOp :- t), PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
forall p b a t s (bs1 :: [*]).
(Grammar p b (a :- t) -> Grammar p s (a :- t))
-> Coproduct p s bs1 a t -> Coproduct p s (b : bs1) a t
With (Grammar Position (PrimExp v :- (UnOp :- t)) (PrimExp v :- t)
-> Grammar Position (Sexp :- t) (PrimExp v :- (UnOp :- t))
-> Grammar Position (Sexp :- t) (PrimExp v :- t)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Grammar Position (List :- t) (List :- (PrimExp v :- (UnOp :- t)))
-> Grammar Position (Sexp :- t) (PrimExp v :- (UnOp :- t))
forall t t'.
Grammar Position (List :- t) (List :- t')
-> Grammar Position (Sexp :- t) t'
Sexp.list (Grammar Position (Sexp :- t) t
-> Grammar Position (List :- t) (List :- t)
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el (Text -> Grammar Position (Sexp :- t) t
forall t. Text -> Grammar Position (Sexp :- t) t
Sexp.sym Text
"un-op") Grammar Position (List :- t) (List :- t)
-> Grammar
     Position (List :- t) (List :- (PrimExp v :- (UnOp :- t)))
-> Grammar
     Position (List :- t) (List :- (PrimExp v :- (UnOp :- t)))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar Position (Sexp :- t) (UnOp :- t)
-> Grammar Position (List :- t) (List :- (UnOp :- t))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar Position (Sexp :- t) (UnOp :- t)
forall a. SexpIso a => SexpGrammar a
sexpIso Grammar Position (List :- t) (List :- (UnOp :- t))
-> Grammar
     Position (List :- (UnOp :- t)) (List :- (PrimExp v :- (UnOp :- t)))
-> Grammar
     Position (List :- t) (List :- (PrimExp v :- (UnOp :- t)))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar Position (Sexp :- (UnOp :- t)) (PrimExp v :- (UnOp :- t))
-> Grammar
     Position (List :- (UnOp :- t)) (List :- (PrimExp v :- (UnOp :- t)))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar Position (Sexp :- (UnOp :- t)) (PrimExp v :- (UnOp :- t))
forall a. SexpIso a => SexpGrammar a
sexpIso)) (Coproduct
   Position
   (Sexp :- t)
   '[PrimExp v :- (ConvOp :- t),
     PrimType :- ([PrimExp v] :- (String :- t))]
   (PrimExp v)
   t
 -> Coproduct
      Position
      (Sexp :- t)
      '[PrimExp v :- (UnOp :- t), PrimExp v :- (ConvOp :- t),
        PrimType :- ([PrimExp v] :- (String :- t))]
      (PrimExp v)
      t)
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (UnOp :- t), PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
forall a b. (a -> b) -> a -> b
$
                (Grammar Position (PrimExp v :- (ConvOp :- t)) (PrimExp v :- t)
 -> Grammar Position (Sexp :- t) (PrimExp v :- t))
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
forall p b a t s (bs1 :: [*]).
(Grammar p b (a :- t) -> Grammar p s (a :- t))
-> Coproduct p s bs1 a t -> Coproduct p s (b : bs1) a t
With (Grammar Position (PrimExp v :- (ConvOp :- t)) (PrimExp v :- t)
-> Grammar Position (Sexp :- t) (PrimExp v :- (ConvOp :- t))
-> Grammar Position (Sexp :- t) (PrimExp v :- t)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Grammar Position (List :- t) (List :- (PrimExp v :- (ConvOp :- t)))
-> Grammar Position (Sexp :- t) (PrimExp v :- (ConvOp :- t))
forall t t'.
Grammar Position (List :- t) (List :- t')
-> Grammar Position (Sexp :- t) t'
Sexp.list (Grammar Position (Sexp :- t) t
-> Grammar Position (List :- t) (List :- t)
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el (Text -> Grammar Position (Sexp :- t) t
forall t. Text -> Grammar Position (Sexp :- t) t
Sexp.sym Text
"conv-op") Grammar Position (List :- t) (List :- t)
-> Grammar
     Position (List :- t) (List :- (PrimExp v :- (ConvOp :- t)))
-> Grammar
     Position (List :- t) (List :- (PrimExp v :- (ConvOp :- t)))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar Position (Sexp :- t) (ConvOp :- t)
-> Grammar Position (List :- t) (List :- (ConvOp :- t))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar Position (Sexp :- t) (ConvOp :- t)
forall a. SexpIso a => SexpGrammar a
sexpIso Grammar Position (List :- t) (List :- (ConvOp :- t))
-> Grammar
     Position
     (List :- (ConvOp :- t))
     (List :- (PrimExp v :- (ConvOp :- t)))
-> Grammar
     Position (List :- t) (List :- (PrimExp v :- (ConvOp :- t)))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar
  Position (Sexp :- (ConvOp :- t)) (PrimExp v :- (ConvOp :- t))
-> Grammar
     Position
     (List :- (ConvOp :- t))
     (List :- (PrimExp v :- (ConvOp :- t)))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar
  Position (Sexp :- (ConvOp :- t)) (PrimExp v :- (ConvOp :- t))
forall a. SexpIso a => SexpGrammar a
sexpIso)) (Coproduct
   Position
   (Sexp :- t)
   '[PrimType :- ([PrimExp v] :- (String :- t))]
   (PrimExp v)
   t
 -> Coproduct
      Position
      (Sexp :- t)
      '[PrimExp v :- (ConvOp :- t),
        PrimType :- ([PrimExp v] :- (String :- t))]
      (PrimExp v)
      t)
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimExp v :- (ConvOp :- t),
       PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
forall a b. (a -> b) -> a -> b
$
                  (Grammar
   Position
   (PrimType :- ([PrimExp v] :- (String :- t)))
   (PrimExp v :- t)
 -> Grammar Position (Sexp :- t) (PrimExp v :- t))
-> Coproduct Position (Sexp :- t) '[] (PrimExp v) t
-> Coproduct
     Position
     (Sexp :- t)
     '[PrimType :- ([PrimExp v] :- (String :- t))]
     (PrimExp v)
     t
forall p b a t s (bs1 :: [*]).
(Grammar p b (a :- t) -> Grammar p s (a :- t))
-> Coproduct p s bs1 a t -> Coproduct p s (b : bs1) a t
With
                    (Grammar
  Position
  (PrimType :- ([PrimExp v] :- (String :- t)))
  (PrimExp v :- t)
-> Grammar
     Position (Sexp :- t) (PrimType :- ([PrimExp v] :- (String :- t)))
-> Grammar Position (Sexp :- t) (PrimExp v :- t)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Grammar
  Position
  (List :- t)
  (List :- (PrimType :- ([PrimExp v] :- (String :- t))))
-> Grammar
     Position (Sexp :- t) (PrimType :- ([PrimExp v] :- (String :- t)))
forall t t'.
Grammar Position (List :- t) (List :- t')
-> Grammar Position (Sexp :- t) t'
Sexp.list (Grammar Position (Sexp :- t) t
-> Grammar Position (List :- t) (List :- t)
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el (Text -> Grammar Position (Sexp :- t) t
forall t. Text -> Grammar Position (Sexp :- t) t
Sexp.sym Text
"fun") Grammar Position (List :- t) (List :- t)
-> Grammar
     Position
     (List :- t)
     (List :- (PrimType :- ([PrimExp v] :- (String :- t))))
-> Grammar
     Position
     (List :- t)
     (List :- (PrimType :- ([PrimExp v] :- (String :- t))))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar Position (Sexp :- t) (String :- t)
-> Grammar Position (List :- t) (List :- (String :- t))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el ((Text -> String)
-> (String -> Text) -> Grammar Position (Text :- t) (String :- t)
forall a b p t. (a -> b) -> (b -> a) -> Grammar p (a :- t) (b :- t)
iso Text -> String
T.unpack String -> Text
T.pack Grammar Position (Text :- t) (String :- t)
-> Grammar Position (Sexp :- t) (Text :- t)
-> Grammar Position (Sexp :- t) (String :- t)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Grammar Position (Sexp :- t) (Text :- t)
forall a. SexpIso a => SexpGrammar a
sexpIso) Grammar Position (List :- t) (List :- (String :- t))
-> Grammar
     Position
     (List :- (String :- t))
     (List :- (PrimType :- ([PrimExp v] :- (String :- t))))
-> Grammar
     Position
     (List :- t)
     (List :- (PrimType :- ([PrimExp v] :- (String :- t))))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar
  Position (Sexp :- (String :- t)) ([PrimExp v] :- (String :- t))
-> Grammar
     Position
     (List :- (String :- t))
     (List :- ([PrimExp v] :- (String :- t)))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar
  Position (Sexp :- (String :- t)) ([PrimExp v] :- (String :- t))
forall a. SexpIso a => SexpGrammar a
sexpIso Grammar
  Position
  (List :- (String :- t))
  (List :- ([PrimExp v] :- (String :- t)))
-> Grammar
     Position
     (List :- ([PrimExp v] :- (String :- t)))
     (List :- (PrimType :- ([PrimExp v] :- (String :- t))))
-> Grammar
     Position
     (List :- (String :- t))
     (List :- (PrimType :- ([PrimExp v] :- (String :- t))))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar
  Position
  (Sexp :- ([PrimExp v] :- (String :- t)))
  (PrimType :- ([PrimExp v] :- (String :- t)))
-> Grammar
     Position
     (List :- ([PrimExp v] :- (String :- t)))
     (List :- (PrimType :- ([PrimExp v] :- (String :- t))))
forall t t'.
Grammar Position (Sexp :- t) t'
-> Grammar Position (List :- t) (List :- t')
Sexp.el Grammar
  Position
  (Sexp :- ([PrimExp v] :- (String :- t)))
  (PrimType :- ([PrimExp v] :- (String :- t)))
forall a. SexpIso a => SexpGrammar a
sexpIso))
                    Coproduct Position (Sexp :- t) '[] (PrimExp v) t
forall p s a t. Coproduct p s '[] a t
End

instance Functor PrimExp where
  fmap :: (a -> b) -> PrimExp a -> PrimExp b
fmap = (a -> b) -> PrimExp a -> PrimExp b
forall (t :: * -> *) a b. Traversable t => (a -> b) -> t a -> t b
fmapDefault

instance Foldable PrimExp where
  foldMap :: (a -> m) -> PrimExp a -> m
foldMap = (a -> m) -> PrimExp a -> m
forall (t :: * -> *) m a.
(Traversable t, Monoid m) =>
(a -> m) -> t a -> m
foldMapDefault

instance Traversable PrimExp where
  traverse :: (a -> f b) -> PrimExp a -> f (PrimExp b)
traverse a -> f b
f (LeafExp a
v PrimType
t) =
    b -> PrimType -> PrimExp b
forall v. v -> PrimType -> PrimExp v
LeafExp (b -> PrimType -> PrimExp b) -> f b -> f (PrimType -> PrimExp b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
v f (PrimType -> PrimExp b) -> f PrimType -> f (PrimExp b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> PrimType -> f PrimType
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimType
t
  traverse a -> f b
_ (ValueExp PrimValue
v) =
    PrimExp b -> f (PrimExp b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PrimExp b -> f (PrimExp b)) -> PrimExp b -> f (PrimExp b)
forall a b. (a -> b) -> a -> b
$ PrimValue -> PrimExp b
forall v. PrimValue -> PrimExp v
ValueExp PrimValue
v
  traverse a -> f b
f (BinOpExp BinOp
op PrimExp a
x PrimExp a
y) =
    PrimExp b -> PrimExp b
forall v. PrimExp v -> PrimExp v
constFoldPrimExp (PrimExp b -> PrimExp b) -> f (PrimExp b) -> f (PrimExp b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (BinOp -> PrimExp b -> PrimExp b -> PrimExp b
forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp BinOp
op (PrimExp b -> PrimExp b -> PrimExp b)
-> f (PrimExp b) -> f (PrimExp b -> PrimExp b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> PrimExp a -> f (PrimExp b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f PrimExp a
x f (PrimExp b -> PrimExp b) -> f (PrimExp b) -> f (PrimExp b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (a -> f b) -> PrimExp a -> f (PrimExp b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f PrimExp a
y)
  traverse a -> f b
f (CmpOpExp CmpOp
op PrimExp a
x PrimExp a
y) =
    CmpOp -> PrimExp b -> PrimExp b -> PrimExp b
forall v. CmpOp -> PrimExp v -> PrimExp v -> PrimExp v
CmpOpExp CmpOp
op (PrimExp b -> PrimExp b -> PrimExp b)
-> f (PrimExp b) -> f (PrimExp b -> PrimExp b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> PrimExp a -> f (PrimExp b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f PrimExp a
x f (PrimExp b -> PrimExp b) -> f (PrimExp b) -> f (PrimExp b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (a -> f b) -> PrimExp a -> f (PrimExp b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f PrimExp a
y
  traverse a -> f b
f (ConvOpExp ConvOp
op PrimExp a
x) =
    ConvOp -> PrimExp b -> PrimExp b
forall v. ConvOp -> PrimExp v -> PrimExp v
ConvOpExp ConvOp
op (PrimExp b -> PrimExp b) -> f (PrimExp b) -> f (PrimExp b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> PrimExp a -> f (PrimExp b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f PrimExp a
x
  traverse a -> f b
f (UnOpExp UnOp
op PrimExp a
x) =
    UnOp -> PrimExp b -> PrimExp b
forall v. UnOp -> PrimExp v -> PrimExp v
UnOpExp UnOp
op (PrimExp b -> PrimExp b) -> f (PrimExp b) -> f (PrimExp b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> PrimExp a -> f (PrimExp b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f PrimExp a
x
  traverse a -> f b
f (FunExp String
h [PrimExp a]
args PrimType
t) =
    String -> [PrimExp b] -> PrimType -> PrimExp b
forall v. String -> [PrimExp v] -> PrimType -> PrimExp v
FunExp String
h ([PrimExp b] -> PrimType -> PrimExp b)
-> f [PrimExp b] -> f (PrimType -> PrimExp b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (PrimExp a -> f (PrimExp b)) -> [PrimExp a] -> f [PrimExp b]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((a -> f b) -> PrimExp a -> f (PrimExp b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f) [PrimExp a]
args f (PrimType -> PrimExp b) -> f PrimType -> f (PrimExp b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> PrimType -> f PrimType
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimType
t

instance FreeIn v => FreeIn (PrimExp v) where
  freeIn' :: PrimExp v -> FV
freeIn' = (v -> FV) -> PrimExp v -> FV
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap v -> FV
forall a. FreeIn a => a -> FV
freeIn'

-- | A 'PrimExp' tagged with a phantom type used to provide type-safe
-- construction.  Does not guarantee that the underlying expression is
-- actually type correct.
newtype TPrimExp t v = TPrimExp {TPrimExp t v -> PrimExp v
untyped :: PrimExp v}
  deriving (TPrimExp t v -> TPrimExp t v -> Bool
(TPrimExp t v -> TPrimExp t v -> Bool)
-> (TPrimExp t v -> TPrimExp t v -> Bool) -> Eq (TPrimExp t v)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall t v. Eq v => TPrimExp t v -> TPrimExp t v -> Bool
/= :: TPrimExp t v -> TPrimExp t v -> Bool
$c/= :: forall t v. Eq v => TPrimExp t v -> TPrimExp t v -> Bool
== :: TPrimExp t v -> TPrimExp t v -> Bool
$c== :: forall t v. Eq v => TPrimExp t v -> TPrimExp t v -> Bool
Eq, Eq (TPrimExp t v)
Eq (TPrimExp t v)
-> (TPrimExp t v -> TPrimExp t v -> Ordering)
-> (TPrimExp t v -> TPrimExp t v -> Bool)
-> (TPrimExp t v -> TPrimExp t v -> Bool)
-> (TPrimExp t v -> TPrimExp t v -> Bool)
-> (TPrimExp t v -> TPrimExp t v -> Bool)
-> (TPrimExp t v -> TPrimExp t v -> TPrimExp t v)
-> (TPrimExp t v -> TPrimExp t v -> TPrimExp t v)
-> Ord (TPrimExp t v)
TPrimExp t v -> TPrimExp t v -> Bool
TPrimExp t v -> TPrimExp t v -> Ordering
TPrimExp t v -> TPrimExp t v -> TPrimExp t v
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall t v. Ord v => Eq (TPrimExp t v)
forall t v. Ord v => TPrimExp t v -> TPrimExp t v -> Bool
forall t v. Ord v => TPrimExp t v -> TPrimExp t v -> Ordering
forall t v. Ord v => TPrimExp t v -> TPrimExp t v -> TPrimExp t v
min :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
$cmin :: forall t v. Ord v => TPrimExp t v -> TPrimExp t v -> TPrimExp t v
max :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
$cmax :: forall t v. Ord v => TPrimExp t v -> TPrimExp t v -> TPrimExp t v
>= :: TPrimExp t v -> TPrimExp t v -> Bool
$c>= :: forall t v. Ord v => TPrimExp t v -> TPrimExp t v -> Bool
> :: TPrimExp t v -> TPrimExp t v -> Bool
$c> :: forall t v. Ord v => TPrimExp t v -> TPrimExp t v -> Bool
<= :: TPrimExp t v -> TPrimExp t v -> Bool
$c<= :: forall t v. Ord v => TPrimExp t v -> TPrimExp t v -> Bool
< :: TPrimExp t v -> TPrimExp t v -> Bool
$c< :: forall t v. Ord v => TPrimExp t v -> TPrimExp t v -> Bool
compare :: TPrimExp t v -> TPrimExp t v -> Ordering
$ccompare :: forall t v. Ord v => TPrimExp t v -> TPrimExp t v -> Ordering
$cp1Ord :: forall t v. Ord v => Eq (TPrimExp t v)
Ord, Int -> TPrimExp t v -> ShowS
[TPrimExp t v] -> ShowS
TPrimExp t v -> String
(Int -> TPrimExp t v -> ShowS)
-> (TPrimExp t v -> String)
-> ([TPrimExp t v] -> ShowS)
-> Show (TPrimExp t v)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall t v. Show v => Int -> TPrimExp t v -> ShowS
forall t v. Show v => [TPrimExp t v] -> ShowS
forall t v. Show v => TPrimExp t v -> String
showList :: [TPrimExp t v] -> ShowS
$cshowList :: forall t v. Show v => [TPrimExp t v] -> ShowS
show :: TPrimExp t v -> String
$cshow :: forall t v. Show v => TPrimExp t v -> String
showsPrec :: Int -> TPrimExp t v -> ShowS
$cshowsPrec :: forall t v. Show v => Int -> TPrimExp t v -> ShowS
Show, (forall x. TPrimExp t v -> Rep (TPrimExp t v) x)
-> (forall x. Rep (TPrimExp t v) x -> TPrimExp t v)
-> Generic (TPrimExp t v)
forall x. Rep (TPrimExp t v) x -> TPrimExp t v
forall x. TPrimExp t v -> Rep (TPrimExp t v) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall t v x. Rep (TPrimExp t v) x -> TPrimExp t v
forall t v x. TPrimExp t v -> Rep (TPrimExp t v) x
$cto :: forall t v x. Rep (TPrimExp t v) x -> TPrimExp t v
$cfrom :: forall t v x. TPrimExp t v -> Rep (TPrimExp t v) x
Generic)

instance SexpIso v => SexpIso (TPrimExp t v) where
  sexpIso :: Grammar Position (Sexp :- t) (TPrimExp t v :- t)
sexpIso = (Grammar Position (PrimExp v :- t) (TPrimExp t v :- t)
 -> Grammar Position (Sexp :- t) (TPrimExp t v :- t))
-> Grammar Position (Sexp :- t) (TPrimExp t v :- t)
forall a b s t (c :: Meta) (d :: Meta) (f :: * -> *) p.
(Generic a, MkPrismList (Rep a), MkStackPrism f,
 Rep a ~ M1 D d (M1 C c f), StackPrismLhs f t ~ b, Constructor c) =>
(Grammar p b (a :- t) -> Grammar p s (a :- t))
-> Grammar p s (a :- t)
with ((Grammar Position (PrimExp v :- t) (TPrimExp t v :- t)
  -> Grammar Position (Sexp :- t) (TPrimExp t v :- t))
 -> Grammar Position (Sexp :- t) (TPrimExp t v :- t))
-> (Grammar Position (PrimExp v :- t) (TPrimExp t v :- t)
    -> Grammar Position (Sexp :- t) (TPrimExp t v :- t))
-> Grammar Position (Sexp :- t) (TPrimExp t v :- t)
forall a b. (a -> b) -> a -> b
$ \Grammar Position (PrimExp v :- t) (TPrimExp t v :- t)
e -> Grammar Position (Sexp :- t) (PrimExp v :- t)
forall a. SexpIso a => SexpGrammar a
sexpIso Grammar Position (Sexp :- t) (PrimExp v :- t)
-> Grammar Position (PrimExp v :- t) (TPrimExp t v :- t)
-> Grammar Position (Sexp :- t) (TPrimExp t v :- t)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Grammar Position (PrimExp v :- t) (TPrimExp t v :- t)
e

instance Functor (TPrimExp t) where
  fmap :: (a -> b) -> TPrimExp t a -> TPrimExp t b
fmap = (a -> b) -> TPrimExp t a -> TPrimExp t b
forall (t :: * -> *) a b. Traversable t => (a -> b) -> t a -> t b
fmapDefault

instance Foldable (TPrimExp t) where
  foldMap :: (a -> m) -> TPrimExp t a -> m
foldMap = (a -> m) -> TPrimExp t a -> m
forall (t :: * -> *) m a.
(Traversable t, Monoid m) =>
(a -> m) -> t a -> m
foldMapDefault

instance Traversable (TPrimExp t) where
  traverse :: (a -> f b) -> TPrimExp t a -> f (TPrimExp t b)
traverse a -> f b
f (TPrimExp PrimExp a
e) = PrimExp b -> TPrimExp t b
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp b -> TPrimExp t b) -> f (PrimExp b) -> f (TPrimExp t b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> PrimExp a -> f (PrimExp b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f PrimExp a
e

instance FreeIn v => FreeIn (TPrimExp t v) where
  freeIn' :: TPrimExp t v -> FV
freeIn' = PrimExp v -> FV
forall a. FreeIn a => a -> FV
freeIn' (PrimExp v -> FV)
-> (TPrimExp t v -> PrimExp v) -> TPrimExp t v -> FV
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TPrimExp t v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped

-- | This expression is of type 'Int8'.
isInt8 :: PrimExp v -> TPrimExp Int8 v
isInt8 :: PrimExp v -> TPrimExp Int8 v
isInt8 = PrimExp v -> TPrimExp Int8 v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp

-- | This expression is of type 'Int16'.
isInt16 :: PrimExp v -> TPrimExp Int16 v
isInt16 :: PrimExp v -> TPrimExp Int16 v
isInt16 = PrimExp v -> TPrimExp Int16 v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp

-- | This expression is of type 'Int32'.
isInt32 :: PrimExp v -> TPrimExp Int32 v
isInt32 :: PrimExp v -> TPrimExp Int32 v
isInt32 = PrimExp v -> TPrimExp Int32 v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp

-- | This expression is of type 'Int64'.
isInt64 :: PrimExp v -> TPrimExp Int64 v
isInt64 :: PrimExp v -> TPrimExp Int64 v
isInt64 = PrimExp v -> TPrimExp Int64 v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp

-- | This is a boolean expression.
isBool :: PrimExp v -> TPrimExp Bool v
isBool :: PrimExp v -> TPrimExp Bool v
isBool = PrimExp v -> TPrimExp Bool v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp

-- | This expression is of type 'Float'.
isF32 :: PrimExp v -> TPrimExp Float v
isF32 :: PrimExp v -> TPrimExp Float v
isF32 = PrimExp v -> TPrimExp Float v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp

-- | This expression is of type 'Double'.
isF64 :: PrimExp v -> TPrimExp Double v
isF64 :: PrimExp v -> TPrimExp Double v
isF64 = PrimExp v -> TPrimExp Double v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp

-- | True if the 'PrimExp' has at least this many nodes.  This can be
-- much more efficient than comparing with 'length' for large
-- 'PrimExp's, as this function is lazy.
primExpSizeAtLeast :: Int -> PrimExp v -> Bool
primExpSizeAtLeast :: Int -> PrimExp v -> Bool
primExpSizeAtLeast Int
k = Bool -> (Int -> Bool) -> Maybe Int -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
k) (Maybe Int -> Bool)
-> (PrimExp v -> Maybe Int) -> PrimExp v -> Bool
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int -> PrimExp v -> Maybe Int
forall v. Int -> PrimExp v -> Maybe Int
descend Int
0
  where
    descend :: Int -> PrimExp v -> Maybe Int
descend Int
i PrimExp v
_
      | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
k = Maybe Int
forall a. Maybe a
Nothing
    descend Int
i LeafExp {} = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
    descend Int
i ValueExp {} = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
    descend Int
i (BinOpExp BinOp
_ PrimExp v
x PrimExp v
y) = do
      Int
x' <- Int -> PrimExp v -> Maybe Int
descend (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) PrimExp v
x
      Int -> PrimExp v -> Maybe Int
descend Int
x' PrimExp v
y
    descend Int
i (CmpOpExp CmpOp
_ PrimExp v
x PrimExp v
y) = do
      Int
x' <- Int -> PrimExp v -> Maybe Int
descend (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) PrimExp v
x
      Int -> PrimExp v -> Maybe Int
descend Int
x' PrimExp v
y
    descend Int
i (ConvOpExp ConvOp
_ PrimExp v
x) = Int -> PrimExp v -> Maybe Int
descend (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) PrimExp v
x
    descend Int
i (UnOpExp UnOp
_ PrimExp v
x) = Int -> PrimExp v -> Maybe Int
descend (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) PrimExp v
x
    descend Int
i (FunExp String
_ [PrimExp v]
args PrimType
_) = (Int -> PrimExp v -> Maybe Int) -> Int -> [PrimExp v] -> Maybe Int
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Int -> PrimExp v -> Maybe Int
descend (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [PrimExp v]
args

-- | Perform quick and dirty constant folding on the top level of a
-- PrimExp.  This is necessary because we want to consider
-- e.g. equality modulo constant folding.
constFoldPrimExp :: PrimExp v -> PrimExp v
constFoldPrimExp :: PrimExp v -> PrimExp v
constFoldPrimExp (BinOpExp Add {} PrimExp v
x PrimExp v
y)
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
zeroIshExp PrimExp v
x = PrimExp v
y
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
zeroIshExp PrimExp v
y = PrimExp v
x
constFoldPrimExp (BinOpExp Sub {} PrimExp v
x PrimExp v
y)
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
zeroIshExp PrimExp v
y = PrimExp v
x
constFoldPrimExp (BinOpExp Mul {} PrimExp v
x PrimExp v
y)
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
oneIshExp PrimExp v
x = PrimExp v
y
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
oneIshExp PrimExp v
y = PrimExp v
x
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
zeroIshExp PrimExp v
x,
    IntType IntType
it <- PrimExp v -> PrimType
forall v. PrimExp v -> PrimType
primExpType PrimExp v
y =
    PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v) -> PrimValue -> PrimExp v
forall a b. (a -> b) -> a -> b
$ IntValue -> PrimValue
IntValue (IntValue -> PrimValue) -> IntValue -> PrimValue
forall a b. (a -> b) -> a -> b
$ IntType -> Int -> IntValue
forall int. Integral int => IntType -> int -> IntValue
intValue IntType
it (Int
0 :: Int)
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
zeroIshExp PrimExp v
y,
    IntType IntType
it <- PrimExp v -> PrimType
forall v. PrimExp v -> PrimType
primExpType PrimExp v
x =
    PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v) -> PrimValue -> PrimExp v
forall a b. (a -> b) -> a -> b
$ IntValue -> PrimValue
IntValue (IntValue -> PrimValue) -> IntValue -> PrimValue
forall a b. (a -> b) -> a -> b
$ IntType -> Int -> IntValue
forall int. Integral int => IntType -> int -> IntValue
intValue IntType
it (Int
0 :: Int)
constFoldPrimExp (BinOpExp SDiv {} PrimExp v
x PrimExp v
y)
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
oneIshExp PrimExp v
y = PrimExp v
x
constFoldPrimExp (BinOpExp SQuot {} PrimExp v
x PrimExp v
y)
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
oneIshExp PrimExp v
y = PrimExp v
x
constFoldPrimExp (BinOpExp UDiv {} PrimExp v
x PrimExp v
y)
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
oneIshExp PrimExp v
y = PrimExp v
x
constFoldPrimExp (BinOpExp BinOp
bop (ValueExp PrimValue
x) (ValueExp PrimValue
y))
  | Just PrimValue
z <- BinOp -> PrimValue -> PrimValue -> Maybe PrimValue
doBinOp BinOp
bop PrimValue
x PrimValue
y =
    PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp PrimValue
z
constFoldPrimExp (BinOpExp BinOp
LogAnd PrimExp v
x PrimExp v
y)
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
oneIshExp PrimExp v
x = PrimExp v
y
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
oneIshExp PrimExp v
y = PrimExp v
x
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
zeroIshExp PrimExp v
x = PrimExp v
x
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
zeroIshExp PrimExp v
y = PrimExp v
y
constFoldPrimExp (BinOpExp BinOp
LogOr PrimExp v
x PrimExp v
y)
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
oneIshExp PrimExp v
x = PrimExp v
x
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
oneIshExp PrimExp v
y = PrimExp v
y
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
zeroIshExp PrimExp v
x = PrimExp v
y
  | PrimExp v -> Bool
forall a. PrimExp a -> Bool
zeroIshExp PrimExp v
y = PrimExp v
x
constFoldPrimExp PrimExp v
e = PrimExp v
e

-- | The class of numeric types that can be used for constructing
-- 'TPrimExp's.
class NumExp t where
  -- | Construct a typed expression from an integer.
  fromInteger' :: Integer -> TPrimExp t v

-- | The class of integer types that can be used for constructing
-- 'TPrimExp's.
class NumExp t => IntExp t

instance NumExp Int8 where
  fromInteger' :: Integer -> TPrimExp Int8 v
fromInteger' = PrimExp v -> TPrimExp Int8 v
forall v. PrimExp v -> TPrimExp Int8 v
isInt8 (PrimExp v -> TPrimExp Int8 v)
-> (Integer -> PrimExp v) -> Integer -> TPrimExp Int8 v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v)
-> (Integer -> PrimValue) -> Integer -> PrimExp v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IntValue -> PrimValue
IntValue (IntValue -> PrimValue)
-> (Integer -> IntValue) -> Integer -> PrimValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int8 -> IntValue
Int8Value (Int8 -> IntValue) -> (Integer -> Int8) -> Integer -> IntValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Integer -> Int8
forall a. Num a => Integer -> a
fromInteger

instance IntExp Int8

instance NumExp Int16 where
  fromInteger' :: Integer -> TPrimExp Int16 v
fromInteger' = PrimExp v -> TPrimExp Int16 v
forall v. PrimExp v -> TPrimExp Int16 v
isInt16 (PrimExp v -> TPrimExp Int16 v)
-> (Integer -> PrimExp v) -> Integer -> TPrimExp Int16 v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v)
-> (Integer -> PrimValue) -> Integer -> PrimExp v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IntValue -> PrimValue
IntValue (IntValue -> PrimValue)
-> (Integer -> IntValue) -> Integer -> PrimValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int16 -> IntValue
Int16Value (Int16 -> IntValue) -> (Integer -> Int16) -> Integer -> IntValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Integer -> Int16
forall a. Num a => Integer -> a
fromInteger

instance IntExp Int16

instance NumExp Int32 where
  fromInteger' :: Integer -> TPrimExp Int32 v
fromInteger' = PrimExp v -> TPrimExp Int32 v
forall v. PrimExp v -> TPrimExp Int32 v
isInt32 (PrimExp v -> TPrimExp Int32 v)
-> (Integer -> PrimExp v) -> Integer -> TPrimExp Int32 v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v)
-> (Integer -> PrimValue) -> Integer -> PrimExp v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IntValue -> PrimValue
IntValue (IntValue -> PrimValue)
-> (Integer -> IntValue) -> Integer -> PrimValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int32 -> IntValue
Int32Value (Int32 -> IntValue) -> (Integer -> Int32) -> Integer -> IntValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Integer -> Int32
forall a. Num a => Integer -> a
fromInteger

instance IntExp Int32

instance NumExp Int64 where
  fromInteger' :: Integer -> TPrimExp Int64 v
fromInteger' = PrimExp v -> TPrimExp Int64 v
forall v. PrimExp v -> TPrimExp Int64 v
isInt64 (PrimExp v -> TPrimExp Int64 v)
-> (Integer -> PrimExp v) -> Integer -> TPrimExp Int64 v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v)
-> (Integer -> PrimValue) -> Integer -> PrimExp v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IntValue -> PrimValue
IntValue (IntValue -> PrimValue)
-> (Integer -> IntValue) -> Integer -> PrimValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int64 -> IntValue
Int64Value (Int64 -> IntValue) -> (Integer -> Int64) -> Integer -> IntValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Integer -> Int64
forall a. Num a => Integer -> a
fromInteger

instance IntExp Int64

-- | The class of floating-point types that can be used for
-- constructing 'TPrimExp's.
class NumExp t => FloatExp t where
  -- | Construct a typed expression from a rational.
  fromRational' :: Rational -> TPrimExp t v

instance NumExp Float where
  fromInteger' :: Integer -> TPrimExp Float v
fromInteger' = PrimExp v -> TPrimExp Float v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Float v)
-> (Integer -> PrimExp v) -> Integer -> TPrimExp Float v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v)
-> (Integer -> PrimValue) -> Integer -> PrimExp v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. FloatValue -> PrimValue
FloatValue (FloatValue -> PrimValue)
-> (Integer -> FloatValue) -> Integer -> PrimValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Float -> FloatValue
Float32Value (Float -> FloatValue)
-> (Integer -> Float) -> Integer -> FloatValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Integer -> Float
forall a. Num a => Integer -> a
fromInteger

instance NumExp Double where
  fromInteger' :: Integer -> TPrimExp Double v
fromInteger' = PrimExp v -> TPrimExp Double v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Double v)
-> (Integer -> PrimExp v) -> Integer -> TPrimExp Double v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v)
-> (Integer -> PrimValue) -> Integer -> PrimExp v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. FloatValue -> PrimValue
FloatValue (FloatValue -> PrimValue)
-> (Integer -> FloatValue) -> Integer -> PrimValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Double -> FloatValue
Float64Value (Double -> FloatValue)
-> (Integer -> Double) -> Integer -> FloatValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Integer -> Double
forall a. Num a => Integer -> a
fromInteger

instance FloatExp Float where
  fromRational' :: Rational -> TPrimExp Float v
fromRational' = PrimExp v -> TPrimExp Float v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Float v)
-> (Rational -> PrimExp v) -> Rational -> TPrimExp Float v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v)
-> (Rational -> PrimValue) -> Rational -> PrimExp v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. FloatValue -> PrimValue
FloatValue (FloatValue -> PrimValue)
-> (Rational -> FloatValue) -> Rational -> PrimValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Float -> FloatValue
Float32Value (Float -> FloatValue)
-> (Rational -> Float) -> Rational -> FloatValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Rational -> Float
forall a. Fractional a => Rational -> a
fromRational

instance FloatExp Double where
  fromRational' :: Rational -> TPrimExp Double v
fromRational' = PrimExp v -> TPrimExp Double v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Double v)
-> (Rational -> PrimExp v) -> Rational -> TPrimExp Double v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v)
-> (Rational -> PrimValue) -> Rational -> PrimExp v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. FloatValue -> PrimValue
FloatValue (FloatValue -> PrimValue)
-> (Rational -> FloatValue) -> Rational -> PrimValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Double -> FloatValue
Float64Value (Double -> FloatValue)
-> (Rational -> Double) -> Rational -> FloatValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Rational -> Double
forall a. Fractional a => Rational -> a
fromRational

instance (NumExp t, Pretty v) => Num (TPrimExp t v) where
  TPrimExp PrimExp v
x + :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
+ TPrimExp PrimExp v
y
    | Just PrimExp v
z <-
        [Maybe (PrimExp v)] -> Maybe (PrimExp v)
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum
          [ (IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
forall v.
(IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asIntOp (IntType -> Overflow -> BinOp
`Add` Overflow
OverflowUndef) PrimExp v
x PrimExp v
y,
            (FloatType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
forall v.
(FloatType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asFloatOp FloatType -> BinOp
FAdd PrimExp v
x PrimExp v
y
          ] =
      PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp t v) -> PrimExp v -> TPrimExp t v
forall a b. (a -> b) -> a -> b
$ PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp PrimExp v
z
    | Bool
otherwise = String -> (PrimExp v, PrimExp v) -> TPrimExp t v
forall a b. Pretty a => String -> a -> b
numBad String
"+" (PrimExp v
x, PrimExp v
y)

  TPrimExp PrimExp v
x - :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
- TPrimExp PrimExp v
y
    | Just PrimExp v
z <-
        [Maybe (PrimExp v)] -> Maybe (PrimExp v)
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum
          [ (IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
forall v.
(IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asIntOp (IntType -> Overflow -> BinOp
`Sub` Overflow
OverflowUndef) PrimExp v
x PrimExp v
y,
            (FloatType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
forall v.
(FloatType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asFloatOp FloatType -> BinOp
FSub PrimExp v
x PrimExp v
y
          ] =
      PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp t v) -> PrimExp v -> TPrimExp t v
forall a b. (a -> b) -> a -> b
$ PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp PrimExp v
z
    | Bool
otherwise = String -> (PrimExp v, PrimExp v) -> TPrimExp t v
forall a b. Pretty a => String -> a -> b
numBad String
"-" (PrimExp v
x, PrimExp v
y)

  TPrimExp PrimExp v
x * :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
* TPrimExp PrimExp v
y
    | Just PrimExp v
z <-
        [Maybe (PrimExp v)] -> Maybe (PrimExp v)
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum
          [ (IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
forall v.
(IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asIntOp (IntType -> Overflow -> BinOp
`Mul` Overflow
OverflowUndef) PrimExp v
x PrimExp v
y,
            (FloatType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
forall v.
(FloatType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asFloatOp FloatType -> BinOp
FMul PrimExp v
x PrimExp v
y
          ] =
      PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp t v) -> PrimExp v -> TPrimExp t v
forall a b. (a -> b) -> a -> b
$ PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp PrimExp v
z
    | Bool
otherwise = String -> (PrimExp v, PrimExp v) -> TPrimExp t v
forall a b. Pretty a => String -> a -> b
numBad String
"*" (PrimExp v
x, PrimExp v
y)

  abs :: TPrimExp t v -> TPrimExp t v
abs (TPrimExp PrimExp v
x)
    | IntType IntType
t <- PrimExp v -> PrimType
forall v. PrimExp v -> PrimType
primExpType PrimExp v
x = PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp t v) -> PrimExp v -> TPrimExp t v
forall a b. (a -> b) -> a -> b
$ UnOp -> PrimExp v -> PrimExp v
forall v. UnOp -> PrimExp v -> PrimExp v
UnOpExp (IntType -> UnOp
Abs IntType
t) PrimExp v
x
    | FloatType FloatType
t <- PrimExp v -> PrimType
forall v. PrimExp v -> PrimType
primExpType PrimExp v
x = PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp t v) -> PrimExp v -> TPrimExp t v
forall a b. (a -> b) -> a -> b
$ UnOp -> PrimExp v -> PrimExp v
forall v. UnOp -> PrimExp v -> PrimExp v
UnOpExp (FloatType -> UnOp
FAbs FloatType
t) PrimExp v
x
    | Bool
otherwise = String -> PrimExp v -> TPrimExp t v
forall a b. Pretty a => String -> a -> b
numBad String
"abs" PrimExp v
x

  signum :: TPrimExp t v -> TPrimExp t v
signum (TPrimExp PrimExp v
x)
    | IntType IntType
t <- PrimExp v -> PrimType
forall v. PrimExp v -> PrimType
primExpType PrimExp v
x = PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp t v) -> PrimExp v -> TPrimExp t v
forall a b. (a -> b) -> a -> b
$ UnOp -> PrimExp v -> PrimExp v
forall v. UnOp -> PrimExp v -> PrimExp v
UnOpExp (IntType -> UnOp
SSignum IntType
t) PrimExp v
x
    | Bool
otherwise = String -> PrimExp v -> TPrimExp t v
forall a b. Pretty a => String -> a -> b
numBad String
"signum" PrimExp v
x

  fromInteger :: Integer -> TPrimExp t v
fromInteger = Integer -> TPrimExp t v
forall t v. NumExp t => Integer -> TPrimExp t v
fromInteger'

instance (FloatExp t, Pretty v) => Fractional (TPrimExp t v) where
  TPrimExp PrimExp v
x / :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
/ TPrimExp PrimExp v
y
    | Just PrimExp v
z <- [Maybe (PrimExp v)] -> Maybe (PrimExp v)
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum [(FloatType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
forall v.
(FloatType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asFloatOp FloatType -> BinOp
FDiv PrimExp v
x PrimExp v
y] = PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp t v) -> PrimExp v -> TPrimExp t v
forall a b. (a -> b) -> a -> b
$ PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp PrimExp v
z
    | Bool
otherwise = String -> (PrimExp v, PrimExp v) -> TPrimExp t v
forall a b. Pretty a => String -> a -> b
numBad String
"/" (PrimExp v
x, PrimExp v
y)

  fromRational :: Rational -> TPrimExp t v
fromRational = Rational -> TPrimExp t v
forall t v. FloatExp t => Rational -> TPrimExp t v
fromRational'

instance (IntExp t, Pretty v) => IntegralExp (TPrimExp t v) where
  TPrimExp PrimExp v
x div :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
`div` TPrimExp PrimExp v
y
    | Just PrimExp v
z <-
        [Maybe (PrimExp v)] -> Maybe (PrimExp v)
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum
          [ (IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
forall v.
(IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asIntOp (IntType -> Safety -> BinOp
`SDiv` Safety
Unsafe) PrimExp v
x PrimExp v
y,
            (FloatType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
forall v.
(FloatType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asFloatOp FloatType -> BinOp
FDiv PrimExp v
x PrimExp v
y
          ] =
      PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp t v) -> PrimExp v -> TPrimExp t v
forall a b. (a -> b) -> a -> b
$ PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp PrimExp v
z
    | Bool
otherwise = String -> (PrimExp v, PrimExp v) -> TPrimExp t v
forall a b. Pretty a => String -> a -> b
numBad String
"div" (PrimExp v
x, PrimExp v
y)

  TPrimExp PrimExp v
x mod :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
`mod` TPrimExp PrimExp v
y
    | Just PrimExp v
z <- [Maybe (PrimExp v)] -> Maybe (PrimExp v)
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum [(IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
forall v.
(IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asIntOp (IntType -> Safety -> BinOp
`SMod` Safety
Unsafe) PrimExp v
x PrimExp v
y] =
      PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp PrimExp v
z
    | Bool
otherwise = String -> (PrimExp v, PrimExp v) -> TPrimExp t v
forall a b. Pretty a => String -> a -> b
numBad String
"mod" (PrimExp v
x, PrimExp v
y)

  TPrimExp PrimExp v
x quot :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
`quot` TPrimExp PrimExp v
y
    | PrimExp v -> Bool
forall a. PrimExp a -> Bool
oneIshExp PrimExp v
y = PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp PrimExp v
x
    | Just PrimExp v
z <- [Maybe (PrimExp v)] -> Maybe (PrimExp v)
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum [(IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
forall v.
(IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asIntOp (IntType -> Safety -> BinOp
`SQuot` Safety
Unsafe) PrimExp v
x PrimExp v
y] =
      PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp t v) -> PrimExp v -> TPrimExp t v
forall a b. (a -> b) -> a -> b
$ PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp PrimExp v
z
    | Bool
otherwise = String -> (PrimExp v, PrimExp v) -> TPrimExp t v
forall a b. Pretty a => String -> a -> b
numBad String
"quot" (PrimExp v
x, PrimExp v
y)

  TPrimExp PrimExp v
x rem :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
`rem` TPrimExp PrimExp v
y
    | Just PrimExp v
z <- [Maybe (PrimExp v)] -> Maybe (PrimExp v)
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum [(IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
forall v.
(IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asIntOp (IntType -> Safety -> BinOp
`SRem` Safety
Unsafe) PrimExp v
x PrimExp v
y] =
      PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp t v) -> PrimExp v -> TPrimExp t v
forall a b. (a -> b) -> a -> b
$ PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp PrimExp v
z
    | Bool
otherwise = String -> (PrimExp v, PrimExp v) -> TPrimExp t v
forall a b. Pretty a => String -> a -> b
numBad String
"rem" (PrimExp v
x, PrimExp v
y)

  TPrimExp PrimExp v
x divUp :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
`divUp` TPrimExp PrimExp v
y
    | Just PrimExp v
z <- [Maybe (PrimExp v)] -> Maybe (PrimExp v)
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum [(IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
forall v.
(IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asIntOp (IntType -> Safety -> BinOp
`SDivUp` Safety
Unsafe) PrimExp v
x PrimExp v
y] =
      PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp t v) -> PrimExp v -> TPrimExp t v
forall a b. (a -> b) -> a -> b
$ PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp PrimExp v
z
    | Bool
otherwise = String -> (PrimExp v, PrimExp v) -> TPrimExp t v
forall a b. Pretty a => String -> a -> b
numBad String
"divRoundingUp" (PrimExp v
x, PrimExp v
y)

  sgn :: TPrimExp t v -> Maybe Int
sgn (TPrimExp (ValueExp (IntValue IntValue
i))) = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
signum (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ IntValue -> Int
forall int. Integral int => IntValue -> int
valueIntegral IntValue
i
  sgn TPrimExp t v
_ = Maybe Int
forall a. Maybe a
Nothing

-- | Lifted logical conjunction.
(.&&.) :: TPrimExp Bool v -> TPrimExp Bool v -> TPrimExp Bool v
TPrimExp PrimExp v
x .&&. :: TPrimExp Bool v -> TPrimExp Bool v -> TPrimExp Bool v
.&&. TPrimExp PrimExp v
y = PrimExp v -> TPrimExp Bool v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Bool v) -> PrimExp v -> TPrimExp Bool v
forall a b. (a -> b) -> a -> b
$ PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp (PrimExp v -> PrimExp v) -> PrimExp v -> PrimExp v
forall a b. (a -> b) -> a -> b
$ BinOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp BinOp
LogAnd PrimExp v
x PrimExp v
y

-- | Lifted logical conjunction.
(.||.) :: TPrimExp Bool v -> TPrimExp Bool v -> TPrimExp Bool v
TPrimExp PrimExp v
x .||. :: TPrimExp Bool v -> TPrimExp Bool v -> TPrimExp Bool v
.||. TPrimExp PrimExp v
y = PrimExp v -> TPrimExp Bool v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Bool v) -> PrimExp v -> TPrimExp Bool v
forall a b. (a -> b) -> a -> b
$ PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp (PrimExp v -> PrimExp v) -> PrimExp v -> PrimExp v
forall a b. (a -> b) -> a -> b
$ BinOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp BinOp
LogOr PrimExp v
x PrimExp v
y

-- | Lifted relational operators; assuming signed numbers in case of
-- integers.
(.<.), (.>.), (.<=.), (.>=.), (.==.) :: TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
TPrimExp PrimExp v
x .<. :: TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.<. TPrimExp PrimExp v
y =
  PrimExp v -> TPrimExp Bool v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Bool v) -> PrimExp v -> TPrimExp Bool v
forall a b. (a -> b) -> a -> b
$
    PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp (PrimExp v -> PrimExp v) -> PrimExp v -> PrimExp v
forall a b. (a -> b) -> a -> b
$
      CmpOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. CmpOp -> PrimExp v -> PrimExp v -> PrimExp v
CmpOpExp CmpOp
cmp PrimExp v
x PrimExp v
y
  where
    cmp :: CmpOp
cmp = case PrimExp v -> PrimType
forall v. PrimExp v -> PrimType
primExpType PrimExp v
x of
      IntType IntType
t -> IntType -> CmpOp
CmpSlt IntType
t
      FloatType FloatType
t -> FloatType -> CmpOp
FCmpLt FloatType
t
      PrimType
_ -> CmpOp
CmpLlt
TPrimExp PrimExp v
x .<=. :: TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.<=. TPrimExp PrimExp v
y =
  PrimExp v -> TPrimExp Bool v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Bool v) -> PrimExp v -> TPrimExp Bool v
forall a b. (a -> b) -> a -> b
$
    PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp (PrimExp v -> PrimExp v) -> PrimExp v -> PrimExp v
forall a b. (a -> b) -> a -> b
$
      CmpOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. CmpOp -> PrimExp v -> PrimExp v -> PrimExp v
CmpOpExp CmpOp
cmp PrimExp v
x PrimExp v
y
  where
    cmp :: CmpOp
cmp = case PrimExp v -> PrimType
forall v. PrimExp v -> PrimType
primExpType PrimExp v
x of
      IntType IntType
t -> IntType -> CmpOp
CmpSle IntType
t
      FloatType FloatType
t -> FloatType -> CmpOp
FCmpLe FloatType
t
      PrimType
_ -> CmpOp
CmpLle
TPrimExp PrimExp v
x .==. :: TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.==. TPrimExp PrimExp v
y =
  PrimExp v -> TPrimExp Bool v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Bool v) -> PrimExp v -> TPrimExp Bool v
forall a b. (a -> b) -> a -> b
$
    PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp (PrimExp v -> PrimExp v) -> PrimExp v -> PrimExp v
forall a b. (a -> b) -> a -> b
$
      CmpOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. CmpOp -> PrimExp v -> PrimExp v -> PrimExp v
CmpOpExp (PrimType -> CmpOp
CmpEq (PrimType -> CmpOp) -> PrimType -> CmpOp
forall a b. (a -> b) -> a -> b
$ PrimExp v -> PrimType
forall v. PrimExp v -> PrimType
primExpType PrimExp v
x PrimType -> PrimType -> PrimType
forall a. Ord a => a -> a -> a
`min` PrimExp v -> PrimType
forall v. PrimExp v -> PrimType
primExpType PrimExp v
y) PrimExp v
x PrimExp v
y
TPrimExp t v
x .>. :: TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.>. TPrimExp t v
y = TPrimExp t v
y TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.<. TPrimExp t v
x
TPrimExp t v
x .>=. :: TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.>=. TPrimExp t v
y = TPrimExp t v
y TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.<=. TPrimExp t v
x

-- | Lifted bitwise operators.
(.&.), (.|.), (.^.) :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
TPrimExp PrimExp v
x .&. :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
.&. TPrimExp PrimExp v
y =
  PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp t v) -> PrimExp v -> TPrimExp t v
forall a b. (a -> b) -> a -> b
$
    PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp (PrimExp v -> PrimExp v) -> PrimExp v -> PrimExp v
forall a b. (a -> b) -> a -> b
$
      BinOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp (IntType -> BinOp
And (IntType -> BinOp) -> IntType -> BinOp
forall a b. (a -> b) -> a -> b
$ PrimExp v -> IntType
forall v. PrimExp v -> IntType
primExpIntType PrimExp v
x) PrimExp v
x PrimExp v
y
TPrimExp PrimExp v
x .|. :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
.|. TPrimExp PrimExp v
y =
  PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp t v) -> PrimExp v -> TPrimExp t v
forall a b. (a -> b) -> a -> b
$
    PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp (PrimExp v -> PrimExp v) -> PrimExp v -> PrimExp v
forall a b. (a -> b) -> a -> b
$
      BinOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp (IntType -> BinOp
Or (IntType -> BinOp) -> IntType -> BinOp
forall a b. (a -> b) -> a -> b
$ PrimExp v -> IntType
forall v. PrimExp v -> IntType
primExpIntType PrimExp v
x) PrimExp v
x PrimExp v
y
TPrimExp PrimExp v
x .^. :: TPrimExp t v -> TPrimExp t v -> TPrimExp t v
.^. TPrimExp PrimExp v
y =
  PrimExp v -> TPrimExp t v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp t v) -> PrimExp v -> TPrimExp t v
forall a b. (a -> b) -> a -> b
$
    PrimExp v -> PrimExp v
forall v. PrimExp v -> PrimExp v
constFoldPrimExp (PrimExp v -> PrimExp v) -> PrimExp v -> PrimExp v
forall a b. (a -> b) -> a -> b
$
      BinOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp (IntType -> BinOp
Xor (IntType -> BinOp) -> IntType -> BinOp
forall a b. (a -> b) -> a -> b
$ PrimExp v -> IntType
forall v. PrimExp v -> IntType
primExpIntType PrimExp v
x) PrimExp v
x PrimExp v
y

infix 4 .==., .<., .>., .<=., .>=.

infixr 3 .&&.

infixr 2 .||.

-- | Untyped smart constructor for sign extension that does a bit of
-- constant folding.
sExt :: IntType -> PrimExp v -> PrimExp v
sExt :: IntType -> PrimExp v -> PrimExp v
sExt IntType
it (ValueExp (IntValue IntValue
v)) = PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v) -> PrimValue -> PrimExp v
forall a b. (a -> b) -> a -> b
$ IntValue -> PrimValue
IntValue (IntValue -> PrimValue) -> IntValue -> PrimValue
forall a b. (a -> b) -> a -> b
$ IntValue -> IntType -> IntValue
doSExt IntValue
v IntType
it
sExt IntType
it PrimExp v
e
  | PrimExp v -> IntType
forall v. PrimExp v -> IntType
primExpIntType PrimExp v
e IntType -> IntType -> Bool
forall a. Eq a => a -> a -> Bool
== IntType
it = PrimExp v
e
  | Bool
otherwise = ConvOp -> PrimExp v -> PrimExp v
forall v. ConvOp -> PrimExp v -> PrimExp v
ConvOpExp (IntType -> IntType -> ConvOp
SExt (PrimExp v -> IntType
forall v. PrimExp v -> IntType
primExpIntType PrimExp v
e) IntType
it) PrimExp v
e

-- | Untyped smart constructor for zero extension that does a bit of
-- constant folding.
zExt :: IntType -> PrimExp v -> PrimExp v
zExt :: IntType -> PrimExp v -> PrimExp v
zExt IntType
it (ValueExp (IntValue IntValue
v)) = PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v) -> PrimValue -> PrimExp v
forall a b. (a -> b) -> a -> b
$ IntValue -> PrimValue
IntValue (IntValue -> PrimValue) -> IntValue -> PrimValue
forall a b. (a -> b) -> a -> b
$ IntValue -> IntType -> IntValue
doZExt IntValue
v IntType
it
zExt IntType
it PrimExp v
e
  | PrimExp v -> IntType
forall v. PrimExp v -> IntType
primExpIntType PrimExp v
e IntType -> IntType -> Bool
forall a. Eq a => a -> a -> Bool
== IntType
it = PrimExp v
e
  | Bool
otherwise = ConvOp -> PrimExp v -> PrimExp v
forall v. ConvOp -> PrimExp v -> PrimExp v
ConvOpExp (IntType -> IntType -> ConvOp
ZExt (PrimExp v -> IntType
forall v. PrimExp v -> IntType
primExpIntType PrimExp v
e) IntType
it) PrimExp v
e

asIntOp :: (IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asIntOp :: (IntType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asIntOp IntType -> BinOp
f PrimExp v
x PrimExp v
y
  | IntType IntType
x_t <- PrimExp v -> PrimType
forall v. PrimExp v -> PrimType
primExpType PrimExp v
x = PrimExp v -> Maybe (PrimExp v)
forall a. a -> Maybe a
Just (PrimExp v -> Maybe (PrimExp v)) -> PrimExp v -> Maybe (PrimExp v)
forall a b. (a -> b) -> a -> b
$ BinOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp (IntType -> BinOp
f IntType
x_t) PrimExp v
x PrimExp v
y
  | Bool
otherwise = Maybe (PrimExp v)
forall a. Maybe a
Nothing

asFloatOp :: (FloatType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asFloatOp :: (FloatType -> BinOp) -> PrimExp v -> PrimExp v -> Maybe (PrimExp v)
asFloatOp FloatType -> BinOp
f PrimExp v
x PrimExp v
y
  | FloatType FloatType
t <- PrimExp v -> PrimType
forall v. PrimExp v -> PrimType
primExpType PrimExp v
x = PrimExp v -> Maybe (PrimExp v)
forall a. a -> Maybe a
Just (PrimExp v -> Maybe (PrimExp v)) -> PrimExp v -> Maybe (PrimExp v)
forall a b. (a -> b) -> a -> b
$ BinOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp (FloatType -> BinOp
f FloatType
t) PrimExp v
x PrimExp v
y
  | Bool
otherwise = Maybe (PrimExp v)
forall a. Maybe a
Nothing

numBad :: Pretty a => String -> a -> b
numBad :: String -> a -> b
numBad String
s a
x =
  String -> b
forall a. HasCallStack => String -> a
error (String -> b) -> String -> b
forall a b. (a -> b) -> a -> b
$ String
"Invalid argument to PrimExp method " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Pretty a => a -> String
pretty a
x

-- | Evaluate a 'PrimExp' in the given monad.  Invokes 'fail' on type
-- errors.
evalPrimExp :: (Pretty v, MonadFail m) => (v -> m PrimValue) -> PrimExp v -> m PrimValue
evalPrimExp :: (v -> m PrimValue) -> PrimExp v -> m PrimValue
evalPrimExp v -> m PrimValue
f (LeafExp v
v PrimType
_) = v -> m PrimValue
f v
v
evalPrimExp v -> m PrimValue
_ (ValueExp PrimValue
v) = PrimValue -> m PrimValue
forall (m :: * -> *) a. Monad m => a -> m a
return PrimValue
v
evalPrimExp v -> m PrimValue
f (BinOpExp BinOp
op PrimExp v
x PrimExp v
y) = do
  PrimValue
x' <- (v -> m PrimValue) -> PrimExp v -> m PrimValue
forall v (m :: * -> *).
(Pretty v, MonadFail m) =>
(v -> m PrimValue) -> PrimExp v -> m PrimValue
evalPrimExp v -> m PrimValue
f PrimExp v
x
  PrimValue
y' <- (v -> m PrimValue) -> PrimExp v -> m PrimValue
forall v (m :: * -> *).
(Pretty v, MonadFail m) =>
(v -> m PrimValue) -> PrimExp v -> m PrimValue
evalPrimExp v -> m PrimValue
f PrimExp v
y
  m PrimValue
-> (PrimValue -> m PrimValue) -> Maybe PrimValue -> m PrimValue
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (BinOp -> (PrimExp v, PrimExp v) -> m PrimValue
forall a b (m :: * -> *) c.
(Pretty a, Pretty b, MonadFail m) =>
a -> b -> m c
evalBad BinOp
op (PrimExp v
x, PrimExp v
y)) PrimValue -> m PrimValue
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe PrimValue -> m PrimValue) -> Maybe PrimValue -> m PrimValue
forall a b. (a -> b) -> a -> b
$ BinOp -> PrimValue -> PrimValue -> Maybe PrimValue
doBinOp BinOp
op PrimValue
x' PrimValue
y'
evalPrimExp v -> m PrimValue
f (CmpOpExp CmpOp
op PrimExp v
x PrimExp v
y) = do
  PrimValue
x' <- (v -> m PrimValue) -> PrimExp v -> m PrimValue
forall v (m :: * -> *).
(Pretty v, MonadFail m) =>
(v -> m PrimValue) -> PrimExp v -> m PrimValue
evalPrimExp v -> m PrimValue
f PrimExp v
x
  PrimValue
y' <- (v -> m PrimValue) -> PrimExp v -> m PrimValue
forall v (m :: * -> *).
(Pretty v, MonadFail m) =>
(v -> m PrimValue) -> PrimExp v -> m PrimValue
evalPrimExp v -> m PrimValue
f PrimExp v
y
  m PrimValue -> (Bool -> m PrimValue) -> Maybe Bool -> m PrimValue
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (CmpOp -> (PrimExp v, PrimExp v) -> m PrimValue
forall a b (m :: * -> *) c.
(Pretty a, Pretty b, MonadFail m) =>
a -> b -> m c
evalBad CmpOp
op (PrimExp v
x, PrimExp v
y)) (PrimValue -> m PrimValue
forall (m :: * -> *) a. Monad m => a -> m a
return (PrimValue -> m PrimValue)
-> (Bool -> PrimValue) -> Bool -> m PrimValue
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Bool -> PrimValue
BoolValue) (Maybe Bool -> m PrimValue) -> Maybe Bool -> m PrimValue
forall a b. (a -> b) -> a -> b
$ CmpOp -> PrimValue -> PrimValue -> Maybe Bool
doCmpOp CmpOp
op PrimValue
x' PrimValue
y'
evalPrimExp v -> m PrimValue
f (UnOpExp UnOp
op PrimExp v
x) = do
  PrimValue
x' <- (v -> m PrimValue) -> PrimExp v -> m PrimValue
forall v (m :: * -> *).
(Pretty v, MonadFail m) =>
(v -> m PrimValue) -> PrimExp v -> m PrimValue
evalPrimExp v -> m PrimValue
f PrimExp v
x
  m PrimValue
-> (PrimValue -> m PrimValue) -> Maybe PrimValue -> m PrimValue
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (UnOp -> PrimExp v -> m PrimValue
forall a b (m :: * -> *) c.
(Pretty a, Pretty b, MonadFail m) =>
a -> b -> m c
evalBad UnOp
op PrimExp v
x) PrimValue -> m PrimValue
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe PrimValue -> m PrimValue) -> Maybe PrimValue -> m PrimValue
forall a b. (a -> b) -> a -> b
$ UnOp -> PrimValue -> Maybe PrimValue
doUnOp UnOp
op PrimValue
x'
evalPrimExp v -> m PrimValue
f (ConvOpExp ConvOp
op PrimExp v
x) = do
  PrimValue
x' <- (v -> m PrimValue) -> PrimExp v -> m PrimValue
forall v (m :: * -> *).
(Pretty v, MonadFail m) =>
(v -> m PrimValue) -> PrimExp v -> m PrimValue
evalPrimExp v -> m PrimValue
f PrimExp v
x
  m PrimValue
-> (PrimValue -> m PrimValue) -> Maybe PrimValue -> m PrimValue
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ConvOp -> PrimExp v -> m PrimValue
forall a b (m :: * -> *) c.
(Pretty a, Pretty b, MonadFail m) =>
a -> b -> m c
evalBad ConvOp
op PrimExp v
x) PrimValue -> m PrimValue
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe PrimValue -> m PrimValue) -> Maybe PrimValue -> m PrimValue
forall a b. (a -> b) -> a -> b
$ ConvOp -> PrimValue -> Maybe PrimValue
doConvOp ConvOp
op PrimValue
x'
evalPrimExp v -> m PrimValue
f (FunExp String
h [PrimExp v]
args PrimType
_) = do
  [PrimValue]
args' <- (PrimExp v -> m PrimValue) -> [PrimExp v] -> m [PrimValue]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ((v -> m PrimValue) -> PrimExp v -> m PrimValue
forall v (m :: * -> *).
(Pretty v, MonadFail m) =>
(v -> m PrimValue) -> PrimExp v -> m PrimValue
evalPrimExp v -> m PrimValue
f) [PrimExp v]
args
  m PrimValue
-> (PrimValue -> m PrimValue) -> Maybe PrimValue -> m PrimValue
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> [PrimExp v] -> m PrimValue
forall a b (m :: * -> *) c.
(Pretty a, Pretty b, MonadFail m) =>
a -> b -> m c
evalBad String
h [PrimExp v]
args) PrimValue -> m PrimValue
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe PrimValue -> m PrimValue) -> Maybe PrimValue -> m PrimValue
forall a b. (a -> b) -> a -> b
$ do
    ([PrimType]
_, PrimType
_, [PrimValue] -> Maybe PrimValue
fun) <- String
-> Map
     String ([PrimType], PrimType, [PrimValue] -> Maybe PrimValue)
-> Maybe ([PrimType], PrimType, [PrimValue] -> Maybe PrimValue)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup String
h Map String ([PrimType], PrimType, [PrimValue] -> Maybe PrimValue)
primFuns
    [PrimValue] -> Maybe PrimValue
fun [PrimValue]
args'

evalBad :: (Pretty a, Pretty b, MonadFail m) => a -> b -> m c
evalBad :: a -> b -> m c
evalBad a
op b
arg =
  String -> m c
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m c) -> String -> m c
forall a b. (a -> b) -> a -> b
$
    String
"evalPrimExp: Type error when applying "
      String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Pretty a => a -> String
pretty a
op
      String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" to "
      String -> ShowS
forall a. [a] -> [a] -> [a]
++ b -> String
forall a. Pretty a => a -> String
pretty b
arg

-- | The type of values returned by a 'PrimExp'.  This function
-- returning does not imply that the 'PrimExp' is type-correct.
primExpType :: PrimExp v -> PrimType
primExpType :: PrimExp v -> PrimType
primExpType (LeafExp v
_ PrimType
t) = PrimType
t
primExpType (ValueExp PrimValue
v) = PrimValue -> PrimType
primValueType PrimValue
v
primExpType (BinOpExp BinOp
op PrimExp v
_ PrimExp v
_) = BinOp -> PrimType
binOpType BinOp
op
primExpType CmpOpExp {} = PrimType
Bool
primExpType (UnOpExp UnOp
op PrimExp v
_) = UnOp -> PrimType
unOpType UnOp
op
primExpType (ConvOpExp ConvOp
op PrimExp v
_) = (PrimType, PrimType) -> PrimType
forall a b. (a, b) -> b
snd ((PrimType, PrimType) -> PrimType)
-> (PrimType, PrimType) -> PrimType
forall a b. (a -> b) -> a -> b
$ ConvOp -> (PrimType, PrimType)
convOpType ConvOp
op
primExpType (FunExp String
_ [PrimExp v]
_ PrimType
t) = PrimType
t

-- | Is the expression a constant zero of some sort?
zeroIshExp :: PrimExp v -> Bool
zeroIshExp :: PrimExp v -> Bool
zeroIshExp (ValueExp PrimValue
v) = PrimValue -> Bool
zeroIsh PrimValue
v
zeroIshExp PrimExp v
_ = Bool
False

-- | Is the expression a constant one of some sort?
oneIshExp :: PrimExp v -> Bool
oneIshExp :: PrimExp v -> Bool
oneIshExp (ValueExp PrimValue
v) = PrimValue -> Bool
oneIsh PrimValue
v
oneIshExp PrimExp v
_ = Bool
False

-- | If the given 'PrimExp' is a constant of the wrong integer type,
-- coerce it to the given integer type.  This is a workaround for an
-- issue in the 'Num' instance.
coerceIntPrimExp :: IntType -> PrimExp v -> PrimExp v
coerceIntPrimExp :: IntType -> PrimExp v -> PrimExp v
coerceIntPrimExp IntType
t (ValueExp (IntValue IntValue
v)) = PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v) -> PrimValue -> PrimExp v
forall a b. (a -> b) -> a -> b
$ IntValue -> PrimValue
IntValue (IntValue -> PrimValue) -> IntValue -> PrimValue
forall a b. (a -> b) -> a -> b
$ IntValue -> IntType -> IntValue
doSExt IntValue
v IntType
t
coerceIntPrimExp IntType
_ PrimExp v
e = PrimExp v
e

primExpIntType :: PrimExp v -> IntType
primExpIntType :: PrimExp v -> IntType
primExpIntType PrimExp v
e = case PrimExp v -> PrimType
forall v. PrimExp v -> PrimType
primExpType PrimExp v
e of
  IntType IntType
t -> IntType
t
  PrimType
_ -> IntType
Int64

-- | Boolean-valued PrimExps.
true, false :: TPrimExp Bool v
true :: TPrimExp Bool v
true = PrimExp v -> TPrimExp Bool v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Bool v) -> PrimExp v -> TPrimExp Bool v
forall a b. (a -> b) -> a -> b
$ PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v) -> PrimValue -> PrimExp v
forall a b. (a -> b) -> a -> b
$ Bool -> PrimValue
BoolValue Bool
True
false :: TPrimExp Bool v
false = PrimExp v -> TPrimExp Bool v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Bool v) -> PrimExp v -> TPrimExp Bool v
forall a b. (a -> b) -> a -> b
$ PrimValue -> PrimExp v
forall v. PrimValue -> PrimExp v
ValueExp (PrimValue -> PrimExp v) -> PrimValue -> PrimExp v
forall a b. (a -> b) -> a -> b
$ Bool -> PrimValue
BoolValue Bool
False

-- | Boolean negation smart constructor.
bNot :: TPrimExp Bool v -> TPrimExp Bool v
bNot :: TPrimExp Bool v -> TPrimExp Bool v
bNot = PrimExp v -> TPrimExp Bool v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Bool v)
-> (TPrimExp Bool v -> PrimExp v)
-> TPrimExp Bool v
-> TPrimExp Bool v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. UnOp -> PrimExp v -> PrimExp v
forall v. UnOp -> PrimExp v -> PrimExp v
UnOpExp UnOp
Not (PrimExp v -> PrimExp v)
-> (TPrimExp Bool v -> PrimExp v) -> TPrimExp Bool v -> PrimExp v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TPrimExp Bool v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped

-- | SMax on 32-bit integers.
sMax32 :: TPrimExp Int32 v -> TPrimExp Int32 v -> TPrimExp Int32 v
sMax32 :: TPrimExp Int32 v -> TPrimExp Int32 v -> TPrimExp Int32 v
sMax32 TPrimExp Int32 v
x TPrimExp Int32 v
y = PrimExp v -> TPrimExp Int32 v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Int32 v) -> PrimExp v -> TPrimExp Int32 v
forall a b. (a -> b) -> a -> b
$ BinOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp (IntType -> BinOp
SMax IntType
Int32) (TPrimExp Int32 v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped TPrimExp Int32 v
x) (TPrimExp Int32 v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped TPrimExp Int32 v
y)

-- | SMin on 32-bit integers.
sMin32 :: TPrimExp Int32 v -> TPrimExp Int32 v -> TPrimExp Int32 v
sMin32 :: TPrimExp Int32 v -> TPrimExp Int32 v -> TPrimExp Int32 v
sMin32 TPrimExp Int32 v
x TPrimExp Int32 v
y = PrimExp v -> TPrimExp Int32 v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Int32 v) -> PrimExp v -> TPrimExp Int32 v
forall a b. (a -> b) -> a -> b
$ BinOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp (IntType -> BinOp
SMin IntType
Int32) (TPrimExp Int32 v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped TPrimExp Int32 v
x) (TPrimExp Int32 v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped TPrimExp Int32 v
y)

-- | SMax on 64-bit integers.
sMax64 :: TPrimExp Int64 v -> TPrimExp Int64 v -> TPrimExp Int64 v
sMax64 :: TPrimExp Int64 v -> TPrimExp Int64 v -> TPrimExp Int64 v
sMax64 TPrimExp Int64 v
x TPrimExp Int64 v
y = PrimExp v -> TPrimExp Int64 v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Int64 v) -> PrimExp v -> TPrimExp Int64 v
forall a b. (a -> b) -> a -> b
$ BinOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp (IntType -> BinOp
SMax IntType
Int64) (TPrimExp Int64 v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped TPrimExp Int64 v
x) (TPrimExp Int64 v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped TPrimExp Int64 v
y)

-- | SMin on 64-bit integers.
sMin64 :: TPrimExp Int64 v -> TPrimExp Int64 v -> TPrimExp Int64 v
sMin64 :: TPrimExp Int64 v -> TPrimExp Int64 v -> TPrimExp Int64 v
sMin64 TPrimExp Int64 v
x TPrimExp Int64 v
y = PrimExp v -> TPrimExp Int64 v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Int64 v) -> PrimExp v -> TPrimExp Int64 v
forall a b. (a -> b) -> a -> b
$ BinOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp (IntType -> BinOp
SMin IntType
Int64) (TPrimExp Int64 v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped TPrimExp Int64 v
x) (TPrimExp Int64 v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped TPrimExp Int64 v
y)

-- | Sign-extend to 32 bit integer.
sExt32 :: IntExp t => TPrimExp t v -> TPrimExp Int32 v
sExt32 :: TPrimExp t v -> TPrimExp Int32 v
sExt32 = PrimExp v -> TPrimExp Int32 v
forall v. PrimExp v -> TPrimExp Int32 v
isInt32 (PrimExp v -> TPrimExp Int32 v)
-> (TPrimExp t v -> PrimExp v) -> TPrimExp t v -> TPrimExp Int32 v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IntType -> PrimExp v -> PrimExp v
forall v. IntType -> PrimExp v -> PrimExp v
sExt IntType
Int32 (PrimExp v -> PrimExp v)
-> (TPrimExp t v -> PrimExp v) -> TPrimExp t v -> PrimExp v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TPrimExp t v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped

-- | Sign-extend to 64 bit integer.
sExt64 :: IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 :: TPrimExp t v -> TPrimExp Int64 v
sExt64 = PrimExp v -> TPrimExp Int64 v
forall v. PrimExp v -> TPrimExp Int64 v
isInt64 (PrimExp v -> TPrimExp Int64 v)
-> (TPrimExp t v -> PrimExp v) -> TPrimExp t v -> TPrimExp Int64 v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IntType -> PrimExp v -> PrimExp v
forall v. IntType -> PrimExp v -> PrimExp v
sExt IntType
Int64 (PrimExp v -> PrimExp v)
-> (TPrimExp t v -> PrimExp v) -> TPrimExp t v -> PrimExp v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TPrimExp t v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped

-- | Zero-extend to 32 bit integer.
zExt32 :: IntExp t => TPrimExp t v -> TPrimExp Int32 v
zExt32 :: TPrimExp t v -> TPrimExp Int32 v
zExt32 = PrimExp v -> TPrimExp Int32 v
forall v. PrimExp v -> TPrimExp Int32 v
isInt32 (PrimExp v -> TPrimExp Int32 v)
-> (TPrimExp t v -> PrimExp v) -> TPrimExp t v -> TPrimExp Int32 v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IntType -> PrimExp v -> PrimExp v
forall v. IntType -> PrimExp v -> PrimExp v
zExt IntType
Int32 (PrimExp v -> PrimExp v)
-> (TPrimExp t v -> PrimExp v) -> TPrimExp t v -> PrimExp v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TPrimExp t v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped

-- | Zero-extend to 64 bit integer.
zExt64 :: IntExp t => TPrimExp t v -> TPrimExp Int64 v
zExt64 :: TPrimExp t v -> TPrimExp Int64 v
zExt64 = PrimExp v -> TPrimExp Int64 v
forall v. PrimExp v -> TPrimExp Int64 v
isInt64 (PrimExp v -> TPrimExp Int64 v)
-> (TPrimExp t v -> PrimExp v) -> TPrimExp t v -> TPrimExp Int64 v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IntType -> PrimExp v -> PrimExp v
forall v. IntType -> PrimExp v -> PrimExp v
zExt IntType
Int64 (PrimExp v -> PrimExp v)
-> (TPrimExp t v -> PrimExp v) -> TPrimExp t v -> PrimExp v
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TPrimExp t v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped

-- | 64-bit float minimum.
fMin64 :: TPrimExp Double v -> TPrimExp Double v -> TPrimExp Double v
fMin64 :: TPrimExp Double v -> TPrimExp Double v -> TPrimExp Double v
fMin64 TPrimExp Double v
x TPrimExp Double v
y = PrimExp v -> TPrimExp Double v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Double v) -> PrimExp v -> TPrimExp Double v
forall a b. (a -> b) -> a -> b
$ BinOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp (FloatType -> BinOp
FMin FloatType
Float64) (TPrimExp Double v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped TPrimExp Double v
x) (TPrimExp Double v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped TPrimExp Double v
y)

-- | 64-bit float maximum.
fMax64 :: TPrimExp Double v -> TPrimExp Double v -> TPrimExp Double v
fMax64 :: TPrimExp Double v -> TPrimExp Double v -> TPrimExp Double v
fMax64 TPrimExp Double v
x TPrimExp Double v
y = PrimExp v -> TPrimExp Double v
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp v -> TPrimExp Double v) -> PrimExp v -> TPrimExp Double v
forall a b. (a -> b) -> a -> b
$ BinOp -> PrimExp v -> PrimExp v -> PrimExp v
forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp (FloatType -> BinOp
FMax FloatType
Float64) (TPrimExp Double v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped TPrimExp Double v
x) (TPrimExp Double v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped TPrimExp Double v
y)

-- Prettyprinting instances

instance Pretty v => Pretty (PrimExp v) where
  ppr :: PrimExp v -> Doc
ppr (LeafExp v
v PrimType
_) = v -> Doc
forall a. Pretty a => a -> Doc
ppr v
v
  ppr (ValueExp PrimValue
v) = PrimValue -> Doc
forall a. Pretty a => a -> Doc
ppr PrimValue
v
  ppr (BinOpExp BinOp
op PrimExp v
x PrimExp v
y) = BinOp -> Doc
forall a. Pretty a => a -> Doc
ppr BinOp
op Doc -> Doc -> Doc
<+> Doc -> Doc
parens (PrimExp v -> Doc
forall a. Pretty a => a -> Doc
ppr PrimExp v
x) Doc -> Doc -> Doc
<+> Doc -> Doc
parens (PrimExp v -> Doc
forall a. Pretty a => a -> Doc
ppr PrimExp v
y)
  ppr (CmpOpExp CmpOp
op PrimExp v
x PrimExp v
y) = CmpOp -> Doc
forall a. Pretty a => a -> Doc
ppr CmpOp
op Doc -> Doc -> Doc
<+> Doc -> Doc
parens (PrimExp v -> Doc
forall a. Pretty a => a -> Doc
ppr PrimExp v
x) Doc -> Doc -> Doc
<+> Doc -> Doc
parens (PrimExp v -> Doc
forall a. Pretty a => a -> Doc
ppr PrimExp v
y)
  ppr (ConvOpExp ConvOp
op PrimExp v
x) = ConvOp -> Doc
forall a. Pretty a => a -> Doc
ppr ConvOp
op Doc -> Doc -> Doc
<+> Doc -> Doc
parens (PrimExp v -> Doc
forall a. Pretty a => a -> Doc
ppr PrimExp v
x)
  ppr (UnOpExp UnOp
op PrimExp v
x) = UnOp -> Doc
forall a. Pretty a => a -> Doc
ppr UnOp
op Doc -> Doc -> Doc
<+> Doc -> Doc
parens (PrimExp v -> Doc
forall a. Pretty a => a -> Doc
ppr PrimExp v
x)
  ppr (FunExp String
h [PrimExp v]
args PrimType
_) = String -> Doc
text String
h Doc -> Doc -> Doc
<+> Doc -> Doc
parens ([Doc] -> Doc
commasep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (PrimExp v -> Doc) -> [PrimExp v] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PrimExp v -> Doc
forall a. Pretty a => a -> Doc
ppr [PrimExp v]
args)

instance Pretty v => Pretty (TPrimExp t v) where
  ppr :: TPrimExp t v -> Doc
ppr = PrimExp v -> Doc
forall a. Pretty a => a -> Doc
ppr (PrimExp v -> Doc)
-> (TPrimExp t v -> PrimExp v) -> TPrimExp t v -> Doc
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TPrimExp t v -> PrimExp v
forall t v. TPrimExp t v -> PrimExp v
untyped

-- | Produce a mapping from the leaves of the 'PrimExp' to their
-- designated types.
leafExpTypes :: Ord a => PrimExp a -> S.Set (a, PrimType)
leafExpTypes :: PrimExp a -> Set (a, PrimType)
leafExpTypes (LeafExp a
x PrimType
ptp) = (a, PrimType) -> Set (a, PrimType)
forall a. a -> Set a
S.singleton (a
x, PrimType
ptp)
leafExpTypes (ValueExp PrimValue
_) = Set (a, PrimType)
forall a. Set a
S.empty
leafExpTypes (UnOpExp UnOp
_ PrimExp a
e) = PrimExp a -> Set (a, PrimType)
forall a. Ord a => PrimExp a -> Set (a, PrimType)
leafExpTypes PrimExp a
e
leafExpTypes (ConvOpExp ConvOp
_ PrimExp a
e) = PrimExp a -> Set (a, PrimType)
forall a. Ord a => PrimExp a -> Set (a, PrimType)
leafExpTypes PrimExp a
e
leafExpTypes (BinOpExp BinOp
_ PrimExp a
e1 PrimExp a
e2) =
  Set (a, PrimType) -> Set (a, PrimType) -> Set (a, PrimType)
forall a. Ord a => Set a -> Set a -> Set a
S.union (PrimExp a -> Set (a, PrimType)
forall a. Ord a => PrimExp a -> Set (a, PrimType)
leafExpTypes PrimExp a
e1) (PrimExp a -> Set (a, PrimType)
forall a. Ord a => PrimExp a -> Set (a, PrimType)
leafExpTypes PrimExp a
e2)
leafExpTypes (CmpOpExp CmpOp
_ PrimExp a
e1 PrimExp a
e2) =
  Set (a, PrimType) -> Set (a, PrimType) -> Set (a, PrimType)
forall a. Ord a => Set a -> Set a -> Set a
S.union (PrimExp a -> Set (a, PrimType)
forall a. Ord a => PrimExp a -> Set (a, PrimType)
leafExpTypes PrimExp a
e1) (PrimExp a -> Set (a, PrimType)
forall a. Ord a => PrimExp a -> Set (a, PrimType)
leafExpTypes PrimExp a
e2)
leafExpTypes (FunExp String
_ [PrimExp a]
pes PrimType
_) =
  [Set (a, PrimType)] -> Set (a, PrimType)
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
S.unions ([Set (a, PrimType)] -> Set (a, PrimType))
-> [Set (a, PrimType)] -> Set (a, PrimType)
forall a b. (a -> b) -> a -> b
$ (PrimExp a -> Set (a, PrimType))
-> [PrimExp a] -> [Set (a, PrimType)]
forall a b. (a -> b) -> [a] -> [b]
map PrimExp a -> Set (a, PrimType)
forall a. Ord a => PrimExp a -> Set (a, PrimType)
leafExpTypes [PrimExp a]
pes