Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous occurrence ‘product’

Tags:

haskell

Code:

product :: (Eq p, Num p) => p -> p
product 1 = 1
product n = n * product (n-1)

Usage:

main = print (product(8))

Error:

GHCi, version 8.10.6: https://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/runner/University-Labs/.ghci
[1 of 1] Compiling Main             ( Main.hs, interpreted )

Main.hs:3:17: error:
    Ambiguous occurrence ‘product’
    It could refer to
       either ‘Prelude.product’,
              imported from ‘Prelude’ at Main.hs:1:1
              (and originally defined in ‘Data.Foldable’)
           or ‘Main.product’, defined at Main.hs:2:1
  |
3 | product n = n * product (n-1)
  |                 ^^^^^^^

Main.hs:5:15: error:
    Ambiguous occurrence ‘product’
    It could refer to
       either ‘Prelude.product’,
              imported from ‘Prelude’ at Main.hs:1:1
              (and originally defined in ‘Data.Foldable’)
           or ‘Main.product’, defined at Main.hs:2:1
  |
5 | main = print (product(8))
  |               ^^^^^^^
Failed, no modules loaded.
 
<interactive>:1:1: error:
    • Variable not in scope: main
    • Perhaps you meant ‘min’ (imported from Prelude)
 ^
like image 321
Alix Blaine Avatar asked Mar 31 '26 04:03

Alix Blaine


1 Answers

There's a function called product in the Prelude which is imported by default. So you have two functions named product: The one you wrote and the one built-in to Haskell.

You can always change your function's name. The function you've written there is typically called the factorial, so you could change the name to that.

You can also refer to your function by its fully qualified name.

main = print (Main.product 8)

(Note that modules not given an explicit name in Haskell are assumed to be named Main, so it's as though your file started with module Main where)

Alternatively, you can explicitly import the Prelude and hide what you don't want.

import Prelude hiding (product)
like image 118
Silvio Mayolo Avatar answered Apr 02 '26 22:04

Silvio Mayolo