never executed always true always false
1 {-# LANGUAGE QuasiQuotes #-}
2
3 module Conjure.Rules.Vertical.Function.FunctionND where
4
5 import Conjure.Rules.Import
6
7
8 rule_Image :: Rule
9 rule_Image = "function-image{FunctionND}" `namedRule` theRule where
10 theRule [essence| image(&f,&x) |] = do
11 Function_ND <- representationOf f
12 [values] <- downX1 f
13 toIndex <- downX1 x
14 let valuesIndexed = make opMatrixIndexing values toIndex
15 return
16 ( "Function image, FunctionND representation"
17 , return valuesIndexed
18 )
19 theRule _ = na "rule_Image"
20
21
22 rule_Comprehension :: Rule
23 rule_Comprehension = "function-comprehension{FunctionND}" `namedRule` theRule where
24 theRule (Comprehension body gensOrConds) = do
25 (gocBefore, (pat, func), gocAfter) <- matchFirst gensOrConds $ \ goc -> case goc of
26 Generator (GenInExpr pat@Single{} expr) -> return (pat, matchDefs [opToSet,opToMSet,opToRelation] expr)
27 _ -> na "rule_Comprehension"
28 Function_ND <- representationOf func
29 DomainFunction _ _ indexDomain _ <- domainOf func
30 [values] <- downX1 func
31 let upd val old = lambdaToFunction pat old val
32 return
33 ( "Mapping over a function, FunctionND representation"
34 , do
35 (jPat, j) <- quantifiedVar
36 let kRange = case indexDomain of
37 DomainTuple ts -> map fromInt [1 .. genericLength ts]
38 DomainRecord rs -> map (fromName . fst) rs
39 _ -> bug $ vcat [ "FunctionND.rule_Comprehension"
40 , "indexDomain:" <+> pretty indexDomain
41 ]
42 toIndex = [ [essence| &j[&k] |] | k <- kRange ]
43 valuesIndexed = make opMatrixIndexing values toIndex
44 val = [essence| (&j, &valuesIndexed) |]
45 return $ Comprehension (upd val body)
46 $ gocBefore
47 ++ [ Generator (GenDomainNoRepr jPat (forgetRepr indexDomain)) ]
48 ++ transformBi (upd val) gocAfter
49 )
50 theRule _ = na "rule_Comprehension"
51
52
53 rule_Comprehension_Defined :: Rule
54 rule_Comprehension_Defined = "function-comprehension_defined{FunctionND}" `namedRule` theRule where
55 theRule (Comprehension body gensOrConds) = do
56 (gocBefore, (pat, expr), gocAfter) <- matchFirst gensOrConds $ \ goc -> case goc of
57 Generator (GenInExpr pat@Single{} expr) -> return (pat, expr)
58 _ -> na "rule_Comprehension_Defined"
59 func <- match opDefined expr
60 Function_ND <- representationOf func
61 DomainFunction _ _ indexDomain _ <- domainOf func
62 let upd val old = lambdaToFunction pat old val
63 return
64 ( "Mapping over a function, FunctionND representation"
65 , do
66 (jPat, j) <- quantifiedVar
67 let val = j
68 return $ Comprehension (upd val body)
69 $ gocBefore
70 ++ [ Generator (GenDomainNoRepr jPat (forgetRepr indexDomain)) ]
71 ++ transformBi (upd val) gocAfter
72 )
73 theRule _ = na "rule_Comprehension_Defined"