[m-users.] Mode error when working with hash_table

Ted Stock testock at gmail.com
Sun Aug 30 22:51:01 AEST 2020


Hi,

I am new to Mercury and I am trying to implement a trie data structure in Mercury using the hash_table library.  I have run into a problem that I cannot solve.  When I try to create an empty data structure with an empty hash_table using the supplied hash_pred type, the compiler complains that the modes don’t match.  Any help is appreciated.

Mercury Compiler, version 20.01.2, on x86_64-apple-darwin19.4.0
Copyright (C) 1993-2012 The University of Melbourne
Copyright (C) 2013-2020 The Mercury team

trie.m: 
     1	%----------------------------------------------------------------------------%
     2	% vim: ft=mercury ff=unix ts=4 sw=4 et
     3	%----------------------------------------------------------------------------%
     4	% File: trie.m
     5	% Copyright © 2020 William Stock
     6	% Main author: William Stock <testock at gmail.com>
     7	% Created on: Wed Aug 26 21:26:31 CEST 2020
     8	% Stability: low
     9	%----------------------------------------------------------------------------%
    10	% TODO: module documentation
    11	%----------------------------------------------------------------------------%
    12	
    13	:- module trie.
    14	
    15	:- interface.
    16	
    17	:- import_module hash_table.
    18	%:- import_module list.
    19	
    20	%----------------------------------------------------------------------------%
    21	
    22	:- type trie(K, V).
    23	
    24	:- func init(hash_pred(K)) = trie(K, V).
    25	
    26	%----------------------------------------------------------------------------%
    27	%----------------------------------------------------------------------------%
    28	
    29	:- implementation.
    30	
    31	:- import_module maybe.
    32	%----------------------------------------------------------------------------%
    33	
    34	:- type trie(K, V)
    35	   ---> trie_(
    36	                hash    :: hash_table.hash_pred(K),
    37	                value   :: maybe(V),
    38	                node    :: hash_table(K, trie(K,V))
    39	        ).
    40	
    41	init(HashPred) = trie_(HashPred, no, hash_table.init_default(HashPred)).
    42	
    43	%----------------------------------------------------------------------------%
    44	:- end_module trie.
    45	%----------------------------------------------------------------------------%


trie.err:
trie.m:041: In clause for `init(in) = out':
trie.m:041:   mode error in conjunction. The next 2 error messages indicate
trie.m:041:   possible causes of this error.
trie.m:041:
trie.m:041:   In clause for `init(in) = out':
trie.m:041:   in argument 1 of call to function `hash_table.init_default'/1:
trie.m:041:   mode error: variable `HashPred' has instantiatedness `ground',
trie.m:041:   expected instantiatedness was
trie.m:041:     named inst hash_table.hash_pred,
trie.m:041:     which expands to
trie.m:041:       (pred(in, out) is det).
trie.m:041:
trie.m:041:   In clause for `init(in) = out':
trie.m:041:   in function result term of clause head:
trie.m:041:   mode error in unification of `HeadVar__2' and
trie.m:041:   `trie.trie_(HashPred, V_4, V_5)'.
trie.m:041:   Variable `HeadVar__2' has instantiatedness `free',
trie.m:041:   term `trie.trie_(HashPred, V_4, V_5)' has instantiatedness
trie.m:041:     named inst trie.trie_(
trie.m:041:       ground,
trie.m:041:       unique(
trie.m:041:         no
trie.m:041:       ),
trie.m:041:       free
trie.m:041:     ).

Second question,  how does hash_table.init_default know what type to make the hash_table?  default_init(K) takes a type K, but what about type V?  How does the compiler know what type to assign to V?

Third question (unrelated): I think that it would be helpful if there were an advanced online course for Mercury on one of the big MOOC sites like Udemy or Coursera.  It is quite an undertaking, but my understanding is that many of the project staff are former academics, so it would probably be like recording a semester of lectures.

Best,
Ted


More information about the users mailing list