egison-5.0.0: Programming language with non-linear pattern-matching against non-free data
LicenseMIT
Safe HaskellNone
LanguageGHC2021

Language.Egison.Type.TensorMapInsertion

Description

This module implements automatic tensorMap insertion for Phase 8 of the Egison compiler. This is the first step of TypedDesugar, before type class expansion. When a function expects a scalar type (e.g., Integer) but receives a Tensor type, this module automatically inserts tensorMap to apply the function element-wise.

Two insertion modes: 1. Direct application: When argument is Tensor and parameter expects scalar, wrap the application with tensorMap. 2. Higher-order functions (simplified approach): When a binary function with constrained/scalar parameter types is passed as an argument, always wrap it with tensorMap2. This handles cases like `foldl1 (+) xs` where elements of xs might be Tensors at runtime.

According to tensor-map-insertion-simple.md: - When a binary function with scalar parameter types is passed as an argument, always wrap it with tensorMap2 - tensorMap/tensorMap2 act as identity for scalar values, so wrapping is safe regardless of whether the actual argument is a tensor or scalar

Example: def f (x : Integer) : Integer := x def t1 := [| 1, 2 |] f t1 --=> tensorMap (t1e -> f t1e) t1

def sum {Num a} (xs: [a]) : a := foldl1 (+) xs --=> def sum {Num a} (xs: [a]) : a := foldl1 (tensorMap2 (+)) xs

Synopsis

Documentation

insertTensorMaps :: TIExpr -> EvalM TIExpr Source #

Insert tensorMap expressions where needed in a TIExpr This is the main entry point for tensorMap insertion