Interface Idris2:有没有在接口实现中使用隐式的方法
我正在使用Idris2和Idris跟踪TDD。在第6章中,我正在使用schema处理数据存储。首先,对于某些上下文:Interface Idris2:有没有在接口实现中使用隐式的方法,interface,idris,implicit-parameters,Interface,Idris,Implicit Parameters,我正在使用Idris2和Idris跟踪TDD。在第6章中,我正在使用schema处理数据存储。首先,对于某些上下文: infixr 5 .+. data Schema = SString | SInt | (.+.) Schema Schema SchemaType : Schema -> Type SchemaType SString = String SchemaType SInt = Int SchemaType (x .+. y)
infixr 5 .+.
data Schema = SString
| SInt
| (.+.) Schema Schema
SchemaType : Schema -> Type
SchemaType SString = String
SchemaType SInt = Int
SchemaType (x .+. y) = (SchemaType x, SchemaType y)
在某些时候,我们希望格式化类型为SchemaType schema
的值以显示给用户。在这本书中,这个问题通过如下编写的显示函数得到解决:
display : SchemaType schema -> String
display {schema = SString} item = show item
display {schema = SInt} item = show item
display {schema = (x .+. y)} (iteml, itemr) = display iteml ++ ", " ++ display itemr
我想知道是否有可能使用Show
界面来实现这一点,这样我就可以调用Show item
我尝试了以下方法:
Show (SchemaType schema) where
show {schema = SString} item = show item
show {schema = SInt} item = show item
show {schema = (x .+. y)} (x, y) = "(" ++ show x ++ ", " ++ show y ++ ")"
但它告诉我模式将被删除,因此无法使用
我曾试图让idris在运行时保存它,但我只是猜测语法和错误,我真的不知道如何解释
尝试1:
抛出:
Error: While processing left hand side of show. Can't match on ?postpone [no locals in scope] (Non linear pattern variable).
/home/stefan/dev/tdd-idris/SchemaDataStore.idr:27:33--27:34
23 |
24 | {schema:_} -> Show (SchemaType schema) where
25 | show {schema = SString} item = show item
26 | show {schema = SInt} item = show item
27 | show {schema = (x .+. y)} (x, y) = "(" ++ show x ++ ", " ++ show y ++ ")"
^
Error: While processing left hand side of show. schema is not a valid argument in show ?item.
/home/stefan/dev/tdd-idris/SchemaDataStore.idr:25:3--25:31
24 | Show ({schema:_} -> SchemaType schema) where
25 | show {schema = SString} item = show item
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: While processing right hand side of show. Sorry, I can't find any elaboration which works. If Builtin.Pair: schema is not accessible in this context.
/home/stefan/dev/tdd-idris/SchemaDataStore.idr:26:11--26:17
24 | Show (SchemaType schema) where
25 | show item =
26 | case (schema, item) of
^^^^^^
尝试2:
抛出:
Error: While processing left hand side of show. Can't match on ?postpone [no locals in scope] (Non linear pattern variable).
/home/stefan/dev/tdd-idris/SchemaDataStore.idr:27:33--27:34
23 |
24 | {schema:_} -> Show (SchemaType schema) where
25 | show {schema = SString} item = show item
26 | show {schema = SInt} item = show item
27 | show {schema = (x .+. y)} (x, y) = "(" ++ show x ++ ", " ++ show y ++ ")"
^
Error: While processing left hand side of show. schema is not a valid argument in show ?item.
/home/stefan/dev/tdd-idris/SchemaDataStore.idr:25:3--25:31
24 | Show ({schema:_} -> SchemaType schema) where
25 | show {schema = SString} item = show item
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: While processing right hand side of show. Sorry, I can't find any elaboration which works. If Builtin.Pair: schema is not accessible in this context.
/home/stefan/dev/tdd-idris/SchemaDataStore.idr:26:11--26:17
24 | Show (SchemaType schema) where
25 | show item =
26 | case (schema, item) of
^^^^^^
尝试3
抛出:
Error: While processing left hand side of show. Can't match on ?postpone [no locals in scope] (Non linear pattern variable).
/home/stefan/dev/tdd-idris/SchemaDataStore.idr:27:33--27:34
23 |
24 | {schema:_} -> Show (SchemaType schema) where
25 | show {schema = SString} item = show item
26 | show {schema = SInt} item = show item
27 | show {schema = (x .+. y)} (x, y) = "(" ++ show x ++ ", " ++ show y ++ ")"
^
Error: While processing left hand side of show. schema is not a valid argument in show ?item.
/home/stefan/dev/tdd-idris/SchemaDataStore.idr:25:3--25:31
24 | Show ({schema:_} -> SchemaType schema) where
25 | show {schema = SString} item = show item
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: While processing right hand side of show. Sorry, I can't find any elaboration which works. If Builtin.Pair: schema is not accessible in this context.
/home/stefan/dev/tdd-idris/SchemaDataStore.idr:26:11--26:17
24 | Show (SchemaType schema) where
25 | show item =
26 | case (schema, item) of
^^^^^^
有人能告诉我吗?我是不是在尝试一些不可能的事情,是不是语法有误?schema
不是show
的参数。我想这就是你想要的:
{schema:}->显示(SchemaType schema)其中
展示项目=
的案例(模式、项目)
(SString,str)=>show str
(SInt,int)=>显示int
((x.+.y)、(左、右))=>“(“++显示x++”,“++显示y++”)
但是,这会产生另一个错误,因为类型类实际上只适用于数据
,并且您正在将其与函数一起使用。也许不和谐的人(在标签描述中)知道如何让它工作。Idris堆栈溢出不是很活跃。这怎么样
infixr 5 .+.
data Schema = SString
| SInt
| (.+.) Schema Schema
SchemaType : Schema -> Type
SchemaType SString = String
SchemaType SInt = Int
SchemaType (x .+. y) = (SchemaType x, SchemaType y)
namespace AdHoc
public export
show : {scm : _} -> SchemaType scm -> String
show {scm = SString } str = show str
show {scm = SInt } num = show num
show {scm = (a .+. b)} (x, y) = "(" ++ AdHoc.show x ++ ", " ++ AdHoc.show y ++ ")"
main : IO ()
main = putStrLn $ show {scm = (SString .+. SInt)} ("adhoc", 0)